Modified: ofbiz/trunk/applications/product/servicedef/services.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services.xml?rev=1052521&r1=1052520&r2=1052521&view=diff ============================================================================== --- ofbiz/trunk/applications/product/servicedef/services.xml (original) +++ ofbiz/trunk/applications/product/servicedef/services.xml Fri Dec 24 15:22:49 2010 @@ -1335,4 +1335,163 @@ under the License. <override name="productPromoContentTypeId" optional="false"/> <override name="productPromoId" optional="false"/> </service> + + <!-- Image Management Services --> + <service name="addMultipleuploadForProduct" default-entity-name="ProductContent" engine="java" + location="org.ofbiz.product.imagemanagement.ImageManagementServices" invoke="addMultipleuploadForProduct" auth="true"> + <implements service="uploadFileInterface"/> + <auto-attributes include="pk" mode="IN" optional="true"/> + <auto-attributes include="nonpk" mode="IN" optional="true"/> + <attribute mode="INOUT" name="contentId" optional="true" type="String"/> + <attribute mode="INOUT" name="dataResourceId" optional="true" type="String"/> + <attribute mode="IN" name="imageResize" optional="true" type="String"/> + <attribute mode="OUT" name="contentFrameId" optional="true" type="String"/> + <attribute mode="OUT" name="dataResourceFrameId" optional="true" type="String"/> + <override name="productContentTypeId" optional="false"/> + <override name="productId" optional="false"/> + </service> + + <service name="multipleUploadProductImages" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="UploadProductImages" auth="true"> + <description>Multiple upload Images For Product</description> + <permission-service service-name="genericContentPermission" main-action="CREATE"/> + <attribute name="productId" type="String" mode="INOUT" optional="false"/> + <attribute name="imageResize" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageOne" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageOne_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageOne_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageTwo" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageTwo_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageTwo_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageThree" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageThree_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageThree_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageFour" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageFour_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageFour_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageFive" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageFive_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageFive_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageSix" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageSix_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageSix_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageSeven" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageSeven_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageSeven_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageEight" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageEight_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageEight_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageNine" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageNine_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageNine_contentType" type="String" mode="IN" optional="true"/> + <attribute name="additionalImageTen" type="java.nio.ByteBuffer" mode="IN" optional="true"/> + <attribute name="_additionalImageTen_fileName" type="String" mode="IN" optional="true"/> + <attribute name="_additionalImageTen_contentType" type="String" mode="IN" optional="true"/> + </service> + + <service name="removeProductContentAndImageFile" default-entity-name="ProductContent" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="removeProductContentAndImageFile" auth="true"> + <description>Remove Content From Product and File Image</description> + <auto-attributes include="pk" mode="IN" optional="false"/> + <auto-attributes include="nonpk" mode="IN" optional="true"/> + </service> + + <service name="removeProductContentForImageManagement" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="removeProductContentForImageManagement" auth="true"> + <description>Delete Product Content Relationship Entity</description> + <attribute mode="IN" name="contentId" optional="false" type="String"/> + <attribute mode="IN" name="productId" optional="false" type="String"/> + </service> + + <service name="removeImageFileForImageManagement" engine="java" + location="org.ofbiz.product.imagemanagement.ImageManagementServices" invoke="removeImageFileForImageManagement" auth="true"> + <description>Delete Image File</description> + <attribute mode="IN" name="contentId" optional="false" type="String"/> + <attribute mode="IN" name="dataResourceName" optional="false" type="String"/> + <attribute mode="IN" name="productId" optional="false" type="String"/> + </service> + + <service name="addImageFrame" engine="java" + location="org.ofbiz.product.imagemanagement.FrameImage" invoke="addImageFrame" auth="true"> + <description>Create Image Frame For Product.</description> + <attribute mode="IN" name="productId" optional="false" type="String"/> + <attribute mode="IN" name="imageName" optional="false" type="String"/> + <attribute mode="IN" name="imageWidth" optional="false" type="String"/> + <attribute mode="IN" name="imageHeight" optional="false" type="String"/> + <attribute mode="IN" name="frameContentId" optional="false" type="String"/> + <attribute mode="IN" name="frameDataResourceId" optional="false" type="String"/> + </service> + + <service name="uploadFrame" engine="java" + location="org.ofbiz.product.imagemanagement.FrameImage" invoke="uploadFrame" auth="true"> + <description>Upload Image Frame.</description> + <attribute mode="IN" name="uploadedFile" optional="false" type="java.nio.ByteBuffer"/> + </service> + + <service name="addImageCaption" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="addImageCaption" auth="true"> + <description>Replace Image To Exist Image.</description> + <attribute mode="IN" name="productId" optional="false" type="String"/> + <attribute mode="IN" name="contentId" optional="false" type="String"/> + <attribute mode="IN" name="description" optional="true" type="String"/> + </service> + + <service name="imageCrop" engine="java" + location="org.ofbiz.product.imagemanagement.CropImage" invoke="imageCrop" auth="true"> + <description>Crop Image</description> + <attribute mode="IN" name="productId" optional="false" type="String"/> + <attribute mode="IN" name="imageName" optional="false" type="String"/> + <attribute mode="IN" name="imageX" optional="false" type="String"/> + <attribute mode="IN" name="imageY" optional="false" type="String"/> + <attribute mode="IN" name="imageW" optional="false" type="String"/> + <attribute mode="IN" name="imageH" optional="false" type="String"/> + </service> + + <service name="imageRotate" engine="java" + location="org.ofbiz.product.imagemanagement.RotateImage" invoke="imageRotate" auth="true"> + <description>Rotate Image</description> + <attribute mode="IN" name="productId" optional="false" type="String"/> + <attribute mode="IN" name="imageName" optional="false" type="String"/> + <attribute mode="IN" name="angle" optional="false" type="String"/> + </service> + + <service name="setImageDetail" engine="simple" default-entity-name="ProductContent" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="setImageDetail" auth="true"> + <auto-attributes include="pk" mode="IN" optional="false"/> + <auto-attributes include="nonpk" mode="IN" optional="true"/> + <attribute name="defaultSequenceNum" mode="IN" type="Integer" optional="true"/> + <attribute name="enable" mode="IN" type="Boolean" optional="true" default-value="true"/> + <attribute name="description" mode="IN" type="String" optional="true"/> + </service> + + <service name="updateStatusImageManagement" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="updateStatusImageManagement" auth="true"> + <attribute name="contentId" mode="IN" type="String" optional="false"/> + <attribute name="checkStatusId" mode="IN" type="String" optional="true"/> + </service> + + <service name="addRejectedReasonImageManagement" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="addRejectedReasonImageManagement" auth="true"> + <attribute name="contentId" mode="IN" type="String" optional="false"/> + <attribute name="description" mode="IN" type="String" optional="true"/> + </service> + + <service name="createImagePartyContent" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="createImagePartyContent" auth="true"> + <attribute name="contentId" mode="IN" type="String" optional="false"/> + </service> + + <service name="removeImagePartyContent" engine="simple" + location="component://product/script/org/ofbiz/product/imagemanagement/ImageManagementServices.xml" invoke="removeImagePartyContent" auth="true"> + <attribute name="partyId" mode="IN" type="String" optional="false"/> + </service> + + <service name="setThumbnail" engine="java" + location="org.ofbiz.product.imagemanagement.ImageManagementServices" invoke="setThumbnail" auth="true"> + <description>Set Thumbnail for product</description> + <attribute mode="IN" name="contentIdTo" optional="false" type="String"/> + <attribute mode="IN" name="productId" optional="false" type="String"/> + </service> + + </services> Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java?rev=1052521&r1=1052520&r2=1052521&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/image/ScaleImage.java Fri Dec 24 15:22:49 2010 @@ -37,6 +37,7 @@ import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilProperties; +import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.service.ServiceUtil; import org.ofbiz.common.image.ImageTransform; @@ -77,6 +78,7 @@ public class ScaleImage { /* VARIABLES */ Locale locale = (Locale) context.get("locale"); + int index; Map<String, Map<String, String>> imgPropertyMap = FastMap.newInstance(); BufferedImage bufImg, bufNewImg; @@ -126,12 +128,14 @@ public class ScaleImage { if (fileLocation.lastIndexOf("/") != -1) { filePathPrefix = fileLocation.substring(0, fileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing slash } - - - - /* get original BUFFERED IMAGE */ - resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + filePathPrefix + filenameToUse, locale)); - + + if (context.get("contentId") != null){ + resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/products/management/" + context.get("productId") + "/" + context.get("clientFileName"), locale)); + } else { + /* get original BUFFERED IMAGE */ + resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + filePathPrefix + filenameToUse, locale)); + } + if (resultBufImgMap.containsKey("responseMessage") && resultBufImgMap.get("responseMessage").equals("success")) { bufImg = (BufferedImage) resultBufImgMap.get("bufferedImage"); @@ -156,7 +160,6 @@ public class ScaleImage { Iterator<String> sizeIter = sizeTypeList.iterator(); while (sizeIter.hasNext()) { String sizeType = sizeIter.next(); - resultScaleImgMap.putAll(ImageTransform.scaleImage(bufImg, imgHeight, imgWidth, imgPropertyMap, sizeType, locale)); if (resultScaleImgMap.containsKey("responseMessage") && resultScaleImgMap.get("responseMessage").equals("success")) { @@ -219,4 +222,160 @@ public class ScaleImage { return ServiceUtil.returnError(errMsg); } } + + public static Map<String, Object> scaleImageManageInAllSize(Map<String, ? extends Object> context, String filenameToUse, String viewType, String viewNumber , String imageType) + throws IllegalArgumentException, ImagingOpException, IOException, JDOMException { + + /* VARIABLES */ + Locale locale = (Locale) context.get("locale"); + List<String> sizeTypeList = null; + if (UtilValidate.isNotEmpty(imageType)) { + sizeTypeList = UtilMisc.toList(imageType); + } else { + sizeTypeList = UtilMisc.toList("small", "medium", "large", "detail"); + } + + int index; + Map<String, Map<String, String>> imgPropertyMap = FastMap.newInstance(); + BufferedImage bufImg, bufNewImg; + double imgHeight, imgWidth; + Map<String, String> imgUrlMap = FastMap.newInstance(); + Map<String, Object> resultXMLMap = FastMap.newInstance(); + Map<String, Object> resultBufImgMap = FastMap.newInstance(); + Map<String, Object> resultScaleImgMap = FastMap.newInstance(); + Map<String, Object> result = FastMap.newInstance(); + + /* ImageProperties.xml */ + String imgPropertyFullPath = System.getProperty("ofbiz.home") + "/applications/product/config/ImageProperties.xml"; + resultXMLMap.putAll((Map<String, Object>) ImageTransform.getXMLValue(imgPropertyFullPath, locale)); + if (resultXMLMap.containsKey("responseMessage") && resultXMLMap.get("responseMessage").equals("success")) { + imgPropertyMap.putAll(UtilGenerics.<Map<String, Map<String, String>>>cast(resultXMLMap.get("xml"))); + } else { + String errMsg = UtilProperties.getMessage(resource, "ScaleImage.unable_to_parse", locale) + " : ImageProperties.xml"; + Debug.logError(errMsg, module); + result.put("errorMessage", errMsg); + return result; + } + + /* IMAGE */ + // get Name and Extension + index = filenameToUse.lastIndexOf("."); + String imgName = filenameToUse.substring(0, index - 1); + String imgExtension = filenameToUse.substring(index + 1); + // paths + String mainFilenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.format"); + String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context); + String imageUrlPrefix = UtilProperties.getPropertyValue("catalog", "image.url.prefix"); + + String id = null; + String type = null; + if (viewType.toLowerCase().contains("main")) { + type = "original"; + id = imgName; + } else if (viewType.toLowerCase().contains("additional") && viewNumber != null && !viewNumber.equals("0")) { + type = "additional"; + id = imgName + "_View_" + viewNumber; + } else { + return ServiceUtil.returnError("View Type : " + type + " is wrong"); + } + FlexibleStringExpander mainFilenameExpander = FlexibleStringExpander.getInstance(mainFilenameFormat); + String fileLocation = mainFilenameExpander.expandString(UtilMisc.toMap("location", "products", "type", type, "id", filenameToUse)); + String filePathPrefix = ""; + if (fileLocation.lastIndexOf("/") != -1) { + filePathPrefix = fileLocation.substring(0, fileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing slash + } + + if (context.get("contentId") != null){ + resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/products/management/" + context.get("productId") + "/" + context.get("clientFileName"), locale)); + } else { + /* get original BUFFERED IMAGE */ + resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + filePathPrefix + filenameToUse, locale)); + } + + if (resultBufImgMap.containsKey("responseMessage") && resultBufImgMap.get("responseMessage").equals("success")) { + bufImg = (BufferedImage) resultBufImgMap.get("bufferedImage"); + + // get Dimensions + imgHeight = (double) bufImg.getHeight(); + imgWidth = (double) bufImg.getWidth(); + if (imgHeight == 0.0 || imgWidth == 0.0) { + String errMsg = UtilProperties.getMessage(resource, "ScaleImage.one_current_image_dimension_is_null", locale) + " : imgHeight = " + imgHeight + " ; imgWidth = " + imgWidth; + Debug.logError(errMsg, module); + result.put("errorMessage", errMsg); + return result; + } + + // new Filename Format + FlexibleStringExpander addFilenameExpander = mainFilenameExpander; + if (viewType.toLowerCase().contains("additional")) { + String addFilenameFormat = UtilProperties.getPropertyValue("catalog", "image.filename.additionalviewsize.format"); + addFilenameExpander = FlexibleStringExpander.getInstance(addFilenameFormat); + } + + /* scale Image for each Size Type */ + Iterator<String> sizeIter = sizeTypeList.iterator(); + while (sizeIter.hasNext()) { + String sizeType = sizeIter.next(); + resultScaleImgMap.putAll(ImageTransform.scaleImage(bufImg, imgHeight, imgWidth, imgPropertyMap, sizeType, locale)); + + if (resultScaleImgMap.containsKey("responseMessage") && resultScaleImgMap.get("responseMessage").equals("success")) { + bufNewImg = (BufferedImage) resultScaleImgMap.get("bufferedImage"); + + // write the New Scaled Image + String newFileLocation = null; + if (viewType.toLowerCase().contains("main")) { + newFileLocation = mainFilenameExpander.expandString(UtilMisc.toMap("location", "products", "type", sizeType, "id", id)); + } else if (viewType.toLowerCase().contains("additional")) { + newFileLocation = addFilenameExpander.expandString(UtilMisc.toMap("location", "products", "viewtype", viewType, "sizetype", sizeType,"id", id)); + } + String newFilePathPrefix = ""; + if (newFileLocation.lastIndexOf("/") != -1) { + newFilePathPrefix = newFileLocation.substring(0, newFileLocation.lastIndexOf("/") + 1); // adding 1 to include the trailing slash + } + + String targetDirectory = imageServerPath + "/" + newFilePathPrefix; + File targetDir = new File(targetDirectory); + if (!targetDir.exists()) { + boolean created = targetDir.mkdirs(); + if (!created) { + String errMsg = UtilProperties.getMessage(resource, "ScaleImage.unable_to_create_target_directory", locale) + " - " + targetDirectory; + Debug.logFatal(errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } + + // write new image + try { + ImageIO.write((RenderedImage) bufNewImg, imgExtension, new File(imageServerPath + "/" + newFilePathPrefix + filenameToUse)); + } catch (IllegalArgumentException e) { + String errMsg = UtilProperties.getMessage(resource, "ScaleImage.one_parameter_is_null", locale) + e.toString(); + Debug.logError(errMsg, module); + result.put("errorMessage", errMsg); + return result; + } catch (IOException e) { + String errMsg = UtilProperties.getMessage(resource, "ScaleImage.error_occurs_during_writing", locale) + e.toString(); + Debug.logError(errMsg, module); + result.put("errorMessage", errMsg); + return result; + } + + /* write Return Result */ + String imageUrl = imageUrlPrefix + "/" + newFilePathPrefix + filenameToUse; + imgUrlMap.put(sizeType, imageUrl); + + } // scaleImgMap + } // sizeIter + + result.put("responseMessage", "success"); + result.put("imageUrlMap", imgUrlMap); + result.put("original", resultBufImgMap); + return result; + + } else { + String errMsg = UtilProperties.getMessage(resource, "ScaleImage.unable_to_scale_original_image", locale) + " : " + filenameToUse; + Debug.logError(errMsg, module); + result.put("errorMessage", errMsg); + return ServiceUtil.returnError(errMsg); + } + } } Added: ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/CropImage.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/CropImage.java?rev=1052521&view=auto ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/CropImage.java (added) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/CropImage.java Fri Dec 24 15:22:49 2010 @@ -0,0 +1,161 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.imagemanagement; + +import java.awt.image.BufferedImage; +import java.awt.image.RenderedImage; +import java.io.File; +import java.io.IOException; +import java.util.*; + +import javax.imageio.ImageIO; + +import javolution.util.FastMap; + +import org.jdom.JDOMException; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilDateTime; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilProperties; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.service.ServiceUtil; + + +public class CropImage { + + public static final String module = CropImage.class.getName(); + public static final String resource = "ProductErrorUiLabels"; + + public static Map<String, Object> imageCrop(DispatchContext dctx, Map<String, ? extends Object> context) + throws IOException, JDOMException { + LocalDispatcher dispatcher = dctx.getDispatcher(); + Delegator delegator = dctx.getDelegator(); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + + String productId = (String) context.get("productId"); + String imageName = (String) context.get("imageName"); + String imageX = (String) context.get("imageX"); + String imageY = (String) context.get("imageY"); + String imageW = (String) context.get("imageW"); + String imageH = (String) context.get("imageH"); + + if (UtilValidate.isNotEmpty(imageName)) { + Map<String, Object> contentCtx = FastMap.newInstance(); + contentCtx.put("contentTypeId", "DOCUMENT"); + contentCtx.put("userLogin", userLogin); + Map<String, Object> contentResult = FastMap.newInstance(); + try { + contentResult = dispatcher.runSync("createContent", contentCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + + Map<String, Object> contentThumb = FastMap.newInstance(); + contentThumb.put("contentTypeId", "DOCUMENT"); + contentThumb.put("userLogin", userLogin); + Map<String, Object> contentThumbResult = FastMap.newInstance(); + try { + contentThumbResult = dispatcher.runSync("createContent", contentThumb); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + + String contentIdThumb = (String) contentThumbResult.get("contentId"); + String contentId = (String) contentResult.get("contentId"); + String filenameToUse = (String) contentResult.get("contentId") + ".jpg"; + String filenameTouseThumb = (String) contentThumbResult.get("contentId") + ".jpg"; + + String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context); + BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/products/management/" + productId + "/" + imageName)); + + int x = Integer.parseInt(imageX); + int y = Integer.parseInt(imageY); + int w = Integer.parseInt(imageW); + int h = Integer.parseInt(imageH); + + BufferedImage bufNewImg = bufImg.getSubimage(x, y, w, h); + String mimeType = imageName.substring(imageName.lastIndexOf(".") + 1); + ImageIO.write((RenderedImage) bufNewImg, mimeType, new File(imageServerPath + "/products/management/" + 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 + "/products/management/" + productId + "/" + filenameTouseThumb)); + + String imageUrlResource = "/images/products/management/" + productId + "/" + filenameToUse; + String imageUrlThumb = "/images/products/management/" + productId + "/" + filenameTouseThumb; + + ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameToUse, imageUrlResource, contentId); + ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameTouseThumb, imageUrlThumb, contentIdThumb); + + Map<String, Object> createContentAssocMap = FastMap.newInstance(); + createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL"); + createContentAssocMap.put("contentId", contentId); + createContentAssocMap.put("contentIdTo", contentIdThumb); + createContentAssocMap.put("userLogin", userLogin); + try { + dispatcher.runSync("createContentAssoc", createContentAssocMap); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + + Map<String, Object> productContentCtx = FastMap.newInstance(); + 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 { + dispatcher.runSync("createProductContent", productContentCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + + Map<String, Object> partyContentCtx = FastMap.newInstance(); + partyContentCtx.put("contentId", contentId); + partyContentCtx.put("userLogin", userLogin); + try { + dispatcher.runSync("createImagePartyContent", partyContentCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + } else { + String errMsg = "Please select Image."; + Debug.logFatal(errMsg, module); + return ServiceUtil.returnError(errMsg); + } + String successMsg = "Crop image successfully."; + Map<String, Object> result = ServiceUtil.returnSuccess(successMsg); + return result; + } +} Added: ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/FrameImage.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/FrameImage.java?rev=1052521&view=auto ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/FrameImage.java (added) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/imagemanagement/FrameImage.java Fri Dec 24 15:22:49 2010 @@ -0,0 +1,413 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.product.imagemanagement; + +import java.awt.AlphaComposite; +import java.awt.Composite; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.RenderingHints; +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.image.BufferedImage; +import java.awt.image.RenderedImage; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; + +import javax.imageio.ImageIO; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.swing.ImageIcon; + +import javolution.util.FastMap; + +import org.jdom.JDOMException; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilDateTime; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilProperties; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.ofbiz.content.layout.LayoutWorker; +import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.util.EntityUtil; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.service.ServiceUtil; + +public class FrameImage { + + public static final String module = FrameImage.class.getName(); + public static final String resource = "ProductErrorUiLabels"; + + public static Map<String, Object> addImageFrame(DispatchContext dctx, Map<String, ? extends Object> context) + throws IOException, JDOMException { + Map<String, Object> result = FastMap.newInstance(); + LocalDispatcher dispatcher = dctx.getDispatcher(); + Delegator delegator = dctx.getDelegator(); + String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), 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"); + + if (UtilValidate.isEmpty(context.get("frameContentId")) || UtilValidate.isEmpty(context.get("frameDataResourceId"))) { + result = ServiceUtil.returnError("Required frame image content ID or dataResource ID parameters. Please upload new frame image or choose the exist frame."); + result.putAll(context); + } + if (UtilValidate.isEmpty(context.get("imageWidth")) || UtilValidate.isEmpty(context.get("imageHeight"))) { + result = ServiceUtil.returnError("Image Width and Image Height are required to create the image. Please enter in Image Width and Image Height fields."); + result.putAll(context); + } + + String frameContentId = (String) context.get("frameContentId"); + String frameDataResourceId = (String) context.get("frameDataResourceId"); + + String frameImageName = null; + try { + GenericValue contentDataResourceView = delegator.findByPrimaryKey("ContentDataResourceView", UtilMisc.toMap("contentId", frameContentId, "drDataResourceId", frameDataResourceId)); + frameImageName = contentDataResourceView.getString("contentName"); + } catch (Exception e) { + Debug.logError(e, module); + result = ServiceUtil.returnError(e.getMessage()); + result.putAll(context); + } + + if (UtilValidate.isNotEmpty(imageName)) { + + // Image Frame + BufferedImage bufImg1 = ImageIO.read(new File(imageServerPath + "/products/management/" + productId + "/" + imageName)); + BufferedImage bufImg2 = ImageIO.read(new File(imageServerPath + "/products/management/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 = FastMap.newInstance(); + contentCtx.put("contentTypeId", "DOCUMENT"); + contentCtx.put("userLogin", userLogin); + Map<String, Object> contentResult = FastMap.newInstance(); + try { + contentResult = dispatcher.runSync("createContent", contentCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + result = ServiceUtil.returnError(e.getMessage()); + result.putAll(context); + } + + Map<String, Object> contentThumb = FastMap.newInstance(); + contentThumb.put("contentTypeId", "DOCUMENT"); + contentThumb.put("userLogin", userLogin); + Map<String, Object> contentThumbResult = FastMap.newInstance(); + try { + contentThumbResult = dispatcher.runSync("createContent", contentThumb); + } 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) contentThumbResult.get("contentId") + ".jpg"; + + Image newImg1 = bufImg1.getScaledInstance((int) width, (int) height , Image.SCALE_SMOOTH); + Image newImg2 = bufImg2.getScaledInstance((int) width , (int) height , Image.SCALE_SMOOTH); + BufferedImage bufNewImg = combineBufferedImage(newImg1, newImg2, bufImgType); + String mimeType = imageName.substring(imageName.lastIndexOf(".") + 1); + ImageIO.write((RenderedImage) bufNewImg, mimeType, new File(imageServerPath + "/products/management/" + 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 + "/products/management/" + productId + "/" + filenameTouseThumb)); + + String imageUrlResource = "/images/products/management/" + productId + "/" + filenameToUse; + String imageUrlThumb = "/images/products/management/" + productId + "/" + filenameTouseThumb; + + ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameToUse, imageUrlResource, contentId); + ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameTouseThumb, imageUrlThumb, contentIdThumb); + + Map<String, Object> createContentAssocMap = FastMap.newInstance(); + createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL"); + createContentAssocMap.put("contentId", contentId); + createContentAssocMap.put("contentIdTo", contentIdThumb); + createContentAssocMap.put("userLogin", userLogin); + try { + dispatcher.runSync("createContentAssoc", createContentAssocMap); + } catch (GenericServiceException e) { + Debug.logError(e, module); + result = ServiceUtil.returnError(e.getMessage()); + result.putAll(context); + } + + Map<String, Object> productContentCtx = FastMap.newInstance(); + 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 { + dispatcher.runSync("createProductContent", productContentCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + result = ServiceUtil.returnError(e.getMessage()); + result.putAll(context); + } + + Map<String, Object> partyContentCtx = FastMap.newInstance(); + partyContentCtx.put("contentId", contentId); + partyContentCtx.put("userLogin", userLogin); + try { + dispatcher.runSync("createImagePartyContent", partyContentCtx); + } catch (GenericServiceException e) { + Debug.logError(e, module); + result = ServiceUtil.returnError(e.getMessage()); + result.putAll(context); + } + } + else{ + String errMsg = "Please select Image."; + Debug.logFatal(errMsg, module); + result = ServiceUtil.returnError(errMsg); + result.putAll(context); + } + String successMsg = "Frame image successfully."; + result = ServiceUtil.returnSuccess(successMsg); + return result; + } + + public static BufferedImage combineBufferedImage(Image image1, Image image2, int bufImgType) { + // Full image loading + image1 = new ImageIcon(image1).getImage(); + image2 = new ImageIcon(image2).getImage(); + + // New BufferedImage creation + BufferedImage bufferedImage = new BufferedImage(image1.getWidth(null), image1.getHeight(null), bufImgType); + Graphics2D g = bufferedImage.createGraphics( ); + g.drawImage(image1, null, null); + + // Draw Image combine + Point2D center = new Point2D.Float(bufferedImage.getHeight() / 2, bufferedImage.getWidth() / 2); + AffineTransform at = AffineTransform.getTranslateInstance(center.getX( ) - (image2.getWidth(null) / 2), center.getY( ) - (image2.getHeight(null) / 2)); + g.transform(at); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.drawImage(image2, 0, 0, null); + Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .35f); + g.setComposite(c); + at = AffineTransform.getTranslateInstance(center.getX( ) - (bufferedImage.getWidth(null) / 2), center.getY( ) - (bufferedImage.getHeight(null) / 2)); + g.setTransform(at); + g.drawImage(bufferedImage, 0, 0, null); + g.dispose(); + + return( bufferedImage ); + } + + public static String uploadFrame(HttpServletRequest request, HttpServletResponse response) { + LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); + HttpSession session = request.getSession(); + GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); + + Map<String, ? extends Object> context = request.getParameterMap(); + String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context); + Map<String, Object> tempFile = LayoutWorker.uploadImageAndParameters(request, "uploadedFile"); + String imageName = tempFile.get("imageFileName").toString(); + String mimType = tempFile.get("uploadMimeType").toString(); + ByteBuffer imageData = (ByteBuffer) tempFile.get("imageData"); + if (UtilValidate.isEmpty(imageName) || UtilValidate.isEmpty(imageData)) { + session.setAttribute("frameContentId", request.getParameter("frameExistContentId").toString()); + session.setAttribute("frameDataResourceId", request.getParameter("frameExistDataResourceId").toString()); + request.setAttribute("_ERROR_MESSAGE_", "There is no frame image, please select the image type *.PNG uploading."); + return "error"; + } + if (!"image/png".equals(mimType)) { + session.setAttribute("frameContentId", request.getParameter("frameExistContentId").toString()); + session.setAttribute("frameDataResourceId", request.getParameter("frameExistDataResourceId").toString()); + request.setAttribute("_ERROR_MESSAGE_", "The selected image type is incorrect, please select the image type *.PNG to upload."); + return "error"; + } + + String contentId = null; + String dataResourceId = null; + try { + String dirPath = "/products/management/frame/"; + File dir = new File(imageServerPath + dirPath); + if (!dir.exists()) { + boolean createDir = dir.mkdir(); + if (!createDir) { + request.setAttribute("_ERROR_MESSAGE_", "Cannot create directory."); + return "error"; + } + } + String imagePath = "/products/management/frame/" + imageName; + File file = new File(imageServerPath + imagePath); + if (file.exists()) { + request.setAttribute("_ERROR_MESSAGE_", "There is an existing frame, please select from the existing frame."); + return "error"; + } + RandomAccessFile out = new RandomAccessFile(file, "rw"); + out.write(imageData.array()); + out.close(); + + //create dataResource + Map<String, Object> dataResourceCtx = FastMap.newInstance(); + dataResourceCtx.put("objectInfo", "/images" + imagePath); + dataResourceCtx.put("dataResourceName", imageName); + dataResourceCtx.put("userLogin", userLogin); + dataResourceCtx.put("dataResourceTypeId", "IMAGE_OBJECT"); + dataResourceCtx.put("mimeTypeId", "image/png"); + dataResourceCtx.put("isPublic", "Y"); + Map<String, Object> dataResourceResult = dispatcher.runSync("createDataResource", dataResourceCtx); + dataResourceId = dataResourceResult.get("dataResourceId").toString(); + //create content + Map<String, Object> contentCtx = FastMap.newInstance(); + contentCtx.put("dataResourceId", dataResourceResult.get("dataResourceId").toString()); + contentCtx.put("contentTypeId", "IMAGE_FRAME"); + contentCtx.put("contentName", imageName); + contentCtx.put("fromDate", UtilDateTime.nowTimestamp()); + contentCtx.put("userLogin", userLogin); + Map<String, Object> contentResult = dispatcher.runSync("createContent", contentCtx); + contentId = contentResult.get("contentId").toString(); + } catch (Exception e) { + request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); + return "error"; + } + session.setAttribute("frameContentId", contentId); + session.setAttribute("frameDataResourceId", dataResourceId); + request.setAttribute("_EVENT_MESSAGE_", "Upload frame image successful."); + return "success"; + } + public static String previewFrameImage(HttpServletRequest request, HttpServletResponse response) throws IOException, JDOMException { + Delegator delegator = (Delegator) request.getAttribute("delegator"); + Map<String, ? extends Object> context = request.getParameterMap(); + HttpSession session = request.getSession(); + String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context); + + String productId = request.getParameter("productId"); + String imageName = request.getParameter("imageName"); + + if (UtilValidate.isEmpty(request.getParameter("frameContentId")) || UtilValidate.isEmpty(request.getParameter("frameDataResourceId"))) { + request.setAttribute("_ERROR_MESSAGE_", "Required frame image content ID or dataResource ID parameters. Please upload new frame image or choose the exist frame."); + return "error"; + } + String frameContentId = (String) request.getParameter("frameContentId"); + String frameDataResourceId = (String) request.getParameter("frameDataResourceId"); + + if (UtilValidate.isEmpty(request.getParameter("imageWidth")) || UtilValidate.isEmpty(request.getParameter("imageHeight"))) { + String errMsg = "Image Width and Image Height are required to preview the image. Please enter in Image Width and Image Height fields."; + session.setAttribute("frameContentId", frameContentId); + session.setAttribute("frameDataResourceId", frameDataResourceId); + request.setAttribute("_ERROR_MESSAGE_", errMsg); + return "error"; + } + + String frameImageName = null; + try { + GenericValue contentDataResourceView = delegator.findByPrimaryKey("ContentDataResourceView", UtilMisc.toMap("contentId", frameContentId, "drDataResourceId", frameDataResourceId)); + frameImageName = contentDataResourceView.getString("contentName"); + } catch (Exception e) { + request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); + return "error"; + } + if (UtilValidate.isNotEmpty(imageName)) { + File file = new File(imageServerPath + "/products/management/previewImage.jpg"); + file.delete(); + // Image Frame + BufferedImage bufImg1 = ImageIO.read(new File(imageServerPath + "/products/management/" + productId + "/" + imageName)); + BufferedImage bufImg2 = ImageIO.read(new File(imageServerPath + "/products/management/frame/" + frameImageName)); + + int bufImgType; + if (BufferedImage.TYPE_CUSTOM == bufImg1.getType()) { + bufImgType = BufferedImage.TYPE_INT_ARGB_PRE; + } else { + bufImgType = bufImg1.getType(); + } + + int width = Integer.parseInt(request.getParameter("imageWidth")); + int height= Integer.parseInt(request.getParameter("imageHeight")); + + Image newImg1 = bufImg1.getScaledInstance((int) width, (int) height , Image.SCALE_SMOOTH); + Image newImg2 = bufImg2.getScaledInstance((int) width , (int) height , Image.SCALE_SMOOTH); + BufferedImage bufNewImg = combineBufferedImage(newImg1, newImg2, bufImgType); + String mimeType = imageName.substring(imageName.lastIndexOf(".") + 1); + ImageIO.write((RenderedImage) bufNewImg, mimeType, new File(imageServerPath + "/products/management/previewImage.jpg")); + + } + else{ + String errMsg = "Please select Image."; + request.setAttribute("_EVENT_MESSAGE_", errMsg); + return "error"; + } + return "success"; + } + public static String chooseFrameImage(HttpServletRequest request, HttpServletResponse response) { + HttpSession session = request.getSession(); + if(UtilValidate.isEmpty(request.getParameter("frameContentId"))) { + if (UtilValidate.isNotEmpty(request.getParameter("frameExistContentId")) && UtilValidate.isNotEmpty(request.getParameter("frameExistDataResourceId"))) { + session.setAttribute("frameExistContentId", request.getParameter("frameExistContentId").toString()); + session.setAttribute("frameDataResourceId", request.getParameter("frameExistDataResourceId").toString()); + } + request.setAttribute("_ERROR_MESSAGE_", "Required frame image content ID"); + return "error"; + } + + String frameContentId = (String) request.getParameter("frameContentId"); + Delegator delegator = (Delegator) request.getAttribute("delegator"); + + String frameDataResourceId = null; + try { + List<GenericValue> contentDataResources = delegator.findByAnd("ContentDataResourceView", UtilMisc.toMap("contentId", frameContentId)); + GenericValue contentDataResource = EntityUtil.getFirst(contentDataResources); + frameDataResourceId = contentDataResource.getString("dataResourceId"); + } catch (Exception e) { + request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); + return "error"; + } + session.setAttribute("frameContentId", frameContentId); + session.setAttribute("frameDataResourceId", frameDataResourceId); + return "success"; + } + public static String deleteFrameImage(HttpServletRequest request, HttpServletResponse response) { + Map<String, ? extends Object> context = request.getParameterMap(); + String imageServerPath = FlexibleStringExpander.expandString(UtilProperties.getPropertyValue("catalog", "image.server.path"), context); + File file = new File(imageServerPath + "/products/management/previewImage.jpg"); + if (file.exists()) { + file.delete(); + } + return "success"; + } +} \ No newline at end of file |
Free forum by Nabble | Edit this page |