use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ImageManagementServices method createNewImageThumbnail.
public static Map<String, Object> createNewImageThumbnail(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dispatcher.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", delegator), context);
String productId = (String) context.get("productId");
String contentId = (String) context.get("contentId");
String dataResourceName = (String) context.get("dataResourceName");
String width = (String) context.get("sizeWidth");
String imageType = ".jpg";
int resizeWidth = Integer.parseInt(width);
int resizeHeight = resizeWidth;
try {
BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceName));
double imgHeight = bufImg.getHeight();
double imgWidth = bufImg.getWidth();
if (dataResourceName.lastIndexOf('.') > 0 && dataResourceName.lastIndexOf('.') < dataResourceName.length()) {
imageType = dataResourceName.substring(dataResourceName.lastIndexOf('.'));
}
String filenameToUse = dataResourceName.substring(0, dataResourceName.length() - 4) + "-" + resizeWidth + imageType;
if (dataResourceName.length() > 3) {
String mimeType = dataResourceName.substring(dataResourceName.length() - 3, dataResourceName.length());
Map<String, Object> resultResize = resizeImage(bufImg, imgHeight, imgWidth, resizeHeight, resizeWidth);
ImageIO.write((RenderedImage) resultResize.get("bufferedImage"), mimeType, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
Map<String, Object> contentThumb = new HashMap<>();
contentThumb.put("contentTypeId", "DOCUMENT");
contentThumb.put("userLogin", userLogin);
Map<String, Object> contentThumbResult;
try {
contentThumbResult = dispatcher.runSync("createContent", contentThumb);
if (ServiceUtil.isError(contentThumbResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(contentThumbResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String contentIdThumb = (String) contentThumbResult.get("contentId");
String imageUrlThumb = imageServerUrl + "/" + productId + "/" + filenameToUse;
createContentAndDataResource(dctx, userLogin, filenameToUse, imageUrlThumb, contentIdThumb, "image/jpeg");
Map<String, Object> createContentAssocMap = new HashMap<>();
createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL");
createContentAssocMap.put("contentId", contentId);
createContentAssocMap.put("contentIdTo", contentIdThumb);
createContentAssocMap.put("userLogin", userLogin);
createContentAssocMap.put("mapKey", width);
try {
Map<String, Object> serviceResult = dispatcher.runSync("createContentAssoc", createContentAssocMap);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
} catch (IOException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String successMsg = UtilProperties.getMessage(resource, "ProductCreateNewThumbnailSizeSuccessful", locale);
return ServiceUtil.returnSuccess(successMsg);
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ImageManagementServices method renameImage.
public static Map<String, Object> renameImage(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", delegator), context);
String productId = (String) context.get("productId");
String contentId = (String) context.get("contentId");
String filenameToUse = (String) context.get("drDataResourceName");
String imageType = filenameToUse.substring(filenameToUse.lastIndexOf('.'));
String imgExtension = filenameToUse.substring(filenameToUse.length() - 3, filenameToUse.length());
String imageUrl = imageServerUrl + "/" + productId + "/" + filenameToUse;
try {
GenericValue productContent = EntityQuery.use(delegator).from("ProductContentAndInfo").where("productId", productId, "contentId", contentId, "productContentTypeId", "IMAGE").queryFirst();
String dataResourceName = (String) productContent.get("drDataResourceName");
String mimeType = filenameToUse.substring(filenameToUse.lastIndexOf('.'));
if (imageType.equals(mimeType)) {
BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceName));
ImageIO.write(bufImg, imgExtension, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
File file = new File(imageServerPath + "/" + productId + "/" + dataResourceName);
if (!file.delete()) {
Debug.logError("File :" + file.getName() + ", couldn't be deleted", module);
}
Map<String, Object> contentUp = new HashMap<>();
contentUp.put("contentId", contentId);
contentUp.put("contentName", filenameToUse);
contentUp.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateContent", contentUp);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
GenericValue content = null;
try {
content = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (content != null) {
GenericValue dataResource = null;
try {
dataResource = content.getRelatedOne("DataResource", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (dataResource != null) {
Map<String, Object> dataResourceCtx = new HashMap<>();
dataResourceCtx.put("dataResourceId", dataResource.getString("dataResourceId"));
dataResourceCtx.put("objectInfo", imageUrl);
dataResourceCtx.put("dataResourceName", filenameToUse);
dataResourceCtx.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateDataResource", dataResourceCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
}
List<GenericValue> contentAssocList = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentId, "contentAssocTypeId", "IMAGE_THUMBNAIL").queryList();
if (contentAssocList.size() > 0) {
for (int i = 0; i < contentAssocList.size(); i++) {
GenericValue contentAssoc = contentAssocList.get(i);
List<GenericValue> dataResourceAssocList = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", contentAssoc.get("contentIdTo")).queryList();
GenericValue dataResourceAssoc = EntityUtil.getFirst(dataResourceAssocList);
String drDataResourceNameAssoc = (String) dataResourceAssoc.get("drDataResourceName");
String filenameToUseAssoc = filenameToUse.substring(0, filenameToUse.length() - 4) + "-" + contentAssoc.get("mapKey") + imageType;
String imageUrlAssoc = imageServerUrl + "/" + productId + "/" + filenameToUseAssoc;
BufferedImage bufImgAssoc = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + drDataResourceNameAssoc));
ImageIO.write(bufImgAssoc, imgExtension, new File(imageServerPath + "/" + productId + "/" + filenameToUseAssoc));
File fileAssoc = new File(imageServerPath + "/" + productId + "/" + drDataResourceNameAssoc);
if (!fileAssoc.delete()) {
Debug.logError("File :" + fileAssoc.getName() + ", couldn't be deleted", module);
}
Map<String, Object> contentAssocMap = new HashMap<>();
contentAssocMap.put("contentId", contentAssoc.get("contentIdTo"));
contentAssocMap.put("contentName", filenameToUseAssoc);
contentAssocMap.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateContent", contentAssocMap);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
GenericValue contentAssocUp = null;
try {
contentAssocUp = EntityQuery.use(delegator).from("Content").where("contentId", contentAssoc.get("contentIdTo")).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (contentAssocUp != null) {
GenericValue dataResourceAssocUp = null;
try {
dataResourceAssocUp = contentAssocUp.getRelatedOne("DataResource", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (dataResourceAssocUp != null) {
Map<String, Object> dataResourceAssocMap = new HashMap<>();
dataResourceAssocMap.put("dataResourceId", dataResourceAssocUp.getString("dataResourceId"));
dataResourceAssocMap.put("objectInfo", imageUrlAssoc);
dataResourceAssocMap.put("dataResourceName", filenameToUseAssoc);
dataResourceAssocMap.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateDataResource", dataResourceAssocMap);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
}
}
}
}
} catch (IOException | IllegalArgumentException | GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String successMsg = UtilProperties.getMessage(resource, "ProductRenameImageSuccessfully.", locale);
return ServiceUtil.returnSuccess(successMsg);
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ReplaceImage method replaceImageToExistImage.
public static Map<String, Object> replaceImageToExistImage(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String productId = (String) context.get("productId");
String contentIdExist = (String) context.get("contentIdExist");
String contentIdReplace = (String) context.get("contentIdReplace");
String dataResourceNameExist = (String) context.get("dataResourceNameExist");
String dataResourceNameReplace = (String) context.get("dataResourceNameReplace");
if (UtilValidate.isNotEmpty(dataResourceNameExist)) {
if (UtilValidate.isNotEmpty(contentIdReplace)) {
if (contentIdExist.equals(contentIdReplace)) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductCannotReplaceBecauseBothImagesAreTheSameImage", locale);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} else {
String errMsg = UtilProperties.getMessage(resourceError, "ProductPleaseChooseImageToReplace", locale);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} else {
String errMsg = UtilProperties.getMessage(resourceError, "ProductPleaseChooseReplacementImage", locale);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
try {
BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceNameReplace));
ImageIO.write(bufImg, "jpg", new File(imageServerPath + "/" + productId + "/" + dataResourceNameExist));
List<GenericValue> contentAssocReplaceList = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentIdReplace, "contentAssocTypeId", "IMAGE_THUMBNAIL").queryList();
if (contentAssocReplaceList.size() > 0) {
for (int i = 0; i < contentAssocReplaceList.size(); i++) {
GenericValue contentAssocReplace = contentAssocReplaceList.get(i);
GenericValue dataResourceAssocReplace = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", contentAssocReplace.get("contentIdTo")).queryFirst();
GenericValue contentAssocExist = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentIdExist, "contentAssocTypeId", "IMAGE_THUMBNAIL", "mapKey", contentAssocReplace.get("mapKey")).queryFirst();
GenericValue dataResourceAssocExist = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", contentAssocExist.get("contentIdTo")).queryFirst();
if (UtilValidate.isNotEmpty(dataResourceAssocExist)) {
BufferedImage bufImgAssocReplace = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceAssocReplace.get("drDataResourceName")));
ImageIO.write(bufImgAssocReplace, "jpg", new File(imageServerPath + "/" + productId + "/" + dataResourceAssocExist.get("drDataResourceName")));
} else {
BufferedImage bufImgAssocReplace = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceAssocReplace.get("drDataResourceName")));
ImageIO.write(bufImgAssocReplace, "jpg", new File(imageServerPath + "/" + productId + "/" + dataResourceNameExist.substring(0, dataResourceNameExist.length() - 4) + "-" + contentAssocReplace.get("mapKey") + ".jpg"));
}
}
}
GenericValue productContent = EntityQuery.use(delegator).from("ProductContent").where("productId", productId, "contentId", contentIdReplace, "productContentTypeId", "IMAGE").queryFirst();
if (productContent != null) {
Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("contentId", contentIdReplace);
productContentCtx.put("productContentTypeId", "IMAGE");
productContentCtx.put("fromDate", productContent.get("fromDate"));
productContentCtx.put("userLogin", userLogin);
Map<String, Object> serviceResult = dispatcher.runSync("removeProductContentAndImageFile", productContentCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
}
} catch (IOException | IllegalArgumentException | GenericEntityException | GenericServiceException e) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductCannotReplaceImage", locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
String successMsg = UtilProperties.getMessage(resource, "ProductReplaceImageSuccessfully", locale);
return ServiceUtil.returnSuccess(successMsg);
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ICalWorker method logInUser.
private static void logInUser(HttpServletRequest request, HttpServletResponse response) throws GenericServiceException, GenericEntityException {
Map<String, Object> serviceMap = WebDavUtil.getCredentialsFromRequest(request);
if (serviceMap == null) {
return;
}
serviceMap.put("locale", UtilHttp.getLocale(request));
GenericValue userLogin = null;
HttpSession session = request.getSession();
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Map<String, Object> result = dispatcher.runSync("userLogin", serviceMap);
if (ServiceUtil.isError(result) || ServiceUtil.isFailure(result)) {
String errorMessage = ServiceUtil.getErrorMessage(result);
request.setAttribute("_ERROR_MESSAGE_", errorMessage);
Debug.logError(errorMessage, module);
throw new GenericServiceException(errorMessage);
}
userLogin = (GenericValue) result.get("userLogin");
request.setAttribute("userLogin", userLogin);
session.setAttribute("userLogin", userLogin);
VisitHandler.getVisitor(request, response);
GenericValue person = userLogin.getRelatedOne("Person", false);
if (person != null) {
request.setAttribute("person", person);
} else {
GenericValue partyGroup = userLogin.getRelatedOne("PartyGroup", false);
if (partyGroup != null) {
request.setAttribute("partyGroup", partyGroup);
}
}
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class WorkEffortServices method processWorkEffortEventReminders.
/**
* Process work effort event reminders. This service is used by the job scheduler.
* @param ctx the dispatch context
* @param context the context
* @return returns the result of the service execution
*/
public static Map<String, Object> processWorkEffortEventReminders(DispatchContext ctx, Map<String, ? extends Object> context) {
Delegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
Locale localePar = (Locale) context.get("locale");
Timestamp now = new Timestamp(System.currentTimeMillis());
List<GenericValue> eventReminders = null;
try {
eventReminders = EntityQuery.use(delegator).from("WorkEffortEventReminder").where(EntityCondition.makeCondition(UtilMisc.<EntityCondition>toList(EntityCondition.makeCondition("reminderDateTime", EntityOperator.EQUALS, null), EntityCondition.makeCondition("reminderDateTime", EntityOperator.LESS_THAN_EQUAL_TO, now)), EntityOperator.OR)).queryList();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "WorkEffortEventRemindersRetrivingError", UtilMisc.toMap("errorString", e), localePar));
}
for (GenericValue reminder : eventReminders) {
if (UtilValidate.isEmpty(reminder.get("contactMechId"))) {
continue;
}
int repeatCount = reminder.get("repeatCount") == null ? 0 : reminder.getLong("repeatCount").intValue();
int currentCount = reminder.get("currentCount") == null ? 0 : reminder.getLong("currentCount").intValue();
GenericValue workEffort = null;
try {
workEffort = reminder.getRelatedOne("WorkEffort", false);
} catch (GenericEntityException e) {
Debug.logWarning("Error while getting work effort: " + e, module);
}
if (workEffort == null) {
try {
reminder.remove();
} catch (GenericEntityException e) {
Debug.logWarning("Error while removing work effort event reminder: " + e, module);
}
continue;
}
Locale locale = reminder.getString("localeId") == null ? Locale.getDefault() : new Locale(reminder.getString("localeId"));
TimeZone timeZone = reminder.getString("timeZoneId") == null ? TimeZone.getDefault() : TimeZone.getTimeZone(reminder.getString("timeZoneId"));
Map<String, Object> parameters = UtilMisc.toMap("locale", locale, "timeZone", timeZone, "workEffortId", reminder.get("workEffortId"));
Map<String, Object> processCtx = UtilMisc.toMap("reminder", reminder, "bodyParameters", parameters, "userLogin", context.get("userLogin"));
Calendar cal = UtilDateTime.toCalendar(now, timeZone, locale);
Timestamp reminderStamp = reminder.getTimestamp("reminderDateTime");
Date eventDateTime = workEffort.getTimestamp("estimatedStartDate");
String tempExprId = workEffort.getString("tempExprId");
if (UtilValidate.isNotEmpty(tempExprId)) {
TemporalExpression temporalExpression = null;
try {
temporalExpression = TemporalExpressionWorker.getTemporalExpression(delegator, tempExprId);
} catch (GenericEntityException e) {
Debug.logWarning("Error while getting temporal expression, id = " + tempExprId + ": " + e, module);
}
if (temporalExpression != null) {
eventDateTime = temporalExpression.first(cal).getTime();
Date reminderDateTime = null;
long reminderOffset = reminder.get("reminderOffset") == null ? 0 : reminder.getLong("reminderOffset").longValue();
if (reminderStamp == null) {
if (reminderOffset != 0) {
cal.setTime(eventDateTime);
TimeDuration duration = TimeDuration.fromLong(reminderOffset);
duration.addToCalendar(cal);
reminderDateTime = cal.getTime();
} else {
reminderDateTime = eventDateTime;
}
} else {
reminderDateTime = new Date(reminderStamp.getTime());
}
if (reminderDateTime.before(now) && reminderStamp != null) {
try {
parameters.put("eventDateTime", new Timestamp(eventDateTime.getTime()));
Map<String, Object> result = dispatcher.runSync("processWorkEffortEventReminder", processCtx);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
if (repeatCount != 0 && currentCount + 1 >= repeatCount) {
reminder.remove();
} else {
cal.setTime(reminderDateTime);
Date newReminderDateTime = null;
if (reminderOffset != 0) {
TimeDuration duration = TimeDuration.fromLong(-reminderOffset);
duration.addToCalendar(cal);
cal.setTime(temporalExpression.next(cal).getTime());
duration = TimeDuration.fromLong(reminderOffset);
duration.addToCalendar(cal);
newReminderDateTime = cal.getTime();
} else {
newReminderDateTime = temporalExpression.next(cal).getTime();
}
reminder.set("currentCount", Long.valueOf(currentCount + 1));
reminder.set("reminderDateTime", new Timestamp(newReminderDateTime.getTime()));
reminder.store();
}
} catch (GenericEntityException e) {
Debug.logWarning("Error while processing temporal expression reminder, id = " + tempExprId + ": " + e, module);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
} else if (reminderStamp == null) {
try {
reminder.set("reminderDateTime", new Timestamp(reminderDateTime.getTime()));
reminder.store();
} catch (GenericEntityException e) {
Debug.logWarning("Error while processing temporal expression reminder, id = " + tempExprId + ": " + e, module);
}
}
}
continue;
}
if (reminderStamp != null) {
Date reminderDateTime = new Date(reminderStamp.getTime());
if (reminderDateTime.before(now)) {
try {
parameters.put("eventDateTime", eventDateTime);
Map<String, Object> result = dispatcher.runSync("processWorkEffortEventReminder", processCtx);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
TimeDuration duration = TimeDuration.fromNumber(reminder.getLong("repeatInterval"));
if ((repeatCount != 0 && currentCount + 1 >= repeatCount) || duration.isZero()) {
reminder.remove();
} else {
cal.setTime(now);
duration.addToCalendar(cal);
reminderDateTime = cal.getTime();
reminder.set("currentCount", Long.valueOf(currentCount + 1));
reminder.set("reminderDateTime", new Timestamp(reminderDateTime.getTime()));
reminder.store();
}
} catch (GenericEntityException e) {
Debug.logWarning("Error while processing event reminder: " + e, module);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
}
}
}
return ServiceUtil.returnSuccess();
}
Aggregations