Skip to content

Commit

Permalink
switch to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-maschmann committed Apr 15, 2024
1 parent d616828 commit 6267180
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 137 deletions.
2 changes: 2 additions & 0 deletions .mvn/jvm.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.base/java.io=ALL-UNNAMED
35 changes: 33 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@
<artifactId>camunda-8-webmodeler-maven-plugin</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>camunda-8-webmodeler-maven-plugin</name>
<packaging>maven-plugin</packaging>
<description>Plugin to download documents from Camunda Web Modeler via REST-Api</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<kotlin.version>1.9.23</kotlin.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<version.java>${java.version}</version.java>
<junit.version>5.9.2</junit.version>
</properties>

<dependencies>
<!-- Kotlin -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>

<!-- Maven -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand All @@ -44,12 +55,14 @@
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<!-- Generate META-INF for Plugins -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.0</version>
<executions>
<execution>
<id>default-descriptor</id>
Expand All @@ -59,8 +72,26 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<packaging>maven-plugin</packaging>

</project>
113 changes: 0 additions & 113 deletions src/main/java/BpmDownloadPlugin.java

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/java/DownloadFile.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/java/ModelerDocument.java

This file was deleted.

122 changes: 122 additions & 0 deletions src/main/kotlin/BpmDownloadPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import io.holunda.webmodeler.rest.CamundaWebModelerClientBuilder
import io.holunda.webmodeler.rest.impl.CamundaWebModelerClient
import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugins.annotations.LifecyclePhase
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import org.openapitools.client.model.FileDto
import org.openapitools.client.model.FileMetadataDto
import org.openapitools.client.model.MilestoneDto
import org.openapitools.client.model.MilestoneMetadataDto
import org.openapitools.client.model.PubSearchDtoFileMetadataDto
import org.openapitools.client.model.PubSearchDtoMilestoneMetadataDto
import org.openapitools.client.model.PubSearchResultDtoFileMetadataDto
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.util.Objects
import java.util.Optional
import java.util.UUID

@Mojo(name = "bpmn-download", defaultPhase = LifecyclePhase.COMPILE)
class BpmDownloadPlugin: AbstractMojo() {
@Parameter(defaultValue = "\${project}", required = true, readonly = true)
lateinit var project: MavenProject

@Parameter(property = "client-id", readonly = true, required = true)
lateinit var clientId: String

@Parameter(property = "client-secret", readonly = true, required = true)
lateinit var clientSecret: String

@Parameter(property = "documents", readonly = true, required = true)
lateinit var documents: List<ModelerDocument>

@Parameter
var path: String? = null

var client: CamundaWebModelerClient? = null

override fun execute() {
client = CamundaWebModelerClientBuilder.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.build()


getLog().info("Starting to download files")
if (path == null) {
path = project.basedir.toPath().resolve("/src/main/resources").toString()
}
getLog().info("Files will be stored under $path")


documents.forEach { it ->
val fileId: Optional<String> = getFileId(it.name)
fileId.ifPresentOrElse(
{ id ->
val downloadFile: DownloadFile =
Optional.ofNullable(it.mileStone).map { milestone -> getMilestoneFile(id, milestone) }
.orElseGet {
val dto: FileDto = client!!.getFile(UUID.fromString(id))
Objects.requireNonNull(dto.getMetadata(), "metadata of file '$id' is null")
DownloadFile(dto.getMetadata().getSimplePath(), dto.getContent())
}
downloadFile(downloadFile)
},
{ getLog().warn("Could not find file for " + it.name) }
)
}
}

private fun getFileId(fileName: String): Optional<String> {
val fileMetadataSearch = FileMetadataDto()
fileMetadataSearch.setName(fileName)
val fileSearchDto = PubSearchDtoFileMetadataDto()
fileSearchDto.setFilter(fileMetadataSearch)

val fileSearchResponse: PubSearchResultDtoFileMetadataDto = client!!.searchFiles(fileSearchDto)
return Optional.ofNullable(fileSearchResponse.getItems())
.map { items -> items.stream().findFirst().map(FileMetadataDto::getId) }
.flatMap { file -> file }
}

private fun getMilestoneFile(fileId: String, milestoneName: String): DownloadFile {
val mileStoneSearch = MilestoneMetadataDto()
mileStoneSearch.setName(milestoneName)
mileStoneSearch.setFileId(fileId)

val pubSearchDtoMilestoneMetadataDto = PubSearchDtoMilestoneMetadataDto()
pubSearchDtoMilestoneMetadataDto.setFilter(mileStoneSearch)

val mileStoneId: String = Optional.ofNullable(
client!!.searchMilestones(
pubSearchDtoMilestoneMetadataDto
)
.getItems()
).map { res -> res.stream().findFirst() }.flatMap { ms -> ms }.map(
MilestoneMetadataDto::getId
).orElseThrow()

val milestone: MilestoneDto = client!!.getMilestone(UUID.fromString(mileStoneId))
val dto: FileDto = client!!.getFile(UUID.fromString(fileId))
Objects.requireNonNull(dto.getMetadata(), "metadata of file '$fileId' and milestone '$milestoneName' is null")
return DownloadFile(
dto.getMetadata().getSimplePath(),
milestone.getContent()
)
}

private fun downloadFile(file: DownloadFile) {
try {
Files.writeString(Path.of(path.toString() + "/" + file.name), file.content)
getLog().info("Created file " + file.name)
} catch (e: IOException) {
getLog().warn("Could not save file for " + file.name)
throw RuntimeException(e)
}
}
}


4 changes: 4 additions & 0 deletions src/main/kotlin/DownloadFile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data class DownloadFile(
val name: String,
val content: String
)
4 changes: 4 additions & 0 deletions src/main/kotlin/ModelerDocument.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ModelerDocument {
lateinit var name: String
var mileStone: String? = null
}

0 comments on commit 6267180

Please sign in to comment.