use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class FrameImage method addImageFrame.
public static Map<String, Object> addImageFrame(DispatchContext dctx, Map<String, ? extends Object> context) throws IOException {
Map<String, Object> result;
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
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 nameOfThumb = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.nameofthumbnail", delegator), context);
GenericValue userLogin = (GenericValue) context.get("userLogin");
String productId = (String) context.get("productId");
String imageName = (String) context.get("imageName");
String imageWidth = (String) context.get("imageWidth");
String imageHeight = (String) context.get("imageHeight");
Locale locale = (Locale) context.get("locale");
if (UtilValidate.isEmpty(context.get("frameContentId")) || UtilValidate.isEmpty(context.get("frameDataResourceId"))) {
result = ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductImageFrameContentIdRequired", locale));
result.putAll(context);
}
if (UtilValidate.isEmpty(context.get("imageWidth")) || UtilValidate.isEmpty(context.get("imageHeight"))) {
result = ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductImageWidthAndHeightRequired", locale));
result.putAll(context);
}
String frameContentId = (String) context.get("frameContentId");
String frameDataResourceId = (String) context.get("frameDataResourceId");
String frameImageName = null;
try {
GenericValue contentDataResourceView = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", frameContentId, "drDataResourceId", frameDataResourceId).queryOne();
frameImageName = contentDataResourceView.getString("contentName");
} catch (GenericEntityException gee) {
Debug.logError(gee, module);
result = ServiceUtil.returnError(gee.getMessage());
result.putAll(context);
}
if (UtilValidate.isNotEmpty(imageName)) {
// Image Frame
BufferedImage bufImg1 = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + imageName));
BufferedImage bufImg2 = ImageIO.read(new File(imageServerPath + "/frame/" + frameImageName));
int bufImgType;
if (BufferedImage.TYPE_CUSTOM == bufImg1.getType()) {
bufImgType = BufferedImage.TYPE_INT_ARGB_PRE;
} else {
bufImgType = bufImg1.getType();
}
int width = Integer.parseInt(imageWidth);
int height = Integer.parseInt(imageHeight);
Map<String, Object> contentCtx = new HashMap<>();
contentCtx.put("contentTypeId", "DOCUMENT");
contentCtx.put("userLogin", userLogin);
Map<String, Object> contentResult = new HashMap<>();
try {
contentResult = dispatcher.runSync("createContent", contentCtx);
if (ServiceUtil.isError(contentResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(contentResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
Map<String, Object> contentThumb = new HashMap<>();
contentThumb.put("contentTypeId", "DOCUMENT");
contentThumb.put("userLogin", userLogin);
Map<String, Object> contentThumbResult = new HashMap<>();
try {
contentThumbResult = dispatcher.runSync("createContent", contentThumb);
if (ServiceUtil.isError(contentThumbResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(contentThumbResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
String contentIdThumb = (String) contentThumbResult.get("contentId");
String contentId = (String) contentResult.get("contentId");
String filenameToUse = (String) contentResult.get("contentId") + ".jpg";
String filenameTouseThumb = (String) contentResult.get("contentId") + nameOfThumb + ".jpg";
Image newImg1 = bufImg1.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Image newImg2 = bufImg2.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage bufNewImg = combineBufferedImage(newImg1, newImg2, bufImgType);
String mimeType = imageName.substring(imageName.lastIndexOf('.') + 1);
ImageIO.write(bufNewImg, mimeType, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
double imgHeight = bufNewImg.getHeight();
double imgWidth = bufNewImg.getWidth();
Map<String, Object> resultResize = ImageManagementServices.resizeImageThumbnail(bufNewImg, imgHeight, imgWidth);
ImageIO.write((RenderedImage) resultResize.get("bufferedImage"), mimeType, new File(imageServerPath + "/" + productId + "/" + filenameTouseThumb));
String imageUrlResource = imageServerUrl + "/" + productId + "/" + filenameToUse;
String imageUrlThumb = imageServerUrl + "/" + productId + "/" + filenameTouseThumb;
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameToUse, imageUrlResource, contentId, "image/jpeg");
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameTouseThumb, 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", "100");
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);
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("productContentTypeId", "IMAGE");
productContentCtx.put("fromDate", UtilDateTime.nowTimestamp());
productContentCtx.put("userLogin", userLogin);
productContentCtx.put("contentId", contentId);
productContentCtx.put("statusId", "IM_PENDING");
try {
Map<String, Object> serviceResult = dispatcher.runSync("createProductContent", productContentCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
Map<String, Object> contentApprovalCtx = new HashMap<>();
contentApprovalCtx.put("contentId", contentId);
contentApprovalCtx.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("createImageContentApproval", contentApprovalCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
} else {
String errMsg = UtilProperties.getMessage(resourceError, "ProductPleaseSelectImage", locale);
Debug.logFatal(errMsg, module);
result = ServiceUtil.returnError(errMsg);
result.putAll(context);
}
String successMsg = UtilProperties.getMessage(resource, "ProductFrameImageSuccessfully", locale);
result = ServiceUtil.returnSuccess(successMsg);
return result;
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class ImageManagementServices method createContentAndDataResource.
public static Map<String, Object> createContentAndDataResource(DispatchContext dctx, GenericValue userLogin, String filenameToUse, String imageUrl, String contentId, String fileContentType) {
Map<String, Object> result = new HashMap<>();
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Map<String, Object> dataResourceCtx = new HashMap<>();
dataResourceCtx.put("objectInfo", imageUrl);
dataResourceCtx.put("dataResourceName", filenameToUse);
dataResourceCtx.put("userLogin", userLogin);
dataResourceCtx.put("dataResourceTypeId", "IMAGE_OBJECT");
dataResourceCtx.put("mimeTypeId", fileContentType);
dataResourceCtx.put("isPublic", "Y");
Map<String, Object> dataResourceResult;
try {
dataResourceResult = dispatcher.runSync("createDataResource", dataResourceCtx);
if (ServiceUtil.isError(dataResourceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(dataResourceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String dataResourceId = (String) dataResourceResult.get("dataResourceId");
result.put("dataResourceFrameId", dataResourceId);
result.put("dataResourceId", dataResourceId);
Map<String, Object> contentUp = new HashMap<>();
contentUp.put("contentId", contentId);
contentUp.put("dataResourceId", dataResourceResult.get("dataResourceId"));
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) {
dataResourceCtx.put("dataResourceId", dataResource.getString("dataResourceId"));
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());
}
}
}
return result;
}
use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.
the class ImageManagementServices method addMultipleuploadForProduct.
public static Map<String, Object> addMultipleuploadForProduct(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> result = new HashMap<>();
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String productId = (String) context.get("productId");
productId = productId.trim();
String productContentTypeId = (String) context.get("productContentTypeId");
ByteBuffer imageData = (ByteBuffer) context.get("uploadedFile");
String uploadFileName = (String) context.get("_uploadedFile_fileName");
String imageResize = (String) context.get("imageResize");
Locale locale = (Locale) context.get("locale");
if (UtilValidate.isNotEmpty(uploadFileName)) {
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 rootTargetDirectory = imageServerPath;
File rootTargetDir = new File(rootTargetDirectory);
if (!rootTargetDir.exists()) {
boolean created = rootTargetDir.mkdirs();
if (!created) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductCannotCreateTheTargetDirectory", locale);
Debug.logFatal(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
}
String sizeType = null;
if (UtilValidate.isNotEmpty(imageResize)) {
sizeType = imageResize;
}
Map<String, Object> contentCtx = new HashMap<>();
contentCtx.put("contentTypeId", "DOCUMENT");
contentCtx.put("userLogin", userLogin);
Map<String, Object> contentResult;
try {
contentResult = dispatcher.runSync("createContent", contentCtx);
if (ServiceUtil.isError(contentResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(contentResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String contentId = (String) contentResult.get("contentId");
result.put("contentFrameId", contentId);
result.put("contentId", contentId);
String fileContentType = (String) context.get("_uploadedFile_contentType");
if ("image/pjpeg".equals(fileContentType)) {
fileContentType = "image/jpeg";
} else if ("image/x-png".equals(fileContentType)) {
fileContentType = "image/png";
}
// Create folder product id.
String targetDirectory = imageServerPath + "/" + productId;
File targetDir = new File(targetDirectory);
if (!targetDir.exists()) {
boolean created = targetDir.mkdirs();
if (!created) {
String errMsg = "Cannot create the target directory";
Debug.logFatal(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
}
File file = new File(imageServerPath + "/" + productId + "/" + uploadFileName);
String imageName = null;
imagePath = imageServerPath + "/" + productId + "/" + uploadFileName;
file = checkExistsImage(file);
if (UtilValidate.isNotEmpty(file)) {
imageName = file.getPath();
imageName = imageName.substring(imageName.lastIndexOf(File.separator) + 1);
} else {
imageName = "";
}
if (UtilValidate.isEmpty(imageResize)) {
// Create image file original to folder product id.
try {
RandomAccessFile out = new RandomAccessFile(file, "rw");
out.write(imageData.array());
out.close();
} catch (FileNotFoundException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductImageViewUnableWriteFile", UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
} catch (IOException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductImageViewUnableWriteBinaryData", UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
}
}
// Scale Image in different sizes
if (UtilValidate.isNotEmpty(imageResize)) {
File fileOriginal = new File(imageServerPath + "/" + productId + "/" + imageName);
fileOriginal = checkExistsImage(fileOriginal);
try {
RandomAccessFile outFile = new RandomAccessFile(fileOriginal, "rw");
outFile.write(imageData.array());
outFile.close();
} catch (FileNotFoundException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductImageViewUnableWriteFile", UtilMisc.toMap("fileName", fileOriginal.getAbsolutePath()), locale));
} catch (IOException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductImageViewUnableWriteBinaryData", UtilMisc.toMap("fileName", fileOriginal.getAbsolutePath()), locale));
}
Map<String, Object> resultResize = new HashMap<>();
try {
resultResize.putAll(scaleImageMangementInAllSize(dctx, context, imageName, sizeType, productId));
} catch (IOException e) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductScaleAdditionalImageInAllDifferentSizesIsImpossible", UtilMisc.toMap("errorString", e.toString()), locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
} catch (JDOMException e) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductErrorsOccurInParsingImageProperties.xml", UtilMisc.toMap("errorString", e.toString()), locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
}
Map<String, Object> contentThumbnail = createContentThumbnail(dctx, context, userLogin, imageData, productId, imageName);
String filenameToUseThumb = (String) contentThumbnail.get("filenameToUseThumb");
String contentIdThumb = (String) contentThumbnail.get("contentIdThumb");
String imageUrl = imageServerUrl + "/" + productId + "/" + imageName;
String imageUrlThumb = imageServerUrl + "/" + productId + "/" + filenameToUseThumb;
createContentAndDataResource(dctx, userLogin, imageName, imageUrl, contentId, fileContentType);
createContentAndDataResource(dctx, userLogin, filenameToUseThumb, imageUrlThumb, contentIdThumb, fileContentType);
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", "100");
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());
}
Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("productContentTypeId", productContentTypeId);
productContentCtx.put("fromDate", UtilDateTime.nowTimestamp());
productContentCtx.put("userLogin", userLogin);
productContentCtx.put("contentId", contentId);
productContentCtx.put("statusId", "IM_PENDING");
try {
Map<String, Object> serviceResult = dispatcher.runSync("createProductContent", productContentCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
Map<String, Object> contentApprovalCtx = new HashMap<>();
contentApprovalCtx.put("contentId", contentId);
contentApprovalCtx.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("createImageContentApproval", contentApprovalCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String autoApproveImage = EntityUtilProperties.getPropertyValue("catalog", "image.management.autoApproveImage", delegator);
if ("Y".equals(autoApproveImage)) {
Map<String, Object> autoApproveCtx = new HashMap<>();
autoApproveCtx.put("contentId", contentId);
autoApproveCtx.put("userLogin", userLogin);
autoApproveCtx.put("checkStatusId", "IM_APPROVED");
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateStatusImageManagement", autoApproveCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
}
return result;
}
use of org.apache.ofbiz.service.GenericServiceException 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.GenericServiceException 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);
}
Aggregations