Ladies_Man

#sonic AttributeHelper final (12aug)

Aug 12th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.22 KB | None | 0 0
  1. package ru.it.metasonic.ruspost.infopoint.utils;
  2.  
  3. import java.rmi.RemoteException;
  4. import java.util.Calendar;
  5. import java.util.HashMap;
  6.  
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9.  
  10. import com.jcom1.api.dto.init.InitGroupData;
  11. import com.jcom1.api.dto.interfaces.IGroupBean;
  12. import com.jcom1.api.exceptions.ConnectionException;
  13. import com.jcom1.api.exceptions.GroupNotAvailableException;
  14. import com.jcom1.api.exceptions.InternalEngineException;
  15. import com.jcom1.api.interfaces.IAuthorizationAdministrationApi;
  16. import com.jcom1.api.util.IdNamePair;
  17. import com.jcom1.api.util.interfaces.IIdNamePair;
  18. import com.jcom1.lookup.LookupFactory;
  19.  
  20. import de.metasonic.businessobjects.model.context.ContextInformation;
  21.  
  22. @SuppressWarnings("deprecation")
  23. public class AttributeHelper {
  24.    
  25.      private static IAuthorizationAdministrationApi administrationApi;
  26.    
  27.      private final static String REGION_GROUP = "region";
  28.      private final static String REGION_CODE = "regionCode";
  29.      private final static String GROUP_TYPE = "type";
  30.      private final static String PARENT_ID = "parentId";
  31.      private final static String CNT_EVENT = "cntEvent";
  32.      private final static String CNT_PLAN = "cntPlan";
  33.      private final static String PLAN_YEAR = "planYear";
  34.      private final static String ATTR_TO_BE_REMOVED = "cnt";
  35.      private final static String CNT = "cnt";
  36.      
  37.      private final static Log log = LogFactory.getLog(AttributeHelper.class);
  38.    
  39.      static {
  40.          try {
  41.              //beware of No Class Def Found Error
  42.             administrationApi = LookupFactory.getInstance().getLookup().
  43.                     lookupAuthorizationAdministrationApi();
  44.         } catch (ConnectionException e) {
  45.             log.error("failed to get administrationApi");
  46.         }
  47.      }
  48.  
  49.  
  50.     /** Generates number (id) for either EventCard or OperationPlan based on provided user id instance of a caller object
  51.      * @param
  52.      * @return String   generated number in "xx-yy-zz" style
  53.      * @throws Exception
  54.      */
  55.      
  56.      public static String generateNumber(String userId, Object obj) throws Exception {
  57.          //String callerClassName = new Exception().getStackTrace()[1].getClassName();
  58.          //String calleeClassName = new Exception().getStackTrace()[0].getClassName();
  59.          String composedNumber = null;
  60.          log.error("caller: " + obj.getClass());
  61.          
  62.          if (obj instanceof ru.it.metasonic.ruspost.infopoint.actions.EventCardActions) {
  63.              composedNumber =  assignEventNumber(userId);
  64.          }
  65.          if (obj instanceof ru.it.metasonic.ruspost.infopoint.actions.OperationPlanActions) {
  66.              composedNumber = composePlanNumber(userId);
  67.          }
  68.  
  69.          return composedNumber;
  70.      }
  71.      
  72.    
  73.      private static String composePlanNumber(String userId) throws RemoteException, RusPostException {
  74.         String userGroupId = getRegionGroupId(userId);
  75.         log.error("user id:" + userId);
  76.         log.error("group id:" + userGroupId);
  77.         String planNumber = null;
  78.        
  79.         HashMap<String, String> attributesMap = retrieveGroupAttributes(userGroupId);
  80.        
  81.         String regionCode = attributesMap.get(REGION_CODE);
  82.         String cntPlanStr = attributesMap.get(CNT_PLAN);
  83.         String planYearStr = attributesMap.get(PLAN_YEAR);
  84.        
  85.         if (null != regionCode) {
  86.  
  87.             //if there is no cntPlan attribute then just set is to 0
  88.             int countPlan;
  89.             if (null == cntPlanStr) {
  90.                 countPlan = 0;
  91.             } else {
  92.                 countPlan = Integer.parseInt(cntPlanStr);
  93.             }
  94.             countPlan++;
  95.             log.error("cntPlan: " + countPlan);
  96.            
  97.             //same with planYear attribute
  98.             int planYear;
  99.             if (null == planYearStr) {
  100.                 planYear = Calendar.getInstance().get(Calendar.YEAR) % 100;
  101.             } else {
  102.                 planYear = Integer.parseInt(planYearStr);
  103.             }
  104.             log.error("planYear: " + planYear);
  105.            
  106.             //reset counter on New Year
  107.             int currYear = Calendar.getInstance().get(Calendar.YEAR) % 100;
  108.             if (currYear != planYear) {
  109.                 log.error("Happy New Year!");
  110.                 planYear = currYear;
  111.                 countPlan = 0;
  112.             }
  113.            
  114.             planNumber = regionCode + "-" + planYear + "-" + String.format("%05d", countPlan);
  115.             log.error("planNumber:" + planNumber);
  116.  
  117.             //update attributes in map
  118.             attributesMap.put(CNT_PLAN, String.valueOf(countPlan));
  119.             attributesMap.put(PLAN_YEAR, String.valueOf(planYear));
  120.             //update attributes in group
  121.             updateGroupAttributes(userGroupId, attributesMap);
  122.  
  123.         } else {
  124.             log.error("There is no regionCode for ufps with id = " + userGroupId );
  125.         }
  126.  
  127.         return planNumber;
  128.      }
  129.    
  130.    
  131.      private static String assignEventNumber(String userId) throws RemoteException, Exception {
  132.         String parentGroupId = AttributeHelper.getParentGroupId(userId);
  133.         String eventId = null;
  134.  
  135.         if (null != parentGroupId) {
  136.            
  137.             HashMap<String, String> attributesMap = retrieveGroupAttributes(parentGroupId);
  138.            
  139.             String regionCode = attributesMap.get(REGION_CODE);
  140.             String countEventStr = attributesMap.get(CNT_EVENT);
  141.            
  142.            
  143.             if (null != regionCode) {
  144.                
  145.                 //if there is no cnt attr then just set is to 0
  146.                 int countEvent;
  147.                 if(null == countEventStr) {
  148.                     countEvent = 0;
  149.                 } else {
  150.                     countEvent = Integer.parseInt(countEventStr);
  151.                 }
  152.                 countEvent++;
  153.  
  154.                 eventId = regionCode + "-" + String.format("%07d", countEvent);
  155.                 log.error("eventId: " + eventId);
  156.                
  157.                 attributesMap.put(CNT_EVENT, String.valueOf(countEvent));
  158.                 updateGroupAttributes(parentGroupId, attributesMap);
  159.  
  160.             }else{
  161.                 log.error("There is no regionCode for ufps with id: " + parentGroupId );
  162.             }
  163.            
  164.         }else{
  165.             log.error("There is no parent id of ufps: " + parentGroupId);
  166.         }
  167.  
  168.         return eventId;
  169.     }
  170.      
  171.      
  172.     /**
  173.     * Retrieves attributes of group provided with id and puts them into hash map
  174.     * @param
  175.     * @return
  176.      * @throws InternalEngineException
  177.      * @throws RemoteException
  178.      * @throws GroupNotAvailableException
  179.     */
  180.  
  181.     public static HashMap<String, String> retrieveGroupAttributes(String groupId)
  182.             throws GroupNotAvailableException, RemoteException, InternalEngineException {
  183.        
  184.         if (null == groupId || groupId.isEmpty()) {
  185.             log.error("bad group id. returning empty map");
  186.             return new HashMap<String, String>();
  187.         }
  188.         log.error("retrieving group attributes...");
  189.  
  190.         IIdNamePair[] attributes = administrationApi.getGroup(groupId).getAttributes();
  191.         HashMap<String, String> retrievedAttributes = new HashMap<String, String>();
  192.        
  193.         log.error("retrieved attrMap(" + attributes.length + "):");
  194.         for (int i = 0; i < attributes.length; i++) {
  195.             log.error("[" + i + "]: " + attributes[i].getId() + "|" + attributes[i].getName());
  196.             retrievedAttributes.put(attributes[i].getId(), attributes[i].getName());
  197.         }
  198.    
  199.         //remove old attr "cnt" (former 'countEvent') if present
  200.         //just in case we still have it some groups
  201.         retrievedAttributes.remove(ATTR_TO_BE_REMOVED);
  202.        
  203.         return retrievedAttributes;
  204.     }
  205.      
  206.  
  207.     /**
  208.     * Updates attributes of group (provided with id) with attributes from hash map
  209.     * @param
  210.     * @return
  211.     * @throws RusPostException
  212.     */
  213.      
  214.     public static void updateGroupAttributes(String groupId, HashMap<String, String> attributesMap) throws RemoteException {
  215.         if (null == groupId || groupId.isEmpty() || null == attributesMap) {
  216.             log.error("bad group id or map");
  217.             return;
  218.         }
  219.        
  220.         log.error("updating group attributes...");
  221.         IdNamePair[] newAttr = new IdNamePair[attributesMap.keySet().size()];
  222.        
  223.         int i = 0;
  224.         log.error("after upd attrMap(" + attributesMap.keySet().size() + "):");
  225.         for (String key : attributesMap.keySet()) {
  226.             log.error("[" + i + "]: " + key + "|" + attributesMap.get(key));
  227.             newAttr[i++] = new IdNamePair(key, attributesMap.get(key));
  228.         }
  229.        
  230.         InitGroupData initGroupData = new InitGroupData(groupId);
  231.         initGroupData.setAttributes(newAttr);
  232.         administrationApi.updateGroupData(groupId, initGroupData);
  233.     }
  234.      
  235.  
  236.     public static String getRegionGroupId(String userId) throws RusPostException {
  237.         try {
  238.  
  239.             IGroupBean[] groupsOfUser = administrationApi.getGroupsOfUser(userId);
  240.             if (groupsOfUser == null)
  241.                 return null;
  242.             String groupType = null;
  243.             // searching for postamt group that contains parent id of ufps group
  244.             for (IGroupBean iGroupBean : groupsOfUser) {
  245.  
  246.                 IIdNamePair[] attributes = iGroupBean.getAttributes();
  247.                 if (attributes == null)
  248.                     continue;
  249.                 groupType = getAttributeByKey(attributes, GROUP_TYPE);
  250.                 if (groupType == null)
  251.                     continue;
  252.                 if (groupType.equals(REGION_GROUP)) {
  253.                     return iGroupBean.getId();
  254.                 }
  255.             }
  256.             log.debug("region  group with parent id attribute wasn't found for user with id  : "
  257.                     + userId);
  258.         } catch (Throwable th) {
  259.             throw new RusPostException(th);
  260.         }
  261.         return null;
  262.     }
  263.    
  264.    
  265.     /**
  266.    * Get  name of  a region group  that current user belongs to
  267.    * @param userId,
  268.    * @return ufps id or null if there are no groups with attributes  type=Region and ParentId=N
  269.    * @throws RusPostException
  270.    */
  271.  
  272.     public static String getRegionGroupName(String userId) throws RusPostException {
  273.         try {
  274.            
  275.             IGroupBean[] groupsOfUser = administrationApi.getGroupsOfUser(userId);//administrationApi.getGroupsOfUser(userId);
  276.             if (groupsOfUser == null)
  277.                 return null;
  278.             String groupType = null;
  279.             // searching for postamt group that contains parent id of ufps group
  280.             for (IGroupBean iGroupBean : groupsOfUser) {
  281.    
  282.                 IIdNamePair[] attributes = iGroupBean.getAttributes();
  283.                 if (attributes == null)
  284.                     continue;
  285.                 groupType = getAttributeByKey(attributes, GROUP_TYPE);
  286.                 if (groupType == null)
  287.                     continue;
  288.                 if (groupType.equals(REGION_GROUP)) {
  289.                     return iGroupBean.getName();
  290.                 }
  291.             }
  292.             log.debug("region  group with parent id attribute wasn't found for user with id  : "
  293.                     + userId);
  294.         } catch (Throwable th) {
  295.             throw new RusPostException(th);
  296.         }
  297.         return null;
  298.     }
  299.    
  300.    
  301.     /**
  302.    * Get  id of ufps group supervising postmat that current user belongs to
  303.    * @param userId
  304.    * @return ufps id or null if there are no groups with attributes  type=Region and ParentId=N
  305.    * @throws RemoteException
  306.    */
  307.    
  308.     public static String getParentGroupId(String userId) throws RemoteException {
  309.         IGroupBean[] groupsOfUser = administrationApi.getGroupsOfUser(userId);
  310.         String parentId = null;
  311.         String groupType = null;
  312.         //searching for postamt group that contains parent id of ufps group
  313.         for (IGroupBean iGroupBean : groupsOfUser) {
  314.             log.debug("searching for poatamt group " + iGroupBean.getName());
  315.             IIdNamePair[] attributes = iGroupBean.getAttributes();
  316.             groupType = getAttributeByKey(attributes, GROUP_TYPE);
  317.             if(groupType == null || !groupType.equals(REGION_GROUP)){
  318.                 continue;
  319.             }
  320.              parentId = getAttributeByKey(attributes, PARENT_ID);
  321.              
  322.              if(parentId != null){
  323.                  return parentId;
  324.              }
  325.            }
  326.         log.debug("region  group with parent id attribute wasn't found for user with id  : " + userId);
  327.         return null;
  328.        
  329.     }
  330.    
  331.    
  332.     public static String getAttributeByKey(IIdNamePair[] attributes, String key) {
  333.         for (IIdNamePair attribute : attributes) {
  334.             if (attribute.getId().equals(key)) {
  335.                 return attribute.getName();
  336.             }
  337.  
  338.         }
  339.         return null;
  340.  
  341.     }
  342.  
  343.  
  344.     private void assignEventNumberLegacy(ContextInformation context, Object commonInfo, String userId)
  345.             throws RemoteException, Exception {
  346.    
  347.         String parentGroupId = AttributeHelper.getParentGroupId(userId);
  348.         log.debug("parentGroupId: " + parentGroupId);
  349.         if (parentGroupId != null) {
  350.             IGroupBean ufpsGroup = administrationApi.getGroup(parentGroupId);
  351.             IIdNamePair[] attributes = ufpsGroup.getAttributes();
  352.             String regionCode = AttributeHelper.getAttributeByKey(attributes, REGION_CODE);
  353.             log.debug("regionCode: " + regionCode);
  354.             String cntStr = AttributeHelper.getAttributeByKey(attributes,CNT);
  355.             log.debug("cntStr: " + cntStr);
  356.             int deltaAttrSize= 0;
  357.             if(regionCode != null ){
  358.                 Long cnt = null;
  359.                 //if there is no cnt attr then just set is to 0
  360.                 if(cntStr == null){
  361.                    cnt = 0l;
  362.                    deltaAttrSize = 1;
  363.                 }else{
  364.                     cnt = Long.parseLong(cntStr);
  365.                 }
  366.                 cnt++;
  367.                 //assign a new number
  368.                 String eventId = regionCode + "-"  + String.format("%07d", cnt);
  369.                 log.debug("eventId: " + eventId);
  370.                 BOHelper.setAttribute(commonInfo, "eventNumber", eventId);
  371.                
  372.                 //update cnt in group attributes
  373.                 //extremely stupid way but the only possible in Metasonic api
  374.                 InitGroupData initGroupData = new InitGroupData(parentGroupId);
  375.                 log.debug("attributes size: " +attributes.length );
  376.                 IdNamePair[] newAttr= new IdNamePair[attributes.length + deltaAttrSize];
  377.                 int i = 0;
  378.                 for (IIdNamePair iIdNamePair : attributes) {
  379.                     if(iIdNamePair.getId().equals(CNT)){
  380.                         continue;
  381.                     }
  382.                     newAttr[i] = (IdNamePair)iIdNamePair;
  383.                     log.debug("befor gr update  new attr: " +newAttr[i].getId() + "|" + newAttr[i].getName() );
  384.                     i++;
  385.                 }
  386.                 newAttr[i] = new IdNamePair(CNT, cnt.toString());
  387.                 log.debug("befor gr update new attr: " +newAttr[i].getId() + "|" + newAttr[i].getName() );
  388.                 log.debug("befor gr updateattr size: " +newAttr.length );
  389.                 initGroupData.setAttributes(newAttr);
  390.                 log.debug("befor gr update parentGroupId: " + parentGroupId );
  391.                 administrationApi.updateGroupData(parentGroupId, initGroupData);
  392.             }else{
  393.                 log.error("There is no  regionCode for  ufps with id = " + parentGroupId );
  394.             }
  395.            
  396.         }else{
  397.             log.error("There is no  parent id of ufps: " + parentGroupId);
  398.         }
  399.        
  400.     }
  401. }
Advertisement
Add Comment
Please, Sign In to add comment