Search in sources :

Example 96 with GenericValue

use of org.apache.ofbiz.entity.GenericValue 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 97 with GenericValue

use of org.apache.ofbiz.entity.GenericValue 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 98 with GenericValue

use of org.apache.ofbiz.entity.GenericValue 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 99 with GenericValue

use of org.apache.ofbiz.entity.GenericValue 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)

Example 100 with GenericValue

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

the class WorkEffortKeywordIndex method indexKeywords.

public static void indexKeywords(GenericValue workEffort) throws GenericEntityException {
    if (workEffort == null) {
        return;
    }
    Delegator delegator = workEffort.getDelegator();
    if (delegator == null) {
        return;
    }
    String workEffortId = workEffort.getString("workEffortId");
    String separators = KeywordSearchUtil.getSeparators();
    String stopWordBagOr = KeywordSearchUtil.getStopWordBagOr();
    String stopWordBagAnd = KeywordSearchUtil.getStopWordBagAnd();
    boolean removeStems = KeywordSearchUtil.getRemoveStems();
    Set<String> stemSet = KeywordSearchUtil.getStemSet();
    Map<String, Long> keywords = new TreeMap<>();
    List<String> strings = new LinkedList<>();
    int widWeight = 1;
    try {
        widWeight = EntityUtilProperties.getPropertyAsInteger("workeffort", "index.weight.WorkEffort.workEffortId", 1).intValue();
    } catch (Exception e) {
        Debug.logWarning("Could not parse weight number: " + e.toString(), module);
    }
    keywords.put(workEffort.getString("workEffortId").toLowerCase(Locale.getDefault()), Long.valueOf(widWeight));
    addWeightedKeywordSourceString(workEffort, "workEffortName", strings);
    addWeightedKeywordSourceString(workEffort, "workEffortTypeId", strings);
    addWeightedKeywordSourceString(workEffort, "currentStatusId", strings);
    if (!"0".equals(EntityUtilProperties.getPropertyValue("workeffort", "index.weight.WorkEffortNoteAndData.noteInfo", "1", delegator))) {
        List<GenericValue> workEffortNotes = EntityQuery.use(delegator).from("WorkEffortNoteAndData").where("workEffortId", workEffortId).queryList();
        for (GenericValue workEffortNote : workEffortNotes) {
            addWeightedKeywordSourceString(workEffortNote, "noteInfo", strings);
        }
    }
    // WorkEffortAttribute
    if (!"0".equals(EntityUtilProperties.getPropertyValue("workeffort", "index.weight.WorkEffortAttribute.attrName", "1", delegator)) || !"0".equals(EntityUtilProperties.getPropertyValue("workeffort", "index.weight.WorkEffortAttribute.attrValue", "1", delegator))) {
        List<GenericValue> workEffortAttributes = EntityQuery.use(delegator).from("WorkEffortAttribute").where("workEffortId", workEffortId).queryList();
        for (GenericValue workEffortAttribute : workEffortAttributes) {
            addWeightedKeywordSourceString(workEffortAttribute, "attrName", strings);
            addWeightedKeywordSourceString(workEffortAttribute, "attrValue", strings);
        }
    }
    String workEffortContentTypes = EntityUtilProperties.getPropertyValue("workeffort", "index.include.WorkEffortContentTypes", delegator);
    for (String workEffortContentTypeId : workEffortContentTypes.split(",")) {
        int weight = 1;
        try {
            weight = EntityUtilProperties.getPropertyAsInteger("workeffort", "index.weight.WorkEffortContent." + workEffortContentTypeId, 1).intValue();
        } catch (Exception e) {
            Debug.logWarning("Could not parse weight number: " + e.toString(), module);
        }
        List<GenericValue> workEffortContentAndInfos = EntityQuery.use(delegator).from("WorkEffortContentAndInfo").where("workEffortId", workEffortId, "workEffortContentTypeId", workEffortContentTypeId).queryList();
        for (GenericValue workEffortContentAndInfo : workEffortContentAndInfos) {
            addWeightedDataResourceString(workEffortContentAndInfo, weight, strings, delegator, workEffort);
            List<GenericValue> alternateViews = workEffortContentAndInfo.getRelated("ContentAssocDataResourceViewTo", UtilMisc.toMap("caContentAssocTypeId", "ALTERNATE_LOCALE"), UtilMisc.toList("-caFromDate"), false);
            alternateViews = EntityUtil.filterByDate(alternateViews, UtilDateTime.nowTimestamp(), "caFromDate", "caThruDate", true);
            for (GenericValue thisView : alternateViews) {
                addWeightedDataResourceString(thisView, weight, strings, delegator, workEffort);
            }
        }
    }
    for (String str : strings) {
        // call process keywords method here
        KeywordSearchUtil.processKeywordsForIndex(str, keywords, separators, stopWordBagAnd, stopWordBagOr, removeStems, stemSet);
    }
    List<GenericValue> toBeStored = new LinkedList<>();
    for (Map.Entry<String, Long> entry : keywords.entrySet()) {
        if (entry.getKey().length() < 60) {
            // ignore very long strings, cannot be stored anyway
            GenericValue workEffortKeyword = delegator.makeValue("WorkEffortKeyword", UtilMisc.toMap("workEffortId", workEffort.getString("workEffortId"), "keyword", entry.getKey(), "relevancyWeight", entry.getValue()));
            toBeStored.add(workEffortKeyword);
        }
    }
    if (toBeStored.size() > 0) {
        if (Debug.verboseOn()) {
            Debug.logVerbose("WorkEffortKeywordIndex indexKeywords Storing " + toBeStored.size() + " keywords for workEffortId " + workEffort.getString("workEffortId"), module);
        }
        delegator.storeAll(toBeStored);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)1422 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)871 Delegator (org.apache.ofbiz.entity.Delegator)721 Locale (java.util.Locale)505 HashMap (java.util.HashMap)463 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)370 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)356 BigDecimal (java.math.BigDecimal)338 LinkedList (java.util.LinkedList)312 Timestamp (java.sql.Timestamp)202 GeneralException (org.apache.ofbiz.base.util.GeneralException)168 Map (java.util.Map)155 IOException (java.io.IOException)116 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)97 HttpSession (javax.servlet.http.HttpSession)89 ArrayList (java.util.ArrayList)69 Security (org.apache.ofbiz.security.Security)69 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)59 List (java.util.List)56 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)52