Skip to content

Commit

Permalink
Update built-in calculated properties (cost and end date)
Browse files Browse the repository at this point in the history
Update #2514
  • Loading branch information
dbarashev committed Dec 5, 2024
1 parent 625e440 commit cb9ba06
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class CalculatedPropertyUpdater(
else -> null
}
}

projectDatabase.mapTasks(*(updaters.toTypedArray()))
projectDatabase.updateBuiltInCalculatedColumns()
LOG.debug("<<<")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class LazyProjectDatabaseProxy(
}.toMap()
}
) }

override fun updateBuiltInCalculatedColumns() {
getDatabase().updateBuiltInCalculatedColumns()
}

private val projectEventListenerImpl by lazy { ProjectEventListenerImpl(this, taskManager, calculatedPropertyUpdater, filterUpdater) }

private fun isInitialized(): Boolean = lazyProjectDatabase != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,6 @@ interface ProjectDatabase {
* @param customPropertyManager an instance of the CustomPropertyManager with the actual custom property definitions.
*/
fun onCustomColumnChange(customPropertyManager: CustomPropertyManager)

fun updateBuiltInCalculatedColumns()
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class SqlProjectDatabaseImpl(
override fun onCustomColumnChange(customPropertyManager: CustomPropertyManager) =
customPropertyStorageManager.onCustomColumnChange(customPropertyManager)

override fun updateBuiltInCalculatedColumns() {
runScriptFromResource(dataSource, DB_UPDATE_BUILTIN_CALCULATED_COLUMNS)
}

/**
* Applies updates from Colloboque
*/
Expand Down Expand Up @@ -538,4 +542,5 @@ const val SQL_PROJECT_DATABASE_OPTIONS = ";DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=t
private const val H2_IN_MEMORY_URL = "jdbc:h2:mem:gantt-project-state$SQL_PROJECT_DATABASE_OPTIONS"
private const val DB_INIT_SCRIPT_PATH = "/sql/init-project-database.sql"
private const val DB_INIT_SCRIPT_PATH2 = "/sql/init-project-database-step2.sql"
private const val DB_UPDATE_BUILTIN_CALCULATED_COLUMNS = "/sql/update-builtin-calculated-columns.sql"
private val LOG = GPLogger.create("ProjectDatabase")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- CREATE ALIAS IF NOT EXISTS TASK_COST FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskCost";
-- CREATE ALIAS IF NOT EXISTS TASK_END_DATE FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskEndDate";
CREATE ALIAS IF NOT EXISTS TASK_COST FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskCost";
CREATE ALIAS IF NOT EXISTS TASK_END_DATE FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskEndDate";
--
-- DROP VIEW TaskViewForComputedColumns;
-- DROP TABLE Task CASCADE ;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE Task SET end_date=TASK_END_DATE(num), cost=TASK_COST(num);
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
*/
package biz.ganttproject.storage

import biz.ganttproject.customproperty.CustomPropertyClass
import biz.ganttproject.customproperty.CustomPropertyHolder
import biz.ganttproject.customproperty.CustomPropertyManager
import biz.ganttproject.customproperty.SimpleSelect
import biz.ganttproject.customproperty.*
import biz.ganttproject.storage.db.tables.Task
import net.sourceforge.ganttproject.TestSetupHelper
import net.sourceforge.ganttproject.resource.HumanResourceManager
import net.sourceforge.ganttproject.storage.*
import net.sourceforge.ganttproject.task.CostStub
import net.sourceforge.ganttproject.task.TaskManager
import org.h2.jdbcx.JdbcDataSource
import org.jooq.SQLDialect
Expand All @@ -34,6 +33,8 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.w3c.util.DateParser
import java.math.BigDecimal
import java.sql.SQLException
import java.util.*
import javax.sql.DataSource
Expand Down Expand Up @@ -82,7 +83,7 @@ class CalculatedPropertyTest {
@Test
fun `calculated property value`() {
val foo = customPropertyManager.createDefinition(CustomPropertyClass.TEXT, "foo")
val bar =customPropertyManager.createDefinition(CustomPropertyClass.INTEGER, "bar").also {
val bar = customPropertyManager.createDefinition(CustomPropertyClass.INTEGER, "bar").also {
it.calculationMethod = SimpleSelect(it.id, "duration + 1", resultClass = CustomPropertyClass.INTEGER.javaClass)
}
val baz = customPropertyManager.createDefinition(CustomPropertyClass.TEXT, "baz").also {
Expand All @@ -99,17 +100,50 @@ class CalculatedPropertyTest {
assertEquals(1, tasks.size)

val propertyHolders = createPropertyHolders(taskManager)
val updaters = customPropertyManager.definitions.map {def ->
ColumnConsumer(SimpleSelect(def.id, def.id, resultClass = def.type)) { taskNum, value ->
propertyHolders[taskNum]?.setValue(def, value)
}
}.toList()
projectDatabase.mapTasks(*(updaters.toTypedArray()))
val updater = CalculatedPropertyUpdater(projectDatabase, {customPropertyManager}, {propertyHolders})
updater.update()

assertEquals(2, task.customValues.getValue(bar))
assertEquals("foo--", task.customValues.getValue(baz))
}

@Test
fun `builtin calculated property value`() {
val task = taskManager.newTaskBuilder().withName("task1").withStartDate(Date()).build()
projectDatabase.insertTask(task)

H2Functions.taskManager.set(taskManager)
task.createMutator().let {
it.setCost(CostStub(BigDecimal.ZERO, true))
it.setDuration(taskManager.createLength(2))
it.commit()
}

val updater = CalculatedPropertyUpdater(projectDatabase, {customPropertyManager}, { emptyMap() })
updater.update()

DSL.using(dataSource, SQLDialect.H2).also {
val tasks = it.selectFrom(Task.TASK).fetch()
assertEquals(1, tasks.size)
assertEquals(task.end.time, DateParser.toJavaDate(tasks[0].endDate))
}

val humanResourceManager = HumanResourceManager(null, CustomColumnsManager())
val resource = humanResourceManager.newResourceBuilder().withName("foo").withID(1).withStandardRate(BigDecimal.valueOf(100)).build()
task.assignmentCollection.addAssignment(resource).also {
it.load = 100f;
}

updater.update()
DSL.using(dataSource, SQLDialect.H2).also {
val tasks = it.selectFrom(Task.TASK).fetch()
assertEquals(1, tasks.size)
assertEquals(task.cost.value.toDouble(), tasks[0].cost.toDouble())
assertEquals(200.0, tasks[0].cost.toDouble())
}

}

@Test
fun `column used in a generated column can't be dropped`() {
customPropertyManager.createDefinition(CustomPropertyClass.INTEGER, "bar").also {
Expand Down

0 comments on commit cb9ba06

Please sign in to comment.