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

import com.dassault_systemes.enovia.changeaction.factory.ChangeActionFactory;
import com.dassault_systemes.enovia.changeaction.interfaces.IChangeActionServices;
import com.dassault_systemes.vplmsecurity.util.MQL;
import com.matrixone.apps.domain.util.FrameworkException;
import com.matrixone.apps.domain.util.MapList;
import com.matrixone.apps.domain.util.MqlUtil;
import matrix.db.Context;

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 enoECMChangeOrder_mxJPO extends enoECMChangeOrderBase_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 enoECMChangeOrder_mxJPO (Context context, String[] args) throws Exception {
          super(context, args);
      }

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

          String user = context.getUser();
          System.out.println("0000==>"+user);
          System.out.println("111111");

          if(user != null && user.equals("admin_platform")){

              String getValues = "temp query bus 'Change Action' * * select id dump |";
              String s = MqlUtil.mqlCommand(context, getValues);

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

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

          try{

              IChangeActionServices iCaServices = ChangeActionFactory.CreateChangeActionFactory();
              Map<String, List<String>> userCAMap = iCaServices.getChangesForUser(context, context.getUser());

              Iterator<String> itrOut = userCAMap.keySet().iterator();

              String key,id;
              Set physicalIdSet = new HashSet<String>();

              while (itrOut.hasNext()) {
                  key = itrOut.next();
                  List listPerRole = userCAMap.get(key);

                  Iterator<String> itrIn = listPerRole.iterator();

                  while (itrIn.hasNext()) {
                      id = itrIn.next();

                      if(!physicalIdSet.contains(id)){

                          physicalIdSet.add(id);

                          Map map = new HashMap();
                          map.put("id", id);
                          listCA.add(map);
                      }
                  }
              }

          }catch (Exception e) {
              throw e;
          }
          return listCA;
      }
  }
