use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.
the class PdfSurveyServices method setAcroFieldsFromSurveyResponse.
/**
*/
public static Map<String, Object> setAcroFieldsFromSurveyResponse(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
Map<String, Object> results = ServiceUtil.returnSuccess();
Map<String, Object> acroFieldMap = new HashMap<>();
String surveyResponseId = (String) context.get("surveyResponseId");
String acroFormContentId = null;
try {
String surveyId = null;
if (UtilValidate.isNotEmpty(surveyResponseId)) {
GenericValue surveyResponse = EntityQuery.use(delegator).from("SurveyResponse").where("surveyResponseId", surveyResponseId).queryOne();
if (surveyResponse != null) {
surveyId = surveyResponse.getString("surveyId");
}
}
if (UtilValidate.isNotEmpty(surveyId)) {
GenericValue survey = EntityQuery.use(delegator).from("Survey").where("surveyId", surveyId).queryOne();
if (survey != null) {
acroFormContentId = survey.getString("acroFormContentId");
}
}
List<GenericValue> responses = EntityQuery.use(delegator).from("SurveyResponseAnswer").where("surveyResponseId", surveyResponseId).queryList();
for (GenericValue surveyResponseAnswer : responses) {
String value = null;
String surveyQuestionId = (String) surveyResponseAnswer.get("surveyQuestionId");
GenericValue surveyQuestion = EntityQuery.use(delegator).from("SurveyQuestion").where("surveyQuestionId", surveyQuestionId).cache().queryOne();
GenericValue surveyQuestionAppl = EntityQuery.use(delegator).from("SurveyQuestionAppl").where("surveyId", surveyId, "surveyQuestionId", surveyQuestionId).orderBy("-fromDate").filterByDate().cache().queryFirst();
String questionType = surveyQuestion.getString("surveyQuestionTypeId");
String fieldName = surveyQuestionAppl.getString("externalFieldRef");
if ("OPTION".equals(questionType)) {
value = surveyResponseAnswer.getString("surveyOptionSeqId");
} else if ("BOOLEAN".equals(questionType)) {
value = surveyResponseAnswer.getString("booleanResponse");
} else if ("NUMBER_LONG".equals(questionType) || "NUMBER_CURRENCY".equals(questionType) || "NUMBER_FLOAT".equals(questionType)) {
Double num = surveyResponseAnswer.getDouble("numericResponse");
if (num != null) {
value = num.toString();
}
} else if ("SEPERATOR_LINE".equals(questionType) || "SEPERATOR_TEXT".equals(questionType)) {
// not really a question; ignore completely
} else {
value = surveyResponseAnswer.getString("textResponse");
}
acroFieldMap.put(fieldName, value);
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
try {
ModelService modelService = dispatcher.getDispatchContext().getModelService("setAcroFields");
Map<String, Object> ctx = modelService.makeValid(context, ModelService.IN_PARAM);
ctx.put("acroFieldMap", acroFieldMap);
ctx.put("contentId", acroFormContentId);
Map<String, Object> map = dispatcher.runSync("setAcroFields", ctx);
if (ServiceUtil.isError(map)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(map));
}
String pdfFileNameOut = (String) context.get("pdfFileNameOut");
ByteBuffer outByteBuffer = (ByteBuffer) map.get("outByteBuffer");
results.put("outByteBuffer", outByteBuffer);
if (UtilValidate.isNotEmpty(pdfFileNameOut)) {
FileOutputStream fos = new FileOutputStream(pdfFileNameOut);
fos.write(outByteBuffer.array());
fos.close();
}
} catch (IOException | GenericServiceException e) {
Debug.logError(e, "Error generating PDF: " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPDFGeneratingError", UtilMisc.toMap("errorString", e.toString()), locale));
}
return results;
}
use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.
the class ContentManagementServices method persistContentWithRevision.
public static Map<String, Object> persistContentWithRevision(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> result = null;
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue dataResource = null;
String masterRevisionContentId = (String) context.get("masterRevisionContentId");
String oldDataResourceId = (String) context.get("drDataResourceId");
if (UtilValidate.isEmpty(oldDataResourceId)) {
oldDataResourceId = (String) context.get("dataResourceId");
}
if (UtilValidate.isNotEmpty(oldDataResourceId)) {
try {
dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", oldDataResourceId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e.toString(), module);
return ServiceUtil.returnError(e.toString());
}
}
try {
ModelService persistContentAndAssocModel = dispatcher.getDispatchContext().getModelService("persistContentAndAssoc");
Map<String, Object> ctx = persistContentAndAssocModel.makeValid(context, ModelService.IN_PARAM);
if (dataResource != null) {
ctx.remove("dataResourceId");
ctx.remove("drDataResourceId");
}
result = dispatcher.runSync("persistContentAndAssoc", ctx);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
String contentId = (String) result.get("contentId");
List<String> parentList = new LinkedList<String>();
if (UtilValidate.isEmpty(masterRevisionContentId)) {
Map<String, Object> traversMap = new HashMap<String, Object>();
traversMap.put("contentId", contentId);
traversMap.put("direction", "To");
traversMap.put("contentAssocTypeId", "COMPDOC_PART");
Map<String, Object> traversResult = dispatcher.runSync("traverseContent", traversMap);
if (ServiceUtil.isError(traversResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(traversResult));
}
parentList = UtilGenerics.checkList(traversResult.get("parentList"));
} else {
parentList.add(masterRevisionContentId);
}
// Update ContentRevision and ContentRevisonItem
Map<String, Object> contentRevisionMap = new HashMap<String, Object>();
contentRevisionMap.put("itemContentId", contentId);
contentRevisionMap.put("newDataResourceId", result.get("dataResourceId"));
contentRevisionMap.put("oldDataResourceId", oldDataResourceId);
// need committedByPartyId
for (int i = 0; i < parentList.size(); i++) {
String thisContentId = parentList.get(i);
contentRevisionMap.put("contentId", thisContentId);
result = dispatcher.runSync("persistContentRevisionAndItem", contentRevisionMap);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
} catch (GenericServiceException e) {
Debug.logError(e.toString(), module);
return ServiceUtil.returnError(e.toString());
}
return result;
}
use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.
the class ContentManagementServices method updateContentSubscriptionByOrder.
public static Map<String, Object> updateContentSubscriptionByOrder(DispatchContext dctx, Map<String, ? extends Object> rcontext) throws GenericServiceException {
Map<String, Object> context = UtilMisc.makeMapWritable(rcontext);
Map<String, Object> result = new HashMap<String, Object>();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
LocalDispatcher dispatcher = dctx.getDispatcher();
String orderId = (String) context.get("orderId");
Debug.logInfo("In updateContentSubscriptionByOrder service with orderId: " + orderId, module);
GenericValue orderHeader = null;
try {
GenericValue orderRole = EntityQuery.use(delegator).from("OrderRole").where("orderId", orderId, "roleTypeId", "END_USER_CUSTOMER").queryFirst();
if (orderRole != null) {
String partyId = (String) orderRole.get("partyId");
context.put("partyId", partyId);
} else {
String msg = "No OrderRole found for orderId:" + orderId;
return ServiceUtil.returnFailure(msg);
}
orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
if (orderHeader == null) {
String msg = UtilProperties.getMessage(resource, "ContentNoOrderHeaderFound", UtilMisc.toMap("orderId", orderId), locale);
return ServiceUtil.returnError(msg);
}
Timestamp orderCreatedDate = (Timestamp) orderHeader.get("orderDate");
context.put("orderCreatedDate", orderCreatedDate);
List<GenericValue> orderItemList = orderHeader.getRelated("OrderItem", null, null, false);
ModelService subscriptionModel = dispatcher.getDispatchContext().getModelService("updateContentSubscriptionByProduct");
for (GenericValue orderItem : orderItemList) {
BigDecimal qty = orderItem.getBigDecimal("quantity");
String productId = (String) orderItem.get("productId");
long productContentCount = EntityQuery.use(delegator).from("ProductContent").where("productId", productId, "productContentTypeId", "ONLINE_ACCESS").filterByDate().queryCount();
if (productContentCount > 0) {
context.put("productId", productId);
context.put("quantity", Integer.valueOf(qty.intValue()));
Map<String, Object> ctx = subscriptionModel.makeValid(context, ModelService.IN_PARAM);
result = dispatcher.runSync("updateContentSubscriptionByProduct", ctx);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
}
} catch (GenericEntityException e) {
Debug.logError(e.toString(), module);
return ServiceUtil.returnError(e.toString());
}
return result;
}
use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.
the class ContentManagementServices method updateContentSubscriptionByProduct.
public static Map<String, Object> updateContentSubscriptionByProduct(DispatchContext dctx, Map<String, ? extends Object> rcontext) throws GenericServiceException {
Map<String, Object> context = UtilMisc.makeMapWritable(rcontext);
Map<String, Object> result;
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
LocalDispatcher dispatcher = dctx.getDispatcher();
String productId = (String) context.get("productId");
Integer qty = (Integer) context.get("quantity");
if (qty == null) {
qty = Integer.valueOf(1);
}
GenericValue productContent = null;
try {
List<GenericValue> lst = EntityQuery.use(delegator).from("ProductContent").where("productId", productId, "productContentTypeId", "ONLINE_ACCESS").orderBy("purchaseFromDate", "purchaseThruDate").filterByDate("purchaseFromDate", "purchaseThruDate").cache().queryList();
List<GenericValue> listThrusOnly = EntityUtil.filterOutByCondition(lst, EntityCondition.makeCondition("purchaseThruDate", EntityOperator.EQUALS, null));
if (listThrusOnly.size() > 0) {
productContent = listThrusOnly.get(0);
} else if (lst.size() > 0) {
productContent = lst.get(0);
}
} catch (GenericEntityException e) {
Debug.logError(e.toString(), module);
return ServiceUtil.returnError(e.toString());
}
if (productContent == null) {
String msg = UtilProperties.getMessage(resource, "ContentNoProductContentFound", UtilMisc.toMap("productId", productId), locale);
Debug.logError(msg, module);
return ServiceUtil.returnError(msg);
}
Long useTime = (Long) productContent.get("useTime");
Integer newUseTime = null;
if (UtilValidate.isNotEmpty(useTime)) {
newUseTime = Integer.valueOf(useTime.intValue() * qty.intValue());
}
context.put("useTime", newUseTime);
context.put("useTimeUomId", productContent.get("useTimeUomId"));
context.put("useRoleTypeId", productContent.get("useRoleTypeId"));
context.put("contentId", productContent.get("contentId"));
ModelService subscriptionModel = dispatcher.getDispatchContext().getModelService("updateContentSubscription");
Map<String, Object> ctx = subscriptionModel.makeValid(context, ModelService.IN_PARAM);
result = dispatcher.runSync("updateContentSubscription", ctx);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
return result;
}
use of org.apache.ofbiz.service.ModelService in project ofbiz-framework by apache.
the class ContentManagementServices method persistDataResourceAndData.
public static Map<String, Object> persistDataResourceAndData(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
Map<String, Object> result;
try {
ModelService checkPermModel = dispatcher.getDispatchContext().getModelService("checkContentPermission");
Map<String, Object> ctx = checkPermModel.makeValid(context, ModelService.IN_PARAM);
Map<String, Object> thisResult = dispatcher.runSync("checkContentPermission", ctx);
if (ServiceUtil.isError(thisResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(thisResult));
}
String permissionStatus = (String) thisResult.get("permissionStatus");
if (UtilValidate.isNotEmpty(permissionStatus) && "granted".equalsIgnoreCase(permissionStatus)) {
result = persistDataResourceAndDataMethod(dctx, context);
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentContentNoAccessToUploadImage", locale));
}
} catch (GenericServiceException e) {
Debug.logError(e, e.toString(), module);
return ServiceUtil.returnError(e.toString());
} catch (GenericEntityException e) {
Debug.logError(e, e.toString(), module);
return ServiceUtil.returnError(e.toString());
} catch (Exception e) {
Debug.logError(e, e.toString(), module);
return ServiceUtil.returnError(e.toString());
}
String errorMsg = ServiceUtil.getErrorMessage(result);
if (UtilValidate.isNotEmpty(errorMsg)) {
return ServiceUtil.returnError(errorMsg);
}
return result;
}
Aggregations