Skip to content

Commit

Permalink
Function to update project XML data with the colloboque log records.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarashev committed Jan 22, 2024
1 parent e4c0427 commit 5885de8
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2024 BarD Software s.r.o., Dmitry Barashev.
*
* This file is part of GanttProject, an opensource project management tool.
*
* GanttProject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GanttProject is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GanttProject. If not, see <http://www.gnu.org/licenses/>.
*/
package cloud.ganttproject.colloboque

import biz.ganttproject.core.calendar.WeekendCalendarImpl
import biz.ganttproject.core.io.XmlProjectImporter
import net.sourceforge.ganttproject.GanttProjectImpl
import net.sourceforge.ganttproject.TaskManagerConfigImpl
import net.sourceforge.ganttproject.io.GanttXMLSaver
import net.sourceforge.ganttproject.resource.HumanResourceManager
import net.sourceforge.ganttproject.roles.RoleManager
import net.sourceforge.ganttproject.storage.LazyProjectDatabaseProxy
import net.sourceforge.ganttproject.storage.SqlProjectDatabaseImpl
import net.sourceforge.ganttproject.storage.XlogRecord
import net.sourceforge.ganttproject.task.*
import java.io.ByteArrayOutputStream

/**
* This function applies the `updates` to the contents of `projectXml` and returns the updated XML as a String.
*/
fun updateProjectXml(projectXml: String, updates: XlogRecord): String {
val calendar = WeekendCalendarImpl()
val humanResourceManager = HumanResourceManager(
RoleManager.Access.getInstance().defaultRole,
CustomColumnsManager()
)
val taskManagerConfig = TaskManagerConfigImpl(humanResourceManager, calendar)
val taskManager = TaskManagerImpl(null, taskManagerConfig)
val projectDatabase = LazyProjectDatabaseProxy(databaseFactory = {
SqlProjectDatabaseImpl.createInMemoryDatabase()
}, taskManager = {taskManager}).also {
it.startLog("0")
}
val project = GanttProjectImpl(taskManager, projectDatabase)
XmlProjectImporter(project).import(projectXml)
taskManager.tasks.forEach(projectDatabase::insertTask)
assert(projectDatabase.readAllTasks().size == 1)

if (updates.colloboqueOperations.isEmpty()) {
return projectXml
}

projectDatabase.addExternalUpdatesListener {
val hierarchy = taskManager.taskHierarchy.export()
taskManager.reset()
val databaseTasks = projectDatabase.readAllTasks()
taskManager.importFromDatabase(databaseTasks, hierarchy)
}
projectDatabase.applyUpdate(listOf(updates), "0", "1")
val output = ByteArrayOutputStream()
GanttXMLSaver(project).save(output)
return output.toString(Charsets.UTF_8)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 BarD Software s.r.o., Dmitry Barashev.
*
* This file is part of GanttProject, an opensource project management tool.
*
* GanttProject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GanttProject is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GanttProject. If not, see <http://www.gnu.org/licenses/>.
*/
package cloud.ganttproject.colloboque

import net.sourceforge.ganttproject.storage.BinaryPred
import net.sourceforge.ganttproject.storage.OperationDto
import net.sourceforge.ganttproject.storage.XlogRecord
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class ProjectFileUpdaterTest {

@BeforeEach fun setUp() {
localeApi // This will set a static field in CalendarFactory
}

@Test fun `empty xlog`() {
assertEquals(PROJECT_XML_TEMPLATE, ProjectFileUpdater().applyXlog(PROJECT_XML_TEMPLATE, XlogRecord(emptyList())))
}

@Test fun `apply task name change`() {
val changes = XlogRecord(
listOf(
OperationDto.UpdateOperationDto("task",
updateBinaryConditions = mutableListOf(Triple("uid", BinaryPred.EQ, "qwerty")),
updateRangeConditions = mutableListOf(),
newValues = mutableMapOf("name" to "Task2")
)
)
)
val updatedXml = updateProjectXml(PROJECT_XML_TEMPLATE, changes)
assertTrue(updatedXml.lines().any { it.matches(""".*<task.*name=.Task2.*>""".toRegex()) }) {
"""The result of applying updates:
$updatedXml
""".trimIndent()
}
}
}


private val PROJECT_XML_TEMPLATE = """
<?xml version="1.0" encoding="UTF-8"?>
<project name="" company="" webLink="" view-date="2022-01-01" view-index="0" gantt-divider-location="374" resource-divider-location="322" version="3.0.2906" locale="en">
<tasks empty-milestones="true">
<task id="0" uid="qwerty" name="Task1" color="#99ccff" meeting="false" start="2022-02-10" duration="25" complete="85" expand="true"/>
</tasks>
</project>
""".trimIndent()

0 comments on commit 5885de8

Please sign in to comment.