package com.connor.erp.processresource.EquipmentAcquiredFromERP;

import com.teamcenter.rac.aif.AbstractAIFCommand;
import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentFolder;
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 org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

public class EquipmentAcquiredFromERPHandler extends AbstractHandler {
  private static final String NHL_ACHIEVEERPEQM_ADMIN = "NHL_AchieveERPEqM_Admin";
  
  public Object execute(ExecutionEvent event) throws ExecutionException {
    AbstractAIFUIApplication application = AIFUtility.getCurrentApplication();
    TCSession session = (TCSession)application.getSession();
    if (application == null || application.getTargetComponent() == null) {
      MessageBox.post(application.getDesktop().getShell(), "请选中设备文件夹！！", "警告", 4);
      return null;
    } 
    String user_id = "";
    try {
      user_id = session.getUser().getProperty("user_id");
    } catch (TCException e1) {
      e1.printStackTrace();
    } 
    TCPreferenceService preferenceService = session.getPreferenceService();
    String[] users = preferenceService.getStringArray(4, "NHL_AchieveERPEqM_Admin");
    if (users != null && users.length > 0) {
      int groupLen = users.length;
      boolean hasRight = false;
      for (int i = 0; i < groupLen; i++) {
        if (user_id.equals(users[i].trim())) {
          hasRight = true;
          break;
        } 
      } 
      if (hasRight) {
        if (application.getTargetComponent() instanceof TCComponentFolder) {
          TCComponentFolder targetFolder = (TCComponentFolder)application.getTargetComponent();
          try {
            if (!targetFolder.getProperty("object_name").equals("设备")) {
              MessageBox.post(application.getDesktop().getShell(), "请选中设备文件夹！！", "警告", 4);
              return null;
            } 
          } catch (TCException e1) {
            e1.printStackTrace();
          } 
          try {
            AbstractAIFCommand command = new EquipmentAcquiredFromERPCommand(session, application, targetFolder);
            command.executeModal();
          } catch (Exception e) {
            e.printStackTrace();
          } 
        } else {
          MessageBox.post(application.getDesktop().getShell(), "请选中设备文件夹！！", "警告", 4);
          return null;
        } 
      } else {
        MessageBox.post(application.getDesktop().getShell(), "你没有权限进行当前操作", "警告", 4);
        return null;
      } 
    } else {
      MessageBox.post(application.getDesktop().getShell(), "请配置首选项NHL_AchieveERPEqM_Admin！！", "警告", 4);
      return null;
    } 
    return null;
  }
}
