/*
 ** ${CLASS:enoECMChangeRequest}
 **
 ** Copyright (c) 1993-2020 Dassault Systemes. All Rights Reserved.
 */

import com.dassault_systemes.enovia.enterprisechangemgt.common.ChangeConstants;
import com.matrixone.apps.domain.DomainObject;
import com.matrixone.apps.domain.util.MapList;
import com.matrixone.apps.domain.util.MqlUtil;
import com.matrixone.apps.domain.util.PersonUtil;
import matrix.db.Context;
import matrix.util.StringList;

import java.util.*;

/**
 * The <code>enoECMChangeOrder</code> class contains code for the "Change Order" business type.
 *
 * @version ECM R215  - # Copyright (c) 1992-2020 Dassault Systemes.
 */
  public class enoECMChangeRequest_mxJPO extends enoECMChangeRequestBase_mxJPO
  {
      /**
       * Constructor.
       *
       * @param context the eMatrix <code>Context</code> object.
       * @param args holds no arguments.
       * @throws Exception if the operation fails.
       * @since ECM R215.
       */

      public enoECMChangeRequest_mxJPO (Context context, String[] args) throws Exception {
          super(context, args);
      }

      public MapList getMyChangeRequests(Context context, String args[]) throws Exception{
          MapList objList = new MapList();

          // 获取到user
          String user = context.getUser();

          //如果user为admin_platform则查询对应的全部CA变更信息，并将对应CA的ID返回，用于展示
          if(user != null && user.equals("admin_platform")){

              String getCRValue = "temp query bus 'Change Request' * * select id dump |";
              String crInfo = MqlUtil.mqlCommand(context, getCRValue);

              System.out.println("123=====>"+getCRValue);

              String[] split = crInfo.split("\\n");

              if(split.length - 1 != 0)
              {
                  for (String value : split) {
                      String[] crDetail = value.split("\\|");
                      String objId = crDetail[3];
                      Map map =new HashMap();
                      map.put("id",objId);
                      objList.add(map);
                  }
                  return objList;
              }
          }

          //下面这些代码为系统代码，功能为不同的用户只能获取其对应的CR信息，不能动。
          try{
              System.out.println("  inside getMyChangeRequests   " );
              String objectId 			= PersonUtil.getPersonObjectID(context);

              StringList strCROwned 		= (StringList)getOwnedCR(context, args);
              StringList sRouteCR 		= getRouteTaskAssignedCRs(context, objectId);
              StringList sRouteTemplateCR = getRouteTemplateAssignedCRs(context, objectId);

              Set<String> hs = new HashSet();
              hs.addAll(strCROwned);
              hs.addAll(sRouteCR);
              hs.addAll(sRouteTemplateCR);

              String[] args1 = new String[hs.size()];
              int i=0;
              for(String s : hs )
              {
                  args1[i++] = s;
              }

              StringList objectSelects = new StringList();
              objectSelects.add(ChangeConstants.SELECT_ID);
              objectSelects.add(ChangeConstants.SELECT_POLICY);

              DomainObject domObj = new DomainObject(objectId);
              MapList crListwithPolicyInfo = domObj.getInfo(context, args1 , objectSelects);

              Iterator itr = crListwithPolicyInfo.iterator();
              String id = "";
              while(itr.hasNext()){
                  Map eachCR = (Map)itr.next();
                  String policy = (String) eachCR.get(SELECT_POLICY);
                  System.out.println("  policy  " +  policy);
                  if(!"".equalsIgnoreCase(policy) && policy.equalsIgnoreCase(ChangeConstants.POLICY_REQUEST_FOR_CHANGE))
                      objList.add(eachCR);
              }

              if(objList.size()!=0)
                  return objList;
              else
                  return new MapList();
          }catch (Exception e) {

              throw e;
          }
      }
  }
