You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by mb...@apache.org on 2017/12/16 14:09:53 UTC
svn commit: r1818411 -
/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/
Author: mbrohl
Date: Sat Dec 16 14:09:52 2017
New Revision: 1818411
URL: http://svn.apache.org/viewvc?rev=1818411&view=rev
Log:
Improved: General refactoring and code improvements, package
org.apache.ofbiz.product.imagemanagement.
(OFBIZ-10071)
Thanks Julian Leichert for reporting and providing the patches.
Modified:
ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/CropImage.java
ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java
ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementHelper.java
ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java
ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageUrlServlet.java
ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ReplaceImage.java
ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/RotateImage.java
Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/CropImage.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/CropImage.java?rev=1818411&r1=1818410&r2=1818411&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/CropImage.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/CropImage.java Sat Dec 16 14:09:52 2017
@@ -40,7 +40,6 @@ import org.apache.ofbiz.service.Dispatch
import org.apache.ofbiz.service.GenericServiceException;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.service.ServiceUtil;
-import org.jdom.JDOMException;
public class CropImage {
@@ -50,22 +49,22 @@ public class CropImage {
public static final String resource = "ProductUiLabels";
public static Map<String, Object> imageCrop(DispatchContext dctx, Map<String, ? extends Object> context)
- throws IOException, JDOMException {
+ throws IOException {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dispatcher.getDelegator();
Locale locale = (Locale)context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String nameOfThumb = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.nameofthumbnail", delegator), context);
-
+
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 = new HashMap<String, Object>();
+ Map<String, Object> contentCtx = new HashMap<>();
contentCtx.put("contentTypeId", "DOCUMENT");
contentCtx.put("userLogin", userLogin);
Map<String, Object> contentResult;
@@ -75,8 +74,8 @@ public class CropImage {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> contentThumb = new HashMap<String, Object>();
+
+ Map<String, Object> contentThumb = new HashMap<>();
contentThumb.put("contentTypeId", "DOCUMENT");
contentThumb.put("userLogin", userLogin);
Map<String, Object> contentThumbResult;
@@ -86,38 +85,38 @@ public class CropImage {
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) contentResult.get("contentId") + nameOfThumb + ".jpg";
-
+
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", delegator), context);
BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/" + 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(bufNewImg, mimeType, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
-
+
double imgHeight = bufNewImg.getHeight();
double imgWidth = bufNewImg.getWidth();
-
+
Map<String, Object> resultResize = ImageManagementServices.resizeImageThumbnail(bufNewImg, imgHeight, imgWidth);
ImageIO.write((RenderedImage) resultResize.get("bufferedImage"), mimeType, new File(imageServerPath + "/" + productId + "/" + filenameTouseThumb));
-
+
String imageUrlResource = imageServerUrl + "/" + productId + "/" + filenameToUse;
String imageUrlThumb = imageServerUrl + "/" + productId + "/" + filenameTouseThumb;
-
+
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameToUse, imageUrlResource, contentId, "image/jpeg");
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameTouseThumb, imageUrlThumb, contentIdThumb, "image/jpeg");
-
- Map<String, Object> createContentAssocMap = new HashMap<String, Object>();
+
+ Map<String, Object> createContentAssocMap = new HashMap<>();
createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL");
createContentAssocMap.put("contentId", contentId);
createContentAssocMap.put("contentIdTo", contentIdThumb);
@@ -129,8 +128,8 @@ public class CropImage {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> productContentCtx = new HashMap<String, Object>();
+
+ Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("productContentTypeId", "IMAGE");
productContentCtx.put("fromDate", UtilDateTime.nowTimestamp());
@@ -143,8 +142,8 @@ public class CropImage {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> contentApprovalCtx = new HashMap<String, Object>();
+
+ Map<String, Object> contentApprovalCtx = new HashMap<>();
contentApprovalCtx.put("contentId", contentId);
contentApprovalCtx.put("userLogin", userLogin);
try {
Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java?rev=1818411&r1=1818410&r2=1818411&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java Sat Dec 16 14:09:52 2017
@@ -68,35 +68,35 @@ public class FrameImage {
public static final String resource = "ProductUiLabels";
public static Map<String, Object> addImageFrame(DispatchContext dctx, Map<String, ? extends Object> context)
- throws IOException, JDOMException {
+ throws IOException {
Map<String, Object> result;
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", delegator), context);
String nameOfThumb = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.nameofthumbnail", delegator), context);
-
+
GenericValue userLogin = (GenericValue) context.get("userLogin");
String productId = (String) context.get("productId");
String imageName = (String) context.get("imageName");
String imageWidth = (String) context.get("imageWidth");
String imageHeight = (String) context.get("imageHeight");
Locale locale = (Locale) context.get("locale");
-
+
if (UtilValidate.isEmpty(context.get("frameContentId")) || UtilValidate.isEmpty(context.get("frameDataResourceId"))) {
- result = ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
+ result = ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"ProductImageFrameContentIdRequired", locale));
result.putAll(context);
}
if (UtilValidate.isEmpty(context.get("imageWidth")) || UtilValidate.isEmpty(context.get("imageHeight"))) {
- result = ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
+ result = ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"ProductImageWidthAndHeightRequired", locale));
result.putAll(context);
}
-
+
String frameContentId = (String) context.get("frameContentId");
String frameDataResourceId = (String) context.get("frameDataResourceId");
-
+
String frameImageName = null;
try {
GenericValue contentDataResourceView = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", frameContentId, "drDataResourceId", frameDataResourceId).queryOne();
@@ -105,32 +105,28 @@ public class FrameImage {
Debug.logError(gee, module);
result = ServiceUtil.returnError(gee.getMessage());
result.putAll(context);
- } 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 + "/" + productId + "/" + imageName));
BufferedImage bufImg2 = ImageIO.read(new File(imageServerPath + "/frame/"+frameImageName));
-
+
int bufImgType;
if (BufferedImage.TYPE_CUSTOM == bufImg1.getType()) {
bufImgType = BufferedImage.TYPE_INT_ARGB_PRE;
} else {
bufImgType = bufImg1.getType();
}
-
+
int width = Integer.parseInt(imageWidth);
int height= Integer.parseInt(imageHeight);
-
- Map<String, Object> contentCtx = new HashMap<String, Object>();
+
+ Map<String, Object> contentCtx = new HashMap<>();
contentCtx.put("contentTypeId", "DOCUMENT");
contentCtx.put("userLogin", userLogin);
- Map<String, Object> contentResult = new HashMap<String, Object>();
+ Map<String, Object> contentResult = new HashMap<>();
try {
contentResult = dispatcher.runSync("createContent", contentCtx);
} catch (GenericServiceException e) {
@@ -138,11 +134,11 @@ public class FrameImage {
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
-
- Map<String, Object> contentThumb = new HashMap<String, Object>();
+
+ Map<String, Object> contentThumb = new HashMap<>();
contentThumb.put("contentTypeId", "DOCUMENT");
contentThumb.put("userLogin", userLogin);
- Map<String, Object> contentThumbResult = new HashMap<String, Object>();
+ Map<String, Object> contentThumbResult = new HashMap<>();
try {
contentThumbResult = dispatcher.runSync("createContent", contentThumb);
} catch (GenericServiceException e) {
@@ -150,31 +146,31 @@ public class FrameImage {
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
-
+
String contentIdThumb = (String) contentThumbResult.get("contentId");
String contentId = (String) contentResult.get("contentId");
String filenameToUse = (String) contentResult.get("contentId") + ".jpg";
String filenameTouseThumb = (String) contentResult.get("contentId") + nameOfThumb + ".jpg";
-
+
Image newImg1 = bufImg1.getScaledInstance(width, height , Image.SCALE_SMOOTH);
Image newImg2 = bufImg2.getScaledInstance(width , height , Image.SCALE_SMOOTH);
BufferedImage bufNewImg = combineBufferedImage(newImg1, newImg2, bufImgType);
String mimeType = imageName.substring(imageName.lastIndexOf('.') + 1);
ImageIO.write(bufNewImg, mimeType, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
-
+
double imgHeight = bufNewImg.getHeight();
double imgWidth = bufNewImg.getWidth();
-
+
Map<String, Object> resultResize = ImageManagementServices.resizeImageThumbnail(bufNewImg, imgHeight, imgWidth);
ImageIO.write((RenderedImage) resultResize.get("bufferedImage"), mimeType, new File(imageServerPath + "/" + productId + "/" + filenameTouseThumb));
-
+
String imageUrlResource = imageServerUrl + "/" + productId + "/" + filenameToUse;
String imageUrlThumb = imageServerUrl + "/" + productId + "/" + filenameTouseThumb;
-
+
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameToUse, imageUrlResource, contentId, "image/jpeg");
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameTouseThumb, imageUrlThumb, contentIdThumb, "image/jpeg");
-
- Map<String, Object> createContentAssocMap = new HashMap<String, Object>();
+
+ Map<String, Object> createContentAssocMap = new HashMap<>();
createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL");
createContentAssocMap.put("contentId", contentId);
createContentAssocMap.put("contentIdTo", contentIdThumb);
@@ -187,8 +183,8 @@ public class FrameImage {
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
-
- Map<String, Object> productContentCtx = new HashMap<String, Object>();
+
+ Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("productContentTypeId", "IMAGE");
productContentCtx.put("fromDate", UtilDateTime.nowTimestamp());
@@ -202,8 +198,8 @@ public class FrameImage {
result = ServiceUtil.returnError(e.getMessage());
result.putAll(context);
}
-
- Map<String, Object> contentApprovalCtx = new HashMap<String, Object>();
+
+ Map<String, Object> contentApprovalCtx = new HashMap<>();
contentApprovalCtx.put("contentId", contentId);
contentApprovalCtx.put("userLogin", userLogin);
try {
@@ -224,17 +220,17 @@ public class FrameImage {
result = ServiceUtil.returnSuccess(successMsg);
return result;
}
-
+
public static BufferedImage combineBufferedImage(Image image1, Image image2, int bufImgType) {
- // Full image loading
+ // Full image loading
image1 = new ImageIcon(image1).getImage();
image2 = new ImageIcon(image2).getImage();
-
- // New BufferedImage creation
+
+ // 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() / 2f, bufferedImage.getWidth() / 2f);
AffineTransform at = AffineTransform.getTranslateInstance(center.getX( ) - (image2.getWidth(null) / 2f), center.getY( ) - (image2.getHeight(null) / 2f));
@@ -247,16 +243,16 @@ public class FrameImage {
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");
Delegator delegator = dispatcher.getDelegator();
HttpSession session = request.getSession();
GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
-
+
Map<String, ? extends Object> context = UtilGenerics.checkMap(request.getParameterMap());
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", delegator), context);
@@ -276,7 +272,7 @@ public class FrameImage {
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 {
@@ -298,9 +294,9 @@ public class FrameImage {
RandomAccessFile out = new RandomAccessFile(file, "rw");
out.write(imageData.array());
out.close();
-
+
//create dataResource
- Map<String, Object> dataResourceCtx = new HashMap<String, Object>();
+ Map<String, Object> dataResourceCtx = new HashMap<>();
dataResourceCtx.put("objectInfo", imageServerUrl + imagePath);
dataResourceCtx.put("dataResourceName", imageName);
dataResourceCtx.put("userLogin", userLogin);
@@ -310,7 +306,7 @@ public class FrameImage {
Map<String, Object> dataResourceResult = dispatcher.runSync("createDataResource", dataResourceCtx);
dataResourceId = dataResourceResult.get("dataResourceId").toString();
//create content
- Map<String, Object> contentCtx = new HashMap<String, Object>();
+ Map<String, Object> contentCtx = new HashMap<>();
contentCtx.put("dataResourceId", dataResourceResult.get("dataResourceId").toString());
contentCtx.put("contentTypeId", "IMAGE_FRAME");
contentCtx.put("contentName", imageName);
@@ -318,25 +314,22 @@ public class FrameImage {
contentCtx.put("userLogin", userLogin);
Map<String, Object> contentResult = dispatcher.runSync("createContent", contentCtx);
contentId = contentResult.get("contentId").toString();
- } catch (GenericServiceException gse) {
+ } catch (GenericServiceException | IOException gse) {
request.setAttribute("_ERROR_MESSAGE_", gse.getMessage());
return "error";
- } 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 = UtilGenerics.checkMap(request.getParameterMap());
HttpSession session = request.getSession();
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
-
+
String productId = request.getParameter("productId");
String imageName = null;
try {
@@ -346,8 +339,8 @@ public class FrameImage {
request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
return "error";
}
-
-
+
+
String dirPath = "/preview/";
File dir = new File(imageServerPath + dirPath);
if (!dir.exists()) {
@@ -357,14 +350,14 @@ public class FrameImage {
return "error";
}
}
-
+
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 = request.getParameter("frameContentId");
String frameDataResourceId = 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);
@@ -372,7 +365,7 @@ public class FrameImage {
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
-
+
String frameImageName = null;
try {
GenericValue contentDataResourceView = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", frameContentId, "drDataResourceId", frameDataResourceId).queryOne();
@@ -380,34 +373,32 @@ public class FrameImage {
} catch (GenericEntityException e) {
request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
return "error";
- } catch (Exception e) {
- request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
- return "error";
}
if (UtilValidate.isNotEmpty(imageName)) {
File file = new File(imageServerPath + "/preview/" +"/previewImage.jpg");
- if(!file.delete())
+ if(!file.delete()) {
Debug.logError("File :" + file.getName() + ", couldn't be loaded", module);
+ }
// Image Frame
BufferedImage bufImg1 = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + imageName).getCanonicalFile()); // About Findbugs results, see OFBIZ-9973
BufferedImage bufImg2 = ImageIO.read(new File(imageServerPath + "/frame/" + frameImageName));
-
+
int bufImgType;
if (BufferedImage.TYPE_CUSTOM == bufImg1.getType()) {
bufImgType = BufferedImage.TYPE_INT_ARGB_PRE;
} else {
bufImgType = bufImg1.getType();
}
-
+
int width = Integer.parseInt(request.getParameter("imageWidth"));
int height= Integer.parseInt(request.getParameter("imageHeight"));
-
+
Image newImg1 = bufImg1.getScaledInstance(width, height , Image.SCALE_SMOOTH);
Image newImg2 = bufImg2.getScaledInstance(width , height , Image.SCALE_SMOOTH);
BufferedImage bufNewImg = combineBufferedImage(newImg1, newImg2, bufImgType);
String mimeType = imageName.substring(imageName.lastIndexOf('.') + 1);
ImageIO.write(bufNewImg, mimeType, new File(imageServerPath + "/preview/" + "/previewImage.jpg"));
-
+
}
else{
String errMsg = "Please select Image.";
@@ -416,7 +407,7 @@ public class FrameImage {
}
return "success";
}
-
+
public static String chooseFrameImage(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
if(UtilValidate.isEmpty(request.getParameter("frameContentId"))) {
@@ -427,10 +418,10 @@ public class FrameImage {
request.setAttribute("_ERROR_MESSAGE_", "Required frame image content ID");
return "error";
}
-
+
String frameContentId = request.getParameter("frameContentId");
Delegator delegator = (Delegator) request.getAttribute("delegator");
-
+
String frameDataResourceId = null;
try {
GenericValue contentDataResource = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", frameContentId).queryFirst();
@@ -438,22 +429,20 @@ public class FrameImage {
} catch (GenericEntityException e) {
request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
return "error";
- } 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) throws IOException {
Map<String, ? extends Object> context = UtilGenerics.checkMap(request.getParameterMap());
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", (Delegator) context.get("delegator")), context);
File file = new File(imageServerPath + "/preview/" + "/previewImage.jpg").getCanonicalFile();
if (file.exists()) {
- if (!file.delete())
+ if (!file.delete()) {
Debug.logError("File :" + file.getName() + ", couldn't be deleted", module);
+ }
}
return "success";
}
Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementHelper.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementHelper.java?rev=1818411&r1=1818410&r2=1818411&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementHelper.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementHelper.java Sat Dec 16 14:09:52 2017
@@ -38,7 +38,9 @@ public final class ImageManagementHelper
public static String getInternalImageUrl(HttpServletRequest request, String productId) {
String internalImageUrl = null;
- if (request == null) return internalImageUrl;
+ if (request == null) {
+ return internalImageUrl;
+ }
try {
Delegator delegator = (Delegator) request.getAttribute("delegator");
List<GenericValue> defaultImageList = EntityQuery.use(delegator).from("ProductContentAndInfo").where("productId", productId, "productContentTypeId", "DEFAULT_IMAGE", "statusId", "IM_APPROVED", "drIsPublic", "N").orderBy("sequenceNum").queryList();
Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java?rev=1818411&r1=1818410&r2=1818411&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java Sat Dec 16 14:09:52 2017
@@ -60,17 +60,17 @@ import org.jdom.JDOMException;
* Product Services
*/
public class ImageManagementServices {
-
+
public static final String module = ImageManagementServices.class.getName();
public static final String resourceError = "ProductErrorUiLabels";
public static final String resource = "ProductUiLabels";
private static int imageCount = 0;
private static String imagePath;
-
- public static Map<String, Object> addMultipleuploadForProduct(DispatchContext dctx, Map<String, ? extends Object> context)
- throws IOException, JDOMException {
-
- Map<String, Object> result = new HashMap<String, Object>();
+
+ 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");
@@ -81,7 +81,7 @@ public class ImageManagementServices {
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);
@@ -95,13 +95,13 @@ public class ImageManagementServices {
return ServiceUtil.returnError(errMsg);
}
}
-
+
String sizeType = null;
if (UtilValidate.isNotEmpty(imageResize)) {
sizeType = imageResize;
}
-
- Map<String, Object> contentCtx = new HashMap<String, Object>();
+
+ Map<String, Object> contentCtx = new HashMap<>();
contentCtx.put("contentTypeId", "DOCUMENT");
contentCtx.put("userLogin", userLogin);
Map<String, Object> contentResult;
@@ -111,18 +111,18 @@ public class ImageManagementServices {
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);
@@ -134,7 +134,7 @@ public class ImageManagementServices {
return ServiceUtil.returnError(errMsg);
}
}
-
+
File file = new File(imageServerPath + "/" + productId + "/" + uploadFileName);
String imageName = null;
imagePath = imageServerPath + "/" + productId + "/" + uploadFileName;
@@ -145,7 +145,7 @@ public class ImageManagementServices {
} else {
imageName = "";
}
-
+
if (UtilValidate.isEmpty(imageResize)) {
// Create image file original to folder product id.
try {
@@ -154,34 +154,34 @@ public class ImageManagementServices {
out.close();
} catch (FileNotFoundException e) {
Debug.logError(e, module);
- return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
+ 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,
+ return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"ProductImageViewUnableWriteBinaryData", UtilMisc.toMap("fileName", file.getAbsolutePath()), locale));
}
}
- // Scale Image in different sizes
+ // 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,
+ 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,
+ return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
"ProductImageViewUnableWriteBinaryData", UtilMisc.toMap("fileName", fileOriginal.getAbsolutePath()), locale));
}
-
- Map<String, Object> resultResize = new HashMap<String, Object>();
+
+ Map<String, Object> resultResize = new HashMap<>();
try {
resultResize.putAll(scaleImageMangementInAllSize(dctx, context, imageName, sizeType, productId));
} catch (IOException e) {
@@ -196,18 +196,18 @@ public class ImageManagementServices {
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<String, Object>();
+
+ Map<String, Object> createContentAssocMap = new HashMap<>();
createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL");
createContentAssocMap.put("contentId", contentId);
createContentAssocMap.put("contentIdTo", contentIdThumb);
@@ -219,8 +219,8 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> productContentCtx = new HashMap<String, Object>();
+
+ Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("productContentTypeId", productContentTypeId);
productContentCtx.put("fromDate", UtilDateTime.nowTimestamp());
@@ -233,8 +233,8 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> contentApprovalCtx = new HashMap<String, Object>();
+
+ Map<String, Object> contentApprovalCtx = new HashMap<>();
contentApprovalCtx.put("contentId", contentId);
contentApprovalCtx.put("userLogin", userLogin);
try {
@@ -243,10 +243,10 @@ public class ImageManagementServices {
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<String, Object>();
+ Map<String, Object> autoApproveCtx = new HashMap<>();
autoApproveCtx.put("contentId", contentId);
autoApproveCtx.put("userLogin", userLogin);
autoApproveCtx.put("checkStatusId", "IM_APPROVED");
@@ -260,29 +260,30 @@ public class ImageManagementServices {
}
return result;
}
-
+
public static Map<String, Object> removeImageFileForImageManagement(DispatchContext dctx, Map<String, ? extends Object> context){
String productId = (String) context.get("productId");
String contentId = (String) context.get("contentId");
String dataResourceName = (String) context.get("dataResourceName");
Delegator delegator = dctx.getDelegator();
-
+
try {
if (UtilValidate.isNotEmpty(contentId)) {
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
File file = new File(imageServerPath + "/" + productId + "/" + dataResourceName);
- if (!file.delete())
+ if (!file.delete()) {
Debug.logError("File :" + file.getName() + ", couldn't be deleted", module);
+ }
}
} catch (Exception e) {
return ServiceUtil.returnError(e.getMessage());
}
return ServiceUtil.returnSuccess();
}
-
+
private static Map<String, Object> scaleImageMangementInAllSize(DispatchContext dctx, Map<String, ? extends Object> context, String filenameToUse, String resizeType, String productId)
throws IllegalArgumentException, ImagingOpException, IOException, JDOMException {
-
+
/* VARIABLES */
Locale locale = (Locale) context.get("locale");
List<String> sizeTypeList = null;
@@ -291,17 +292,17 @@ public class ImageManagementServices {
} else {
sizeTypeList = UtilMisc.toList("small","100x75", "150x112", "320x240", "640x480", "800x600", "1024x768", "1280x1024", "1600x1200");
}
-
+
int index;
- Map<String, Map<String, String>> imgPropertyMap = new HashMap<String, Map<String, String>>();
+ Map<String, Map<String, String>> imgPropertyMap = new HashMap<>();
BufferedImage bufImg, bufNewImg;
double imgHeight, imgWidth;
- Map<String, String> imgUrlMap = new HashMap<String, String>();
- Map<String, Object> resultXMLMap = new HashMap<String, Object>();
- Map<String, Object> resultBufImgMap = new HashMap<String, Object>();
- Map<String, Object> resultScaleImgMap = new HashMap<String, Object>();
- Map<String, Object> result = new HashMap<String, Object>();
-
+ Map<String, String> imgUrlMap = new HashMap<>();
+ Map<String, Object> resultXMLMap = new HashMap<>();
+ Map<String, Object> resultBufImgMap = new HashMap<>();
+ Map<String, Object> resultScaleImgMap = new HashMap<>();
+ Map<String, Object> result = new HashMap<>();
+
/* ImageProperties.xml */
String fileName = "component://product/config/ImageProperties.xml";
String imgPropertyFullPath = FlexibleLocation.resolveLocation(fileName).getFile();
@@ -314,7 +315,7 @@ public class ImageManagementServices {
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
-
+
/* IMAGE */
// get Name and Extension
index = filenameToUse.lastIndexOf('.');
@@ -322,14 +323,14 @@ public class ImageManagementServices {
// paths
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", dctx.getDelegator()), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", dctx.getDelegator()), context);
-
-
+
+
/* get original BUFFERED IMAGE */
resultBufImgMap.putAll(ImageTransform.getBufferedImage(imageServerPath + "/" + productId + "/" + filenameToUse, locale));
-
+
if (resultBufImgMap.containsKey("responseMessage") && "success".equals(resultBufImgMap.get("responseMessage"))) {
bufImg = (BufferedImage) resultBufImgMap.get("bufferedImage");
-
+
// get Dimensions
imgHeight = bufImg.getHeight();
imgWidth = bufImg.getWidth();
@@ -339,16 +340,16 @@ public class ImageManagementServices {
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
-
+
/* scale Image for each Size Type */
for (String sizeType : sizeTypeList) {
resultScaleImgMap.putAll(ImageTransform.scaleImage(bufImg, imgHeight, imgWidth, imgPropertyMap, sizeType, locale));
-
+
if (resultScaleImgMap.containsKey("responseMessage") && "success".equals(resultScaleImgMap.get("responseMessage"))) {
bufNewImg = (BufferedImage) resultScaleImgMap.get("bufferedImage");
-
+
// write the New Scaled Image
-
+
String targetDirectory = imageServerPath + "/" + productId;
File targetDir = new File(targetDirectory);
if (!targetDir.exists()) {
@@ -359,13 +360,14 @@ public class ImageManagementServices {
return ServiceUtil.returnError(errMsg);
}
}
-
+
// write new image
try {
ImageIO.write(bufNewImg, imgExtension, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
File deleteFile = new File(imageServerPath + "/" + filenameToUse);
- if (!deleteFile.delete())
+ if (!deleteFile.delete()) {
Debug.logError("File :" + deleteFile.getName() + ", couldn't be deleted", module);
+ }
} catch (IllegalArgumentException e) {
String errMsg = UtilProperties.getMessage(resourceError, "ScaleImage.one_parameter_is_null", locale) + e.toString();
Debug.logError(errMsg, module);
@@ -377,41 +379,41 @@ public class ImageManagementServices {
result.put(ModelService.ERROR_MESSAGE, errMsg);
return result;
}
-
+
/* write Return Result */
String imageUrl = imageServerUrl + "/" + productId + "/" + 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(resourceError, "ScaleImage.unable_to_scale_original_image", locale) + " : " + filenameToUse;
- Debug.logError(errMsg, module);
- result.put(ModelService.ERROR_MESSAGE, errMsg);
- return ServiceUtil.returnError(errMsg);
+
}
+ String errMsg = UtilProperties.getMessage(resourceError, "ScaleImage.unable_to_scale_original_image", locale)
+ + " : " + filenameToUse;
+ Debug.logError(errMsg, module);
+ result.put(ModelService.ERROR_MESSAGE, errMsg);
+ return ServiceUtil.returnError(errMsg);
}
-
+
public static Map<String, Object> createContentAndDataResource(DispatchContext dctx, GenericValue userLogin, String filenameToUse, String imageUrl, String contentId, String fileContentType){
- Map<String, Object> result = new HashMap<String, Object>();
+ Map<String, Object> result = new HashMap<>();
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
-
- Map<String, Object> dataResourceCtx = new HashMap<String, Object>();
-
+
+ Map<String, Object> dataResourceCtx = new HashMap<>();
+
dataResourceCtx.put("objectInfo", imageUrl);
dataResourceCtx.put("dataResourceName", filenameToUse);
dataResourceCtx.put("userLogin", userLogin);
dataResourceCtx.put("dataResourceTypeId", "IMAGE_OBJECT");
dataResourceCtx.put("mimeTypeId", fileContentType);
dataResourceCtx.put("isPublic", "Y");
-
+
Map<String, Object> dataResourceResult;
try {
dataResourceResult = dispatcher.runSync("createDataResource", dataResourceCtx);
@@ -419,12 +421,12 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
String dataResourceId = (String) dataResourceResult.get("dataResourceId");
result.put("dataResourceFrameId", dataResourceId);
result.put("dataResourceId", dataResourceId);
-
- Map<String, Object> contentUp = new HashMap<String, Object>();
+
+ Map<String, Object> contentUp = new HashMap<>();
contentUp.put("contentId", contentId);
contentUp.put("dataResourceId", dataResourceResult.get("dataResourceId"));
contentUp.put("contentName", filenameToUse);
@@ -435,7 +437,7 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
GenericValue content = null;
try {
content = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
@@ -443,7 +445,7 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
if (content != null) {
GenericValue dataResource = null;
try {
@@ -452,7 +454,7 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
if (dataResource != null) {
dataResourceCtx.put("dataResourceId", dataResource.getString("dataResourceId"));
try {
@@ -465,17 +467,17 @@ public class ImageManagementServices {
}
return result;
}
-
+
public static Map<String, Object> createContentThumbnail(DispatchContext dctx, Map<String, ? extends Object> context, GenericValue userLogin, ByteBuffer imageData, String productId, String imageName){
- Map<String, Object> result = new HashMap<String, Object>();
+ Map<String, Object> result = new HashMap<>();
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String nameOfThumb = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.nameofthumbnail", delegator), context);
-
+
// Create content for thumbnail
- Map<String, Object> contentThumb = new HashMap<String, Object>();
+ Map<String, Object> contentThumb = new HashMap<>();
contentThumb.put("contentTypeId", "DOCUMENT");
contentThumb.put("userLogin", userLogin);
Map<String, Object> contentThumbResult;
@@ -485,9 +487,9 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
String contentIdThumb = (String) contentThumbResult.get("contentId");
- result.put("contentIdThumb", contentIdThumb);
+ result.put("contentIdThumb", contentIdThumb);
String filenameToUseThumb = imageName.substring(0 , imageName.indexOf('.')) + nameOfThumb;
String fileContentType = (String) context.get("_uploadedFile_contentType");
if ("image/pjpeg".equals(fileContentType)) {
@@ -495,7 +497,7 @@ public class ImageManagementServices {
} else if ("image/x-png".equals(fileContentType)) {
fileContentType = "image/png";
}
-
+
List<GenericValue> fileExtensionThumb;
try {
fileExtensionThumb = EntityQuery.use(delegator).from("FileExtension").where("mimeTypeId", fileContentType).queryList();
@@ -503,10 +505,10 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
GenericValue extensionThumb = EntityUtil.getFirst(fileExtensionThumb);
if (extensionThumb != null) {
-
+
filenameToUseThumb += "." + extensionThumb.getString("fileExtensionId");
}
result.put("filenameToUseThumb", filenameToUseThumb);
@@ -518,35 +520,35 @@ public class ImageManagementServices {
outFileThumb.close();
} catch (FileNotFoundException e) {
Debug.logError(e, module);
- return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
- "ProductImageViewUnableWriteFile",
+ return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
+ "ProductImageViewUnableWriteFile",
UtilMisc.toMap("fileName", fileOriginalThumb.getAbsolutePath()), locale));
} catch (IOException e) {
Debug.logError(e, module);
- return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
- "ProductImageViewUnableWriteBinaryData",
+ return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
+ "ProductImageViewUnableWriteBinaryData",
UtilMisc.toMap("fileName", fileOriginalThumb.getAbsolutePath()), locale));
}
-
+
return result;
}
-
+
public static Map<String, Object> resizeImageThumbnail(BufferedImage bufImg, double imgHeight, double imgWidth) {
-
+
/* VARIABLES */
BufferedImage bufNewImg;
double defaultHeight, defaultWidth, scaleFactor;
- Map<String, Object> result = new HashMap<String, Object>();
-
+ Map<String, Object> result = new HashMap<>();
+
/* DIMENSIONS from ImageProperties */
defaultHeight = 100;
defaultWidth = 100;
-
+
/* SCALE FACTOR */
// find the right Scale Factor related to the Image Dimensions
if (imgHeight > imgWidth) {
scaleFactor = defaultHeight / imgHeight;
-
+
// get scaleFactor from the smallest width
if (defaultWidth < (imgWidth * scaleFactor)) {
scaleFactor = defaultWidth / imgWidth;
@@ -558,7 +560,7 @@ public class ImageManagementServices {
scaleFactor = defaultHeight / imgHeight;
}
}
-
+
int bufImgType;
if (BufferedImage.TYPE_CUSTOM == bufImg.getType()) {
// apply a type for image majority
@@ -566,12 +568,12 @@ public class ImageManagementServices {
} else {
bufImgType = bufImg.getType();
}
-
+
// scale original image with new size
Image newImg = bufImg.getScaledInstance((int) (imgWidth * scaleFactor), (int) (imgHeight * scaleFactor), Image.SCALE_SMOOTH);
-
+
bufNewImg = ImageTransform.toBufferedImage(newImg, bufImgType);
-
+
result.put("bufferedImage", bufNewImg);
result.put("scaleFactor", scaleFactor);
return result;
@@ -589,23 +591,23 @@ public class ImageManagementServices {
file = new File(filePath + "(" + imageCount + ")." + type);
return checkExistsImage(file);
}
-
+
public static Map<String, Object> resizeImage(BufferedImage bufImg, double imgHeight, double imgWidth, double resizeHeight, double resizeWidth) {
-
+
/* VARIABLES */
BufferedImage bufNewImg;
double defaultHeight, defaultWidth, scaleFactor;
- Map<String, Object> result = new HashMap<String, Object>();
-
+ Map<String, Object> result = new HashMap<>();
+
/* DIMENSIONS from ImageProperties */
defaultHeight = resizeHeight;
defaultWidth = resizeWidth;
-
+
/* SCALE FACTOR */
// find the right Scale Factor related to the Image Dimensions
if (imgHeight > imgWidth) {
scaleFactor = defaultHeight / imgHeight;
-
+
// get scaleFactor from the smallest width
if (defaultWidth < (imgWidth * scaleFactor)) {
scaleFactor = defaultWidth / imgWidth;
@@ -617,7 +619,7 @@ public class ImageManagementServices {
scaleFactor = defaultHeight / imgHeight;
}
}
-
+
int bufImgType;
if (BufferedImage.TYPE_CUSTOM == bufImg.getType()) {
// apply a type for image majority
@@ -625,17 +627,17 @@ public class ImageManagementServices {
} else {
bufImgType = bufImg.getType();
}
-
+
// scale original image with new size
Image newImg = bufImg.getScaledInstance((int) (imgWidth * scaleFactor), (int) (imgHeight * scaleFactor), Image.SCALE_SMOOTH);
-
+
bufNewImg = ImageTransform.toBufferedImage(newImg, bufImgType);
-
+
result.put("bufferedImage", bufNewImg);
result.put("scaleFactor", scaleFactor);
return result;
}
-
+
public static Map<String, Object> createNewImageThumbnail(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dispatcher.getDelegator();
@@ -650,7 +652,7 @@ public class ImageManagementServices {
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();
@@ -658,15 +660,15 @@ public class ImageManagementServices {
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<String, Object>();
+
+ Map<String, Object> contentThumb = new HashMap<>();
contentThumb.put("contentTypeId", "DOCUMENT");
contentThumb.put("userLogin", userLogin);
Map<String, Object> contentThumbResult;
@@ -676,12 +678,12 @@ public class ImageManagementServices {
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<String, Object>();
+
+ Map<String, Object> createContentAssocMap = new HashMap<>();
createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL");
createContentAssocMap.put("contentId", contentId);
createContentAssocMap.put("contentIdTo", contentIdThumb);
@@ -701,7 +703,7 @@ public class ImageManagementServices {
String successMsg = UtilProperties.getMessage(resource, "ProductCreateNewThumbnailSizeSuccessful", locale);
return ServiceUtil.returnSuccess(successMsg);
}
-
+
public static Map<String, Object> resizeImageOfProduct(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale)context.get("locale");
@@ -711,7 +713,7 @@ public class ImageManagementServices {
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();
@@ -727,7 +729,7 @@ public class ImageManagementServices {
String successMsg = UtilProperties.getMessage(resource, "ProductResizeImagesSuccessful", locale);
return ServiceUtil.returnSuccess(successMsg);
}
-
+
public static Map<String, Object> renameImage(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
@@ -741,21 +743,22 @@ public class ImageManagementServices {
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())
+ if (!file.delete()) {
Debug.logError("File :" + file.getName() + ", couldn't be deleted", module);
-
- Map<String, Object> contentUp = new HashMap<String, Object>();
+ }
+
+ Map<String, Object> contentUp = new HashMap<>();
contentUp.put("contentId", contentId);
contentUp.put("contentName", filenameToUse);
contentUp.put("userLogin", userLogin);
@@ -780,9 +783,9 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
if (dataResource != null) {
- Map<String, Object> dataResourceCtx = new HashMap<String, Object>();
+ Map<String, Object> dataResourceCtx = new HashMap<>();
dataResourceCtx.put("dataResourceId", dataResource.getString("dataResourceId"));
dataResourceCtx.put("objectInfo", imageUrl);
dataResourceCtx.put("dataResourceName", filenameToUse);
@@ -795,27 +798,28 @@ public class ImageManagementServices {
}
}
}
-
+
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())
+ if (!fileAssoc.delete()) {
Debug.logError("File :" + fileAssoc.getName() + ", couldn't be deleted", module);
-
- Map<String, Object> contentAssocMap = new HashMap<String, Object>();
+ }
+
+ Map<String, Object> contentAssocMap = new HashMap<>();
contentAssocMap.put("contentId", contentAssoc.get("contentIdTo"));
contentAssocMap.put("contentName", filenameToUseAssoc);
contentAssocMap.put("userLogin", userLogin);
@@ -840,9 +844,9 @@ public class ImageManagementServices {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
+
if (dataResourceAssocUp != null) {
- Map<String, Object> dataResourceAssocMap = new HashMap<String, Object>();
+ Map<String, Object> dataResourceAssocMap = new HashMap<>();
dataResourceAssocMap.put("dataResourceId", dataResourceAssocUp.getString("dataResourceId"));
dataResourceAssocMap.put("objectInfo", imageUrlAssoc);
dataResourceAssocMap.put("dataResourceName", filenameToUseAssoc);
Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageUrlServlet.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageUrlServlet.java?rev=1818411&r1=1818410&r2=1818411&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageUrlServlet.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageUrlServlet.java Sat Dec 16 14:09:52 2017
@@ -64,19 +64,19 @@ public class ImageUrlServlet extends Htt
String pathInfo = request.getPathInfo();
List<String> pathElements = StringUtil.split(pathInfo, "/");
-
- List<String> tagElements = new LinkedList<String>();
+
+ List<String> tagElements = new LinkedList<>();
for (String pathElement : pathElements) {
tagElements.addAll(StringUtil.split(pathElement, "-"));
}
-
+
String lastTagElement = tagElements.get(tagElements.size() - 1);
String contentId = lastTagElement.substring(0, lastTagElement.lastIndexOf('.'));
String sizeTagElement = null;
if(tagElements.size() > 2){
sizeTagElement = tagElements.get(tagElements.size() - 2);
}
-
+
GenericValue content = null;
try {
GenericValue contentResult = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
@@ -88,7 +88,7 @@ public class ImageUrlServlet extends Htt
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
-
+
if (content != null) {
GenericValue dataResource = null;
try {
Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ReplaceImage.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ReplaceImage.java?rev=1818411&r1=1818410&r2=1818411&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ReplaceImage.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ReplaceImage.java Sat Dec 16 14:09:52 2017
@@ -59,7 +59,7 @@ public class ReplaceImage{
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)) {
@@ -79,22 +79,22 @@ public class ReplaceImage{
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")));
@@ -105,11 +105,11 @@ public class ReplaceImage{
}
}
}
-
+
GenericValue productContent = EntityQuery.use(delegator).from("ProductContent").where("productId", productId, "contentId", contentIdReplace, "productContentTypeId", "IMAGE").queryFirst();
-
+
if (productContent != null) {
- Map<String, Object> productContentCtx = new HashMap<String, Object>();
+ Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("contentId", contentIdReplace);
productContentCtx.put("productContentTypeId", "IMAGE");
@@ -117,11 +117,7 @@ public class ReplaceImage{
productContentCtx.put("userLogin", userLogin);
dispatcher.runSync("removeProductContentAndImageFile", productContentCtx);
}
- } catch (GenericEntityException | GenericServiceException e) {
- String errMsg = UtilProperties.getMessage(resourceError, "ProductCannotReplaceImage", locale);
- Debug.logError(e, errMsg, module);
- return ServiceUtil.returnError(errMsg);
- } catch (IOException | IllegalArgumentException e) {
+ } catch (IOException | IllegalArgumentException | GenericEntityException | GenericServiceException e) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductCannotReplaceImage", locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
Modified: ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/RotateImage.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/RotateImage.java?rev=1818411&r1=1818410&r2=1818411&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/RotateImage.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/RotateImage.java Sat Dec 16 14:09:52 2017
@@ -41,7 +41,6 @@ import org.apache.ofbiz.service.Dispatch
import org.apache.ofbiz.service.GenericServiceException;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.service.ServiceUtil;
-import org.jdom.JDOMException;
public class RotateImage {
@@ -51,19 +50,19 @@ public class RotateImage {
public static final String resource = "ProductFUiLabels";
public static Map<String, Object> imageRotate(DispatchContext dctx, Map<String, ? extends Object> context)
- throws IOException, JDOMException {
+ throws IOException {
Locale locale = (Locale)context.get("locale");
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String nameOfThumb = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.nameofthumbnail", delegator), context);
-
+
String productId = (String) context.get("productId");
String imageName = (String) context.get("imageName");
String angle = (String) context.get("angle");
-
+
if (UtilValidate.isNotEmpty(imageName)) {
- Map<String, Object> contentCtx = new HashMap<String, Object>();
+ Map<String, Object> contentCtx = new HashMap<>();
contentCtx.put("contentTypeId", "DOCUMENT");
contentCtx.put("userLogin", userLogin);
Map<String, Object> contentResult;
@@ -73,8 +72,8 @@ public class RotateImage {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> contentThumb = new HashMap<String, Object>();
+
+ Map<String, Object> contentThumb = new HashMap<>();
contentThumb.put("contentTypeId", "DOCUMENT");
contentThumb.put("userLogin", userLogin);
Map<String, Object> contentThumbResult;
@@ -84,23 +83,23 @@ public class RotateImage {
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) contentResult.get("contentId") + nameOfThumb + ".jpg";
-
+
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", delegator), context);
BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + imageName));
-
+
int bufImgType;
if (BufferedImage.TYPE_CUSTOM == bufImg.getType()) {
bufImgType = BufferedImage.TYPE_INT_ARGB_PRE;
} else {
bufImgType = bufImg.getType();
}
-
+
int w = bufImg.getWidth(null);
int h = bufImg.getHeight(null);
BufferedImage bufNewImg = new BufferedImage(w, h, bufImgType);
@@ -108,23 +107,23 @@ public class RotateImage {
g.rotate(Math.toRadians(Double.parseDouble(angle)), w / 2.0, h / 2.0);
g.drawImage(bufImg,0,0,null);
g.dispose();
-
+
String mimeType = imageName.substring(imageName.lastIndexOf('.') + 1);
ImageIO.write(bufNewImg, mimeType, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
-
+
double imgHeight = bufNewImg.getHeight();
double imgWidth = bufNewImg.getWidth();
-
+
Map<String, Object> resultResize = ImageManagementServices.resizeImageThumbnail(bufNewImg, imgHeight, imgWidth);
ImageIO.write((RenderedImage) resultResize.get("bufferedImage"), mimeType, new File(imageServerPath + "/" + productId + "/" + filenameTouseThumb));
-
+
String imageUrlResource = imageServerUrl + "/" + productId + "/" + filenameToUse;
String imageUrlThumb = imageServerUrl + "/" + productId + "/" + filenameTouseThumb;
-
+
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameToUse, imageUrlResource, contentId, "image/jpeg");
ImageManagementServices.createContentAndDataResource(dctx, userLogin, filenameTouseThumb, imageUrlThumb, contentIdThumb, "image/jpeg");
-
- Map<String, Object> createContentAssocMap = new HashMap<String, Object>();
+
+ Map<String, Object> createContentAssocMap = new HashMap<>();
createContentAssocMap.put("contentAssocTypeId", "IMAGE_THUMBNAIL");
createContentAssocMap.put("contentId", contentId);
createContentAssocMap.put("contentIdTo", contentIdThumb);
@@ -136,8 +135,8 @@ public class RotateImage {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> productContentCtx = new HashMap<String, Object>();
+
+ Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("productContentTypeId", "IMAGE");
productContentCtx.put("fromDate", UtilDateTime.nowTimestamp());
@@ -150,8 +149,8 @@ public class RotateImage {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
-
- Map<String, Object> contentApprovalCtx = new HashMap<String, Object>();
+
+ Map<String, Object> contentApprovalCtx = new HashMap<>();
contentApprovalCtx.put("contentId", contentId);
contentApprovalCtx.put("userLogin", userLogin);
try {