 package com.connor.erp.ProcessImport;
 
 import com.connor.erp.ProcessImport.ProcessAddTransferToERP.ProcessAddTransferToERPCommand;
 import com.connor.erp.ProcessImport.ProcessChgTransferToERP.ProcessChgTransferToERPCommand;
 import com.connor.erp.toolkit.AimType;
 import com.connor.erp.toolkit.FileType;
 import com.teamcenter.rac.aif.AbstractAIFUIApplication;
 import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
 import com.teamcenter.rac.aifrcp.AIFUtility;
 import com.teamcenter.rac.kernel.TCComponent;
 import com.teamcenter.rac.kernel.TCComponentItemRevision;
 import com.teamcenter.rac.kernel.TCComponentMEProcessRevision;
 import com.teamcenter.rac.kernel.TCComponentSignoff;
 import com.teamcenter.rac.kernel.TCComponentTask;
 import com.teamcenter.rac.kernel.TCComponentUser;
 import com.teamcenter.rac.kernel.TCException;
 import com.teamcenter.rac.kernel.TCPreferenceService;
 import com.teamcenter.rac.kernel.TCSession;
 import com.teamcenter.rac.util.MessageBox;
 import java.util.ArrayList;
 import java.util.List;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 
 public class ProcessImportHandler extends AbstractHandler {
   private static final String NHL_ROUTEIMPERP_MENUAUTH = "NHL_RouteimpERP_MenuAuth";
   
   private static final String NHL_ROUTEIMPERP_PRIVILEGES = "NHL_RouteimpERP_Privileges";
   
/*  52 */   public List<TCComponentItemRevision> listProcessRevisions = new ArrayList<TCComponentItemRevision>();
   
/*  54 */   public StringBuffer detailInfo = new StringBuffer("");
   
   public AbstractAIFUIApplication application;
   
   public TCSession session;
   
   public boolean isPrivilege = false;
   
   TCPreferenceService preferenceService;
   
   TCComponentUser currentUser;
   
   int targetComLen;
   
   InterfaceAIFComponent[] targetComponents;
   
   public AimType aimType;
   
   public Object execute(ExecutionEvent event) throws ExecutionException {
/*  80 */     return null;
   }
   
   public boolean checkTarget(AimType aimType) {
/*  84 */     this.aimType = aimType;
/*  85 */     this.application = AIFUtility.getCurrentApplication();
/*  86 */     this.session = (TCSession)this.application.getSession();
/*  87 */     if (this.application.getTargetComponent() == null || (this.application.getTargetComponents()).length == 0) {
/*  88 */       MessageBox.post(this.application.getDesktop().getShell(), "请您选择流程中的任务或工艺版本对象！", "警告", 4);
/*  89 */       return false;
     } 
/*  91 */     if (!this.listProcessRevisions.isEmpty())
/*  92 */       this.listProcessRevisions.clear(); 
/*  94 */     if (!this.detailInfo.equals(""))
/*  95 */       this.detailInfo = new StringBuffer(""); 
/*  97 */     this.preferenceService = this.session.getPreferenceService();
/*  98 */     this.targetComponents = this.application.getTargetComponents();
/*  99 */     InterfaceAIFComponent targetComponent = this.targetComponents[0];
/* 100 */     this.targetComLen = this.targetComponents.length;
/* 102 */     String[] userNames = this.preferenceService.getStringArray(4, "NHL_RouteimpERP_Privileges");
/* 103 */     this.currentUser = this.session.getUser();
/* 104 */     String userID = "";
     try {
/* 106 */       userID = this.currentUser.getProperty("user_id");
/* 107 */     } catch (TCException e1) {
/* 108 */       e1.printStackTrace();
     } 
/* 110 */     if (userNames != null && userNames.length > 0) {
/* 111 */       int nameLen = userNames.length;
/* 112 */       for (int i = 0; i < nameLen; i++) {
/* 113 */         if (userNames[i].trim().equals(userID)) {
/* 114 */           this.isPrivilege = true;
           break;
         } 
       } 
     } else {
/* 119 */       MessageBox.post(this.application.getDesktop().getShell(), "请配置首选项NHL_RouteimpERP_Privileges！！", "警告", 4);
/* 120 */       return false;
     } 
/* 122 */     if (targetComponent instanceof TCComponentTask) {
/* 123 */       if (this.isPrivilege) {
/* 124 */         for (int i = 0; i < this.targetComLen; i++) {
/* 125 */           if (this.targetComponents[i] instanceof TCComponentTask) {
/* 126 */             TCComponentTask task = (TCComponentTask)this.targetComponents[i];
             try {
/* 128 */               TCComponent[] attachmentComponents = task.getRelatedComponents("root_target_attachments");
/* 129 */               int attachmentsLen = attachmentComponents.length;
/* 130 */               for (int j = 0; j < attachmentsLen; j++) {
/* 131 */                 if (attachmentComponents[j] instanceof TCComponentMEProcessRevision) {
/* 132 */                   TCComponentMEProcessRevision processRev = (TCComponentMEProcessRevision)attachmentComponents[j];
/* 133 */                   if (processRev.getTCClass().getSuperClass().toString().equals("NHL8_MeprocessRevision") && !processRev.getType().equals("NHL8_RigOutGXRevision") ) {
/* 134 */                     if ((processRev.getReferenceListProperty("release_status_list")).length == 0)
/* 135 */                       this.detailInfo.append(processRev + " 对象未发布<br>"); 
/* 137 */                     if ((processRev.getRelatedComponents("IMAN_METarget")).length == 0) {
	
									if(!processRev.getType().equals("NHL8_RigOutGXRevision")) {
										this.detailInfo.append(processRev + " 制造目标关系没有关联任何零组件<br>"); 
									}
								}
/* 138 */                       
/* 140 */                     if (!this.listProcessRevisions.contains(processRev))
/* 141 */                       this.listProcessRevisions.add(processRev); 
                   } 
                 } 
               } 
/* 146 */             } catch (TCException e) {
/* 147 */               e.printStackTrace();
/* 148 */             } catch (Exception e) {
/* 149 */               e.printStackTrace();
             } 
           } else {
/* 152 */             MessageBox.post(this.application.getDesktop().getShell(), "请确认选中的是任务对象！！", "警告", 4);
/* 153 */             return false;
           } 
         } 
/* 157 */       } else if (aimType.toString().equals("add")) {
/* 158 */         if (!ProcessImportApi.getTaskRouteAddResult(this.application, this.session, FileType.route, "工艺路线新增结果", this, "task"))
/* 159 */           return false; 
/* 161 */       } else if (aimType.toString().equals("chg") && 
/* 162 */         !ProcessImportApi.getTaskRouteChgResult(this.application, this.session, FileType.route, "工艺路线变更结果", this, "task")) {
/* 163 */         return false;
       } 
/* 169 */     } else if (targetComponent instanceof TCComponentMEProcessRevision) {
/* 170 */       TCComponentMEProcessRevision processRev = (TCComponentMEProcessRevision)targetComponent;
       try {
/* 172 */         if (processRev.getTCClass().getSuperClass().toString().equals("NHL8_MeprocessRevision") && !processRev.getType().equals("NHL8_RigOutGXRevision")) {
/* 173 */           if (this.isPrivilege) {
/* 174 */             for (int i = 0; i < this.targetComLen; i++) {
/* 175 */               if (this.targetComponents[i] instanceof TCComponentMEProcessRevision) {
/* 176 */                 TCComponentMEProcessRevision revision = (TCComponentMEProcessRevision)this.targetComponents[i];
                 try {
/* 178 */                   if ((revision.getReferenceListProperty("release_status_list")).length == 0)
/* 179 */                     this.detailInfo.append(revision + " 对象未发布<br>"); 
/* 181 */                   if ((revision.getRelatedComponents("IMAN_METarget")).length == 0) {
							if(!processRev.getType().equals("NHL8_RigOutGXRevision")) {
								this.detailInfo.append(processRev + " 制造目标关系没有关联任何零组件<br>"); 
							}
						}
/* 182 */                     
/* 184 */                   this.listProcessRevisions.add(revision);
/* 185 */                 } catch (TCException e) {
/* 186 */                   e.printStackTrace();
                 } 
               } else {
/* 189 */                 MessageBox.post(this.application.getDesktop().getShell(), "请选中正确的工艺路线版本对象！！", "警告", 4);
/* 190 */                 return false;
               } 
             } 
/* 194 */           } else if (aimType.toString().equals("add")) {
/* 195 */             if (!ProcessImportApi.getTaskRouteAddResult(this.application, this.session, FileType.route, "工艺路线新增结果", this, "route"))
/* 196 */               return false; 
/* 198 */           } else if (aimType.toString().equals("chg") && 
/* 199 */             !ProcessImportApi.getTaskRouteChgResult(this.application, this.session, FileType.route, "工艺路线变更结果", this, "route")) {
/* 200 */             return false;
           } 
         } else {
/* 205 */           MessageBox.post(this.application.getDesktop().getShell(), "请您选择流程中的任务或工艺版本对象！！", "警告", 4);
/* 206 */           return false;
         } 
/* 208 */       } catch (TCException e) {
/* 209 */         e.printStackTrace();
       } 
     } else {
/* 212 */       MessageBox.post(this.application.getDesktop().getShell(), "请您选择流程中的任务或工艺版本对象！！！", "警告", 4);
/* 213 */       return false;
     } 
/* 216 */     return true;
   }
   
   public void postDialogForTask(boolean isNeedPost) {
/* 220 */     String[] processAndTaskNames = this.preferenceService.getStringArray(4, "NHL_RouteimpERP_MenuAuth");
/* 221 */     List<String> listProcessAndTaskNames = new ArrayList<String>();
/* 222 */     if (processAndTaskNames != null && processAndTaskNames.length > 0) {
/* 223 */       int nameLen = processAndTaskNames.length;
/* 224 */       for (int j = 0; j < nameLen; j++)
/* 225 */         listProcessAndTaskNames.add(processAndTaskNames[j].trim().replace("：", ":")); 
     } else {
/* 228 */       MessageBox.post(this.application.getDesktop().getShell(), "请配置首选项NHL_RouteimpERP_MenuAuth！！", "警告", 4);
       return;
     } 
/* 232 */     for (int i = 0; i < this.targetComLen; i++) {
/* 233 */       if (this.targetComponents[i] instanceof TCComponentTask) {
/* 234 */         TCComponentTask task = (TCComponentTask)this.targetComponents[i];
/* 235 */         String taskType = task.getType();
/* 236 */         String processAndTaskName = "";
/* 237 */         boolean isTaskPerformer = false;
         try {
/* 239 */           if (taskType.equals("EPMPerformSignoffTask")) {
/* 240 */             processAndTaskName = task.getRoot().getProcessDefinition().getName() + ":" + task.getParent().getName();
/* 241 */             TCComponentSignoff[] signoffs = task.getPredecessors()[0].getValidSignoffs();
/* 242 */             int signoffNum = signoffs.length;
/* 243 */             for (int j = 0; j < signoffNum; j++) {
/* 244 */               if (signoffs[j].getGroupMember().getUser().equals(this.currentUser)) {
/* 245 */                 isTaskPerformer = true;
                 break;
               } 
             } 
           } 
/* 250 */           if (listProcessAndTaskNames.contains(processAndTaskName) && isTaskPerformer) {
/* 251 */             TCComponent[] attachmentComponents = task.getRelatedComponents("root_target_attachments");
/* 252 */             int attachmentsLen = attachmentComponents.length;
/* 253 */             for (int j = 0; j < attachmentsLen; j++) {
/* 254 */               if (attachmentComponents[j] instanceof TCComponentMEProcessRevision) {
/* 255 */                 TCComponentMEProcessRevision processRev = (TCComponentMEProcessRevision)attachmentComponents[j];
/* 256 */                 if (processRev.getTCClass().getSuperClass().toString().equals("NHL8_MeprocessRevision")) {
/* 257 */                   if ((processRev.getReferenceListProperty("release_status_list")).length == 0) {
/* 258 */                     MessageBox.post(processRev + " 对象未发布", "警告", 4);
                     return;
                   } 
/* 261 */                   if ((processRev.getRelatedComponents("IMAN_METarget")).length == 0) {
	
							if(!processRev.getType().equals("NHL8_RigOutGXRevision")) {
								MessageBox.post(processRev + "制造目标关系没有关联任何零组件!", "提示", 4);
//								this.detailInfo.append(processRev + " 制造目标关系没有关联任何零组件<br>"); 
							}
/* 262 */                     
                     return;
                   } 
/* 266 */                   if (!this.listProcessRevisions.contains(processRev))
/* 267 */                     this.listProcessRevisions.add(processRev); 
                 } 
               } 
             } 
           } else {
/* 273 */             MessageBox.post(this.application.getDesktop().getShell(), "请选中有权限的任务对象！！", "警告", 4);
             return;
           } 
/* 276 */         } catch (TCException e) {
/* 277 */           e.printStackTrace();
/* 278 */         } catch (Exception e) {
/* 279 */           e.printStackTrace();
         } 
       } else {
/* 282 */         MessageBox.post(this.application.getDesktop().getShell(), "请确认选中的是任务对象！！", "警告", 4);
         return;
       } 
     } 
/* 286 */     if (isNeedPost && 
/* 287 */       this.listProcessRevisions.size() > 0)
/* 288 */       if (this.aimType.toString().equals("add")) {
         try {
/* 290 */           ProcessAddTransferToERPCommand processAddTransferToERPCommand = new ProcessAddTransferToERPCommand(this.session, this.application, this.listProcessRevisions, this.detailInfo, this.isPrivilege);
/* 291 */           processAddTransferToERPCommand.executeModal();
/* 292 */         } catch (Exception e) {
/* 293 */           e.printStackTrace();
         } 
/* 295 */       } else if (this.aimType.toString().equals("chg")) {
         try {
/* 297 */           ProcessChgTransferToERPCommand processChgTransferToERPCommand = new ProcessChgTransferToERPCommand(this.session, this.application, this.listProcessRevisions, this.detailInfo, this.isPrivilege);
/* 298 */           processChgTransferToERPCommand.executeModal();
/* 299 */         } catch (Exception e) {
/* 300 */           e.printStackTrace();
         } 
       }  
   }
   
   public void postDialogForRoute(boolean isNeedPost) {
/* 308 */     String[] processAndTaskNames = this.preferenceService.getStringArray(4, "NHL_RouteimpERP_MenuAuth");
/* 309 */     List<String> listProcessAndTaskNames = new ArrayList<String>();
/* 310 */     if (processAndTaskNames != null && processAndTaskNames.length > 0) {
/* 311 */       int nameLen = processAndTaskNames.length;
/* 312 */       for (int j = 0; j < nameLen; j++)
/* 313 */         listProcessAndTaskNames.add(processAndTaskNames[j].trim().replace("：", ":")); 
     } else {
/* 316 */       MessageBox.post(this.application.getDesktop().getShell(), "请配置首选项NHL_RouteimpERP_MenuAuth！！", "警告", 4);
       return;
     } 
/* 319 */     for (int i = 0; i < this.targetComLen; i++) {
/* 320 */       if (this.targetComponents[i] instanceof TCComponentMEProcessRevision) {
/* 321 */         TCComponentMEProcessRevision revision = (TCComponentMEProcessRevision)this.targetComponents[i];
/* 322 */         if (!revRefRightProcess((TCComponentItemRevision)revision, listProcessAndTaskNames, this.currentUser))
           return; 
         try {
/* 326 */           if ((revision.getRelatedComponents("IMAN_METarget")).length == 0 && !revision.getType().equals("NHL8_RigOutGXRevision")) {
/* 327 */             MessageBox.post(this.application.getDesktop().getShell(), revision + " 制造目标关系没有关联任何零组件！！", "警告", 4);
             return;
           } 
/* 330 */           this.listProcessRevisions.add(revision);
/* 331 */         } catch (TCException e) {
/* 332 */           e.printStackTrace();
         } 
       } else {
/* 335 */         MessageBox.post(this.application.getDesktop().getShell(), "请选中正确的工艺路线版本对象！！", "警告", 4);
         return;
       } 
     } 
/* 339 */     if (isNeedPost && 
/* 340 */       this.listProcessRevisions.size() > 0)
/* 341 */       if (this.aimType.toString().equals("add")) {
         try {
/* 343 */           ProcessAddTransferToERPCommand processAddTransferToERPCommand = new ProcessAddTransferToERPCommand(this.session, this.application, this.listProcessRevisions, this.detailInfo, this.isPrivilege);
/* 344 */           processAddTransferToERPCommand.executeModal();
/* 345 */         } catch (Exception e) {
/* 346 */           e.printStackTrace();
         } 
/* 348 */       } else if (this.aimType.toString().equals("chg")) {
         try {
/* 350 */           ProcessChgTransferToERPCommand processChgTransferToERPCommand = new ProcessChgTransferToERPCommand(this.session, this.application, this.listProcessRevisions, this.detailInfo, this.isPrivilege);
/* 351 */           processChgTransferToERPCommand.executeModal();
/* 352 */         } catch (Exception e) {
/* 353 */           e.printStackTrace();
         } 
       }  
   }
   
   private boolean revRefRightProcess(TCComponentItemRevision revision, List<String> listProcessAndTaskNames, TCComponentUser currentUser) {
     try {
/* 369 */       if ((revision.getReferenceListProperty("release_status_list")).length == 0) {
/* 370 */         MessageBox.post(this.application.getDesktop().getShell(), revision + " 对象未发布！！", "警告", 4);
/* 371 */         return false;
       } 
/* 373 */       TCComponent[] processStageList = revision.getReferenceListProperty("fnd0StartedWorkflowTasks");
/* 374 */       int listSize = processStageList.length;
/* 375 */       TCComponentTask referenceTask = null;
/* 376 */       if (listSize > 0) {
/* 377 */         String rootTaskName = processStageList[0].getProperty("object_name");
/* 378 */         for (int j = 1; j < listSize; j++) {
/* 379 */           if (listProcessAndTaskNames.contains(rootTaskName + ":" + processStageList[j].getProperty("object_name"))) {
/* 380 */             referenceTask = (TCComponentTask)processStageList[j];
             break;
           } 
         } 
/* 384 */         if (referenceTask != null) {
/* 385 */           TCComponentTask[] subTasks = referenceTask.getSubtasks();
/* 386 */           int subTaskNum = subTasks.length;
/* 387 */           boolean isTaskPerformer = false;
/* 388 */           for (int i = 0; i < subTaskNum; i++) {
/* 389 */             TCComponentTask subTask = subTasks[i];
/* 390 */             String taskType = subTask.getType();
/* 391 */             if (taskType.equals("EPMPerformSignoffTask")) {
/* 392 */               TCComponentSignoff[] signoffs = subTask.getPredecessors()[0].getValidSignoffs();
/* 393 */               int signoffNum = signoffs.length;
/* 394 */               for (int k = 0; k < signoffNum; k++) {
/* 395 */                 if (signoffs[k].getGroupMember().getUser().equals(currentUser)) {
/* 396 */                   isTaskPerformer = true;
                   break;
                 } 
               } 
               break;
             } 
           } 
/* 403 */           if (!isTaskPerformer) {
/* 404 */             MessageBox.post(this.application.getDesktop().getShell(), "请确定对Item版本对象的操作权限！！", "警告", 4);
/* 405 */             return false;
           } 
         } else {
/* 408 */           MessageBox.post(this.application.getDesktop().getShell(), "请选中在正确流程节点中的Item版本对象！！", "警告", 4);
/* 409 */           return false;
         } 
       } else {
/* 412 */         MessageBox.post(this.application.getDesktop().getShell(), "请选中在流程中的Item版本对象！！", "警告", 4);
/* 413 */         return false;
       } 
/* 416 */     } catch (TCException e) {
/* 417 */       e.printStackTrace();
     } 
/* 419 */     return true;
   }
 }

