Class:
@Service(“uploadAndDeploy”)
public class UploadAndDeploy implements MapBasedFlowableFutureJavaDelegate {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
CASDbOperationManager dbOperationManager;
@Autowired
CASHttpRequestExecutor httpRequestExecutor;
@Lookup
public DeployHandlerV2 getV2() {
return null;
}
@SneakyThrows
@Override
public Map<String, Object> execute(ReadOnlyDelegateExecution execution) {
//this variable is picked directly from casimport.bpmn20.xml
String moduleName = (String) execution.getVariable(FlowableConstants.contextVariables.CURRENTMODULEFORDEPLOYMENT);
logger.info(MessageFormat.format(CASEngineConstants.ImportLogs.TASK_UPLOAD_AND_DEPLOY, execution.getProcessInstanceId(), moduleName));
Map<String, Map<String, String>> deploymentDetails = (Map<String, Map<String, String>>) execution.getVariable(FlowableConstants.contextVariables.deploymentDetails);
Map<String, String> moduleDeploymentDetails = new HashMap<>();
int numOfDeployedModules = (int) execution.getVariable(FlowableConstants.contextVariables.NUMOFDEPLOYEDMODULES);
String processInstanceId = execution.getProcessInstanceId();
DeployHandlerV2 deployHandlerV2 = getV2();
//getting execution variable from flowable context
String service = ((Map<String, List<String>>) execution.getVariable(FlowableConstants.contextVariables.SERVICENAMESMAP)).get(moduleName).get(0);
String location = ((Map<String, String>) execution.getVariable(FlowableConstants.contextVariables.FILELOCATIONMAP)).get(moduleName);
Map<String, Object> configMap = (Map<String, Object>) ((Map<String, Object>) execution.getVariable(FlowableConstants.contextVariables.MODULECONFIGMAP)).getOrDefault(moduleName, null);
String mtarPath = (String) execution.getVariable(FlowableConstants.contextVariables.MTAR);
File mtar = new File(mtarPath);
String user = (String) execution.getVariable(FlowableConstants.contextVariables.USERNAME);
String zoneID = (String) execution.getVariable(FlowableConstants.contextVariables.ZONE);
if (StringUtils.isEmpty(user)) {
user = zoneID;
}
Map<String, Object> outputMap = new HashMap<>();
Map<String, Object> resultMap = new HashMap<>();
try {
DeployerClientConfiguration deployerClientConfiguration = (DeployerClientConfiguration) execution.getVariable(moduleName);//fetches object from memory
logger.info(MessageFormat.format(CASEngineConstants.ImportLogs.START_DEPLOYMENT, processInstanceId, moduleName));
String token = deployerClientConfiguration.getToken();
String version = deployerClientConfiguration.getVersion();
Map<String, String> headerInfo = deployerClientConfiguration.getHeaders();
try (CloseableHttpClient closeableHttpClient = httpRequestExecutor.getCustomClient(token, headerInfo)) {
if (GACDConstants.Versions.v2.equalsIgnoreCase(version)) {
deployHandlerV2.init(closeableHttpClient, deployerClientConfiguration.getProviderUrl(), deployerClientConfiguration.getPath());
resultMap = deployHandlerV2.deployV2(mtar, location, configMap, user, moduleName, processInstanceId);
}
}
resultMap.put(CASEngineConstants.Common.CONTENTTYPE, service);
String contentId = (String) resultMap.getOrDefault(FlowableConstants.contextVariables.CONTENTID, null);
if (StringUtils.isNotEmpty(contentId)) {
moduleDeploymentDetails.put(FlowableConstants.contextVariables.CONTENTID, contentId);
}
String deployResourceId = (String) resultMap.getOrDefault(FlowableConstants.contextVariables.DEPLOYRESOURCEID, null);
if (StringUtils.isNotEmpty(deployResourceId)) {
moduleDeploymentDetails.put(FlowableConstants.contextVariables.DEPLOYRESOURCEID, deployResourceId);
} else {
logger.info(MessageFormat.format(CASEngineConstants.ImportLogs.NO_DEPLOYRESOURCEID_FOUND, processInstanceId, moduleName));
dbOperationManager.saveLogs(MessageFormat.format(CASEngineConstants.ImportLogs.NO_DEPLOYRESOURCEID_FOUND, processInstanceId, moduleName), processInstanceId, CASEngineConstants.severity.ERROR, zoneID, "");
throw new CASException(MessageFormat.format(CASEngineConstants.ImportLogs.NO_DEPLOYRESOURCEID_FOUND, processInstanceId, moduleName), HttpStatus.INTERNAL_SERVER_ERROR);
}
dbOperationManager.saveLogs(MessageFormat.format(CASEngineConstants.ImportLogs.DEPLOYMENT_STARTED, moduleName), processInstanceId, CASEngineConstants.severity.INFO, zoneID, null);
//setting deploymentDetails
deploymentDetails.put(moduleName, moduleDeploymentDetails);
outputMap.put(FlowableConstants.contextVariables.deploymentDetails, deploymentDetails);
//increasing NUMOFDEPLOYEDMODULES to move on to next module
outputMap.put(FlowableConstants.contextVariables.NUMOFDEPLOYEDMODULES, ++numOfDeployedModules);
//update flag UPLOAD_AND_DEPLOY_FAILED in flowable context to false
outputMap.put(FlowableConstants.contextVariables.isUploadAndDeployFailed, false);
} catch (Exception ex) {
outputMap.put(FlowableConstants.contextVariables.isUploadAndDeployFailed, true);
outputMap.put(FlowableConstants.contextVariables.ERRORMSG, ex.getMessage());
}
return outputMap;
}
}