Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.arkea.activiti.credit.handler;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- import org.activiti.engine.ActivitiException;
- import org.activiti.engine.HistoryService;
- import org.activiti.engine.RuntimeService;
- import org.activiti.engine.history.HistoricActivityInstance;
- import org.activiti.engine.history.HistoricProcessInstance;
- import org.activiti.engine.runtime.ProcessInstance;
- import org.apache.commons.lang3.EnumUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.thrift.TException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import com.arkea.activiti.credit.util.MapperProcessUtil;
- import com.arkea.activiti.credit.util.ProcessDefinition;
- import com.arkea.activiti.listener.LoggerExecutionListener;
- import com.arkea.activiti.utils.LoggerUtils;
- import com.arkea.domi.bpm.thrift.credit.data.HistoriqueProcess;
- import com.arkea.domi.bpm.thrift.credit.data.PersonneProcess;
- import com.arkea.domi.bpm.thrift.credit.data.ProcessStatus;
- import com.arkea.domi.bpm.thrift.credit.exception.CreditErrorCode;
- import com.arkea.domi.bpm.thrift.credit.exception.CreditException;
- import com.arkea.domi.bpm.thrift.credit.io.GetHistoriqueProcessRequest;
- import com.arkea.domi.bpm.thrift.credit.io.GetHistoriqueProcessResponse;
- import com.arkea.domi.bpm.thrift.credit.io.GetStatusProcessRequest;
- import com.arkea.domi.bpm.thrift.credit.io.GetStatusProcessResponse;
- import com.arkea.domi.bpm.thrift.credit.services.Credit;
- /**
- * Thrift Handler class for credit Iface interface
- *
- * @author c2873
- */
- @Component
- public class CreditIfaceHandler implements Credit.Iface {
- private static final Logger LOG = LoggerFactory.getLogger(CreditIfaceHandler.class);
- @Autowired
- private RuntimeService runtimeService;
- @Autowired
- private HistoryService historyService;
- /**
- * Set current thread name
- *
- * @param suffix
- * the thread suffix (should be method name)
- */
- private void setThreadName(final String suffix) {
- final Thread thread = Thread.currentThread(); // NOPMD
- thread.setName("Thrift-Credit-" + thread.getId() + "-" + suffix);
- }
- @Override
- @SuppressWarnings("unchecked")
- public GetStatusProcessResponse getStatusProcess(GetStatusProcessRequest request) throws CreditException, TException {
- setThreadName("getStatusProcess");
- final long start = new Date().getTime();
- LOG.debug("Enter in getStatusProcess");
- LoggerUtils.logThriftRequest(LOG, request, "credit.Credit.getStatusProcess");
- try {
- if (StringUtils.isBlank(request.getBusinessKey())) {
- throw new Exception("Numero de dossier vide");
- }
- ProcessInstance instance = runtimeService.createProcessInstanceQuery()
- .processInstanceBusinessKey(request.getBusinessKey()).singleResult();
- GetStatusProcessResponse response = new GetStatusProcessResponse();
- String idEne = runtimeService.getVariable(instance.getId(), ProcessDefinition.VAR_ID_ENE).toString();
- String jsonListPersonne = runtimeService.getVariable(instance.getId(), ProcessDefinition.VAR_LIST_PERSONNE).toString();
- String activityId = instance.getActivityId();
- if (activityId == null) {
- activityId = ProcessDefinition.ST_RECEPTION_OFFRE;
- }
- List<PersonneProcess> list = Arrays.asList(MapperProcessUtil.getMapper().readValue(jsonListPersonne,
- PersonneProcess[].class));
- response.setPersonneStatus(list);
- response.setIdEne(idEne);
- if (EnumUtils.isValidEnum(ProcessStatus.class, activityId)) {
- response.setProcessStatus(ProcessStatus.valueOf(activityId));
- } else {
- response.setProcessStatus(ProcessStatus.ERROR);
- LOG.error("Erreur sur le dossier: " + request.getBusinessKey());
- LOG.error("Status Non valide : " + activityId);
- LOG.error("Identifiant Processus : " + instance.getId());
- }
- response.setRequest(request);
- return response;
- } catch (ActivitiException eActiviti) {
- LOG.error("Activiti Error in getStatusProcess : " + eActiviti.getMessage(), eActiviti);
- final CreditException error = new CreditException(); // NOPMD - Thrift
- // exception: stack trace is set latter
- error.setErrorCode(CreditErrorCode.PROCESS_NOT_FOUND);
- error.setMessage(eActiviti.getMessage());
- error.setStackTrace(eActiviti.getStackTrace());
- throw error;
- } catch (final Exception e) {
- LOG.error("Unexpected getStatusProcess error: " + e.getMessage(), e);
- LOG.error("Dossier Credics liΓ© : " + request.getBusinessKey());
- final CreditException error = new CreditException(); // NOPMD - Thrift
- // exception: stack trace is set latter
- error.setErrorCode(CreditErrorCode.UNKNOWN);
- error.setMessage(e.getMessage());
- error.setStackTrace(e.getStackTrace());
- throw error;
- } finally {
- LOG.debug("Exit getStatusProcess");
- LoggerUtils.logServiceTime(LOG, "credit.Credit.getStatusProcess", new Date().getTime() - start);
- }
- }
- @Override
- public GetHistoriqueProcessResponse getHistoriqueProcess(GetHistoriqueProcessRequest request) throws CreditException,
- TException {
- setThreadName("getHistoriqueProcess");
- final long start = new Date().getTime();
- LOG.debug("Enter in getHistoriqueProcess");
- LoggerUtils.logThriftRequest(LOG, request, "credit.Credit.getHistoriqueProcess");
- try {
- HistoricProcessInstance historyPInstance = historyService.createHistoricProcessInstanceQuery()
- .processInstanceBusinessKey(request.getBusinessKey()).singleResult();
- List<HistoricActivityInstance> history = historyService.createHistoricActivityInstanceQuery()
- .processInstanceId(historyPInstance.getId()).orderByHistoricActivityInstanceStartTime().asc().list();
- GetHistoriqueProcessResponse response = new GetHistoriqueProcessResponse();
- List<HistoriqueProcess> historyProcess = new ArrayList<HistoriqueProcess>();
- // Initialisation de l'enum
- ProcessStatus status = null;
- for (HistoricActivityInstance activity : history) {
- if (EnumUtils.isValidEnum(ProcessStatus.class, activity.getActivityId())) {
- status = ProcessStatus.valueOf(activity.getActivityId());
- } else {
- continue;
- }
- long endTime = 0L;
- long duration = 0L;
- boolean isFinished = false;
- if (activity.getEndTime() != null) {
- endTime = activity.getEndTime().getTime();
- isFinished = true;
- duration = activity.getDurationInMillis();
- }
- historyProcess.add(new HistoriqueProcess(status, activity.getStartTime().getTime(), endTime, duration, isFinished));
- }
- response.setHistoriqueProcess(historyProcess);
- response.setProcessIsFinished(historyPInstance.getEndTime() != null);
- response.setRequest(request);
- return response;
- } catch (ActivitiException eActiviti) {
- LOG.error("Activiti Error in getHistoriqueProcess : " + eActiviti.getMessage(), eActiviti);
- final CreditException error = new CreditException(); // NOPMD - Thrift
- // exception: stack trace is set latter
- error.setErrorCode(CreditErrorCode.PROCESS_NOT_FOUND);
- error.setMessage(eActiviti.getMessage());
- error.setStackTrace(eActiviti.getStackTrace());
- throw error;
- } catch (final Exception e) {
- LOG.error("Unexpected getHistoriqueProcess error: " + e.getMessage(), e);
- final CreditException error = new CreditException(); // NOPMD - Thrift
- // exception: stack trace is set latter
- error.setErrorCode(CreditErrorCode.UNKNOWN);
- error.setMessage(e.getMessage());
- error.setStackTrace(e.getStackTrace());
- throw error;
- } finally {
- LOG.debug("Exit getHistoriqueProcess");
- LoggerUtils.logServiceTime(LOG, "credit.Credit.getHistoriqueProcess", new Date().getTime() - start);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement