Search in sources :

Example 71 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class ImageManagementServices method resizeImageOfProduct.

public static Map<String, Object> resizeImageOfProduct(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
    String productId = (String) context.get("productId");
    String dataResourceName = (String) context.get("dataResourceName");
    String width = (String) context.get("resizeWidth");
    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();
        String filenameToUse = dataResourceName;
        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));
    } catch (Exception e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    String successMsg = UtilProperties.getMessage(resource, "ProductResizeImagesSuccessful", locale);
    return ServiceUtil.returnSuccess(successMsg);
}
Also used : Locale(java.util.Locale) Delegator(org.apache.ofbiz.entity.Delegator) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) JDOMException(org.jdom.JDOMException) ImagingOpException(java.awt.image.ImagingOpException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 72 with Delegator

use of org.apache.ofbiz.entity.Delegator 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;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) ByteBuffer(java.nio.ByteBuffer) Delegator(org.apache.ofbiz.entity.Delegator) RandomAccessFile(java.io.RandomAccessFile) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 73 with Delegator

use of org.apache.ofbiz.entity.Delegator 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);
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Delegator(org.apache.ofbiz.entity.Delegator) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 74 with Delegator

use of org.apache.ofbiz.entity.Delegator 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);
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 75 with Delegator

use of org.apache.ofbiz.entity.Delegator 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);
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) File(java.io.File)

Aggregations

Delegator (org.apache.ofbiz.entity.Delegator)869 GenericValue (org.apache.ofbiz.entity.GenericValue)721 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)611 Locale (java.util.Locale)485 HashMap (java.util.HashMap)328 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)324 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)278 BigDecimal (java.math.BigDecimal)205 LinkedList (java.util.LinkedList)166 Timestamp (java.sql.Timestamp)163 GeneralException (org.apache.ofbiz.base.util.GeneralException)130 IOException (java.io.IOException)117 Map (java.util.Map)113 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)61 Security (org.apache.ofbiz.security.Security)60 HttpSession (javax.servlet.http.HttpSession)59 Properties (java.util.Properties)37 UtilProperties (org.apache.ofbiz.base.util.UtilProperties)37 EntityUtilProperties (org.apache.ofbiz.entity.util.EntityUtilProperties)35 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)33