import javax.xml.namespace.QName; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.appiancorp.common.xml.JaxbConversionException; import com.appiancorp.common.xml.JaxbConverter; import com.appiancorp.services.ServiceContext; import com.appiancorp.services.ServiceContextFactory; import com.appiancorp.suiteapi.cfg.ConfigurationLoader; import com.appiancorp.suiteapi.common.ServiceLocator; import com.appiancorp.suiteapi.common.exceptions.InvalidPriorityException; import com.appiancorp.suiteapi.common.exceptions.InvalidProcessModelException; import com.appiancorp.suiteapi.common.exceptions.InvalidStateException; import com.appiancorp.suiteapi.common.exceptions.InvalidUserException; import com.appiancorp.suiteapi.common.exceptions.InvalidVersionException; import com.appiancorp.suiteapi.common.exceptions.PrivilegeException; import com.appiancorp.suiteapi.common.exceptions.StorageLimitException; import com.appiancorp.suiteapi.process.ProcessDesignService; import com.appiancorp.suiteapi.process.ProcessExecutionService; import com.appiancorp.suiteapi.process.ProcessStartConfig; import com.appiancorp.suiteapi.process.ProcessVariable; import com.appiancorp.suiteapi.process.ProcessVariableInstance; import com.appiancorp.suiteapi.process.exceptions.InvalidProcessException; import com.appiancorp.suiteapi.process.exceptions.InvalidProcessVariableNameException; import com.appiancorp.suiteapi.type.TypeService; public class AppianHandler { ServiceContext adminContext; ProcessDesignService pds; TypeService ts; ProcessExecutionService pes; Long parameterTypeId = null; Long bulkAddParameterTypeId = null; Long pmAssignmentTypeId = null; Long sendEmailTypeId = null; static Log log = LogFactory.getLog(AppianHandler.class); /** * Initialization method, sets up appian connection * */ public void init() { CacheConfigLoaderOverride.initializeDefaultCache(); //adminContext = ServiceLocator.getAdministratorServiceContext(); //ConfigurationLoader.initializeConfigurations(); adminContext = ServiceContextFactory.getAdministratorServiceContext(); log.info("adminContext: "+adminContext); pds = ServiceLocator.getProcessDesignService(adminContext); //pes = ServiceLocator.getProcessExecutionService(adminContext); ts = ServiceLocator.getTypeService(adminContext); parameterTypeId = ts.getTypeByQualifiedName(new QName("http://appian.domain/dto","AppianProcessObject")).getId(); log.info(">>Appian Response on AppianProcessObject id as "+parameterTypeId); bulkAddParameterTypeId = ts.getTypeByQualifiedName(new QName("http://appian.domain/dto","BulkAddProcessObject")).getId(); log.info(">>Appian Response on BulkAddProcessObject id as "+bulkAddParameterTypeId); pmAssignmentTypeId = ts.getTypeByQualifiedName(new QName("http://appian.domain/dto","PMAssignmentEntity")).getId(); log.info(">>Appian Response on pmAssignmentTypeId id as "+pmAssignmentTypeId); sendEmailTypeId = ts.getTypeByQualifiedName(new QName("http://appian.domain/dto","EmailDetail")).getId(); log.info(">>Appian Response on EmailDetail id as " + sendEmailTypeId); } /** * Start an appian process */ @SuppressWarnings("deprecation") public long startProcess(AppianProcessObject po, long pmId, String parameterName){ /* 10-digit fix*/ populateTextField(po); /* fix ends*/ long processID = 0; ProcessVariable[] processParameters = new ProcessVariable[1]; ProcessVariable parameter = new ProcessVariable(); parameter.setInstanceType(parameterTypeId); parameter.setName(parameterName); parameter.setParameter(true); try { log.info("short name: " +po.getEntityDetail().getShortName()); log.info("entity type: " +po.getEntityDetail().getEntityType()); log.info("alt id: " +po.getEntityDetail().getAltId()); log.info("entity id: " +po.getEntityDetail().getDatalessKey()); parameter.setValue(JaxbConverter.toTypedValue(po, parameterTypeId, ts).getValue()); } catch (JaxbConversionException e) { throw new AppianServiceException("AppianHandler - Appian process - JaxbConversion Error: "+e.getMessage(), e); } processParameters[0] = parameter; try { processID = pds.initiateProcess(pmId, new ProcessStartConfig(processParameters)); } catch (InvalidProcessModelException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - InvalidProcessModelException: "+e.getMessage(), e); } catch (InvalidPriorityException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - InvalidPriorityException: "+e.getMessage(), e); } catch (InvalidVersionException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - InvalidVersionException: "+e.getMessage(), e); } catch (PrivilegeException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - PrivilegeException: "+e.getMessage(), e); } catch (InvalidStateException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - InvalidStateException: "+e.getMessage(), e); } catch (StorageLimitException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - StorageLimitException: "+e.getMessage(), e); } catch (InvalidUserException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - InvalidUserException: "+e.getMessage(), e); } catch (IllegalArgumentException e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - IllegalArgumentException: "+e.getMessage(), e); } catch (Exception e) { throw new AppianServiceException("AppianHandler - Failed to start Appian process - General Exception: "+e.getMessage(), e); } return processID; } /** * Update an existing process' details */ @SuppressWarnings("deprecation") public void updateProcessVariables(long processID, AppianProcessObject ec, String parameterName){ /* 10-digit fix*/ populateTextField(ec); /* fix ends*/ try { ProcessVariableInstance pvi; pvi = pes.getProcessVariable(processID, parameterName); pvi.setValue(pvi.getRunningValue()); pvi.setRunningValue(JaxbConverter.toTypedValue(ec, pvi.getInstanceType(), ts).getValue()); pes.setProcessVariable(processID, pvi); } catch (InvalidProcessException e) { throw new AppianServiceException("AppianHandler - Failed to update Appian process variables - InvalidProcessException: "+e.getMessage(), e); } catch (InvalidProcessVariableNameException e) { throw new AppianServiceException("AppianHandler - Failed to update Appian process variables - InvalidProcessVariableNameException: "+e.getMessage(), e); } catch (PrivilegeException e) { throw new AppianServiceException("AppianHandler - Failed to update Appian process variables - PrivilegeException: "+e.getMessage(), e); } catch (JaxbConversionException e) { throw new AppianServiceException("AppianHandler - Failed to update Appian process variables - JaxbConversionException: "+e.getMessage(), e); } } /** * Get an existing process' details */ @SuppressWarnings("deprecation") public AppianProcessObject getProcessVariables(long processID, String parameterName){ AppianProcessObject ec = new AppianProcessObject(); ProcessVariableInstance pvi; try { pvi = pes.getProcessVariable(processID, parameterName); pvi.setValue(pvi.getRunningValue()); ec = JaxbConverter.toObject(pvi, AppianProcessObject.class, ts); } catch (InvalidProcessException e) { throw new AppianServiceException("AppianHandler - Failed to retrieve Appian process variables - InvalidProcessException: "+e.getMessage(), e); } catch (InvalidProcessVariableNameException e) { throw new AppianServiceException("AppianHandler - Failed to retrieve Appian process variables - InvalidProcessVariableNameException: "+e.getMessage(), e); } catch (PrivilegeException e) { throw new AppianServiceException("AppianHandler - Failed to retrieve Appian process variables - PrivilegeException: "+e.getMessage(), e); } catch (JaxbConversionException e) { throw new AppianServiceException("AppianHandler - Failed to retrieve Appian process variables - JaxbConversionException: "+e.getMessage(), e); } /* 10-digit fix*/ populateLongField(ec); /* fix ends*/ return ec; } public Long getProcessModelId(String pmUUID){ try { return pds.getProcessModelIdByUuid(pmUUID); } catch (InvalidProcessModelException e) { throw new AppianServiceException("AppianHandler - Failed to retrieve Appian process model Id - InvalidProcessModelException: "+e.getMessage(), e); } catch (PrivilegeException e) { throw new AppianServiceException("AppianHandler - Failed to update Appian process variables - PrivilegeException: "+e.getMessage(), e); } } }