//----- Console workflow management use cases ------
@Override
public PackageDefinition deployWorkflowPackage(String appId, String version, byte[] packageXpdl, boolean createNewApp) throws Exception {
PackageDefinition packageDef = null;
AppDefinition appDef = null;
String packageId = workflowManager.getPackageIdFromDefinition(packageXpdl);
// get app version
if (appId != null && !appId.isEmpty()) {
appDef = loadAppDefinition(appId, version);
// verify packageId
if (appDef != null && !packageId.equalsIgnoreCase(appDef.getAppId())) {
throw new UnsupportedOperationException("Package ID does not match App ID");
}
} else {
appDef = loadAppDefinition(packageId, null);
}
if (appDef != null || createNewApp) {
Long originalVersion = null;
//to fix package id letter case issue
if (appDef != null && !packageId.equals(appDef.getAppId())) {
packageXpdl = StringUtil.searchAndReplaceByteContent(packageXpdl, packageId, appDef.getAppId());
packageId = appDef.getAppId();
}
// deploy package
String versionStr = workflowManager.getCurrentPackageVersion(packageId);
String packageIdToUpload = (versionStr != null && !versionStr.isEmpty()) ? packageId : null;
workflowManager.processUpload(packageIdToUpload, packageXpdl);
// load package
versionStr = workflowManager.getCurrentPackageVersion(packageId);
WorkflowPackage workflowPackage = workflowManager.getPackage(packageId, versionStr);
// create app from package if not specified
if (appDef == null) {
appDef = new AppDefinition();
appDef.setAppId(packageId);
appDef.setName(workflowPackage.getPackageName());
appDef.setVersion(new Long(1));
createAppDefinition(appDef);
}
// get package definition
packageDef = appDef.getPackageDefinition();
Long packageVersion = Long.parseLong(versionStr);
if (packageDef == null) {
packageDef = packageDefinitionDao.createPackageDefinition(appDef, packageVersion);
//if app version is the only version for the app and no package is found, set process start white list to admin user
if (appDefinitionDao.countVersions(appId) == 1) {
Collection<WorkflowProcess> processList = workflowManager.getProcessList(appDef.getAppId(), packageVersion.toString());
for (WorkflowProcess wp : processList) {
String processIdWithoutVersion = WorkflowUtil.getProcessDefIdWithoutVersion(wp.getId());
PackageParticipant participant = new PackageParticipant();
participant.setProcessDefId(processIdWithoutVersion);
participant.setParticipantId(WorkflowUtil.PROCESS_START_WHITE_LIST);
participant.setType(PackageParticipant.TYPE_ROLE);
participant.setValue(PackageParticipant.VALUE_ROLE_ADMIN);
packageDefinitionDao.addAppParticipant(appDef.getAppId(), appDef.getVersion(), participant);
}
}
} else {
originalVersion = packageDef.getVersion();
packageDefinitionDao.updatePackageDefinitionVersion(packageDef, packageVersion);
}
if (originalVersion != null) {
updateRunningProcesses(packageId, originalVersion, packageVersion);