Search in sources :

Example 36 with ModelService

use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.

the class ScriptHelperImpl method createServiceMap.

public Map<String, ? extends Object> createServiceMap(String serviceName, Map<String, ? extends Object> inputMap) throws ScriptException {
    Assert.notNull("serviceName", serviceName, "inputMap", inputMap);
    Map<String, Object> toMap = new HashMap<String, Object>();
    ModelService modelService = null;
    try {
        modelService = ctxHelper.getDispatcher().getDispatchContext().getModelService(serviceName);
    } catch (GenericServiceException e) {
        String errMsg = "Error running script " + ctxHelper.getScriptName() + ": Problem invoking the createServiceMap method: get service definition for service name [" + serviceName + "]: " + e.getMessage();
        Debug.logWarning(e, errMsg, module);
        throw new ScriptException(errMsg);
    }
    toMap.putAll(modelService.makeValid(inputMap, ModelService.IN_PARAM, true, UtilGenerics.checkList(ctxHelper.getErrorMessages()), ctxHelper.getTimeZone(), ctxHelper.getLocale()));
    return toMap;
}
Also used : ScriptException(javax.script.ScriptException) HashMap(java.util.HashMap) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ModelService(org.apache.ofbiz.service.ModelService)

Example 37 with ModelService

use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.

the class HttpEngine method httpEngine.

/**
 * Event for handling HTTP services
 * @param request HttpServletRequest object
 * @param response HttpServletResponse object
 * @return null
 */
public static String httpEngine(HttpServletRequest request, HttpServletResponse response) {
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String serviceName = request.getParameter("serviceName");
    String serviceMode = request.getParameter("serviceMode");
    String xmlContext = request.getParameter("serviceContext");
    Map<String, Object> result = new HashMap<>();
    Map<String, Object> context = null;
    if (serviceName == null) {
        result.put(ModelService.ERROR_MESSAGE, "Cannot have null service name");
    }
    if (serviceMode == null) {
        serviceMode = "SYNC";
    }
    // deserialize the context
    if (!result.containsKey(ModelService.ERROR_MESSAGE)) {
        if (xmlContext != null) {
            try {
                Object o = XmlSerializer.deserialize(xmlContext, delegator);
                if (o instanceof Map<?, ?>) {
                    context = UtilGenerics.checkMap(o);
                } else {
                    Debug.logError("Context not an instance of Map error", module);
                    result.put(ModelService.ERROR_MESSAGE, "Context not an instance of Map");
                }
            } catch (Exception e) {
                Debug.logError(e, "Deserialization error", module);
                result.put(ModelService.ERROR_MESSAGE, "Error occurred deserializing context: " + e.toString());
            }
        }
    }
    // invoke the service
    if (!result.containsKey(ModelService.ERROR_MESSAGE)) {
        try {
            ModelService model = dispatcher.getDispatchContext().getModelService(serviceName);
            if (model.export || exportAll) {
                if ("ASYNC".equals(serviceMode)) {
                    dispatcher.runAsync(serviceName, context);
                } else {
                    result = dispatcher.runSync(serviceName, context);
                }
            } else {
                Debug.logWarning("Attempt to invoke a non-exported service: " + serviceName, module);
                throw new GenericServiceException("Cannot find requested service");
            }
        } catch (GenericServiceException e) {
            Debug.logError(e, "Service invocation error", module);
            result.put(ModelService.ERROR_MESSAGE, "Service invocation error: " + e.toString());
        }
    }
    // backup error message
    StringBuilder errorMessage = new StringBuilder();
    // process the result
    String resultString = null;
    try {
        resultString = XmlSerializer.serialize(result);
    } catch (Exception e) {
        Debug.logError(e, "Cannot serialize result", module);
        if (result.containsKey(ModelService.ERROR_MESSAGE)) {
            errorMessage.append(result.get(ModelService.ERROR_MESSAGE));
        }
        errorMessage.append("::");
        errorMessage.append(e);
    }
    // handle the response
    try {
        PrintWriter out = response.getWriter();
        response.setContentType("plain/text");
        if (errorMessage.length() > 0) {
            response.setContentLength(errorMessage.toString().getBytes("UTF-8").length);
            out.write(errorMessage.toString());
        } else {
            response.setContentLength(resultString.getBytes("UTF-8").length);
            out.write(resultString);
        }
        out.flush();
        response.flushBuffer();
    } catch (IOException e) {
        Debug.logError(e, "Problems w/ getting the servlet writer.", module);
        return "error";
    }
    return null;
}
Also used : LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) IOException(java.io.IOException) HttpClientException(org.apache.ofbiz.base.util.HttpClientException) IOException(java.io.IOException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ModelService(org.apache.ofbiz.service.ModelService) Delegator(org.apache.ofbiz.entity.Delegator) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 38 with ModelService

use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.

the class LabelReferences method getAutoFieldsServiceTag.

private void getAutoFieldsServiceTag(Element element, Set<String> fieldNames) throws GenericServiceException {
    String serviceName = UtilFormatOut.checkNull(element.getAttribute("service-name"));
    String defaultFieldType = UtilFormatOut.checkNull(element.getAttribute("default-field-type"));
    if (UtilValidate.isNotEmpty(serviceName) && (!("hidden".equals(defaultFieldType)))) {
        ModelService modelService = dispatchContext.getModelService(serviceName);
        List<ModelParam> modelParams = modelService.getInModelParamList();
        Iterator<ModelParam> modelParamIter = modelParams.iterator();
        while (modelParamIter.hasNext()) {
            ModelParam modelParam = modelParamIter.next();
            // skip auto params that the service engine populates...
            if ("userLogin".equals(modelParam.name) || "locale".equals(modelParam.name) || "timeZone".equals(modelParam.name)) {
                continue;
            }
            if (modelParam.formDisplay) {
                if (UtilValidate.isNotEmpty(modelParam.entityName) && UtilValidate.isNotEmpty(modelParam.fieldName)) {
                    ModelEntity modelEntity;
                    modelEntity = delegator.getModelEntity(modelParam.entityName);
                    if (modelEntity != null) {
                        ModelField modelField = modelEntity.getField(modelParam.fieldName);
                        if (modelField != null) {
                            fieldNames.add(modelField.getName());
                        }
                    }
                }
            }
        }
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) ModelParam(org.apache.ofbiz.service.ModelParam) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) ModelService(org.apache.ofbiz.service.ModelService)

Aggregations

ModelService (org.apache.ofbiz.service.ModelService)38 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)33 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)28 HashMap (java.util.HashMap)24 GenericValue (org.apache.ofbiz.entity.GenericValue)22 Locale (java.util.Locale)20 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)19 Delegator (org.apache.ofbiz.entity.Delegator)17 ModelParam (org.apache.ofbiz.service.ModelParam)6 ServiceAuthException (org.apache.ofbiz.service.ServiceAuthException)6 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 GeneralException (org.apache.ofbiz.base.util.GeneralException)5 DispatchContext (org.apache.ofbiz.service.DispatchContext)5 IOException (java.io.IOException)4 BigDecimal (java.math.BigDecimal)4 Timestamp (java.sql.Timestamp)4 ByteBuffer (java.nio.ByteBuffer)3 TimeZone (java.util.TimeZone)3 HttpSession (javax.servlet.http.HttpSession)3