Advertisement
Ladies_Man

#sonic autorunBackup 1021_initial

Nov 15th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.85 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.ResultSet;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10.  
  11. import org.apache.commons.logging.Log;
  12. import org.apache.commons.logging.LogFactory;
  13. import org.joda.time.DateTime;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15.  
  16. import ru.it.metasonic.ruspost.infopoint.utils.AttributeHelper;
  17. import ru.it.metasonic.ruspost.infopoint.utils.BOHelper;
  18. import ru.lbpm.metasonic.businessobjects.DbHelper;
  19. import ru.lbpm.metasonic.businessobjects.DbHelper.ValueWrapper;
  20.  
  21. import com.jcom1.api.dto.init.InitProcessInstanceData;
  22. import com.jcom1.api.dto.interfaces.IProcessInstanceBean;
  23. import com.jcom1.api.dto.interfaces.IStorageInstanceBean;
  24. import com.jcom1.api.dto.interfaces.ISubjectInstanceBean;
  25. import com.jcom1.api.exceptions.ConnectionException;
  26. import com.jcom1.api.interfaces.IAuthorizationAdministrationApi;
  27. import com.jcom1.api.interfaces.IAuthorizationApi;
  28. import com.jcom1.api.interfaces.IBo20AccessApi;
  29. import com.jcom1.api.interfaces.ICommonApi;
  30. import com.jcom1.api.interfaces.IInstanceAdministrationApi;
  31. import com.jcom1.api.util.Pair;
  32. import com.jcom1.runtime.refinement.AbstractRefinement;
  33. import com.jcom1.runtime.refinement.FunctionStateRequest;
  34. import com.jcom1.runtime.refinement.FunctionStateResponse;
  35. import com.jcom1.runtime.refinement.RefinementGenerator;
  36.  
  37. import de.metasonic.businessobjects.model.interfaces.IView;
  38. import de.metasonic.refinement.global.api.ApiUtils;
  39.  
  40. /**
  41.  * @generated
  42.  */
  43. /**
  44.  * This method is called when the named function state is reached State:
  45.  * автозапуск формирования StateType: FUNCTION
  46.  *
  47.  * Return values "FunctionTransitionDescription_WZGO8WRYEeaW6tb5t7D8Ag" when
  48.  * result is "" to go to state Ожидание запуска
  49.  *
  50.  * @return return the id of the transition to follow e.g. // return new
  51.  *         FunctionStateResponse("transitionXXXXXXXX");
  52.  */
  53. @RefinementGenerator(id = "InternalSubjectDescription_rhsOwWRXEeaW6tb5t7D8Ag")
  54. public class Оператор_по_контролю_ОПС_ОтделаRefinement extends
  55.         AbstractRefinement {
  56.  
  57.     @Autowired
  58.     ICommonApi commonApi;
  59.  
  60.     @Autowired
  61.     IBo20AccessApi accessApi;
  62.  
  63.     @Autowired
  64.     IAuthorizationApi authApi;
  65.  
  66.     @Autowired
  67.     IAuthorizationAdministrationApi administrationApi;
  68.  
  69.     private final String opsControlDescriptionId = "ProcessDescription_pn9a4VMdEeaL5PGyiwnPkA";
  70.     private final String opsControlFirstStateDescrId = "FunctionStateDescription_NZI2wWqtEeaiwr50C3BsOA";
  71.     private final String opsControlRecieveStateDescrId = "ReceiveStateDescription_qpvqI2UHEea_wrD0qXtRKA";
  72.     private final String opsControlFirstStateTransitionDescrId = "FunctionTransitionDescription_1_ah0WqtEeaiwr50C3BsOA";
  73.  
  74.     private final String DB_URL = "jdbc:hsqldb:hsql://localhost/jcom1";
  75.     private final String DB_USER = "sa";
  76.     private final String DB_PSW = "";
  77.  
  78.     private final String UFPS_INDEX = "UFPS_INDEX";
  79.     private final String UFPS_NAME = "UFPS_NAME";
  80.     private final String POSTAMT_INDEX = "POSTAMT_INDEX";
  81.     private final String POSTAMT_NAME = "POSTAMT_NAME";
  82.     private final String UM_GROUP = "UM_GROUP";
  83.     private final String UM_USER = "UM_USER";
  84.     private final String UM_GROUP_AS_INDEX = "INDX";
  85.     private final String UM_GROUP_AS_ROLE = "ROLE";
  86.  
  87.     private final String DEPARTMENT_CONTROLLER_GROUP_ID = "DEPARTMENT_CONTROLLER_GROUP_ID";
  88.  
  89.     private final String COMMON_INFO = "commonInfo";
  90.     private final String POSTAMT = "infoPoint";
  91.     private final String METADATA = "metaData";
  92.  
  93.     private int departmentControllerGroupId;
  94.     /**
  95.      * @generated
  96.      */
  97.     private final static Log log = LogFactory
  98.             .getLog("com.jcom1.refinement.автозапуск_автоформирования_плана.оператор_по_контролю_опс_отделаrefinement");
  99.  
  100.     /**
  101.      * This method is called when the named function state is reached State:
  102.      * автозапуск формирования StateType: FUNCTION
  103.      *
  104.      * Return values "FunctionTransitionDescription_WZGO8WRYEeaW6tb5t7D8Ag" when
  105.      * result is "" to go to state Ожидание запуска
  106.      *
  107.      * @return return the id of the transition to follow e.g. // return new
  108.      *         FunctionStateResponse("transitionXXXXXXXX");
  109.      */
  110.     @RefinementGenerator(id = "FunctionStateDescription_G_53Y2RYEeaW6tb5t7D8Ag")
  111.     public FunctionStateResponse functionавтозапуск_формирования(
  112.             FunctionStateRequest functionStateRequest) {
  113.         log.debug("\n");
  114.  
  115.         IInstanceAdministrationApi instanceAdminApi = null;
  116.         try {
  117.             instanceAdminApi = ApiUtils.lookupInstanceAdministrationApi();
  118.         } catch (ConnectionException e1) {
  119.             log.error("error retrieving instanceAdministrationApi");
  120.         }
  121.  
  122.         String departmentControllerGroupIdQuery = "SELECT ID AS DEPARTMENT_CONTROLLER_GROUP_ID FROM UM_GROUP WHERE NAME = ?";
  123.  
  124.         String ufpsQuery = "SELECT DISTINCT UFPS_INDEX, UFPS_NAME FROM POSTAMT_VIEW";
  125.  
  126.         // query to get user belonging to RUSPOST_DEPARTMENT_CONTROLLER group
  127.         // and a right region group
  128.         String potentialOwnersQuery = "SELECT A.UM_GROUP AS UFPS_INDEX, A.UM_USER, B.UM_GROUP AS DEPARTMENT_CONTROLLER_GROUP_ID "
  129.                 + "FROM UM_GROUP_UM_USER A "
  130.                 + "LEFT JOIN UM_GROUP_UM_USER B "
  131.                 + "ON A.UM_USER = B.UM_USER "
  132.                 + "WHERE A.UM_GROUP = ? AND B.UM_GROUP = ?";
  133.  
  134.         String postamtQuery = "SELECT POSTAMT_INDEX, POSTAMT_NAME FROM POSTAMT_VIEW WHERE UFPS_INDEX = ?";
  135.  
  136.         try (final Connection connection = DbHelper
  137.                 .getConnection(DbHelper.PROP_DB_INT);) {
  138.             log.debug("connection has been set");
  139.  
  140.             // form query as SELECT TOP 1 ID AS ...
  141.             departmentControllerGroupId = getDepartmentControllerGroupId(
  142.                     connection, departmentControllerGroupIdQuery,
  143.                     AttributeHelper.RUSPOST_DEPARTMENT_CONTROLLER_ROLE);
  144.             log.debug(DEPARTMENT_CONTROLLER_GROUP_ID + ": "
  145.                     + departmentControllerGroupId);
  146.  
  147.             // get all ufps
  148.             List<Pair> ufpsIndexNamePairs = getUfpsIndexNamePairs(connection,
  149.                     ufpsQuery);
  150.  
  151.             for (Pair ufpsIndexNamePair : ufpsIndexNamePairs) {
  152.  
  153.                 String ufpsIndex = ufpsIndexNamePair.getFirstElement();
  154.                 String ufpsName = ufpsIndexNamePair.getSecondElement();
  155.                 log.debug("     ufps: " + ufpsIndex + " / " + ufpsName);
  156.  
  157.                 // retrieve potential owners as:
  158.                 // users from current Ufps group that also belong to
  159.                 // RUSPOST-DEPARTMENT-CONTROLLER group
  160.                 // form query as ... WHERE a.UM_GROUP = 249030 AND b.UM_GROUP =
  161.                 // 13
  162.                 List<String> potentialOwners = getPotentialOwners(connection,
  163.                         potentialOwnersQuery, Integer.valueOf(ufpsIndex),
  164.                         departmentControllerGroupId);
  165.  
  166.                 if (0 == potentialOwners.size()) {
  167.                     log.warn("POTENTIAL ERROR in the system configuration. Skipping plan process creation for Ufps: "
  168.                             + ufpsIndex
  169.                             + "/"
  170.                             + ufpsName
  171.                             + "Reason: no potential owners were found for this Ufps");
  172.                     continue;
  173.                 }
  174.  
  175.                 // now as we got potOwners list
  176.                 // lets iterate over all postamts of current ufps
  177.                 List<Pair> postamtIndexNamePairs = getPostamtIndexNamePairs(
  178.                         connection, postamtQuery, ufpsIndex);
  179.  
  180.                 for (Pair postamtIndexNamePair : postamtIndexNamePairs) {
  181.  
  182.                     String postamtIndex = postamtIndexNamePair
  183.                             .getFirstElement();
  184.                     String postamtName = postamtIndexNamePair
  185.                             .getSecondElement();
  186.                     log.debug("     postamt: " + postamtIndex + " / "
  187.                             + postamtName);
  188.  
  189.                     // ============================================================================================================
  190.                     // now as we got single postamt
  191.                     // lets create a process for it
  192.                     log.debug("launching process");
  193.                     IProcessInstanceBean processInstanceBean = null;
  194.                     String procName = null;
  195.                     try {
  196.                         SimpleDateFormat sdf = new SimpleDateFormat(
  197.                                 "dd/MM/yyyy HH:mm:ss");
  198.                         procName = postamtName + " Контроль ОПС ["
  199.                                 + sdf.format(new Date()) + "]";
  200.                         String creatorId = commonApi.getSubjectInstanceBean(
  201.                                 functionStateRequest.getSubjectInstanceId())
  202.                                 .getEditor();
  203.                         int priority = 3;
  204.  
  205.                         processInstanceBean = createProcessInstance(
  206.                                 opsControlDescriptionId, procName, creatorId,
  207.                                 priority);
  208.  
  209.                         log.debug("process '" + procName + "' has been created");
  210.                     } catch (Exception e) {
  211.                         log.error("error creating process instance with name: "
  212.                                 + procName);
  213.                     }
  214.  
  215.                     if (null != processInstanceBean) {
  216.                         log.debug("retrieving prime BO and setting pot :) owners");
  217.                         ISubjectInstanceBean primeSubjectInstanceBean = getPrimeSubject(processInstanceBean);
  218.  
  219.                         // retrieve prime BO to preset it's attributes
  220.                         IView operationPlan = createPrimeBO(primeSubjectInstanceBean);
  221.  
  222.                         if (null != operationPlan) {
  223.                             // ============================================================================================================
  224.                             log.debug("retrieving opPlanCommonInfo");
  225.  
  226.                             // explicitly create commonInfo BO
  227.                             Class<?> opPlanCommonInfoType = BOHelper.getType(
  228.                                     operationPlan, COMMON_INFO);
  229.                             log.debug("comInfoTypeClass:"
  230.                                     + opPlanCommonInfoType);
  231.                             Object opPlanCommonInfoImpl = opPlanCommonInfoType
  232.                                     .newInstance();
  233.                             BOHelper.setAttribute(operationPlan, COMMON_INFO,
  234.                                     opPlanCommonInfoImpl);
  235.  
  236.                             Object commonInfo = BOHelper.getAttribute(
  237.                                     operationPlan, COMMON_INFO);
  238.  
  239.                             // retrieve newly created commonInfo and set
  240.                             // it'sattributes
  241.                             if (null != commonInfo) {
  242.                                 log.debug("commonInfo retrieved. setting date attributes");
  243.  
  244.                                 DateTime today = DateTime.now();
  245.                                 Date periodStart = today.minusMonths(1)
  246.                                         .toDate();
  247.                                 Date periodEnd = today.toDate();
  248.                                 Date deadline = today.plusWeeks(1).toDate();
  249.  
  250.                                 BOHelper.setAttribute(commonInfo,
  251.                                         "periodStart", periodStart);
  252.                                 BOHelper.setAttribute(commonInfo, "periodEnd",
  253.                                         periodEnd);
  254.                                 BOHelper.setAttribute(commonInfo, "createDate",
  255.                                         periodEnd);
  256.                                 BOHelper.setAttribute(commonInfo, "deadline",
  257.                                         deadline);
  258.  
  259.                                 log.debug("date attribuites has been set");
  260.                             } else {
  261.                                 log.debug("error retrieving commonInfo");
  262.                             }
  263.  
  264.                             // ============================================================================================================
  265.                             log.debug("retrieving opPlanPostamt");
  266.  
  267.                             // explicitly create postamt BO
  268.                             Class<?> postamtType = BOHelper.getType(
  269.                                     operationPlan, POSTAMT);
  270.                             log.debug("postamtTypeCLass: " + postamtType);
  271.                             IView postamtImpl = (IView) (postamtType
  272.                                     .newInstance());
  273.                             BOHelper.setAttribute(postamtImpl,
  274.                                     "postamtPostamtIndex", postamtIndex);
  275.  
  276.                             List<IView> postamtViews = accessApi.getViews(
  277.                                     primeSubjectInstanceBean, postamtImpl);
  278.  
  279.                             if (1 == postamtViews.size()) {
  280.                                 BOHelper.setAttribute(operationPlan, POSTAMT,
  281.                                         postamtViews.get(0));
  282.                                 log.debug("postamt has been set");
  283.                             } else {
  284.                                 log.debug("error retrieving postamt by index: "
  285.                                         + postamtIndex
  286.                                         + " - no postamts at all or more than one postamt exists");
  287.                             }
  288.  
  289.                             // ============================================================================================================
  290.  
  291.                             // first of all - release subj instance
  292.                             // from current proc stater
  293.                             // while proc at it's fist stage
  294.                             log.debug("releasing process from user-proc_starter");
  295.                             commonApi.releaseSubjectInstance(Long
  296.                                     .valueOf(primeSubjectInstanceBean.getId()),
  297.                                     opsControlFirstStateDescrId);
  298.  
  299.                             // get any potential owner and assign him a task
  300.                             log.debug("setting new assignee from pot.owners");
  301.                             String singlePotentialOwner = potentialOwners
  302.                                     .get(0);
  303.                             commonApi.assignSubjectInstance(Long
  304.                                     .valueOf(primeSubjectInstanceBean.getId()),
  305.                                     singlePotentialOwner);
  306.  
  307.                             // save BO for the operation plan number
  308.                             // to be composed correctly (correct region index)
  309.                             log.debug("saving BO changes");
  310.                             saveNewPrimeBO(primeSubjectInstanceBean,
  311.                                     operationPlan);
  312.  
  313.                             // push the process to the Edit stage
  314.                             log.debug("pushing process forward");
  315.                             String sid = primeSubjectInstanceBean.getId();
  316.                             boolean ignoreMissingData = true;
  317.  
  318.                             commonApi.followFunctionTransition(sid,
  319.                                     opsControlFirstStateTransitionDescrId,
  320.                                     primeSubjectInstanceBean.getEditor(),
  321.                                     ignoreMissingData,
  322.                                     primeSubjectInstanceBean.getEditor());
  323.  
  324.                             log.debug("proc has been pushed successfully");
  325.  
  326.                             // now release assignee from the task
  327.                             // and set him as pot owner
  328.                             log.debug("setting potential owners");
  329.                             commonApi.releaseSubjectInstance(Long
  330.                                     .valueOf(primeSubjectInstanceBean.getId()),
  331.                                     opsControlRecieveStateDescrId);
  332.                             instanceAdminApi.setPotentialOwners(Long
  333.                                     .valueOf(primeSubjectInstanceBean.getId()),
  334.                                     potentialOwners.toArray(new String[0]));
  335.  
  336.                             ISubjectInstanceBean newPrimeBean = getPrimeSubject(processInstanceBean);
  337.                             log.debug("Editor has been removed. Potential owners["
  338.                                     + newPrimeBean.getPotentialOwners().length
  339.                                     + "/" + potentialOwners.size() + "]:");
  340.                             for (String o : newPrimeBean.getPotentialOwners())
  341.                                 log.debug("-->" + o);
  342.  
  343.                         } else {
  344.                             log.debug("theres no opPlan BO for process: "
  345.                                     + procName);
  346.                         }
  347.                     } else {
  348.                         log.debug("theres no procInstanceBean for process: "
  349.                                 + procName);
  350.                     }
  351.                     log.debug("all done");
  352.                     log.debug("\n");
  353.  
  354.                 }
  355.             }
  356.  
  357.         } catch (Exception e2) {
  358.             e2.printStackTrace();
  359.         }
  360.  
  361.         return null;
  362.     }
  363.  
  364.     private int getDepartmentControllerGroupId(Connection connection,
  365.             String query, String departmentControllerGroupId) throws Exception {
  366.         log.debug("getRuspostDepartmentControllerGroupId begin");
  367.         log.debug("executing: " + query);
  368.  
  369.         List<ValueWrapper> valueWrappers = new ArrayList<ValueWrapper>();
  370.  
  371.         valueWrappers.add(new ValueWrapper(departmentControllerGroupId,
  372.                 String.class));
  373.  
  374.         int id = (int) DbHelper.executeQuery(connection, query, valueWrappers,
  375.                 true, new DbHelper.ResulSetHandler() {
  376.                     @Override
  377.                     public Object processResultSet(ResultSet rerSet)
  378.                             throws Exception {
  379.  
  380.                         if (rerSet.next())
  381.                             return rerSet
  382.                                     .getInt(DEPARTMENT_CONTROLLER_GROUP_ID);
  383.  
  384.                         return -1;
  385.                     }
  386.                 });
  387.  
  388.         log.debug("getRuspostDepartmentControllerGroupId end");
  389.         return id;
  390.     }
  391.  
  392.     private List<Pair> getUfpsIndexNamePairs(Connection connection, String query)
  393.             throws Exception {
  394.         log.debug("getUfpsIndexNamePairs begin");
  395.         log.debug("executing: " + query);
  396.  
  397.         List<ValueWrapper> valueWrappers = new ArrayList<ValueWrapper>();
  398.  
  399.         List<Pair> ufpsIndexNamePairs = (List<Pair>) DbHelper.executeQuery(
  400.                 connection, query, valueWrappers, true,
  401.                 new DbHelper.ResulSetHandler() {
  402.                     @Override
  403.                     public Object processResultSet(ResultSet result)
  404.                             throws Exception {
  405.                         List<Pair> ufpsIndexNamePairs = new ArrayList<Pair>();
  406.  
  407.                         while (result.next()) {
  408.  
  409.                             int ufpsIndex = result.getInt(UFPS_INDEX);
  410.                             String ufpsName = result.getString(UFPS_NAME);
  411.                             log.debug("  ufps: " + ufpsIndex + " / " + ufpsName);
  412.  
  413.                             ufpsIndexNamePairs.add(new Pair(String
  414.                                     .valueOf(ufpsIndex), ufpsName));
  415.                         }
  416.  
  417.                         return ufpsIndexNamePairs;
  418.                     }
  419.                 });
  420.  
  421.         log.debug("getUfpsIndexNamePairs end");
  422.         return ufpsIndexNamePairs;
  423.     }
  424.  
  425.     private List<Pair> getPostamtIndexNamePairs(Connection connection,
  426.             String query, String ufpsIndex) throws Exception {
  427.         log.debug("getPostamtIndexNamePairs begin");
  428.         log.debug("executing: " + query);
  429.         int ufpsIndexInt = Integer.valueOf(ufpsIndex);
  430.  
  431.         List<ValueWrapper> valueWrappers = new ArrayList<ValueWrapper>();
  432.  
  433.         valueWrappers.add(new ValueWrapper(ufpsIndexInt, Integer.class));
  434.  
  435.         List<Pair> postamtIndexNamePairs = (List<Pair>) DbHelper.executeQuery(
  436.                 connection, query, valueWrappers, true,
  437.                 new DbHelper.ResulSetHandler() {
  438.                     @Override
  439.                     public Object processResultSet(ResultSet result)
  440.                             throws Exception {
  441.                         List<Pair> postamtIndexNamePairs = new ArrayList<Pair>();
  442.  
  443.                         while (result.next()) {
  444.  
  445.                             int postamtIndex = result.getInt(POSTAMT_INDEX);
  446.                             String postamtName = result.getString(POSTAMT_NAME);
  447.                             log.debug("  postamt: " + postamtIndex + " / "
  448.                                     + postamtName);
  449.  
  450.                             postamtIndexNamePairs.add(new Pair(String
  451.                                     .valueOf(postamtIndex), postamtName));
  452.                         }
  453.  
  454.                         return postamtIndexNamePairs;
  455.                     }
  456.                 });
  457.  
  458.         log.debug("getPostamtIndexNamePairs end");
  459.         return postamtIndexNamePairs;
  460.     }
  461.  
  462.     private List<String> getPotentialOwners(Connection connection,
  463.             String query, int ufpsIndex, int departmentControllerGroupId)
  464.             throws Exception {
  465.         log.debug("getPotentialOwners begin");
  466.         log.debug("executing: " + query);
  467.  
  468.         List<ValueWrapper> valueWrappers = new ArrayList<ValueWrapper>();
  469.  
  470.         valueWrappers.add(new ValueWrapper(ufpsIndex, Integer.class));
  471.         valueWrappers.add(new ValueWrapper(departmentControllerGroupId,
  472.                 Integer.class));
  473.  
  474.         List<String> potOwnersIds = (List<String>) DbHelper.executeQuery(
  475.                 connection, query, valueWrappers, true,
  476.                 new DbHelper.ResulSetHandler() {
  477.                     @Override
  478.                     public Object processResultSet(ResultSet result)
  479.                             throws Exception {
  480.                         List<String> potOwnersIds = new ArrayList<String>();
  481.  
  482.                         while (result.next()) {
  483.  
  484.                             int ufpsIndex = result.getInt(UFPS_INDEX);
  485.                             int userId = result.getInt(UM_USER);
  486.                             int departmentControllerGroupId = result
  487.                                     .getInt(DEPARTMENT_CONTROLLER_GROUP_ID);
  488.  
  489.                             log.debug("  user: " + ufpsIndex + " / [" + userId
  490.                                     + "] / " + departmentControllerGroupId);
  491.  
  492.                             potOwnersIds.add(String.valueOf(userId));
  493.                         }
  494.  
  495.                         return potOwnersIds;
  496.                     }
  497.                 });
  498.  
  499.         log.debug("getPotentialOwners end");
  500.         return potOwnersIds;
  501.     }
  502.  
  503.     private ISubjectInstanceBean getPrimeSubject(
  504.             IProcessInstanceBean processInstanceBean) throws Exception {
  505.         ICommonApi commonApi = ApiUtils.lookupCommonApi();
  506.         String subjectInstanceId = processInstanceBean.getSubjectInstances()[0];
  507.         ISubjectInstanceBean subjectInstanceBean = commonApi
  508.                 .getSubjectInstanceBean(subjectInstanceId);
  509.         return subjectInstanceBean;
  510.     }
  511.  
  512.     private IView createPrimeBO(ISubjectInstanceBean subjectInstanceBean)
  513.             throws Exception {
  514.         IBo20AccessApi bo20AccessApi = ApiUtils.lookupBo20AccessApi();
  515.         Set<IStorageInstanceBean> storages = bo20AccessApi.getStorages(
  516.                 subjectInstanceBean, null);
  517.         IStorageInstanceBean storage = storages.iterator().next();
  518.         IView iView = bo20AccessApi.createIViewOnStorage(subjectInstanceBean,
  519.                 storage);
  520.         return iView;
  521.     }
  522.  
  523.     private void saveNewPrimeBO(ISubjectInstanceBean subjectInstanceBean,
  524.             IView iView) throws Exception {
  525.         IBo20AccessApi bo20AccessApi = ApiUtils.lookupBo20AccessApi();
  526.         Set<IStorageInstanceBean> storages = bo20AccessApi.getStorages(
  527.                 subjectInstanceBean, null);
  528.         IStorageInstanceBean storage = storages.iterator().next();
  529.         bo20AccessApi
  530.                 .saveNewIViewInStorage(subjectInstanceBean, iView, storage);
  531.     }
  532.  
  533.     private Map<String, String> processVersions = new HashMap<String, String>();
  534.  
  535.     private IProcessInstanceBean createProcessInstance(
  536.             String processDescriptionId, String name, String userId,
  537.             int priority) throws Exception {
  538.         ICommonApi commonApi = ApiUtils.lookupCommonApi();
  539.  
  540.         String processVersionId = getLatestProcessVersionId(processDescriptionId);
  541.  
  542.         InitProcessInstanceData initProcessInstanceData = new InitProcessInstanceData(
  543.                 processVersionId,
  544.                 com.jcom1.api.constants.Constants.Process.Mode.PRODUCTIVE);
  545.  
  546.         initProcessInstanceData.setName(name);
  547.         initProcessInstanceData.setCreatorId(userId);
  548.         initProcessInstanceData.setPriority(priority);
  549.  
  550.         String processInstanceId = commonApi
  551.                 .createProcessInstance(initProcessInstanceData);
  552.  
  553.         IProcessInstanceBean processInstance = commonApi
  554.                 .getProcessInstanceBean(processInstanceId);
  555.  
  556.         return processInstance;
  557.     }
  558.  
  559.     private String getLatestProcessVersionIdFromDB(Connection connection,
  560.             String query, String processDescriptionId) throws Exception {
  561.         log.debug("getLatestProcessVersionIdFromDB begin");
  562.         log.debug("executing: " + query);
  563.         final String LATEST_VER_ID = "LATEST_VER_ID";
  564.  
  565.         List<ValueWrapper> valueWrappers = new ArrayList<ValueWrapper>();
  566.  
  567.         valueWrappers.add(new ValueWrapper(processDescriptionId, String.class));
  568.  
  569.         String lastestVerId = (String) DbHelper.executeQuery(connection, query,
  570.                 valueWrappers, true, new DbHelper.ResulSetHandler() {
  571.                     @Override
  572.                     public Object processResultSet(ResultSet rerSet)
  573.                             throws Exception {
  574.  
  575.                         if (rerSet.next())
  576.                             return rerSet.getString(LATEST_VER_ID);
  577.  
  578.                         return String.valueOf(-1);
  579.                     }
  580.                 });
  581.  
  582.         return lastestVerId;
  583.     }
  584.  
  585.     private String getLatestProcessVersionId(String processDescriptionId)
  586.             throws Exception {
  587.         log.debug("getLatestProcessVersionId begin");
  588.         String processVersion = processVersions.get(processDescriptionId);
  589.  
  590.         if (processVersion == null) {
  591.  
  592.             String latestProcVersionQuery = "SELECT ID AS LATEST_VER_ID "
  593.                     + "FROM RT_PROCDESC " + "WHERE UPLOADDATE = ( "
  594.                     + "     SELECT MAX(UPLOADDATE) " + "        FROM RT_PROCDESC "
  595.                     + "     WHERE JPASS_ID = ?)";
  596.  
  597.             try (final Connection connection = DbHelper
  598.                     .getConnection(DbHelper.PROP_DB_INT);) {
  599.  
  600.                 processVersion = getLatestProcessVersionIdFromDB(connection,
  601.                         latestProcVersionQuery, processDescriptionId);
  602.                 processVersions.put(processDescriptionId, processVersion);
  603.  
  604.             } catch (Exception e2) {
  605.                 log.error("error while retrieving latest process version for process: "
  606.                         + processDescriptionId);
  607.             }
  608.         }
  609.         log.debug("latest ver: " + processVersion);
  610.         log.debug("getLatestProcessVersionId end");
  611.         return processVersion;
  612.     }
  613.  
  614. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement