Advertisement
Guest User

Untitled

a guest
May 25th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1.  
  2. package com.arkea.activiti.credit.handler;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Date;
  7. import java.util.List;
  8.  
  9. import org.activiti.engine.ActivitiException;
  10. import org.activiti.engine.HistoryService;
  11. import org.activiti.engine.RuntimeService;
  12. import org.activiti.engine.history.HistoricActivityInstance;
  13. import org.activiti.engine.history.HistoricProcessInstance;
  14. import org.activiti.engine.runtime.ProcessInstance;
  15. import org.apache.commons.lang3.EnumUtils;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.apache.thrift.TException;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Component;
  22.  
  23. import com.arkea.activiti.credit.util.MapperProcessUtil;
  24. import com.arkea.activiti.credit.util.ProcessDefinition;
  25. import com.arkea.activiti.listener.LoggerExecutionListener;
  26. import com.arkea.activiti.utils.LoggerUtils;
  27. import com.arkea.domi.bpm.thrift.credit.data.HistoriqueProcess;
  28. import com.arkea.domi.bpm.thrift.credit.data.PersonneProcess;
  29. import com.arkea.domi.bpm.thrift.credit.data.ProcessStatus;
  30. import com.arkea.domi.bpm.thrift.credit.exception.CreditErrorCode;
  31. import com.arkea.domi.bpm.thrift.credit.exception.CreditException;
  32. import com.arkea.domi.bpm.thrift.credit.io.GetHistoriqueProcessRequest;
  33. import com.arkea.domi.bpm.thrift.credit.io.GetHistoriqueProcessResponse;
  34. import com.arkea.domi.bpm.thrift.credit.io.GetStatusProcessRequest;
  35. import com.arkea.domi.bpm.thrift.credit.io.GetStatusProcessResponse;
  36. import com.arkea.domi.bpm.thrift.credit.services.Credit;
  37.  
  38. /**
  39. * Thrift Handler class for credit Iface interface
  40. *
  41. * @author c2873
  42. */
  43. @Component
  44. public class CreditIfaceHandler implements Credit.Iface {
  45.  
  46. private static final Logger LOG = LoggerFactory.getLogger(CreditIfaceHandler.class);
  47.  
  48. @Autowired
  49. private RuntimeService runtimeService;
  50.  
  51. @Autowired
  52. private HistoryService historyService;
  53.  
  54. /**
  55. * Set current thread name
  56. *
  57. * @param suffix
  58. * the thread suffix (should be method name)
  59. */
  60. private void setThreadName(final String suffix) {
  61. final Thread thread = Thread.currentThread(); // NOPMD
  62. thread.setName("Thrift-Credit-" + thread.getId() + "-" + suffix);
  63. }
  64.  
  65. @Override
  66. @SuppressWarnings("unchecked")
  67. public GetStatusProcessResponse getStatusProcess(GetStatusProcessRequest request) throws CreditException, TException {
  68. setThreadName("getStatusProcess");
  69. final long start = new Date().getTime();
  70.  
  71. LOG.debug("Enter in getStatusProcess");
  72. LoggerUtils.logThriftRequest(LOG, request, "credit.Credit.getStatusProcess");
  73. try {
  74. if (StringUtils.isBlank(request.getBusinessKey())) {
  75. throw new Exception("Numero de dossier vide");
  76. }
  77. ProcessInstance instance = runtimeService.createProcessInstanceQuery()
  78. .processInstanceBusinessKey(request.getBusinessKey()).singleResult();
  79. GetStatusProcessResponse response = new GetStatusProcessResponse();
  80. String idEne = runtimeService.getVariable(instance.getId(), ProcessDefinition.VAR_ID_ENE).toString();
  81. String jsonListPersonne = runtimeService.getVariable(instance.getId(), ProcessDefinition.VAR_LIST_PERSONNE).toString();
  82.  
  83. String activityId = instance.getActivityId();
  84.  
  85. if (activityId == null) {
  86. activityId = ProcessDefinition.ST_RECEPTION_OFFRE;
  87. }
  88.  
  89. List<PersonneProcess> list = Arrays.asList(MapperProcessUtil.getMapper().readValue(jsonListPersonne,
  90. PersonneProcess[].class));
  91. response.setPersonneStatus(list);
  92. response.setIdEne(idEne);
  93.  
  94. if (EnumUtils.isValidEnum(ProcessStatus.class, activityId)) {
  95. response.setProcessStatus(ProcessStatus.valueOf(activityId));
  96. } else {
  97. response.setProcessStatus(ProcessStatus.ERROR);
  98. LOG.error("Erreur sur le dossier: " + request.getBusinessKey());
  99. LOG.error("Status Non valide : " + activityId);
  100. LOG.error("Identifiant Processus : " + instance.getId());
  101. }
  102. response.setRequest(request);
  103. return response;
  104. } catch (ActivitiException eActiviti) {
  105. LOG.error("Activiti Error in getStatusProcess : " + eActiviti.getMessage(), eActiviti);
  106. final CreditException error = new CreditException(); // NOPMD - Thrift
  107. // exception: stack trace is set latter
  108. error.setErrorCode(CreditErrorCode.PROCESS_NOT_FOUND);
  109. error.setMessage(eActiviti.getMessage());
  110. error.setStackTrace(eActiviti.getStackTrace());
  111. throw error;
  112.  
  113. } catch (final Exception e) {
  114. LOG.error("Unexpected getStatusProcess error: " + e.getMessage(), e);
  115. LOG.error("Dossier Credics liΓ© : " + request.getBusinessKey());
  116. final CreditException error = new CreditException(); // NOPMD - Thrift
  117. // exception: stack trace is set latter
  118. error.setErrorCode(CreditErrorCode.UNKNOWN);
  119. error.setMessage(e.getMessage());
  120. error.setStackTrace(e.getStackTrace());
  121. throw error;
  122.  
  123. } finally {
  124. LOG.debug("Exit getStatusProcess");
  125. LoggerUtils.logServiceTime(LOG, "credit.Credit.getStatusProcess", new Date().getTime() - start);
  126. }
  127. }
  128.  
  129. @Override
  130. public GetHistoriqueProcessResponse getHistoriqueProcess(GetHistoriqueProcessRequest request) throws CreditException,
  131. TException {
  132. setThreadName("getHistoriqueProcess");
  133. final long start = new Date().getTime();
  134.  
  135. LOG.debug("Enter in getHistoriqueProcess");
  136. LoggerUtils.logThriftRequest(LOG, request, "credit.Credit.getHistoriqueProcess");
  137. try {
  138. HistoricProcessInstance historyPInstance = historyService.createHistoricProcessInstanceQuery()
  139. .processInstanceBusinessKey(request.getBusinessKey()).singleResult();
  140. List<HistoricActivityInstance> history = historyService.createHistoricActivityInstanceQuery()
  141. .processInstanceId(historyPInstance.getId()).orderByHistoricActivityInstanceStartTime().asc().list();
  142.  
  143. GetHistoriqueProcessResponse response = new GetHistoriqueProcessResponse();
  144. List<HistoriqueProcess> historyProcess = new ArrayList<HistoriqueProcess>();
  145.  
  146. // Initialisation de l'enum
  147. ProcessStatus status = null;
  148. for (HistoricActivityInstance activity : history) {
  149. if (EnumUtils.isValidEnum(ProcessStatus.class, activity.getActivityId())) {
  150. status = ProcessStatus.valueOf(activity.getActivityId());
  151. } else {
  152. continue;
  153. }
  154. long endTime = 0L;
  155. long duration = 0L;
  156. boolean isFinished = false;
  157.  
  158. if (activity.getEndTime() != null) {
  159. endTime = activity.getEndTime().getTime();
  160. isFinished = true;
  161. duration = activity.getDurationInMillis();
  162. }
  163. historyProcess.add(new HistoriqueProcess(status, activity.getStartTime().getTime(), endTime, duration, isFinished));
  164. }
  165.  
  166. response.setHistoriqueProcess(historyProcess);
  167. response.setProcessIsFinished(historyPInstance.getEndTime() != null);
  168. response.setRequest(request);
  169. return response;
  170. } catch (ActivitiException eActiviti) {
  171. LOG.error("Activiti Error in getHistoriqueProcess : " + eActiviti.getMessage(), eActiviti);
  172. final CreditException error = new CreditException(); // NOPMD - Thrift
  173. // exception: stack trace is set latter
  174. error.setErrorCode(CreditErrorCode.PROCESS_NOT_FOUND);
  175. error.setMessage(eActiviti.getMessage());
  176. error.setStackTrace(eActiviti.getStackTrace());
  177. throw error;
  178.  
  179. } catch (final Exception e) {
  180. LOG.error("Unexpected getHistoriqueProcess error: " + e.getMessage(), e);
  181. final CreditException error = new CreditException(); // NOPMD - Thrift
  182. // exception: stack trace is set latter
  183. error.setErrorCode(CreditErrorCode.UNKNOWN);
  184. error.setMessage(e.getMessage());
  185. error.setStackTrace(e.getStackTrace());
  186. throw error;
  187.  
  188. } finally {
  189. LOG.debug("Exit getHistoriqueProcess");
  190. LoggerUtils.logServiceTime(LOG, "credit.Credit.getHistoriqueProcess", new Date().getTime() - start);
  191. }
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement