Skip to content

Commit cb9ba06

Browse files
committed
Update built-in calculated properties (cost and end date)
Update #2514
1 parent 625e440 commit cb9ba06

File tree

7 files changed

+62
-13
lines changed

7 files changed

+62
-13
lines changed

ganttproject/src/main/java/biz/ganttproject/customproperty/CalculatedPropertyUpdater.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class CalculatedPropertyUpdater(
3838
else -> null
3939
}
4040
}
41+
4142
projectDatabase.mapTasks(*(updaters.toTypedArray()))
43+
projectDatabase.updateBuiltInCalculatedColumns()
4244
LOG.debug("<<<")
4345
}
4446
}

ganttproject/src/main/java/net/sourceforge/ganttproject/storage/LazyProjectDatabaseProxy.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class LazyProjectDatabaseProxy(
5050
}.toMap()
5151
}
5252
) }
53+
54+
override fun updateBuiltInCalculatedColumns() {
55+
getDatabase().updateBuiltInCalculatedColumns()
56+
}
57+
5358
private val projectEventListenerImpl by lazy { ProjectEventListenerImpl(this, taskManager, calculatedPropertyUpdater, filterUpdater) }
5459

5560
private fun isInitialized(): Boolean = lazyProjectDatabase != null

ganttproject/src/main/java/net/sourceforge/ganttproject/storage/ProjectDatabase.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,6 @@ interface ProjectDatabase {
167167
* @param customPropertyManager an instance of the CustomPropertyManager with the actual custom property definitions.
168168
*/
169169
fun onCustomColumnChange(customPropertyManager: CustomPropertyManager)
170+
171+
fun updateBuiltInCalculatedColumns()
170172
}

ganttproject/src/main/java/net/sourceforge/ganttproject/storage/SqlProjectDatabaseImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class SqlProjectDatabaseImpl(
8484
override fun onCustomColumnChange(customPropertyManager: CustomPropertyManager) =
8585
customPropertyStorageManager.onCustomColumnChange(customPropertyManager)
8686

87+
override fun updateBuiltInCalculatedColumns() {
88+
runScriptFromResource(dataSource, DB_UPDATE_BUILTIN_CALCULATED_COLUMNS)
89+
}
90+
8791
/**
8892
* Applies updates from Colloboque
8993
*/
@@ -538,4 +542,5 @@ const val SQL_PROJECT_DATABASE_OPTIONS = ";DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=t
538542
private const val H2_IN_MEMORY_URL = "jdbc:h2:mem:gantt-project-state$SQL_PROJECT_DATABASE_OPTIONS"
539543
private const val DB_INIT_SCRIPT_PATH = "/sql/init-project-database.sql"
540544
private const val DB_INIT_SCRIPT_PATH2 = "/sql/init-project-database-step2.sql"
545+
private const val DB_UPDATE_BUILTIN_CALCULATED_COLUMNS = "/sql/update-builtin-calculated-columns.sql"
541546
private val LOG = GPLogger.create("ProjectDatabase")

ganttproject/src/main/resources/resources/sql/init-project-database-step2.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
-- CREATE ALIAS IF NOT EXISTS TASK_COST FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskCost";
2-
-- CREATE ALIAS IF NOT EXISTS TASK_END_DATE FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskEndDate";
1+
CREATE ALIAS IF NOT EXISTS TASK_COST FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskCost";
2+
CREATE ALIAS IF NOT EXISTS TASK_END_DATE FOR "net.sourceforge.ganttproject.storage.H2FunctionsKt.taskEndDate";
33
--
44
-- DROP VIEW TaskViewForComputedColumns;
55
-- DROP TABLE Task CASCADE ;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UPDATE Task SET end_date=TASK_END_DATE(num), cost=TASK_COST(num);

ganttproject/src/test/java/biz/ganttproject/storage/SqlCustomPropertyStorageManagerTest.kt

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
*/
1919
package biz.ganttproject.storage
2020

21-
import biz.ganttproject.customproperty.CustomPropertyClass
22-
import biz.ganttproject.customproperty.CustomPropertyHolder
23-
import biz.ganttproject.customproperty.CustomPropertyManager
24-
import biz.ganttproject.customproperty.SimpleSelect
21+
import biz.ganttproject.customproperty.*
2522
import biz.ganttproject.storage.db.tables.Task
2623
import net.sourceforge.ganttproject.TestSetupHelper
24+
import net.sourceforge.ganttproject.resource.HumanResourceManager
2725
import net.sourceforge.ganttproject.storage.*
26+
import net.sourceforge.ganttproject.task.CostStub
2827
import net.sourceforge.ganttproject.task.TaskManager
2928
import org.h2.jdbcx.JdbcDataSource
3029
import org.jooq.SQLDialect
@@ -34,6 +33,8 @@ import org.junit.jupiter.api.Assertions.assertEquals
3433
import org.junit.jupiter.api.BeforeEach
3534
import org.junit.jupiter.api.Test
3635
import org.junit.jupiter.api.assertThrows
36+
import org.w3c.util.DateParser
37+
import java.math.BigDecimal
3738
import java.sql.SQLException
3839
import java.util.*
3940
import javax.sql.DataSource
@@ -82,7 +83,7 @@ class CalculatedPropertyTest {
8283
@Test
8384
fun `calculated property value`() {
8485
val foo = customPropertyManager.createDefinition(CustomPropertyClass.TEXT, "foo")
85-
val bar =customPropertyManager.createDefinition(CustomPropertyClass.INTEGER, "bar").also {
86+
val bar = customPropertyManager.createDefinition(CustomPropertyClass.INTEGER, "bar").also {
8687
it.calculationMethod = SimpleSelect(it.id, "duration + 1", resultClass = CustomPropertyClass.INTEGER.javaClass)
8788
}
8889
val baz = customPropertyManager.createDefinition(CustomPropertyClass.TEXT, "baz").also {
@@ -99,17 +100,50 @@ class CalculatedPropertyTest {
99100
assertEquals(1, tasks.size)
100101

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

109106
assertEquals(2, task.customValues.getValue(bar))
110107
assertEquals("foo--", task.customValues.getValue(baz))
111108
}
112109

110+
@Test
111+
fun `builtin calculated property value`() {
112+
val task = taskManager.newTaskBuilder().withName("task1").withStartDate(Date()).build()
113+
projectDatabase.insertTask(task)
114+
115+
H2Functions.taskManager.set(taskManager)
116+
task.createMutator().let {
117+
it.setCost(CostStub(BigDecimal.ZERO, true))
118+
it.setDuration(taskManager.createLength(2))
119+
it.commit()
120+
}
121+
122+
val updater = CalculatedPropertyUpdater(projectDatabase, {customPropertyManager}, { emptyMap() })
123+
updater.update()
124+
125+
DSL.using(dataSource, SQLDialect.H2).also {
126+
val tasks = it.selectFrom(Task.TASK).fetch()
127+
assertEquals(1, tasks.size)
128+
assertEquals(task.end.time, DateParser.toJavaDate(tasks[0].endDate))
129+
}
130+
131+
val humanResourceManager = HumanResourceManager(null, CustomColumnsManager())
132+
val resource = humanResourceManager.newResourceBuilder().withName("foo").withID(1).withStandardRate(BigDecimal.valueOf(100)).build()
133+
task.assignmentCollection.addAssignment(resource).also {
134+
it.load = 100f;
135+
}
136+
137+
updater.update()
138+
DSL.using(dataSource, SQLDialect.H2).also {
139+
val tasks = it.selectFrom(Task.TASK).fetch()
140+
assertEquals(1, tasks.size)
141+
assertEquals(task.cost.value.toDouble(), tasks[0].cost.toDouble())
142+
assertEquals(200.0, tasks[0].cost.toDouble())
143+
}
144+
145+
}
146+
113147
@Test
114148
fun `column used in a generated column can't be dropped`() {
115149
customPropertyManager.createDefinition(CustomPropertyClass.INTEGER, "bar").also {

0 commit comments

Comments
 (0)