Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use java-client to push processes #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.jetbrains.kotlin.jvm").version("1.3.21")
id("org.jetbrains.kotlin.jvm").version("1.3.50")
id("com.bonitasoft.gradle.bonita-formatting").version("0.1.53")
application
}

repositories {
jcenter()
mavenLocal()
mavenCentral()
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.bonitasoft.engine:bonita-client:7.10.5")
implementation("com.bonitasoft.engine:bonita-client-sp:7.10.5")
implementation("org.bonitasoft.web:bonita-java-client:0.0.1")
implementation("com.github.javafaker:javafaker:1.0.2")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/org/bonitasoft/example/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
*/
package org.bonitasoft.example

import com.github.javafaker.Faker
import com.bonitasoft.engine.api.APIClient
import com.bonitasoft.engine.profile.ProfileCreator
import org.bonitasoft.engine.api.APIClient
import org.bonitasoft.engine.api.ApiAccessType
import org.bonitasoft.engine.api.ProfileAPI
import org.bonitasoft.engine.identity.*
import org.bonitasoft.engine.identity.Group
import org.bonitasoft.engine.identity.Role
import org.bonitasoft.engine.identity.User
import org.bonitasoft.engine.profile.ProfileMemberCreator
import org.bonitasoft.engine.search.SearchOptionsBuilder
import org.bonitasoft.engine.util.APITypeManager
import org.bonitasoft.example.processes.*
import org.bonitasoft.example.processes.GeneratedProcessWithForms

class App {

Expand Down
47 changes: 47 additions & 0 deletions src/main/kotlin/org/bonitasoft/example/DeployUsingRestApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020 Bonitasoft S.A.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bonitasoft.example

import org.bonitasoft.engine.bpm.bar.BusinessArchiveFactory
import org.bonitasoft.example.processes.GeneratedProcessWithForms
import org.bonitasoft.web.client.BonitaClient
import org.bonitasoft.web.client.services.policies.ProcessImportPolicy
import java.nio.file.Files

class DeployUsingRestApi {

fun run(url: String) {
val client = BonitaClient.builder(url)
.build()


client.login("install", "install")
val barFile = Files.createTempFile("GeneratedProcessWithForms",".bar").toFile()
barFile.delete()
BusinessArchiveFactory.writeBusinessArchiveToFile(GeneratedProcessWithForms().businessArchive, barFile)


client.processes().importProcess(barFile, ProcessImportPolicy.REPLACE_DUPLICATES)

}
}




fun main(args: Array<String>) {
DeployUsingRestApi().run(args.getOrElse(0) { "http://ec2-54-75-10-130.eu-west-1.compute.amazonaws.com:8080/bonita" })
//App().run(args.getOrElse(0) { "http://localhost:8080" })
}
65 changes: 31 additions & 34 deletions src/main/kotlin/org/bonitasoft/example/SetupOrganization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.bonitasoft.example

import com.bonitasoft.engine.profile.ProfileCreator
import com.github.javafaker.Faker
import org.bonitasoft.engine.api.APIClient
import org.bonitasoft.engine.identity.GroupCreator
Expand All @@ -25,24 +24,24 @@ import kotlin.math.min

class SetupOrganization : Consumer<APIClient> {

companion object SetupOragnization {
companion object {
val f = Faker()

val users = LinkedHashMap<String, String>() // f.name().username()
val groups = LinkedHashMap<String, String>() // f.commerce().department()
val roles = ArrayList<String>() // f.job().title()
val profiles = ArrayList<String>() // f.commerce().department()
val users = mutableMapOf<String, String>()
val groups = mutableMapOf<String, String>()
val roles = mutableListOf<String>()
val profiles = mutableListOf<String>()
}

private fun getRandomGroupPath(listOfGroups: LinkedHashMap<String, String>) : String {
var parentNumber = f.number().numberBetween(0, listOfGroups.size)
private fun getRandomGroupPath(listOfGroups: Map<String, String>): String {
val parentNumber = f.number().numberBetween(0, listOfGroups.size)

if (parentNumber == 0) {
return ""
}

var parentKey = listOfGroups.keys.toTypedArray()[parentNumber]
var parentValue = listOfGroups[parentKey]
val parentKey = listOfGroups.keys.toTypedArray()[parentNumber]
val parentValue = listOfGroups[parentKey]

return "$parentValue/$parentKey"
}
Expand Down Expand Up @@ -89,24 +88,22 @@ class SetupOrganization : Consumer<APIClient> {
// create all the profiles
profiles.map {
apiClient.safeExec {
val apiClientSp = this as com.bonitasoft.engine.api.APIClient

val profileAPI = apiClientSp.profileAPI

var pc = ProfileCreator(it)

val trueOrFalse = f.number().numberBetween(0, 2)
if (trueOrFalse == 0) {
pc.setDescription(f.harryPotter().quote())
}

profileAPI.createProfile(pc)
//TODO replace by profile import
//
// val pc = ProfileCreator(it)
//
// val trueOrFalse = f.number().numberBetween(0, 2)
// if (trueOrFalse == 0) {
// pc.setDescription(f.harryPotter().quote())
// }
//
// profileAPI.createProfile(pc)
}
}
apiClient.safeExec {
val apiClientSp = this as com.bonitasoft.engine.api.APIClient
apiClientSp.profileAPI.createProfile(ProfileCreator("Administrator"))
apiClientSp.profileAPI.createProfile(ProfileCreator("User"))
//TODO replace by profile import
// profileAPI.createProfile(ProfileCreator("Administrator"))
// profileAPI.createProfile(ProfileCreator("User"))
}


Expand All @@ -129,7 +126,7 @@ class SetupOrganization : Consumer<APIClient> {
apiClient.safeExec {
if (it != null) {
val numberOfProfilesToAdd = f.number().numberBetween(10, min(30, profiles.size))
var profilesWorkingList = profiles.clone() as ArrayList<String>
val profilesWorkingList = mutableListOf<String>().apply { addAll(profiles) }

for (i in 0 until numberOfProfilesToAdd) {
val random = f.number().numberBetween(0, profilesWorkingList.size)
Expand All @@ -145,7 +142,7 @@ class SetupOrganization : Consumer<APIClient> {
identityAPI.createRole("member")
}
roles.map {
var rc = RoleCreator(it)
val rc = RoleCreator(it)
rc.setDisplayName(it)
val trueOrFalse = f.number().numberBetween(0, 2)
if (trueOrFalse == 0) {
Expand All @@ -158,7 +155,7 @@ class SetupOrganization : Consumer<APIClient> {
apiClient.safeExec {
if (it != null) {
val numberOfProfilesToAdd = f.number().numberBetween(50, min(100, profiles.size))
var profilesWorkingList = profiles.clone() as ArrayList<String>
val profilesWorkingList = mutableListOf<String>().apply { addAll(profiles) }

for (i in 0 until numberOfProfilesToAdd) {
val random = f.number().numberBetween(0, profilesWorkingList.size)
Expand All @@ -174,7 +171,7 @@ class SetupOrganization : Consumer<APIClient> {
apiClient.safeExec {
val numberOfMemberships = f.number().numberBetween(1, min(20, groups.size))

var groupsWorkingList = groups.clone() as LinkedHashMap<String, String>
val groupsWorkingList = mutableMapOf<String, String>().apply { putAll(groups) }
for(i in 0 until numberOfMemberships) {
var randomGroup = f.number().numberBetween(0, groupsWorkingList.size)
var randomGroupPath = groupsWorkingList.values.toTypedArray()[randomGroup]
Expand All @@ -201,8 +198,8 @@ class SetupOrganization : Consumer<APIClient> {
})
}
users.map {
var managerUserIdPosition = f.number().numberBetween(0, users.keys.indexOf(it.key))
var managerId: Long
val managerUserIdPosition = f.number().numberBetween(0, users.keys.indexOf(it.key))
val managerId: Long
if (users.keys.indexOf(it.key) == 0) {
managerId = apiClient.identityAPI.getUserByUserName("walter.bates").id
} else {
Expand All @@ -223,8 +220,8 @@ class SetupOrganization : Consumer<APIClient> {
}.forEach {
apiClient.safeExec {
if (it != null) {
val numberOfProfilesToAdd = f.number().numberBetween(1, min(20, profiles.size))
var profilesWorkingList = profiles.clone() as ArrayList<String>
val numberOfProfilesToAdd = f.number().numberBetween(1, min(20, profiles.size))
val profilesWorkingList = mutableListOf<String>().apply { addAll(profiles) }

for(i in 0 until numberOfProfilesToAdd) {
val random = f.number().numberBetween(0, profilesWorkingList.size)
Expand All @@ -234,7 +231,7 @@ class SetupOrganization : Consumer<APIClient> {

val numberOfMemberships = f.number().numberBetween(1, min(20, groups.size))

var groupsWorkingList = groups.clone() as LinkedHashMap<String, String>
val groupsWorkingList = mutableMapOf<String, String>().apply { putAll(groups) }
for(i in 0 until numberOfMemberships) {
val randomGroup = f.number().numberBetween(0, groupsWorkingList.size)
val randomRole = f.number().numberBetween(0, roles.size)
Expand Down
12 changes: 2 additions & 10 deletions src/main/kotlin/org/bonitasoft/example/processes/BasicProcess.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
package org.bonitasoft.example.processes

import org.bonitasoft.engine.api.APIClient
import org.bonitasoft.engine.bpm.bar.BarResource
import org.bonitasoft.engine.bpm.bar.BusinessArchiveBuilder
import org.bonitasoft.engine.bpm.bar.actorMapping.Actor
import org.bonitasoft.engine.bpm.bar.actorMapping.ActorMapping
import org.bonitasoft.engine.bpm.flownode.GatewayType
import org.bonitasoft.engine.bpm.process.DesignProcessDefinition
import org.bonitasoft.engine.bpm.process.impl.ProcessDefinitionBuilder
import org.bonitasoft.example.toExpression
import java.util.*

class BasicProcess(private val number: Int) : BonitaProcess() {
override fun process(): ProcessDefinitionBuilder =
Expand All @@ -39,7 +37,7 @@ class BasicProcess(private val number: Int) : BonitaProcess() {
addTransition("start", "user1")
}

override fun withResources(bar: BusinessArchiveBuilder) {
override fun withResources(bar: BusinessArchiveBuilder, processDefinition: DesignProcessDefinition) {
bar.apply {
actorMapping = ActorMapping().apply {
addActor(Actor("theActor").apply {
Expand All @@ -50,10 +48,4 @@ class BasicProcess(private val number: Int) : BonitaProcess() {
// addClasspathResource(BarResource("jar$number.jar", ByteArray(2 * 1000 * 1000).apply { Random().nextBytes(this) }))
}
}

override fun accept(client: APIClient) {
super.accept(client)
val deployed = client.processAPI.getProcessDefinitionId(name, version)
deployed.apply { client.processAPI.startProcess(this) }
}
}
43 changes: 23 additions & 20 deletions src/main/kotlin/org/bonitasoft/example/processes/BonitaProcess.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import org.bonitasoft.engine.bpm.bar.BusinessArchiveBuilder
import org.bonitasoft.engine.bpm.bar.actorMapping.Actor
import org.bonitasoft.engine.bpm.bar.actorMapping.ActorMapping
import org.bonitasoft.engine.bpm.bar.form.model.FormMappingDefinition
import org.bonitasoft.engine.bpm.bar.form.model.FormMappingModel
import org.bonitasoft.engine.bpm.flownode.UserTaskDefinition
import org.bonitasoft.engine.bpm.process.DesignProcessDefinition
import org.bonitasoft.engine.bpm.process.impl.ProcessDefinitionBuilder
import org.bonitasoft.engine.form.FormMappingTarget
import org.bonitasoft.engine.form.FormMappingType
Expand All @@ -30,8 +32,9 @@ import java.util.function.Consumer
abstract class BonitaProcess : Consumer<APIClient> {
val businessArchive: BusinessArchive by lazy {
BusinessArchiveBuilder().createNewBusinessArchive().apply {
setProcessDefinition(process().done())
withResources(this)
val processDefinition = process().done()
setProcessDefinition(processDefinition)
withResources(this, processDefinition)
}.done()
}

Expand All @@ -40,23 +43,7 @@ abstract class BonitaProcess : Consumer<APIClient> {

var processDefinitionId: Long? = null

override fun accept(client: APIClient) {
client.safeExec {
businessArchive.processDefinition.actorsList.forEach { actor ->
businessArchive.actorMapping = ActorMapping().apply {
addActor(Actor(actor.name).apply {
users.add("walter.bates")
})
}
}
businessArchive.processDefinition.flowElementContainer.activities.forEach {
if (it is UserTaskDefinition) {
businessArchive.formMappingModel.addFormMapping(FormMappingDefinition("custompage_taskAutogeneratedForm", FormMappingType.TASK, FormMappingTarget.INTERNAL, it.name))
}
}
businessArchive.formMappingModel.addFormMapping(FormMappingDefinition("custompage_processAutogeneratedForm", FormMappingType.PROCESS_START, FormMappingTarget.INTERNAL))
businessArchive.formMappingModel.addFormMapping(FormMappingDefinition("custompage_caseoverview", FormMappingType.PROCESS_OVERVIEW, FormMappingTarget.INTERNAL))
}
final override fun accept(client: APIClient) {
client.safeExec {
println("Disable process $name $version")
processDefinitionId = processAPI.getProcessDefinitionId(name, version)
Expand All @@ -80,7 +67,23 @@ abstract class BonitaProcess : Consumer<APIClient> {
}
}

open fun withResources(bar: BusinessArchiveBuilder) {
open fun withResources(bar: BusinessArchiveBuilder, processDefinition: DesignProcessDefinition) {
processDefinition.actorsList.forEach { actor ->
bar.actorMapping = ActorMapping().apply {
addActor(Actor(actor.name).apply {
users.add("walter.bates")
})
}
}
val formMappingModel = FormMappingModel()
bar.setFormMappings(formMappingModel)
processDefinition.flowElementContainer.activities.forEach {
if (it is UserTaskDefinition) {
formMappingModel.addFormMapping(FormMappingDefinition("custompage_taskAutogeneratedForm", FormMappingType.TASK, FormMappingTarget.INTERNAL, it.name))
}
}
formMappingModel.addFormMapping(FormMappingDefinition("custompage_processAutogeneratedForm", FormMappingType.PROCESS_START, FormMappingTarget.INTERNAL))
formMappingModel.addFormMapping(FormMappingDefinition("custompage_caseoverview", FormMappingType.PROCESS_OVERVIEW, FormMappingTarget.INTERNAL))
}

abstract fun process(): ProcessDefinitionBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.bonitasoft.example.processes
import org.bonitasoft.engine.bpm.bar.BusinessArchiveBuilder
import org.bonitasoft.engine.bpm.bar.actorMapping.Actor
import org.bonitasoft.engine.bpm.bar.actorMapping.ActorMapping
import org.bonitasoft.engine.bpm.process.DesignProcessDefinition
import org.bonitasoft.engine.bpm.process.impl.ProcessDefinitionBuilder
import org.bonitasoft.example.toExpression
import org.bonitasoft.example.toParameter
Expand All @@ -32,7 +33,7 @@ class CallProcessXTimes(private val targetProcessName: String, private val targe
}


override fun withResources(bar: BusinessArchiveBuilder) {
override fun withResources(bar: BusinessArchiveBuilder, processDefinition: DesignProcessDefinition) {
bar.apply {
actorMapping = ActorMapping().apply {
addActor(Actor("theActor").apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.bonitasoft.engine.bpm.bar.BarResource
import org.bonitasoft.engine.bpm.bar.BusinessArchiveBuilder
import org.bonitasoft.engine.bpm.bar.actorMapping.Actor
import org.bonitasoft.engine.bpm.bar.actorMapping.ActorMapping
import org.bonitasoft.engine.bpm.flownode.GatewayType
import org.bonitasoft.engine.bpm.process.DesignProcessDefinition
import org.bonitasoft.engine.bpm.process.impl.ProcessDefinitionBuilder
import org.bonitasoft.engine.expression.ExpressionBuilder
import org.bonitasoft.engine.operation.OperationBuilder
Expand Down Expand Up @@ -162,7 +162,7 @@ class ComplexProcess(private val number: Int) : BonitaProcess() {
addTransition("user1", "auto2")
}

override fun withResources(bar: BusinessArchiveBuilder) {
override fun withResources(bar: BusinessArchiveBuilder, processDefinition: DesignProcessDefinition) {
bar.apply {
actorMapping = ActorMapping().apply {
addActor(Actor("theActor").apply {
Expand All @@ -172,10 +172,4 @@ class ComplexProcess(private val number: Int) : BonitaProcess() {
addClasspathResource(BarResource("jar$number.jar", ByteArray(2 * 1000 * 1000).apply { Random().nextBytes(this) }))
}
}

override fun accept(client: APIClient) {
super.accept(client)
val deployed = client.processAPI.getProcessDefinitionId(name, version)
deployed.apply { client.processAPI.startProcess(this) }
}
}
Loading