Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 47 additions & 33 deletions src/main/kotlin/data/csv_data_source/schema/AuditSchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ class AuditSchema(
}

override fun toRow(entity: AuditLogDto): List<String> {
return if (checkAuditLogIsNotValid(entity)) emptyList()
else auditLogToStringsList(entity)
return if (checkAuditLogDtoIsNotValid(entity).not()) {
mapAuditLogDtoToList(entity)
} else {
emptyList()
}
}

override fun fromRow(row: List<String>): AuditLogDto? {
return if (checkRowIsNotValidAuditLog(row)) null
else stringsListToAuditLog(row)
return if (checkRowIsNotValidAuditLogDto(row).not()) {
mapListToAuditLogDto(row)
} else {
null
}
}

override fun getId(entity: AuditLogDto): String? {
return entity.id.ifEmpty { null }
}

private fun auditLogToStringsList(auditLog: AuditLogDto): List<String> {
private fun mapAuditLogDtoToList(auditLog: AuditLogDto): List<String> {
return listOf(
auditLog.id,
auditLog.timestamp.toString(),
Expand All @@ -40,52 +46,60 @@ class AuditSchema(
)
}

private fun stringsListToAuditLog(row: List<String>): AuditLogDto {
private fun mapListToAuditLogDto(row: List<String>): AuditLogDto {
return AuditLogDto(
id = row[AuditLogIndex.ID],
timestamp = row[AuditLogIndex.TIMES_TAMP].toLong() ,
timestamp = row[AuditLogIndex.TIMES_TAMP].toLong(),
createdByUserId = row[AuditLogIndex.CREATE_BY],
auditAction = stringToAuditAction(row[AuditLogIndex.AUDIT_ACTION]),
auditAction = mapStringToAuditAction(row[AuditLogIndex.AUDIT_ACTION]),
changesDescription = row[AuditLogIndex.CHANGES_DESCRIPTION].ifEmpty { null },
entityType = stringToEntityType(row[AuditLogIndex.ENTITY_TYPE]),
entityType = mapStringToEntityType(row[AuditLogIndex.ENTITY_TYPE]),
entityId = row[AuditLogIndex.ENTITY_ID]
)
}

private fun checkRowIsNotValidAuditLog(row: List<String>): Boolean {
return (row[AuditLogIndex.ID].isEmpty() ||
row[AuditLogIndex.TIMES_TAMP].isEmpty() ||
row[AuditLogIndex.CREATE_BY].isEmpty() ||
row[AuditLogIndex.AUDIT_ACTION].isEmpty() ||
row[AuditLogIndex.ENTITY_ID].isEmpty() ||
row[AuditLogIndex.AUDIT_ACTION] !in enumValues<AuditLog.AuditAction>().map { it.name } ||
row[AuditLogIndex.ENTITY_TYPE] !in enumValues<AuditLog.EntityType>().map { it.name }
)
private fun checkRowIsNotValidAuditLogDto(row: List<String>): Boolean {
return (row[AuditLogIndex.ID].isEmpty() || row[AuditLogIndex.TIMES_TAMP].isEmpty() || row[AuditLogIndex.CREATE_BY].isEmpty() || row[AuditLogIndex.AUDIT_ACTION].isEmpty() || row[AuditLogIndex.ENTITY_ID].isEmpty() || row[AuditLogIndex.AUDIT_ACTION] !in enumValues<AuditLog.AuditAction>().map { it.name } || row[AuditLogIndex.ENTITY_TYPE] !in enumValues<AuditLog.EntityType>().map { it.name })
}

private fun stringToAuditAction(auditAction: String): AuditLog.AuditAction {
private fun mapStringToAuditAction(auditAction: String): AuditLog.AuditAction {
return when (auditAction) {
AuditLog.AuditAction.CREATE.toString() -> AuditLog.AuditAction.CREATE
AuditLog.AuditAction.UPDATE.toString() -> AuditLog.AuditAction.UPDATE
AuditLog.AuditAction.DELETE.toString() -> AuditLog.AuditAction.DELETE
else -> AuditLog.AuditAction.CREATE
AuditLog.AuditAction.CREATE.toString() -> {
AuditLog.AuditAction.CREATE
}

AuditLog.AuditAction.UPDATE.toString() -> {
AuditLog.AuditAction.UPDATE
}

AuditLog.AuditAction.DELETE.toString() -> {
AuditLog.AuditAction.DELETE
}

else -> {
AuditLog.AuditAction.CREATE
}
}
}

private fun stringToEntityType(entityType: String): AuditLog.EntityType {
private fun mapStringToEntityType(entityType: String): AuditLog.EntityType {
return when (entityType) {
AuditLog.EntityType.TASK.toString() -> AuditLog.EntityType.TASK
AuditLog.EntityType.PROJECT.toString() -> AuditLog.EntityType.PROJECT
else -> AuditLog.EntityType.TASK
AuditLog.EntityType.TASK.toString() -> {
AuditLog.EntityType.TASK
}

AuditLog.EntityType.PROJECT.toString() -> {
AuditLog.EntityType.PROJECT
}

else -> {
AuditLog.EntityType.TASK
}
}
}

private fun checkAuditLogIsNotValid(auditLog: AuditLogDto): Boolean {
return (auditLog.id.isEmpty() ||
auditLog.timestamp <= 0 ||
auditLog.createdByUserId.isEmpty() ||
auditLog.entityId.isEmpty()
)
private fun checkAuditLogDtoIsNotValid(auditLog: AuditLogDto): Boolean {
return (auditLog.id.isEmpty() || auditLog.timestamp <= 0 || auditLog.createdByUserId.isEmpty() || auditLog.entityId.isEmpty())
}

private companion object {
Expand Down
55 changes: 33 additions & 22 deletions src/main/kotlin/data/csv_data_source/schema/ProjectSchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ import com.berlin.data.ProjectIndex
import com.berlin.data.dto.ProjectDto

class ProjectSchema(
override val fileName: String,
override val header: List<String>
override val fileName: String, override val header: List<String>
) : BaseSchema<ProjectDto> {

init {
require(fileName.isNotEmpty() && header.size == NUMBER_OF_ATTRIBUTES)
}

override fun toRow(entity: ProjectDto): List<String> {
return if (checkProjectIsNotValid(entity)) emptyList()
else projectToStringsList(entity)
return if (checkProjectDtoIsNotValid(entity).not()) {
mapProjectDtoToList(entity)
} else {
emptyList()
}
}

override fun fromRow(row: List<String>): ProjectDto? {
return if (checkRowIsNotValidProject(row)) null
else stringsListToProject(row)
return if (checkRowIsNotValidProjectDto(row).not()) {
mapListToProjectDto(row)
} else {
null
}
}

override fun getId(entity: ProjectDto): String? {
return entity.id.ifEmpty { null }
}

private fun projectToStringsList(project: ProjectDto): List<String> {
private fun mapProjectDtoToList(project: ProjectDto): List<String> {
return listOf(
project.id,
project.title,
Expand All @@ -36,31 +41,37 @@ class ProjectSchema(
)
}

private fun stringsListToProject(row: List<String>): ProjectDto {
private fun mapListToProjectDto(row: List<String>): ProjectDto {
return ProjectDto(
id = row[ProjectIndex.ID],
title = row[ProjectIndex.NAME],
description = row[ProjectIndex.DESCRIPTION].ifEmpty { null },
statesId = row[ProjectIndex.STATES_ID].let { if (it == "[]") null else stringListToList(it) },
tasksId = row[ProjectIndex.TASKS_ID].let { if (it == "[]") null else stringListToList(it) }
)
statesId = row[ProjectIndex.STATES_ID].let {
if ((it == "[]").not()) {
mapRowToList(it)
} else {
null
}
},
tasksId = row[ProjectIndex.TASKS_ID].let {
if ((it == "[]").not()) {
mapRowToList(it)
} else {
null
}
})
}

private fun checkRowIsNotValidProject(row: List<String>): Boolean {
return (row.isEmpty()||
row[ProjectIndex.ID].isEmpty() ||
row[ProjectIndex.NAME].isEmpty())
private fun checkRowIsNotValidProjectDto(row: List<String>): Boolean {
return (row.isEmpty() || row[ProjectIndex.ID].isEmpty() || row[ProjectIndex.NAME].isEmpty())
}

private fun checkProjectIsNotValid(project: ProjectDto): Boolean {
return project.id.isEmpty() ||
project.title.isEmpty()
private fun checkProjectDtoIsNotValid(project: ProjectDto): Boolean {
return project.id.isEmpty() || project.title.isEmpty()
}

private fun stringListToList(listString: String): List<String> {
return listString
.removeSurrounding("[", "]")
.split(",")
private fun mapRowToList(row: String): List<String> {
return row.removeSurrounding("[", "]").split(",")
}

private companion object {
Expand Down
35 changes: 15 additions & 20 deletions src/main/kotlin/data/csv_data_source/schema/TaskSchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ import com.berlin.data.TaskIndex
import com.berlin.data.dto.TaskDto

class TaskSchema(
override val fileName: String,
override val header: List<String>
override val fileName: String, override val header: List<String>
) : BaseSchema<TaskDto> {

init {
require(fileName.isNotEmpty() && header.size == NUMBER_OF_ATTRIBUTES)
}

override fun toRow(entity: TaskDto): List<String> {
return if (checkTaskIsNotValid(entity)) emptyList()
else taskToStringsList(entity)
return if (checkTaskIsNotValid(entity).not()) {
mapTaskDtoToList(entity)
} else {
emptyList()
}
}

override fun fromRow(row: List<String>): TaskDto? {
return if (checkRowIsNotValidTask(row)) null
else stringsListToTask(row)
return if (checkRowIsNotValidTask(row).not()) {
mapListToTaskDto(row)
} else {
null
}
}

override fun getId(entity: TaskDto): String? {
return entity.id.ifEmpty { null }
}

private fun taskToStringsList(task: TaskDto): List<String> {
private fun mapTaskDtoToList(task: TaskDto): List<String> {
return listOf(
task.id,
task.projectId,
Expand All @@ -38,7 +43,7 @@ class TaskSchema(
)
}

private fun stringsListToTask(row: List<String>): TaskDto {
private fun mapListToTaskDto(row: List<String>): TaskDto {
return TaskDto(
id = row[TaskIndex.ID],
projectId = row[TaskIndex.PROJECT_ID],
Expand All @@ -51,21 +56,11 @@ class TaskSchema(
}

private fun checkRowIsNotValidTask(row: List<String>): Boolean {
return (row[TaskIndex.ID].isEmpty() ||
row[TaskIndex.PROJECT_ID].isEmpty() ||
row[TaskIndex.TITLE].isEmpty() ||
row[TaskIndex.STATE_ID].isEmpty() ||
row[TaskIndex.ASSIGNED_TO_USER_ID].isEmpty() ||
row[TaskIndex.CREATE_BY_USER_ID].isEmpty())
return (row[TaskIndex.ID].isEmpty() || row[TaskIndex.PROJECT_ID].isEmpty() || row[TaskIndex.TITLE].isEmpty() || row[TaskIndex.STATE_ID].isEmpty() || row[TaskIndex.ASSIGNED_TO_USER_ID].isEmpty() || row[TaskIndex.CREATE_BY_USER_ID].isEmpty())
}

private fun checkTaskIsNotValid(task: TaskDto): Boolean {
return (task.id.isEmpty() ||
task.projectId.isEmpty() ||
task.title.isEmpty() ||
task.stateId.isEmpty() ||
task.assignedToUserId.isEmpty() ||
task.createByUserId.isEmpty())
return (task.id.isEmpty() || task.projectId.isEmpty() || task.title.isEmpty() || task.stateId.isEmpty() || task.assignedToUserId.isEmpty() || task.createByUserId.isEmpty())
}

private companion object {
Expand Down
41 changes: 19 additions & 22 deletions src/main/kotlin/data/csv_data_source/schema/TaskStateSchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,51 @@ import com.berlin.data.StateIndex
import com.berlin.data.dto.TaskStateDto

class TaskStateSchema(
override val fileName: String,
override val header: List<String>
override val fileName: String, override val header: List<String>
) : BaseSchema<TaskStateDto> {

init {
require(fileName.isNotEmpty() && header.size == NUMBER_OF_ATTRIBUTES)
}

override fun toRow(entity: TaskStateDto): List<String> {
return if (checkStateIsNotValid(entity)) emptyList()
else stateToStringsList(entity)
return if (checkTaskStateDtoIsNotValid(entity).not()) {
mapTaskStateDtoToList(entity)
} else {
emptyList()
}
}

override fun fromRow(row: List<String>): TaskStateDto? {
return if (checkRowIsNotValidState(row)) null
else stringsListToState(row)
return if (checkRowIsNotValidTaskStateDto(row).not()) {
mapListToTaskStateDto(row)
} else {
null
}
}

override fun getId(entity: TaskStateDto): String? {
return entity.id.ifEmpty { null }
}

private fun stateToStringsList(state: TaskStateDto): List<String> {
private fun mapTaskStateDtoToList(state: TaskStateDto): List<String> {
return listOf(
state.id,
state.name,
state.projectId
state.id, state.name, state.projectId
)
}

private fun stringsListToState(row: List<String>): TaskStateDto {
private fun mapListToTaskStateDto(row: List<String>): TaskStateDto {
return TaskStateDto(
id = row[StateIndex.ID],
name = row[StateIndex.NAME],
projectId = row[StateIndex.PROJECT_ID]
id = row[StateIndex.ID], name = row[StateIndex.NAME], projectId = row[StateIndex.PROJECT_ID]
)
}

private fun checkRowIsNotValidState(row: List<String>): Boolean {
return (row[StateIndex.ID].isEmpty() ||
row[StateIndex.NAME].isEmpty() ||
row[StateIndex.PROJECT_ID].isEmpty())
private fun checkRowIsNotValidTaskStateDto(row: List<String>): Boolean {
return (row[StateIndex.ID].isEmpty() || row[StateIndex.NAME].isEmpty() || row[StateIndex.PROJECT_ID].isEmpty())
}

private fun checkStateIsNotValid(state: TaskStateDto): Boolean {
return (state.id.isEmpty() ||
state.name.isEmpty() ||
state.projectId.isEmpty())
private fun checkTaskStateDtoIsNotValid(state: TaskStateDto): Boolean {
return (state.id.isEmpty() || state.name.isEmpty() || state.projectId.isEmpty())
}

private companion object {
Expand Down
Loading
Loading