Ladies_Man

#sonic OperationPlanActions_27 cool stuff

Aug 2nd, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.76 KB | None | 0 0
  1. package ru.it.metasonic.ruspost.infopoint.actions;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.ParameterizedType;
  5. import java.lang.reflect.Type;
  6. import java.rmi.RemoteException;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.Set;
  10.  
  11. import org.apache.commons.logging.Log;
  12. import org.apache.commons.logging.LogFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14.  
  15. import ru.it.metasonic.ruspost.infopoint.utils.BOHelper;
  16. import ru.it.metasonic.ruspost.infopoint.utils.RusPostException;
  17. import ru.it.metasonic.ruspost.infopoint.utils.StorageAPI;
  18. import ru.it.metasonic.ruspost.infopoint.utils.StorageNames;
  19.  
  20. import com.jcom1.api.constants.Constants;
  21. import com.jcom1.api.dto.interfaces.IGroupBean;
  22. import com.jcom1.api.dto.interfaces.IStorageInstanceBean;
  23. import com.jcom1.api.dto.interfaces.ISubjectInstanceBean;
  24. import com.jcom1.api.dto.interfaces.storages.IStorageInstanceItemBeanReducedToId;
  25. import com.jcom1.api.exceptions.InternalEngineException;
  26. import com.jcom1.api.exceptions.SubjectInstanceNotAvailableException;
  27. import com.jcom1.api.interfaces.IAuthorizationAdministrationApi;
  28. import com.jcom1.api.interfaces.IAuthorizationApi;
  29. import com.jcom1.api.interfaces.IBo20AccessApi;
  30. import com.jcom1.api.interfaces.ICommonApi;
  31. import com.jcom1.api.util.IViewStorageInstanceItemPair;
  32. import com.jcom1.api.util.interfaces.IIdNamePair;
  33.  
  34. import de.metasonic.businessobjects.exceptions.ServerRuleExecutionException;
  35. import de.metasonic.businessobjects.model.context.ContextInformation;
  36. import de.metasonic.businessobjects.model.interfaces.IView;
  37. import de.metasonic.businessobjects.model.rules.AbstractServerAction;
  38.  
  39. /**
  40.  * Just implement your action methods here.
  41.  */
  42. public class OperationPlanActions extends AbstractServerAction {
  43.  
  44.     @Autowired
  45.     IBo20AccessApi boApi;
  46.    
  47.     @Autowired
  48.     ICommonApi commonApi;
  49.    
  50.     @Autowired
  51.     IBo20AccessApi accessApi;
  52.    
  53.     @Autowired
  54.     IAuthorizationApi authApi;
  55.    
  56.     @Autowired
  57.       IAuthorizationAdministrationApi administrationApi;
  58.    
  59.      private final static String REGION_GROUP = "region";
  60.      private final static String REGION_CODE = "regionCode";
  61.      private final static String GROUP_TYPE = "type";
  62.      private final static String PARENT_ID = "parentId";
  63.      private final static String CNT = "cnt";
  64.    
  65.     private final static Log log = LogFactory
  66.             .getLog(OperationPlanActions.class);
  67.    
  68.    
  69.     public void updateInnerPostamt() throws RusPostException, ServerRuleExecutionException  {
  70.         log.error(">>> updateInnerPostamt begin");
  71.         IView iView = getBean();
  72.         try {
  73.             Object postamt = BOHelper.getAttribute(iView, "postamt");
  74.            
  75.             if (null == postamt) {
  76.                 log.error("bad postamt");
  77.             }
  78.  
  79.             String postamtIndex = (String)BOHelper.getAttribute(postamt, "postamtPostamtIndex");
  80.             String postamtName = (String)BOHelper.getAttribute(postamt, "postamtPostamtName");
  81.             log.error("retrieved index/name:" + postamtIndex + " " + postamtName);
  82.            
  83.            
  84.             Object operationList = BOHelper.getAttribute(iView, "operationList");
  85.            
  86.             if (null == operationList) {
  87.                 log.error("bad operationList");
  88.             }
  89.            
  90.            
  91.             IView innerMeta = (IView)BOHelper.getAttribute(operationList, "metaData");
  92.            
  93.             if (null == innerMeta) {
  94.  
  95.                 Class<?> metaDataClass = BOHelper.getType(operationList, "metaData");
  96.                 log.error(metaDataClass);
  97.                 innerMeta = (IView) (metaDataClass.newInstance());         
  98.                 log.error("err2");
  99.                 BOHelper.setAttribute(operationList, "metaData", innerMeta);
  100.                
  101.                 log.error("meta retrieved and set");
  102.             }
  103.            
  104.             if (null == innerMeta) {
  105.                 log.error("still bad");
  106.             }
  107.            
  108.             /*
  109.             IView selectedPostamt = (IView)BOHelper.getAttribute(iView, "metaData");
  110.            
  111.             if (null == selectedPostamt) {
  112.                 Class<?> metaDataClass = BOHelper.getType(iView, "metaData");
  113.                 selectedPostamt = (IView) (metaDataClass.newInstance());           
  114.                 BOHelper.setAttribute(iView, "metaData", selectedPostamt);
  115.             }
  116.            
  117.             if (null != postamtIndex && null != postamtName && null!= selectedPostamt) {
  118.                 BOHelper.setAttribute(selectedPostamt, "selectedPostamtIndex", postamtIndex);
  119.                 BOHelper.setAttribute(selectedPostamt, "selectedPostamtName", postamtName);
  120.                 log.error("meta data values set");
  121.             }*/
  122.            
  123.            
  124.         } catch (ServerRuleExecutionException ex) {
  125.             throw ex;
  126.         } catch (RusPostException ex) {
  127.             throw ex;
  128.         } catch (Throwable th) {
  129.             throw new RusPostException(th);
  130.         }
  131.         log.error("<<< updateInnerPostamt end");
  132.        
  133.     }
  134.    
  135.    
  136.     public void copyPostamtFromParent() throws RusPostException, ServerRuleExecutionException, SubjectInstanceNotAvailableException, RemoteException, InternalEngineException  {
  137.         log.error(">>> copyPostamtFromParent begin");
  138.         IView operationList = getBean();
  139.        
  140.         ContextInformation context = getContextInformation();
  141.         Long id = context.getSubjectInstanceId();
  142.         ISubjectInstanceBean subjInstBean = commonApi.getSubjectInstanceBean(id.toString());
  143.         try {
  144.                 StorageAPI storageAPI = new StorageAPI(boApi);
  145.                Set<IStorageInstanceBean> storages = storageAPI.getStorages(subjInstBean);
  146.                
  147.                //prepare storage infrastructure for План мероприятий
  148.                IStorageInstanceBean planStorage = storageAPI.findStorageByName(storages, StorageNames.OPERATION_PLAN_FOLDER);
  149.                
  150.                Set<IStorageInstanceItemBeanReducedToId> planStorageItems = boApi
  151.                   .getItems(subjInstBean, planStorage);
  152.                
  153.                
  154.                
  155.                int i = 0;
  156.                while (planStorageItems.iterator().hasNext()) {
  157.                    
  158.                    IStorageInstanceItemBeanReducedToId tmpInst = planStorageItems.iterator().next();
  159.                    
  160.                    log.error("storage_items[" + i + "]:");
  161.                    log.error(tmpInst.toString());
  162.                    
  163.                    IViewStorageInstanceItemPair storageInstanceItem = boApi
  164.                            .getIViewAndItem(
  165.                                    subjInstBean,
  166.                                    tmpInst,
  167.                              Constants.Storage.BusinessObject20.Action.UPDATE);
  168.                
  169.                    log.error(storageInstanceItem.toString());
  170.                    
  171.                    //IView operationPlanView = storageInstanceItem.getiView();
  172.                    
  173.                }
  174.                
  175.                /*if (planStorageItems.iterator().hasNext()) {
  176.                                
  177.                    Object postamt = (Object) (BOHelper.getAttribute(operationPlanView,"postamt"));
  178.                    
  179.                    String postamtIndex = (String)(BOHelper.getAttribute(postamt, "postamtPostamtIndex"));
  180.                    String postamtName = (String)(BOHelper.getAttribute(postamt, "postamtPostamtName"));            
  181.                    
  182.                    BOHelper.setAttribute(operationList, "postamtIndex", postamtIndex);
  183.                    BOHelper.setAttribute(operationList, "postamtName", postamtName);       
  184.                }*/
  185.  
  186.         } catch (Throwable th) {
  187.             throw new RusPostException(th);
  188.         }
  189.         log.error("<<< copyPostamtFromParent end");
  190.     }
  191.    
  192.    
  193.     public void setPostamtIntoSearchMask() throws RusPostException, ServerRuleExecutionException, SubjectInstanceNotAvailableException, RemoteException, InternalEngineException  {
  194.         log.error(">>> setPostamtIntoSearchMask begin");
  195.         IView postOfficeCard = getBean();
  196.        
  197.         ContextInformation context = getContextInformation();
  198.         Long id = context.getSubjectInstanceId();
  199.         ISubjectInstanceBean subjInstBean = commonApi.getSubjectInstanceBean(id.toString());
  200.         try {
  201.             StorageAPI storageAPI = new StorageAPI(boApi);
  202.                Set<IStorageInstanceBean> storages = storageAPI.getStorages(subjInstBean);
  203.                
  204.                //prepare storage infrastructure for План мероприятий
  205.                IStorageInstanceBean planStorage = storageAPI.findStorageByName(storages, StorageNames.OPERATION_PLAN_FOLDER);
  206.      
  207.                Set<IStorageInstanceItemBeanReducedToId> planStorageItems = boApi
  208.                   .getItems(subjInstBean, planStorage);
  209.                
  210.                if (planStorageItems.iterator().hasNext()) {
  211.                    
  212.                    IStorageInstanceItemBeanReducedToId tmpInst = planStorageItems.iterator().next();
  213.                    
  214.                    IViewStorageInstanceItemPair storageInstanceItem = boApi
  215.                            .getIViewAndItem(
  216.                                    subjInstBean,
  217.                                    tmpInst,
  218.                              Constants.Storage.BusinessObject20.Action.UPDATE);
  219.                
  220.                    IView operationPlanView = storageInstanceItem.getiView();
  221.                    
  222.                    
  223.                    Object postamt = (Object) (BOHelper.getAttribute(operationPlanView,"postamt"));
  224.                    
  225.                    String postamtIndex = (String)(BOHelper.getAttribute(postamt, "postamtPostamtIndex"));
  226.                    String postamtName = (String)(BOHelper.getAttribute(postamt, "postamtPostamtName"));
  227.                    
  228.                    
  229.                    BOHelper.setAttribute(postOfficeCard, "postamtIndex", postamtIndex);
  230.                    BOHelper.setAttribute(postOfficeCard, "postamtName", postamtName);
  231.                    
  232.                    
  233.                    
  234.                    /*Object operationList = (Object) (BOHelper.getAttribute(operationPlanView, "operationList"));
  235.                    
  236.                    if (null == operationList) {
  237.                        log.error("bad list");
  238.                    }*/
  239.                    
  240.                    
  241.                    
  242.                    
  243.                    
  244.                    
  245.                   /* Collection<Object> collection;
  246.                    try {
  247.                        collection = (Collection<Object>) (BOHelper.getAttribute(operationPlanView, "operationList"));
  248.                        
  249.                     if(collection == null){
  250.                         collection = new ArrayList<Object>();
  251.                         BOHelper.setAttribute(operationPlanView, "operationList", collection);
  252.                     }
  253.                    
  254.                    
  255.                     //create new OperationOPSResult
  256.                     Field officeListField = BOHelper.getField(operationCardView, "officeList");
  257.                     Type generic = officeListField.getGenericType();
  258.                     final ParameterizedType ptype = (ParameterizedType) generic;
  259.                           final Class<?> genericType = (Class<?>) ptype
  260.                                           .getActualTypeArguments()[0];
  261.                           log.debug("genericType: " + genericType);
  262.                     IView operationOPSResultImpl = (IView) (genericType.newInstance());  
  263.                     BOHelper.setAttribute(operationOPSResultImpl, "indexOPS", opsIndex);
  264.                    
  265.                     //add to collection
  266.                     collection.add(operationOPSResultImpl);*/
  267.                    
  268.                }
  269.                
  270.                
  271.            
  272.         } catch (Throwable th) {
  273.             throw new RusPostException(th);
  274.         }
  275.         log.error("<<< setPostamtIntoSearchMask end");
  276.        
  277.     }
  278.    
  279.    
  280.    
  281.     public void updateOuterUfps() throws RusPostException, ServerRuleExecutionException  {
  282.         log.debug("updateOuterUfps begin");
  283.         IView iView = getBean();
  284.        
  285.         try {
  286.            
  287.             IView metaData = (IView)BOHelper.getAttribute(iView, "metaData");
  288.            
  289.             if (null == metaData) {
  290.                 //explicitly get inline BO because it can be not initialized yet
  291.                 Class<?> metaDataClass = BOHelper.getType(iView, "metaData");
  292.                 metaData = (IView) (metaDataClass.newInstance());          
  293.                 BOHelper.setAttribute(iView, "metaData", metaData);
  294.             }
  295.  
  296.             ContextInformation context = getContextInformation();
  297.  
  298.             String userId = context.getEditorId();
  299.             if (null == userId || userId.isEmpty()) {
  300.                 userId = context.getAgentId();
  301.             }
  302.  
  303.             String ufpsIndex = "dummy id";
  304.             String ufpsName = "dummy name";
  305.  
  306.             ufpsIndex = getRegionGroupId(userId);
  307.             ufpsName = getRegionGroupName(userId);
  308.  
  309.             log.debug("current user ufpsGroupId/Name: " + ufpsIndex + " " + ufpsName);
  310.            
  311.             BOHelper.setAttribute(metaData, "regionGroupIndex", ufpsIndex);
  312.             BOHelper.setAttribute(metaData, "regionGroupName", ufpsName);
  313.            
  314.         } catch (ServerRuleExecutionException ex) {
  315.             throw ex;
  316.         } catch (RusPostException ex) {
  317.             throw ex;
  318.         } catch (Throwable th) {
  319.             throw new RusPostException(th);
  320.         }
  321.        
  322.         log.debug("updateOuterUfps end");
  323.     }
  324.    
  325.    
  326.     public void updateOuterPostamt() throws RusPostException, ServerRuleExecutionException  {
  327.         log.debug("updateOuterPostamt begin");
  328.         IView iView = getBean();
  329.         try {
  330.             Object postamt = BOHelper.getAttribute(iView, "postamt");
  331.            
  332.             if (null == postamt) {
  333.                 log.debug("bad postamt");
  334.             }
  335.            
  336.             String postamtIndex = (String)BOHelper.getAttribute(postamt, "postamtPostamtIndex");
  337.             String postamtName = (String)BOHelper.getAttribute(postamt, "postamtPostamtName");
  338.            
  339.             log.debug("index/name:" + postamtIndex + " " + postamtName);
  340.            
  341.             IView selectedPostamt = (IView)BOHelper.getAttribute(iView, "metaData");
  342.            
  343.             if (null == selectedPostamt) {
  344.                 Class<?> metaDataClass = BOHelper.getType(iView, "metaData");
  345.                 selectedPostamt = (IView) (metaDataClass.newInstance());           
  346.                 BOHelper.setAttribute(iView, "metaData", selectedPostamt);
  347.             }
  348.            
  349.             if (null != postamtIndex && null != postamtName && null!= selectedPostamt) {
  350.                 BOHelper.setAttribute(selectedPostamt, "postamtIndex", postamtIndex);
  351.                 BOHelper.setAttribute(selectedPostamt, "postamtName", postamtName);
  352.                 log.debug("meta data values set");
  353.             }
  354.            
  355.            
  356.         } catch (ServerRuleExecutionException ex) {
  357.             throw ex;
  358.         } catch (RusPostException ex) {
  359.             throw ex;
  360.         } catch (Throwable th) {
  361.             throw new RusPostException(th);
  362.         }
  363.         log.debug("updateOuterPostamt end");
  364.        
  365.     }
  366.    
  367.    
  368.     private String getRegionGroupId(String userId) throws RusPostException {
  369.         try {
  370.  
  371.             IGroupBean[] groupsOfUser = administrationApi
  372.                     .getGroupsOfUser(userId);
  373.             if (groupsOfUser == null)
  374.                 return null;
  375.             String groupType = null;
  376.             // searching for postamt group that contains parent id of ufps group
  377.             for (IGroupBean iGroupBean : groupsOfUser) {
  378.  
  379.                 IIdNamePair[] attributes = iGroupBean.getAttributes();
  380.                 if (attributes == null)
  381.                     continue;
  382.                 groupType = getAttributeByKey(attributes, GROUP_TYPE);
  383.                 if (groupType == null)
  384.                     continue;
  385.                 if (groupType.equals(REGION_GROUP)) {
  386.                     return iGroupBean.getId();
  387.                 }
  388.             }
  389.             log.debug("region  group with parent id attribute wasn't found for user with id  : "
  390.                     + userId);
  391.         } catch (Throwable th) {
  392.             throw new RusPostException(th);
  393.         }
  394.         return null;
  395.     }
  396.    
  397.     /**
  398.      * Get  name of  a region group  that current user belongs to
  399.      * @param userId
  400.      * @return ufps id or null if there are no groups with attributes  type=Region and ParentId=N
  401.      * @throws RusPostException
  402.      */
  403.  
  404.     private String getRegionGroupName(String userId) throws RusPostException {
  405.         try {
  406.    
  407.             IGroupBean[] groupsOfUser = administrationApi
  408.                     .getGroupsOfUser(userId);
  409.             if (groupsOfUser == null)
  410.                 return null;
  411.             String groupType = null;
  412.             // searching for postamt group that contains parent id of ufps group
  413.             for (IGroupBean iGroupBean : groupsOfUser) {
  414.    
  415.                 IIdNamePair[] attributes = iGroupBean.getAttributes();
  416.                 if (attributes == null)
  417.                     continue;
  418.                 groupType = getAttributeByKey(attributes, GROUP_TYPE);
  419.                 if (groupType == null)
  420.                     continue;
  421.                 if (groupType.equals(REGION_GROUP)) {
  422.                     return iGroupBean.getName();
  423.                 }
  424.             }
  425.             log.debug("region  group with parent id attribute wasn't found for user with id  : "
  426.                     + userId);
  427.         } catch (Throwable th) {
  428.             throw new RusPostException(th);
  429.         }
  430.         return null;
  431.     }
  432.    
  433.    
  434.     private static String getAttributeByKey(IIdNamePair[] attributes, String key) {
  435.         for (IIdNamePair attribute : attributes) {
  436.             if (attribute.getId().equals(key)) {
  437.                 return attribute.getName();
  438.             }
  439.  
  440.         }
  441.         return null;
  442.  
  443.     }
  444.  
  445. }
Advertisement
Add Comment
Please, Sign In to add comment