use of org.apache.ofbiz.service.ModelParam in project ofbiz-framework by apache.
the class SOAPClientEngine method serviceInvoker.
// Invoke the remote SOAP service
private Map<String, Object> serviceInvoker(ModelService modelService, Map<String, Object> context) throws GenericServiceException {
Delegator delegator = dispatcher.getDelegator();
if (modelService.location == null || modelService.invoke == null) {
throw new GenericServiceException("Cannot locate service to invoke");
}
ServiceClient client = null;
QName serviceName = null;
String axis2Repo = "/framework/service/config/axis2";
String axis2RepoLocation = System.getProperty("ofbiz.home") + axis2Repo;
String axis2XmlFile = "/framework/service/config/axis2/conf/axis2.xml";
String axis2XmlFileLocation = System.getProperty("ofbiz.home") + axis2XmlFile;
try {
ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(axis2RepoLocation, axis2XmlFileLocation);
client = new ServiceClient(configContext, null);
Options options = new Options();
EndpointReference endPoint = new EndpointReference(this.getLocation(modelService));
options.setTo(endPoint);
client.setOptions(options);
} catch (AxisFault e) {
throw new GenericServiceException("RPC service error", e);
}
List<ModelParam> inModelParamList = modelService.getInModelParamList();
if (Debug.infoOn()) {
Debug.logInfo("[SOAPClientEngine.invoke] : Parameter length - " + inModelParamList.size(), module);
}
if (UtilValidate.isNotEmpty(modelService.nameSpace)) {
serviceName = new QName(modelService.nameSpace, modelService.invoke);
} else {
serviceName = new QName(modelService.invoke);
}
int i = 0;
Map<String, Object> parameterMap = new HashMap<>();
for (ModelParam p : inModelParamList) {
if (Debug.infoOn()) {
Debug.logInfo("[SOAPClientEngine.invoke} : Parameter: " + p.name + " (" + p.mode + ") - " + i, module);
}
// exclude params that ModelServiceReader insert into (internal params)
if (!p.internal) {
parameterMap.put(p.name, context.get(p.name));
}
i++;
}
OMElement parameterSer = null;
try {
String xmlParameters = SoapSerializer.serialize(parameterMap);
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlParameters));
OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(reader);
parameterSer = builder.getDocumentElement();
} catch (Exception e) {
Debug.logError(e, module);
}
Map<String, Object> results = null;
try {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMElement payload = factory.createOMElement(serviceName);
payload.addChild(parameterSer.getFirstElement());
OMElement respOMElement = client.sendReceive(payload);
client.cleanupTransport();
results = UtilGenerics.cast(SoapSerializer.deserialize(respOMElement.toString(), delegator));
} catch (Exception e) {
Debug.logError(e, module);
}
return results;
}
use of org.apache.ofbiz.service.ModelParam in project ofbiz-framework by apache.
the class EntityAutoEngine method runSync.
/**
* @see org.apache.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.apache.ofbiz.service.ModelService, java.util.Map)
*/
@Override
public Map<String, Object> runSync(String localName, ModelService modelService, Map<String, Object> parameters) throws GenericServiceException {
// static java service methods should be: public Map<String, Object> methodName(DispatchContext dctx, Map<String, Object> context)
DispatchContext dctx = dispatcher.getLocalContext(localName);
Locale locale = (Locale) parameters.get("locale");
Map<String, Object> result = ServiceUtil.returnSuccess();
// check the package and method names
if (modelService.invoke == null || !availableInvokeActionNames.contains(modelService.invoke)) {
throw new GenericServiceException("In Service [" + modelService.name + "] the invoke value must be create, update, or delete for entity-auto engine");
}
if (UtilValidate.isEmpty(modelService.defaultEntityName)) {
throw new GenericServiceException("In Service [" + modelService.name + "] you must specify a default-entity-name for entity-auto engine");
}
ModelEntity modelEntity = dctx.getDelegator().getModelEntity(modelService.defaultEntityName);
if (modelEntity == null) {
throw new GenericServiceException("In Service [" + modelService.name + "] the specified default-entity-name [" + modelService.defaultEntityName + "] is not valid");
}
try {
boolean allPksInOnly = true;
List<String> pkFieldNameOutOnly = null;
/* Check for each pk if it's :
* 1. part IN
* 2. or part IN and OUT, but without value but present on parameters map
* Help the engine to determinate the operation to realize for a create call or validate that
* any pk is present for update/delete call.
*/
for (ModelField pkField : modelEntity.getPkFieldsUnmodifiable()) {
ModelParam pkParam = modelService.getParam(pkField.getName());
boolean pkValueInParameters = pkParam.isIn() && UtilValidate.isNotEmpty(parameters.get(pkParam.getFieldName()));
if (pkParam.isOut() && !pkValueInParameters) {
if (pkFieldNameOutOnly == null) {
pkFieldNameOutOnly = new LinkedList<>();
allPksInOnly = false;
}
pkFieldNameOutOnly.add(pkField.getName());
}
}
switch(modelService.invoke) {
case "create":
result = invokeCreate(dctx, parameters, modelService, modelEntity, allPksInOnly, pkFieldNameOutOnly);
break;
case "update":
result = invokeUpdate(dctx, parameters, modelService, modelEntity, allPksInOnly);
break;
case "delete":
result = invokeDelete(dctx, parameters, modelService, modelEntity, allPksInOnly);
break;
case "expire":
result = invokeExpire(dctx, parameters, modelService, modelEntity, allPksInOnly);
if (ServiceUtil.isSuccess(result)) {
result = invokeUpdate(dctx, parameters, modelService, modelEntity, allPksInOnly);
}
break;
default:
break;
}
GenericValue crudValue = (GenericValue) result.get("crudValue");
if (crudValue != null) {
result.remove("crudValue");
result.putAll(modelService.makeValid(crudValue, ModelService.OUT_PARAM));
}
} catch (GeneralException e) {
Debug.logError(e, "Error doing entity-auto operation for entity [" + modelEntity.getEntityName() + "] in service [" + modelService.name + "]: " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ServiceEntityAutoOperation", UtilMisc.toMap("entityName", modelEntity.getEntityName(), "serviceName", modelService.name, "errorString", e.toString()), locale));
}
result.put(ModelService.SUCCESS_MESSAGE, ServiceUtil.makeSuccessMessage(result, "", "", "", ""));
return result;
}
use of org.apache.ofbiz.service.ModelParam in project ofbiz-framework by apache.
the class EntityAutoEngine method invokeUpdate.
private static Map<String, Object> invokeUpdate(DispatchContext dctx, Map<String, Object> parameters, ModelService modelService, ModelEntity modelEntity, boolean allPksInOnly) throws GeneralException {
Locale locale = (Locale) parameters.get("locale");
Map<String, Object> localContext = new HashMap<>();
localContext.put("parameters", parameters);
Map<String, Object> result = ServiceUtil.returnSuccess();
// check to make sure that all primary key fields are defined as IN attributes
if (!allPksInOnly) {
throw new GenericServiceException("In Service [" + modelService.name + "] which uses the entity-auto engine with the update invoke option not all pk fields have the mode IN");
}
GenericValue lookedUpValue = PrimaryKeyFinder.runFind(modelEntity, parameters, dctx.getDelegator(), false, true, null, null);
if (lookedUpValue == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ServiceValueNotFound", locale));
}
// localContext.put("lookedUpValue", lookedUpValue);
// populate the oldStatusId out if there is a service parameter for it, and before we do the set non-pk fields
/*
<auto-attributes include="pk" mode="IN" optional="false"/>
<attribute name="oldStatusId" type="String" mode="OUT" optional="false"/>
*
<field-to-result field-name="lookedUpValue.statusId" result-name="oldStatusId"/>
*/
ModelParam statusIdParam = modelService.getParam("statusId");
ModelField statusIdField = modelEntity.getField("statusId");
ModelParam oldStatusIdParam = modelService.getParam("oldStatusId");
if (statusIdParam != null && statusIdParam.isIn() && oldStatusIdParam != null && oldStatusIdParam.isOut() && statusIdField != null) {
result.put("oldStatusId", lookedUpValue.get("statusId"));
}
// do the StatusValidChange check
/*
<if-compare-field field="lookedUpValue.statusId" operator="not-equals" to-field="parameters.statusId">
<!-- if the record exists there should be a statusId, but just in case make it so it won't blow up -->
<if-not-empty field="lookedUpValue.statusId">
<!-- if statusId change is not in the StatusValidChange list, complain... -->
<entity-one entity-name="StatusValidChange" value-name="statusValidChange" auto-field-map="false">
<field-map field-name="statusId" env-name="lookedUpValue.statusId"/>
<field-map field-name="statusIdTo" env-name="parameters.statusId"/>
</entity-one>
<if-empty field="statusValidChange">
<!-- no valid change record found? return an error... -->
<add-error><fail-property resource="CommonUiLabels" property="CommonErrorNoStatusValidChange"/></add-error>
<check-errors/>
</if-empty>
</if-not-empty>
</if-compare-field>
*/
String parameterStatusId = (String) parameters.get("statusId");
if (statusIdParam != null && statusIdParam.isIn() && UtilValidate.isNotEmpty(parameterStatusId) && statusIdField != null) {
String lookedUpStatusId = (String) lookedUpValue.get("statusId");
if (UtilValidate.isNotEmpty(lookedUpStatusId) && !parameterStatusId.equals(lookedUpStatusId)) {
// there was an old status, and in this call we are trying to change it, so do the StatusValidChange check
GenericValue statusValidChange = dctx.getDelegator().findOne("StatusValidChange", true, "statusId", lookedUpStatusId, "statusIdTo", parameterStatusId);
if (statusValidChange == null) {
// uh-oh, no valid change...
return ServiceUtil.returnError(UtilProperties.getMessage("CommonUiLabels", "CommonErrorNoStatusValidChange", localContext, locale));
}
}
}
// NOTE: nothing here to maintain the status history, that should be done with a custom service called by SECA rule
lookedUpValue.setNonPKFields(parameters, true);
if (modelEntity.getField("lastModifiedDate") != null || modelEntity.getField("changedDate") != null) {
if (modelEntity.getField("lastModifiedDate") != null) {
lookedUpValue.set("lastModifiedDate", UtilDateTime.nowTimestamp());
} else {
lookedUpValue.set("changedDate", UtilDateTime.nowTimestamp());
}
if (modelEntity.getField("lastModifiedByUserLogin") != null || modelEntity.getField("changedByUserLogin") != null) {
GenericValue userLogin = (GenericValue) parameters.get("userLogin");
if (userLogin != null) {
if (modelEntity.getField("lastModifiedByUserLogin") != null) {
lookedUpValue.set("lastModifiedByUserLogin", userLogin.get("userLoginId"));
} else {
lookedUpValue.set("changedByUserLogin", userLogin.get("userLoginId"));
}
}
}
}
if (modelEntity.getField("changeByUserLoginId") != null) {
if (modelEntity.getEntityName().endsWith("Status")) {
// Oh update on EntityStatus concept detected ... not possible, return invalid request
throw new GenericServiceException("You call a updating operation on entity that track the activity, sorry I can't do that, please amazing developer check your service definition ;)");
}
GenericValue userLogin = (GenericValue) parameters.get("userLogin");
if (userLogin != null) {
lookedUpValue.set("changeByUserLoginId", userLogin.get("userLoginId"));
} else {
throw new GenericServiceException("You call a updating operation on entity that track the activity, sorry I can't do that, please amazing developer check your service definition ;)");
}
}
lookedUpValue.store();
result.put("crudValue", lookedUpValue);
result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage("ServiceUiLabels", "EntityUpdatedSuccessfully", UtilMisc.toMap("entityName", modelEntity.getEntityName()), locale));
return result;
}
use of org.apache.ofbiz.service.ModelParam in project ofbiz-framework by apache.
the class ServiceArtifactInfo method createEoModelMap.
public Map<String, Object> createEoModelMap(Set<ServiceArtifactInfo> callingServiceSet, Set<ServiceArtifactInfo> calledServiceSet, Set<ServiceEcaArtifactInfo> callingServiceEcaSet, Set<ServiceEcaArtifactInfo> calledServiceEcaSet, boolean useMoreDetailedNames) {
if (callingServiceSet == null)
callingServiceSet = new HashSet<ServiceArtifactInfo>();
if (calledServiceSet == null)
calledServiceSet = new HashSet<ServiceArtifactInfo>();
if (callingServiceEcaSet == null)
callingServiceEcaSet = new HashSet<ServiceEcaArtifactInfo>();
if (calledServiceEcaSet == null)
calledServiceEcaSet = new HashSet<ServiceEcaArtifactInfo>();
Map<String, Object> topLevelMap = new HashMap<String, Object>();
topLevelMap.put("name", this.getDisplayPrefixedName());
topLevelMap.put("className", "EOGenericRecord");
// for classProperties add attribute names AND relationship names to get a nice, complete chart
List<String> classPropertiesList = new LinkedList<String>();
topLevelMap.put("classProperties", classPropertiesList);
for (ModelParam param : this.modelService.getModelParamList()) {
// skip the internal parameters, very redundant in the diagrams
if (param.internal)
continue;
if (useMoreDetailedNames) {
classPropertiesList.add(param.getShortDisplayDescription());
} else {
classPropertiesList.add(param.name);
}
}
for (ServiceArtifactInfo sai : callingServiceSet) {
classPropertiesList.add(sai.getDisplayPrefixedName());
}
for (ServiceArtifactInfo sai : calledServiceSet) {
classPropertiesList.add(sai.getDisplayPrefixedName());
}
for (ServiceEcaArtifactInfo seai : callingServiceEcaSet) {
classPropertiesList.add(seai.getDisplayPrefixedName());
}
for (ServiceEcaArtifactInfo seai : calledServiceEcaSet) {
classPropertiesList.add(seai.getDisplayPrefixedName());
}
// attributes
List<Map<String, Object>> attributesList = new LinkedList<Map<String, Object>>();
topLevelMap.put("attributes", attributesList);
for (ModelParam param : this.modelService.getModelParamList()) {
Map<String, Object> attributeMap = new HashMap<String, Object>();
attributesList.add(attributeMap);
if (useMoreDetailedNames) {
attributeMap.put("name", param.getShortDisplayDescription());
} else {
attributeMap.put("name", param.name);
}
attributeMap.put("valueClassName", param.type);
attributeMap.put("externalType", param.type);
}
// relationships
List<Map<String, Object>> relationshipsMapList = new LinkedList<Map<String, Object>>();
for (ServiceArtifactInfo sai : callingServiceSet) {
Map<String, Object> relationshipMap = new HashMap<String, Object>();
relationshipsMapList.add(relationshipMap);
relationshipMap.put("name", sai.getDisplayPrefixedName());
relationshipMap.put("destination", sai.getDisplayPrefixedName());
relationshipMap.put("isToMany", "N");
relationshipMap.put("isMandatory", "Y");
// not sure if we can use these, or need them, for this type of diagram
// relationshipMap.put("joinSemantic", "EOInnerJoin");
// relationshipMap.put("joins", joinsMapList);
// joinsMap.put("sourceAttribute", keyMap.getFieldName());
// joinsMap.put("destinationAttribute", keyMap.getRelFieldName());
}
for (ServiceArtifactInfo sai : calledServiceSet) {
Map<String, Object> relationshipMap = new HashMap<String, Object>();
relationshipsMapList.add(relationshipMap);
relationshipMap.put("name", sai.getDisplayPrefixedName());
relationshipMap.put("destination", sai.getDisplayPrefixedName());
relationshipMap.put("isToMany", "Y");
relationshipMap.put("isMandatory", "Y");
// not sure if we can use these, or need them, for this type of diagram
// relationshipMap.put("joinSemantic", "EOInnerJoin");
// relationshipMap.put("joins", joinsMapList);
// joinsMap.put("sourceAttribute", keyMap.getFieldName());
// joinsMap.put("destinationAttribute", keyMap.getRelFieldName());
}
for (ServiceEcaArtifactInfo seai : callingServiceEcaSet) {
Map<String, Object> relationshipMap = new HashMap<String, Object>();
relationshipsMapList.add(relationshipMap);
relationshipMap.put("name", seai.getDisplayPrefixedName());
relationshipMap.put("destination", seai.getDisplayPrefixedName());
relationshipMap.put("isToMany", "N");
relationshipMap.put("isMandatory", "Y");
}
for (ServiceEcaArtifactInfo seai : calledServiceEcaSet) {
Map<String, Object> relationshipMap = new HashMap<String, Object>();
relationshipsMapList.add(relationshipMap);
relationshipMap.put("name", seai.getDisplayPrefixedName());
relationshipMap.put("destination", seai.getDisplayPrefixedName());
relationshipMap.put("isToMany", "Y");
relationshipMap.put("isMandatory", "Y");
}
if (relationshipsMapList.size() > 0) {
topLevelMap.put("relationships", relationshipsMapList);
}
return topLevelMap;
}
use of org.apache.ofbiz.service.ModelParam in project ofbiz-framework by apache.
the class XMLRPCClientEngine method serviceInvoker.
/*
* Invoke the remote XMLRPC SERVICE : This engine convert all value in IN mode to one struct.
*/
private Map<String, Object> serviceInvoker(ModelService modelService, Map<String, Object> context) throws GenericServiceException {
if (modelService.location == null || modelService.invoke == null) {
throw new GenericServiceException("Cannot locate service to invoke");
}
XmlRpcClientConfigImpl config = null;
XmlRpcClient client = null;
String serviceName = modelService.invoke;
String engine = modelService.engineName;
String url = null;
String login = null;
String password = null;
String keyStoreComponent = null;
String keyStoreName = null;
String keyAlias = null;
try {
url = ServiceConfigUtil.getEngineParameter(engine, "url");
if (Start.getInstance().getConfig().portOffset != 0) {
String s = url.substring(url.lastIndexOf(":") + 1);
Integer rpcPort = Integer.valueOf(s.substring(0, s.indexOf("/")));
Integer port = rpcPort + Start.getInstance().getConfig().portOffset;
url = url.replace(rpcPort.toString(), port.toString());
}
login = ServiceConfigUtil.getEngineParameter(engine, "login");
password = ServiceConfigUtil.getEngineParameter(engine, "password");
keyStoreComponent = ServiceConfigUtil.getEngineParameter(engine, "keyStoreComponent");
keyStoreName = ServiceConfigUtil.getEngineParameter(engine, "keyStoreName");
keyAlias = ServiceConfigUtil.getEngineParameter(engine, "keyAlias");
config = new XmlRpcClientConfigImpl();
config.setBasicUserName(login);
config.setBasicPassword(password);
config.setServerURL(new URL(url));
} catch (MalformedURLException | GenericConfigException e) {
throw new GenericServiceException("Cannot invoke service : engine parameters are not correct");
}
if (UtilValidate.isNotEmpty(keyStoreComponent) && UtilValidate.isNotEmpty(keyStoreName) && UtilValidate.isNotEmpty(keyAlias)) {
client = new XmlRpcClient(config, keyStoreComponent, keyStoreName, keyAlias);
} else {
client = new XmlRpcClient(config);
}
List<ModelParam> inModelParamList = modelService.getInModelParamList();
if (Debug.verboseOn()) {
Debug.logVerbose("[XMLRPCClientEngine.invoke] : Parameter length - " + inModelParamList.size(), module);
for (ModelParam p : inModelParamList) {
Debug.logVerbose("[XMLRPCClientEngine.invoke} : Parameter: " + p.name + " (" + p.mode + ")", module);
}
}
Map<String, Object> result = null;
Map<String, Object> params = new HashMap<>();
for (ModelParam modelParam : modelService.getModelParamList()) {
// don't include OUT parameters in this list, only IN and INOUT
if (ModelService.OUT_PARAM.equals(modelParam.mode) || modelParam.internal) {
continue;
}
Object paramValue = context.get(modelParam.name);
if (paramValue != null) {
params.put(modelParam.name, paramValue);
}
}
List<Map<String, Object>> listParams = UtilMisc.toList(params);
try {
result = UtilGenerics.cast(client.execute(serviceName, listParams.toArray()));
} catch (XmlRpcException e) {
result = ServiceUtil.returnError(e.getLocalizedMessage());
}
return result;
}
Aggregations