Ejemplo n.º 1
0
    protected Map<String, Object> getContext(XmlRpcRequest xmlRpcReq, String serviceName)
        throws XmlRpcException {
      ModelService model;
      try {
        model = dispatcher.getDispatchContext().getModelService(serviceName);
      } catch (GenericServiceException e) {
        throw new XmlRpcException(e.getMessage(), e);
      }

      // context placeholder
      Map<String, Object> context = FastMap.newInstance();

      if (model != null) {
        int parameterCount = xmlRpcReq.getParameterCount();

        // more than one parameter; use list notation based on service def order
        if (parameterCount > 1) {
          int x = 0;
          for (String name : model.getParameterNames("IN", true, true)) {
            context.put(name, xmlRpcReq.getParameter(x));
            x++;

            if (x == parameterCount) {
              break;
            }
          }

          // only one parameter; if its a map use it as the context; otherwise make sure the service
          // takes one param
        } else if (parameterCount == 1) {
          Object param = xmlRpcReq.getParameter(0);
          if (param instanceof Map<?, ?>) {
            context = checkMap(param, String.class, Object.class);
          } else {
            if (model.getDefinedInCount() == 1) {
              String paramName = model.getInParamNames().iterator().next();
              context.put(paramName, xmlRpcReq.getParameter(0));
            } else {
              throw new XmlRpcException(
                  "More than one parameter defined on service; cannot call via RPC with parameter list");
            }
          }
        }

        // do map value conversions
        context = model.makeValid(context, ModelService.IN_PARAM);
      }

      return context;
    }
Ejemplo n.º 2
0
  private Map<String, Object> serviceInvoker(
      String localName, ModelService modelService, Map<String, Object> context)
      throws GenericServiceException {
    if (UtilValidate.isEmpty(modelService.location)) {
      throw new GenericServiceException("Cannot run Groovy service with empty location");
    }
    Map<String, Object> params = FastMap.newInstance();
    params.putAll(context);
    context.put(ScriptUtil.PARAMETERS_KEY, params);

    DispatchContext dctx = dispatcher.getLocalContext(localName);
    context.put("dctx", dctx);
    context.put("dispatcher", dctx.getDispatcher());
    context.put("delegator", dispatcher.getDelegator());
    try {
      ScriptContext scriptContext = ScriptUtil.createScriptContext(context, protectedKeys);
      ScriptHelper scriptHelper =
          (ScriptHelper) scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY);
      if (scriptHelper != null) {
        context.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
      }
      Script script =
          InvokerHelper.createScript(
              GroovyUtil.getScriptClassFromLocation(
                  this.getLocation(modelService), groovyClassLoader),
              GroovyUtil.getBinding(context));
      Object resultObj = null;
      if (UtilValidate.isEmpty(modelService.invoke)) {
        resultObj = script.run();
      } else {
        resultObj = script.invokeMethod(modelService.invoke, EMPTY_ARGS);
      }
      if (resultObj == null) {
        resultObj = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
      }
      if (resultObj != null && resultObj instanceof Map<?, ?>) {
        return cast(resultObj);
      }
      Map<String, Object> result = ServiceUtil.returnSuccess();
      result.putAll(
          modelService.makeValid(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE), "OUT"));
      return result;
    } catch (GeneralException ge) {
      throw new GenericServiceException(ge);
    } catch (Exception e) {
      return ServiceUtil.returnError(e.getMessage());
    }
  }
Ejemplo n.º 3
0
    public Object execute(XmlRpcRequest xmlRpcReq) throws XmlRpcException {
      DispatchContext dctx = dispatcher.getDispatchContext();
      String serviceName = xmlRpcReq.getMethodName();
      ModelService model = null;
      try {
        model = dctx.getModelService(serviceName);
      } catch (GenericServiceException e) {
        throw new XmlRpcException(e.getMessage(), e);
      }

      // check remote invocation security
      if (model == null || !model.export) {
        throw new XmlRpcException("Unknown method");
      }

      // prepare the context -- single parameter type struct (map)
      Map<String, Object> context = this.getContext(xmlRpcReq, serviceName);

      // add in auth parameters
      XmlRpcHttpRequestConfig config = (XmlRpcHttpRequestConfig) xmlRpcReq.getConfig();
      String username = config.getBasicUserName();
      String password = config.getBasicPassword();
      if (UtilValidate.isNotEmpty(username)) {
        context.put("login.username", username);
        context.put("login.password", password);
      }

      // add the locale to the context
      context.put("locale", Locale.getDefault());

      // invoke the service
      Map<String, Object> resp;
      try {
        resp = dispatcher.runSync(serviceName, context);
      } catch (GenericServiceException e) {
        throw new XmlRpcException(e.getMessage(), e);
      }
      if (ServiceUtil.isError(resp)) {
        Debug.logError(ServiceUtil.getErrorMessage(resp), module);
        throw new XmlRpcException(ServiceUtil.getErrorMessage(resp));
      }

      // return only definied parameters
      return model.makeValid(resp, ModelService.OUT_PARAM, false, null);
    }
Ejemplo n.º 4
0
    @Override
    public boolean eval(Map<String, Object> context) {
      // if no user is logged in, treat as if the user does not have permission
      GenericValue userLogin = (GenericValue) context.get("userLogin");
      if (userLogin != null) {
        String serviceName = serviceExdr.expandString(context);
        String mainAction = actionExdr.expandString(context);
        String contextMap = ctxMapExdr.expandString(context);
        String resource = resExdr.expandString(context);
        if (UtilValidate.isEmpty(resource)) {
          resource = serviceName;
        }

        if (UtilValidate.isEmpty(serviceName)) {
          Debug.logWarning("No permission service-name specified!", module);
          return false;
        }

        Map<String, Object> serviceContext = UtilGenerics.toMap(context.get(contextMap));
        if (serviceContext != null) {
          // copy the required internal fields
          serviceContext.put("userLogin", context.get("userLogin"));
          serviceContext.put("locale", context.get("locale"));
        } else {
          serviceContext = context;
        }

        // get the service objects
        LocalDispatcher dispatcher = this.modelScreen.getDispatcher(context);
        DispatchContext dctx = dispatcher.getDispatchContext();

        // get the service
        ModelService permService;
        try {
          permService = dctx.getModelService(serviceName);
        } catch (GenericServiceException e) {
          Debug.logError(e, module);
          return false;
        }

        if (permService != null) {
          // build the context
          Map<String, Object> svcCtx = permService.makeValid(serviceContext, ModelService.IN_PARAM);
          svcCtx.put("resourceDescription", resource);
          if (UtilValidate.isNotEmpty(mainAction)) {
            svcCtx.put("mainAction", mainAction);
          }

          // invoke the service
          Map<String, Object> resp;
          try {
            resp = dispatcher.runSync(permService.name, svcCtx, 300, true);
          } catch (GenericServiceException e) {
            Debug.logError(e, module);
            return false;
          }
          if (ServiceUtil.isError(resp) || ServiceUtil.isFailure(resp)) {
            Debug.logError(ServiceUtil.getErrorMessage(resp), module);
            return false;
          }
          Boolean hasPermission = (Boolean) resp.get("hasPermission");
          if (hasPermission != null) {
            return hasPermission.booleanValue();
          }
        }
      }
      return false;
    }
Ejemplo n.º 5
0
  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;
  }