/*
         <![CDATA[
         result = "";
         var useEsign = Packages.com.selectica.config.Config.getPropertyAsBoolean(Packages.com.selectica.config.ConfigKeys.ESIGNATURE_ENABLED);
         var ndaIsStandardContract = thisBundle.getParameterValueObjectFromAnyComponent("ReqCNDADetails", "isStandardContract");
         if (useEsign != null && ndaIsStandardContract != null &&
                 (useEsign.toString().equalsIgnoreCase("true") || useEsign.toString().equalsIgnoreCase("yes")) &&
                 "yes".equalsIgnoreCase(ndaIsStandardContract.toString())){
             var cpName = thisBundle.getParameterValueObjectFromAnyComponent("ReqCNDADetails", "cpName");
             var cpNameBundle = user.getBundleForTrackingNumber(cpName);
             var cp = cpNameBundle.getInfoValueObject("name").toString();
             if (cp != "") {
                 var uEntity = root.getValue("user");
                 var m = uEntity.getBundleMap("DCPBO");
                 var mIt = m.values().iterator();
                 while (mIt.hasNext()) {
                     var b = mIt.next();
                     var s = b.getInfoValueObject("dirStatus").toString();
                     if (s.equals("Active")) {
                         var n = b.getInfoValueObject("name").toString();
                         var cpPrimSignerTitle = b.getParameterValueObjectFromAnyComponent("ReqContact", "prContactTitle");
                         if (cpPrimSignerTitle != null && !cpPrimSignerTitle.toString().isEmpty() && n.equalsIgnoreCase(cp.toString())) {
                             result = cpPrimSignerTitle.toString();
                             break;
                         }
                     }
                 }
             }
         }
         ]]>
  */
  @Override
  public String process() throws Exception {
    String useEsignProp =
        Config.getProperty(ConfigKeys.ESIGNATURE_ENABLED); // @todo move to RCFHelper !

    boolean useEsign = "true".equalsIgnoreCase(useEsignProp);
    ContractInfoComponent info = getHelper().getInfoComponentStub();
    boolean isStandardContract = "yes".equalsIgnoreCase(info.getIsStandardContract());

    if (useEsign && isStandardContract) {
      String cpName = info.getCpName();
      if (cpName != null && !cpName.isEmpty()) {
        RCFUserWrapper userWrapper = getHelper().getRCFUserWrapper();
        RCFBundleServiceHelper serviceHelper = RCFBundleServiceHelper.getInstance();
        RCFBundleWrapper bundleWrapper =
            serviceHelper.getBundleByTrackingNumber(Integer.valueOf(cpName), userWrapper);
        if (bundleWrapper != null) {
          Contact contact = getHelper().getComponentStubForBundle(bundleWrapper, "ReqContact");
          if (contact != null) {
            return contact.getPrContactTitle();
          }
        }
      }
    }
    return null;
  }
  @Override
  public Boolean process() throws Exception {
    CFR1Details info = getHelper().getCurrentComponentStub();
    RCFUserWrapper userWrapper = getHelper().getRCFUserWrapper();
    if (getHelper().isCurrentComponentNew()) {
      String event = info.getBundleCreationWfEvent();

      String curName =
          getHelper().getComponentDefinitionHelper(info).getLocalizedString("contractTypeName")
              + String.valueOf(info.getTrackingNumber());
      if (!"WFAmendmentActivation"
          .equals(event)) { // when Amendment activated we should not create a new record
        ContractStats stats = getHelper().addNewComponent("ReqContractStats");
        Date today = new Date();
        String dateString = Validator.createYYYYMMDDHHMMSSStringFromDate(today);
        stats.setContStartTime(new Date());
        stats.setLastRequestStartTime(new Date());
        stats.setNoAssemble(1);
        info.setContractStatus(ContractStatus.DRAFT.getStatus());
      }
      if ("TaskAuthAmendWF".equals(event)) {
        Integer parent = info.getParentContractID();
        if (parent != null) {
          RCFBundleWrapper bundleWrapper =
              RCFBundleServiceHelper.getInstance().getBundleByTrackingNumber(parent, userWrapper);
          if (bundleWrapper != null) {
            CFR1Details parentInfo = getHelper().getInfoComponentStubForBundle(bundleWrapper);
            String parentName = parentInfo.getName();
            info.setName(parentName);
          }
        }
      } else if ("RelatedWFCreate".equals(event)) {
        Integer parent = info.getParentContractID();
        if (parent != null) {
          RCFBundleWrapper bundleWrapper =
              RCFBundleServiceHelper.getInstance().getBundleByTrackingNumber(parent, userWrapper);
          if (bundleWrapper != null) {
            Integer trackingNumber = info.getTrackingNumber();
            if (!curName.contains(String.valueOf(trackingNumber))) {
              curName = curName + "( " + trackingNumber + " )";
            }
            RCFServiceAPI serviceAPI = RCFServiceAPI.getInstance();
            String msg = serviceAPI.getLocalizedString("wf.create.sub.name", userWrapper);

            CFR1Details parentInfo = getHelper().getInfoComponentStubForBundle(bundleWrapper);
            String parentName = parentInfo.getName();
            int index = parentName.indexOf(msg);
            if (index > 1) {
              parentName = parentName.substring(0, index - 1);
            }
            info.setName(curName + " " + msg + " " + parentName);
          }
        }
      } else {
        info.setContractStatus(ContractStatus.DRAFT.getStatus());
        info.setName(curName);
      }
    }

    String currentEvent = getHelper().getCurrentEventVariable();
    if (currentEvent != null && "TaskWFChangeParent".equals(currentEvent)) {
      String curName =
          getHelper().getComponentDefinitionHelper(info).getLocalizedString("contractTypeName")
              + String.valueOf(info.getTrackingNumber());

      Integer parent = info.getParentContractID();
      if (parent == null || parent == 0) {
        info.setName(curName);
      } else {
        RCFBundleWrapper bundleWrapper =
            RCFBundleServiceHelper.getInstance().getBundleByTrackingNumber(parent, userWrapper);
        if (bundleWrapper != null) {
          Integer trackingNumber = info.getTrackingNumber();
          if (!curName.contains(String.valueOf(trackingNumber))) {
            curName = curName + "( " + trackingNumber + " )";
          }
          RCFServiceAPI serviceAPI = RCFServiceAPI.getInstance();
          String msg = serviceAPI.getLocalizedString("wf.create.sub.name", userWrapper);

          CFR1Details parentInfo = getHelper().getInfoComponentStubForBundle(bundleWrapper);
          String parentName = parentInfo.getName();
          int index = parentName.indexOf(msg);
          if (index > 1) {
            parentName = parentName.substring(0, index - 1);
          }
          info.setName(curName + " " + msg + " " + parentName);
        }
      }
    }
    return true;
  }