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;
}
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;
}
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());
}
}
}
}
}
}
}
Aggregations