Clover icon

Magnolia Imaging Module Compatibility 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:37:35 EEST
  2. Package info.magnolia.imaging.operations.cropresize

File SelectedCropAndResize.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
45% of files have more coverage

Code metrics

4
16
3
1
110
54
6
0.38
5.33
3
2
20.7% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
SelectedCropAndResize 63 16 20.7% 6 17
0.2608695626.1%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2009-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.operations.cropresize;
35   
36    import info.magnolia.cms.core.Content;
37    import info.magnolia.cms.core.NodeData;
38    import info.magnolia.imaging.ImagingException;
39    import info.magnolia.imaging.ParameterProvider;
40   
41    import java.awt.image.BufferedImage;
42    import java.util.Collections;
43    import java.util.Map;
44   
45    import javax.jcr.RepositoryException;
46   
47    import org.apache.commons.lang3.StringUtils;
48   
49    import net.sf.json.JSONObject;
50   
51    /**
52    * A CropAndResize implementation which uses coordinates as selected in a UI component. This currently
53    * expects a JSon string representation of CroppingInfo (wrapped in map, which is something that might change...)
54    *
55    * Given the goo that's Magnolia's current "hiding" of binary nodes, disguising them as properties,
56    * it is not possible (?) for a dialog to be configured to store more properties *inside* an image's
57    * node; so we resort to 'cropInfoSiblingPropertySuffix': we will look for a property that has the
58    * same node as the ParameterProvider's node, suffixed, and which is a *sibling* of the image's node,
59    * not a children.
60    * @deprecated since 3.4 with no replacement.
61    */
62    @Deprecated
 
63    public class SelectedCropAndResize extends AbstractCropAndResize<ParameterProvider<Content>> {
64    private int targetWidth;
65    private int targetHeight;
66    private String cropInfoSiblingPropertySuffix;
67   
 
68  0 toggle @Override
69    protected Coords getCroopCoords(BufferedImage source, ParameterProvider<Content> params) throws ImagingException {
70  0 try {
71  0 final Content imageNode = params.getParameter();
72  0 final String cropInfoPropertyName = imageNode.getName() + cropInfoSiblingPropertySuffix;
73  0 final Content parent = imageNode.getParent();
74  0 if (!parent.hasNodeData(cropInfoPropertyName)) {
75  0 throw new ImagingException("There is no '" + cropInfoPropertyName + "' property at " + parent.getHandle());
76    }
77  0 final NodeData cropInfoND = parent.getNodeData(cropInfoPropertyName);
78  0 final CroppingInfo cropInfo = decode(cropInfoND.getString());
79  0 return cropInfo.getCoords();
80    } catch (RepositoryException e) {
81  0 throw new RuntimeException(e);
82    }
83    }
84   
 
85  0 toggle @Override
86    protected Size getEffectiveTargetSize(BufferedImage source, Coords cropCoords, ParameterProvider<Content> params) {
87  0 return Size.conformToCropRatio(cropCoords, targetWidth, targetHeight);
88    }
89   
 
90    toggle public void setCropInfoSiblingPropertySuffix(String cropInfoSiblingPropertySuffix) {
91    this.cropInfoSiblingPropertySuffix = cropInfoSiblingPropertySuffix;
92    }
93   
 
94    toggle public void setTargetWidth(int targetWidth) {
95    this.targetWidth = targetWidth;
96    }
97   
 
98    toggle public void setTargetHeight(int targetHeight) {
99    this.targetHeight = targetHeight;
100    }
101   
 
102  1 toggle protected CroppingInfo decode(String jsonString) {
103  1 if (StringUtils.isEmpty(jsonString)) {
104  0 return null;
105    }
106  1 final JSONObject json = JSONObject.fromObject(jsonString);
107  1 final Map map = (Map) JSONObject.toBean(json, Map.class, Collections.singletonMap("CropperInfo", CroppingInfo.class));
108  1 return (CroppingInfo) map.get("CropperInfo");
109    }
110    }