View Javadoc
1   /**
2    * This file Copyright (c) 2015-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.templating.imaging.parameters;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.jcr.util.PropertyUtil;
38  import info.magnolia.module.site.SiteModule;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.templating.imaging.VariationAwareImagingSupport;
41  import info.magnolia.templating.imaging.variation.ImageOperationProvidingVariation;
42  
43  import javax.jcr.Binary;
44  import javax.jcr.Node;
45  import javax.jcr.RepositoryException;
46  
47  import org.apache.jackrabbit.JcrConstants;
48  
49  /**
50   * Parameter implementation which is {@link info.magnolia.module.site.theme.Theme}-aware.
51   */
52  public class ThemeAwareParameter {
53  
54      private final String themeName;
55  
56      private final String variationName;
57  
58      private final Node node;
59  
60      public ThemeAwareParameter(String themeName, String variationName, Node node) {
61          this.themeName = themeName;
62          this.variationName = variationName;
63          this.node = node;
64      }
65  
66      public ImageOperationProvidingVariation getImageVariation() {
67          final VariationAwareImagingSupport imaging = (VariationAwareImagingSupport) Components.getComponent(SiteModule.class).getTheme(themeName).getImaging();
68          return (ImageOperationProvidingVariation) imaging.getVariations().get(variationName);
69      }
70  
71      public Node getNode() {
72          return node;
73      }
74  
75      public Binary getBinary() throws RepositoryException {
76          return node.getProperty(JcrConstants.JCR_DATA).getBinary();
77      }
78  
79      public String getExtension() {
80          // TODO if extension property isn't found log error and return null instead of empty string? Or return jpg?
81          return PropertyUtil.getString(node, FileProperties.EXTENSION, "");
82      }
83  
84      /**
85       * {@link info.magnolia.module.site.theme.Theme}-aware cache path for imaging.
86       *
87       * @see info.magnolia.templating.imaging.parameters.ThemeAwareParameterCachingStrategy
88       */
89      public String getCachePath() {
90          try {
91              return "/" + themeName + "/" + variationName + "/" + node.getSession().getWorkspace().getName() + node.getPath();
92          } catch (RepositoryException e) {
93              // TODO log error and return null?
94              return "";
95          }
96      }
97  
98      @Override
99      public boolean equals(Object o) {
100         if (this == o) return true;
101         if (o == null || getClass() != o.getClass()) return false;
102 
103         ThemeAwareParameter that = (ThemeAwareParameter) o;
104 
105         if (!node.equals(that.node)) return false;
106         if (!themeName.equals(that.themeName)) return false;
107         if (!variationName.equals(that.variationName)) return false;
108 
109         return true;
110     }
111 
112     @Override
113     public int hashCode() {
114         int result = themeName.hashCode();
115         result = 31 * result + variationName.hashCode();
116         result = 31 * result + node.hashCode();
117         return result;
118     }
119 }