public static Map<String, Object> assembleCrmsfaPartyFormMergeContext(
      Delegator delegator, String partyId) {
    Map<String, Object> templateContext = new HashMap<String, Object>();
    if (UtilValidate.isNotEmpty(partyId)) {
      try {
        String email =
            PartyContactHelper.getElectronicAddressByPurpose(
                partyId, "EMAIL_ADDRESS", "PRIMARY_EMAIL", delegator);
        if (UtilValidate.isNotEmpty(email)) {
          templateContext.put("email", email);
        }
        GenericValue address =
            PartyContactHelper.getPostalAddressValueByPurpose(
                partyId, "PRIMARY_LOCATION", true, delegator);
        if (UtilValidate.isNotEmpty(address)) {
          templateContext.put("attnName", address.get("attnName"));
          templateContext.put("toName", address.get("toName"));
          templateContext.put("address1", address.get("address1"));
          templateContext.put("address2", address.get("address2"));
          templateContext.put("city", address.get("city"));
          templateContext.put("zip", address.get("postalCode"));

          GenericValue stateProvGeo = address.getRelatedOne("StateProvinceGeo");
          if (UtilValidate.isNotEmpty(stateProvGeo)) {
            templateContext.put("state", stateProvGeo.get("geoName"));
          }
          GenericValue countryGeo = address.getRelatedOne("CountryGeo");
          if (UtilValidate.isNotEmpty(countryGeo)) {
            templateContext.put("country", countryGeo.get("geoName"));
          }
        }
        GenericValue party =
            delegator.findByPrimaryKey("PartySummaryCRMView", UtilMisc.toMap("partyId", partyId));
        Map<String, Object> partyMap = party.getAllFields();
        if (UtilValidate.isNotEmpty(partyMap)) {
          Iterator<String> pmf = partyMap.keySet().iterator();
          while (pmf.hasNext()) {
            String fieldName = pmf.next();
            Object value = partyMap.get(fieldName);
            if (UtilValidate.isNotEmpty(value)) {
              templateContext.put(fieldName, value);
            }
          }
        }

        templateContext.put(
            "fullName", org.ofbiz.party.party.PartyHelper.getPartyName(party, false));

      } catch (GenericEntityException ge) {
        Debug.logError(ge, MODULE);
      }
    }
    return templateContext;
  }
  public static Map<String, Object> assembleCrmsfaOrderFormMergeContext(
      Delegator delegator, String orderId) {
    Map<String, Object> templateContext = new HashMap<String, Object>();
    if (UtilValidate.isNotEmpty(orderId)) {
      try {
        OrderReadHelper orh = new OrderReadHelper(delegator, orderId);
        templateContext.put("orderId", orderId);
        templateContext.put("externalOrderId", orh.getOrderHeader().get("externalId"));
        templateContext.put("orderDate", orh.getOrderHeader().getTimestamp("orderDate"));

        GenericValue billingParty = orh.getBillToParty();
        if (UtilValidate.isNotEmpty(billingParty)) {
          if ("Person".equalsIgnoreCase(billingParty.getEntityName())) {
            templateContext.put("orderBillingFirstName", billingParty.get("firstName"));
            templateContext.put("orderBillingLastName", billingParty.get("lastName"));
          }
          templateContext.put("orderPartyId", billingParty.get("partyId"));
          templateContext.put(
              "orderBillingFullName",
              org.ofbiz.party.party.PartyHelper.getPartyName(billingParty, false));
        }

        templateContext.put("orderSubtotal", orh.getOrderItemsSubTotal());
        templateContext.put("orderTaxTotal", orh.getTaxTotal());
        templateContext.put("orderShippingTotal", orh.getShippingTotal());
        templateContext.put("orderGrandTotal", orh.getOrderGrandTotal());
        templateContext.put(
            "orderPaymentTotal",
            orh.getOrderGrandTotal().subtract(UtilOrder.getOrderOpenAmount(orh)));

        GenericValue shippingParty = orh.getShipToParty();
        if (UtilValidate.isNotEmpty(shippingParty)) {
          if ("Person".equalsIgnoreCase(shippingParty.getEntityName())) {
            templateContext.put("orderShippingFirstName", shippingParty.get("firstName"));
            templateContext.put("orderShippingLastName", shippingParty.get("lastName"));
          } else {
            templateContext.put("orderShippingCompanyName", shippingParty.get("groupName"));
          }
          templateContext.put(
              "orderShippingFullName",
              org.ofbiz.party.party.PartyHelper.getPartyName(shippingParty, false));
        }

        List<GenericValue> orderItemVals = orh.getOrderItems();
        List<Map<String, Object>> orderItems = new ArrayList<Map<String, Object>>();
        for (GenericValue orderItemVal : orderItemVals) {
          Map<String, Object> orderItem = orderItemVal.getAllFields();
          GenericValue product = orderItemVal.getRelatedOne("Product");
          if (UtilValidate.isEmpty(product)) {
            continue;
          }
          for (String fieldName : (Set<String>) product.keySet()) {
            orderItem.put(fieldName, product.get(fieldName));
          }
          orderItems.add(orderItem);
        }
        templateContext.put("orderItems", orderItems);

      } catch (GenericEntityException e) {
        Debug.logError(e, MODULE);
      }
    }
    return templateContext;
  }
  public static Map<String, Object> createContact(
      DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Security security = dctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = UtilCommon.getLocale(context);

    if (!security.hasPermission("CRMSFA_CONTACT_CREATE", userLogin)) {
      return UtilMessage.createAndLogServiceError("CrmErrorPermissionDenied", locale, MODULE);
    }

    // the net result of creating an contact is the generation of a Contact partyId
    String contactPartyId = (String) context.get("partyId");
    try {
      // make sure user has the right crmsfa roles defined.  otherwise the contact will be created
      // as deactivated.
      if (UtilValidate.isEmpty(
          PartyHelper.getFirstValidTeamMemberRoleTypeId(
              userLogin.getString("partyId"), delegator))) {
        return UtilMessage.createAndLogServiceError(
            "CrmError_NoRoleForCreateParty",
            UtilMisc.toMap(
                "userPartyName",
                org.ofbiz.party.party.PartyHelper.getPartyName(
                    delegator, userLogin.getString("partyId"), false),
                "requiredRoleTypes",
                PartyHelper.TEAM_MEMBER_ROLES),
            locale,
            MODULE);
      }

      // if we're given the partyId to create, then verify it is free to use
      if (contactPartyId != null) {
        Map<String, Object> findMap = UtilMisc.<String, Object>toMap("partyId", contactPartyId);
        GenericValue party = delegator.findByPrimaryKey("Party", findMap);
        if (party != null) {
          return UtilMessage.createAndLogServiceError(
              "person.create.person_exists", findMap, locale, MODULE);
        }
      }

      // create the Party and Person, which results in a partyId
      Map<String, Object> input =
          UtilMisc.<String, Object>toMap(
              "firstName", context.get("firstName"), "lastName", context.get("lastName"));
      if (contactPartyId != null) {
        input.put("partyId", contactPartyId);
      }
      input.put("firstNameLocal", context.get("firstNameLocal"));
      input.put("lastNameLocal", context.get("lastNameLocal"));
      input.put("personalTitle", context.get("personalTitle"));
      input.put("preferredCurrencyUomId", context.get("preferredCurrencyUomId"));
      input.put("description", context.get("description"));
      input.put("birthDate", context.get("birthDate"));
      Map<String, Object> serviceResults = dispatcher.runSync("createPerson", input);
      if (ServiceUtil.isError(serviceResults)) {
        return UtilMessage.createAndLogServiceError(
            serviceResults, "CrmErrorCreateContactFail", locale, MODULE);
      }
      contactPartyId = (String) serviceResults.get("partyId");

      // create a PartyRole for the resulting Contact partyId with roleTypeId = CONTACT
      serviceResults =
          dispatcher.runSync(
              "createPartyRole",
              UtilMisc.toMap(
                  "partyId", contactPartyId, "roleTypeId", "CONTACT", "userLogin", userLogin));
      if (ServiceUtil.isError(serviceResults)) {
        return UtilMessage.createAndLogServiceError(
            serviceResults, "CrmErrorCreateContactFail", locale, MODULE);
      }

      // create PartySupplementalData
      GenericValue partyData =
          delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", contactPartyId));
      partyData.setNonPKFields(context);
      partyData.create();

      // create a party relationship between the userLogin and the Contact with
      // partyRelationshipTypeId RESPONSIBLE_FOR
      createResponsibleContactRelationshipForParty(
          userLogin.getString("partyId"), contactPartyId, userLogin, delegator, dispatcher);

      // if initial marketing campaign is provided, add it
      String marketingCampaignId = (String) context.get("marketingCampaignId");
      if (marketingCampaignId != null) {
        serviceResults =
            dispatcher.runSync(
                "crmsfa.addContactMarketingCampaign",
                UtilMisc.toMap(
                    "partyId",
                    contactPartyId,
                    "marketingCampaignId",
                    marketingCampaignId,
                    "userLogin",
                    userLogin));
        if (ServiceUtil.isError(serviceResults)) {
          return UtilMessage.createAndLogServiceError(
              serviceResults, "CrmErrorCreateContactFail", locale, MODULE);
        }
      }

      // create basic contact info
      ModelService service = dctx.getModelService("crmsfa.createBasicContactInfoForParty");
      input = service.makeValid(context, "IN");
      input.put("partyId", contactPartyId);
      serviceResults = dispatcher.runSync(service.name, input);
      if (ServiceUtil.isError(serviceResults)) {
        return UtilMessage.createAndLogServiceError(
            serviceResults, "CrmErrorCreateContactFail", locale, MODULE);
      }

      // Sumit:  priority of warehouse for the specified party..
      String priorityOne = (String) context.get("warehousePriorityOne");
      String priorityTwo = (String) context.get("warehousePriorityTwo");
      String priorityThree = (String) context.get("warehousePriorityThree");
      String priorityFour = (String) context.get("warehousePriorityFour");

      if (UtilValidate.isNotEmpty(priorityOne)
          && UtilValidate.isNotEmpty(priorityTwo)
          && UtilValidate.isNotEmpty(priorityThree)
          && UtilValidate.isNotEmpty(priorityFour)) {
        Set<String> priorityList = new LinkedHashSet<String>();
        priorityList.add(priorityOne);
        priorityList.add(priorityTwo);
        priorityList.add(priorityThree);
        priorityList.add(priorityFour);
        List<GenericValue> warehousePriority = new ArrayList<GenericValue>();

        GenericValue facilityPriorityOne = delegator.makeValue("FacilityPartyPriority");
        Long count = 0L;
        for (String priority : priorityList) {
          count++;
          facilityPriorityOne.set("facilityId", priority);
          facilityPriorityOne.set("partyId", contactPartyId);
          facilityPriorityOne.set("priority", count);
          facilityPriorityOne.set("thruDate", UtilDateTime.nowTimestamp());
          warehousePriority.add(facilityPriorityOne);
        }
        delegator.storeAll(warehousePriority);
      }

    } catch (GenericServiceException e) {
      return UtilMessage.createAndLogServiceError(e, "CrmErrorCreateContactFail", locale, MODULE);
    } catch (GenericEntityException e) {
      return UtilMessage.createAndLogServiceError(e, "CrmErrorCreateContactFail", locale, MODULE);
    }

    // return the partyId of the newly created Contact
    Map<String, Object> results = ServiceUtil.returnSuccess();
    results.put("partyId", contactPartyId);
    results.put("contactPartyId", contactPartyId);
    return results;
  }