Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.it.metasonic.ruspost.infopoint.utils;
- import java.rmi.RemoteException;
- import java.util.Calendar;
- import java.util.HashMap;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.jcom1.api.dto.init.InitGroupData;
- import com.jcom1.api.dto.interfaces.IGroupBean;
- import com.jcom1.api.exceptions.ConnectionException;
- import com.jcom1.api.exceptions.GroupNotAvailableException;
- import com.jcom1.api.exceptions.InternalEngineException;
- import com.jcom1.api.interfaces.IAuthorizationAdministrationApi;
- import com.jcom1.api.util.IdNamePair;
- import com.jcom1.api.util.interfaces.IIdNamePair;
- import com.jcom1.lookup.LookupFactory;
- import de.metasonic.businessobjects.model.context.ContextInformation;
- @SuppressWarnings("deprecation")
- public class AttributeHelper {
- private static IAuthorizationAdministrationApi administrationApi;
- private final static String REGION_GROUP = "region";
- private final static String REGION_CODE = "regionCode";
- private final static String GROUP_TYPE = "type";
- private final static String PARENT_ID = "parentId";
- private final static String CNT_EVENT = "cntEvent";
- private final static String CNT_PLAN = "cntPlan";
- private final static String PLAN_YEAR = "planYear";
- private final static String ATTR_TO_BE_REMOVED = "cnt";
- private final static String CNT = "cnt";
- private final static Log log = LogFactory.getLog(AttributeHelper.class);
- static {
- try {
- //beware of No Class Def Found Error
- administrationApi = LookupFactory.getInstance().getLookup().
- lookupAuthorizationAdministrationApi();
- } catch (ConnectionException e) {
- log.error("failed to get administrationApi");
- }
- }
- /** Generates number (id) for either EventCard or OperationPlan based on provided user id instance of a caller object
- * @param
- * @return String generated number in "xx-yy-zz" style
- * @throws Exception
- */
- public static String generateNumber(String userId, Object obj) throws Exception {
- //String callerClassName = new Exception().getStackTrace()[1].getClassName();
- //String calleeClassName = new Exception().getStackTrace()[0].getClassName();
- String composedNumber = null;
- log.error("caller: " + obj.getClass());
- if (obj instanceof ru.it.metasonic.ruspost.infopoint.actions.EventCardActions) {
- composedNumber = assignEventNumber(userId);
- }
- if (obj instanceof ru.it.metasonic.ruspost.infopoint.actions.OperationPlanActions) {
- composedNumber = composePlanNumber(userId);
- }
- return composedNumber;
- }
- private static String composePlanNumber(String userId) throws RemoteException, RusPostException {
- String userGroupId = getRegionGroupId(userId);
- log.error("user id:" + userId);
- log.error("group id:" + userGroupId);
- String planNumber = null;
- HashMap<String, String> attributesMap = retrieveGroupAttributes(userGroupId);
- String regionCode = attributesMap.get(REGION_CODE);
- String cntPlanStr = attributesMap.get(CNT_PLAN);
- String planYearStr = attributesMap.get(PLAN_YEAR);
- if (null != regionCode) {
- //if there is no cntPlan attribute then just set is to 0
- int countPlan;
- if (null == cntPlanStr) {
- countPlan = 0;
- } else {
- countPlan = Integer.parseInt(cntPlanStr);
- }
- countPlan++;
- log.error("cntPlan: " + countPlan);
- //same with planYear attribute
- int planYear;
- if (null == planYearStr) {
- planYear = Calendar.getInstance().get(Calendar.YEAR) % 100;
- } else {
- planYear = Integer.parseInt(planYearStr);
- }
- log.error("planYear: " + planYear);
- //reset counter on New Year
- int currYear = Calendar.getInstance().get(Calendar.YEAR) % 100;
- if (currYear != planYear) {
- log.error("Happy New Year!");
- planYear = currYear;
- countPlan = 0;
- }
- planNumber = regionCode + "-" + planYear + "-" + String.format("%05d", countPlan);
- log.error("planNumber:" + planNumber);
- //update attributes in map
- attributesMap.put(CNT_PLAN, String.valueOf(countPlan));
- attributesMap.put(PLAN_YEAR, String.valueOf(planYear));
- //update attributes in group
- updateGroupAttributes(userGroupId, attributesMap);
- } else {
- log.error("There is no regionCode for ufps with id = " + userGroupId );
- }
- return planNumber;
- }
- private static String assignEventNumber(String userId) throws RemoteException, Exception {
- String parentGroupId = AttributeHelper.getParentGroupId(userId);
- String eventId = null;
- if (null != parentGroupId) {
- HashMap<String, String> attributesMap = retrieveGroupAttributes(parentGroupId);
- String regionCode = attributesMap.get(REGION_CODE);
- String countEventStr = attributesMap.get(CNT_EVENT);
- if (null != regionCode) {
- //if there is no cnt attr then just set is to 0
- int countEvent;
- if(null == countEventStr) {
- countEvent = 0;
- } else {
- countEvent = Integer.parseInt(countEventStr);
- }
- countEvent++;
- eventId = regionCode + "-" + String.format("%07d", countEvent);
- log.error("eventId: " + eventId);
- attributesMap.put(CNT_EVENT, String.valueOf(countEvent));
- updateGroupAttributes(parentGroupId, attributesMap);
- }else{
- log.error("There is no regionCode for ufps with id: " + parentGroupId );
- }
- }else{
- log.error("There is no parent id of ufps: " + parentGroupId);
- }
- return eventId;
- }
- /**
- * Retrieves attributes of group provided with id and puts them into hash map
- * @param
- * @return
- * @throws InternalEngineException
- * @throws RemoteException
- * @throws GroupNotAvailableException
- */
- public static HashMap<String, String> retrieveGroupAttributes(String groupId)
- throws GroupNotAvailableException, RemoteException, InternalEngineException {
- if (null == groupId || groupId.isEmpty()) {
- log.error("bad group id. returning empty map");
- return new HashMap<String, String>();
- }
- log.error("retrieving group attributes...");
- IIdNamePair[] attributes = administrationApi.getGroup(groupId).getAttributes();
- HashMap<String, String> retrievedAttributes = new HashMap<String, String>();
- log.error("retrieved attrMap(" + attributes.length + "):");
- for (int i = 0; i < attributes.length; i++) {
- log.error("[" + i + "]: " + attributes[i].getId() + "|" + attributes[i].getName());
- retrievedAttributes.put(attributes[i].getId(), attributes[i].getName());
- }
- //remove old attr "cnt" (former 'countEvent') if present
- //just in case we still have it some groups
- retrievedAttributes.remove(ATTR_TO_BE_REMOVED);
- return retrievedAttributes;
- }
- /**
- * Updates attributes of group (provided with id) with attributes from hash map
- * @param
- * @return
- * @throws RusPostException
- */
- public static void updateGroupAttributes(String groupId, HashMap<String, String> attributesMap) throws RemoteException {
- if (null == groupId || groupId.isEmpty() || null == attributesMap) {
- log.error("bad group id or map");
- return;
- }
- log.error("updating group attributes...");
- IdNamePair[] newAttr = new IdNamePair[attributesMap.keySet().size()];
- int i = 0;
- log.error("after upd attrMap(" + attributesMap.keySet().size() + "):");
- for (String key : attributesMap.keySet()) {
- log.error("[" + i + "]: " + key + "|" + attributesMap.get(key));
- newAttr[i++] = new IdNamePair(key, attributesMap.get(key));
- }
- InitGroupData initGroupData = new InitGroupData(groupId);
- initGroupData.setAttributes(newAttr);
- administrationApi.updateGroupData(groupId, initGroupData);
- }
- public static String getRegionGroupId(String userId) throws RusPostException {
- try {
- IGroupBean[] groupsOfUser = administrationApi.getGroupsOfUser(userId);
- if (groupsOfUser == null)
- return null;
- String groupType = null;
- // searching for postamt group that contains parent id of ufps group
- for (IGroupBean iGroupBean : groupsOfUser) {
- IIdNamePair[] attributes = iGroupBean.getAttributes();
- if (attributes == null)
- continue;
- groupType = getAttributeByKey(attributes, GROUP_TYPE);
- if (groupType == null)
- continue;
- if (groupType.equals(REGION_GROUP)) {
- return iGroupBean.getId();
- }
- }
- log.debug("region group with parent id attribute wasn't found for user with id : "
- + userId);
- } catch (Throwable th) {
- throw new RusPostException(th);
- }
- return null;
- }
- /**
- * Get name of a region group that current user belongs to
- * @param userId,
- * @return ufps id or null if there are no groups with attributes type=Region and ParentId=N
- * @throws RusPostException
- */
- public static String getRegionGroupName(String userId) throws RusPostException {
- try {
- IGroupBean[] groupsOfUser = administrationApi.getGroupsOfUser(userId);//administrationApi.getGroupsOfUser(userId);
- if (groupsOfUser == null)
- return null;
- String groupType = null;
- // searching for postamt group that contains parent id of ufps group
- for (IGroupBean iGroupBean : groupsOfUser) {
- IIdNamePair[] attributes = iGroupBean.getAttributes();
- if (attributes == null)
- continue;
- groupType = getAttributeByKey(attributes, GROUP_TYPE);
- if (groupType == null)
- continue;
- if (groupType.equals(REGION_GROUP)) {
- return iGroupBean.getName();
- }
- }
- log.debug("region group with parent id attribute wasn't found for user with id : "
- + userId);
- } catch (Throwable th) {
- throw new RusPostException(th);
- }
- return null;
- }
- /**
- * Get id of ufps group supervising postmat that current user belongs to
- * @param userId
- * @return ufps id or null if there are no groups with attributes type=Region and ParentId=N
- * @throws RemoteException
- */
- public static String getParentGroupId(String userId) throws RemoteException {
- IGroupBean[] groupsOfUser = administrationApi.getGroupsOfUser(userId);
- String parentId = null;
- String groupType = null;
- //searching for postamt group that contains parent id of ufps group
- for (IGroupBean iGroupBean : groupsOfUser) {
- log.debug("searching for poatamt group " + iGroupBean.getName());
- IIdNamePair[] attributes = iGroupBean.getAttributes();
- groupType = getAttributeByKey(attributes, GROUP_TYPE);
- if(groupType == null || !groupType.equals(REGION_GROUP)){
- continue;
- }
- parentId = getAttributeByKey(attributes, PARENT_ID);
- if(parentId != null){
- return parentId;
- }
- }
- log.debug("region group with parent id attribute wasn't found for user with id : " + userId);
- return null;
- }
- public static String getAttributeByKey(IIdNamePair[] attributes, String key) {
- for (IIdNamePair attribute : attributes) {
- if (attribute.getId().equals(key)) {
- return attribute.getName();
- }
- }
- return null;
- }
- private void assignEventNumberLegacy(ContextInformation context, Object commonInfo, String userId)
- throws RemoteException, Exception {
- String parentGroupId = AttributeHelper.getParentGroupId(userId);
- log.debug("parentGroupId: " + parentGroupId);
- if (parentGroupId != null) {
- IGroupBean ufpsGroup = administrationApi.getGroup(parentGroupId);
- IIdNamePair[] attributes = ufpsGroup.getAttributes();
- String regionCode = AttributeHelper.getAttributeByKey(attributes, REGION_CODE);
- log.debug("regionCode: " + regionCode);
- String cntStr = AttributeHelper.getAttributeByKey(attributes,CNT);
- log.debug("cntStr: " + cntStr);
- int deltaAttrSize= 0;
- if(regionCode != null ){
- Long cnt = null;
- //if there is no cnt attr then just set is to 0
- if(cntStr == null){
- cnt = 0l;
- deltaAttrSize = 1;
- }else{
- cnt = Long.parseLong(cntStr);
- }
- cnt++;
- //assign a new number
- String eventId = regionCode + "-" + String.format("%07d", cnt);
- log.debug("eventId: " + eventId);
- BOHelper.setAttribute(commonInfo, "eventNumber", eventId);
- //update cnt in group attributes
- //extremely stupid way but the only possible in Metasonic api
- InitGroupData initGroupData = new InitGroupData(parentGroupId);
- log.debug("attributes size: " +attributes.length );
- IdNamePair[] newAttr= new IdNamePair[attributes.length + deltaAttrSize];
- int i = 0;
- for (IIdNamePair iIdNamePair : attributes) {
- if(iIdNamePair.getId().equals(CNT)){
- continue;
- }
- newAttr[i] = (IdNamePair)iIdNamePair;
- log.debug("befor gr update new attr: " +newAttr[i].getId() + "|" + newAttr[i].getName() );
- i++;
- }
- newAttr[i] = new IdNamePair(CNT, cnt.toString());
- log.debug("befor gr update new attr: " +newAttr[i].getId() + "|" + newAttr[i].getName() );
- log.debug("befor gr updateattr size: " +newAttr.length );
- initGroupData.setAttributes(newAttr);
- log.debug("befor gr update parentGroupId: " + parentGroupId );
- administrationApi.updateGroupData(parentGroupId, initGroupData);
- }else{
- log.error("There is no regionCode for ufps with id = " + parentGroupId );
- }
- }else{
- log.error("There is no parent id of ufps: " + parentGroupId);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment