View Javadoc
1   /**
2    * This file Copyright (c) 2015 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.models.components;
35  
36  import info.magnolia.dam.api.Asset;
37  import info.magnolia.dam.api.AssetRendition;
38  import info.magnolia.dam.templating.functions.DamTemplatingFunctions;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.jcr.util.PropertyUtil;
42  import info.magnolia.rendering.model.RenderingModel;
43  import info.magnolia.rendering.template.RenderableDefinition;
44  import info.magnolia.repository.RepositoryConstants;
45  import info.magnolia.templating.functions.TemplatingFunctions;
46  
47  import javax.inject.Inject;
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  
51  import org.apache.commons.lang3.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * Teaser model to link to pages in the workspace {@link RepositoryConstants#WEBSITE}.
57   *
58   * @param <RD> The renderable definition.
59   */
60  public class TeaserInternalModel<RD extends RenderableDefinition> extends AbstractTeaserModel {
61  
62      private static final Logger log = LoggerFactory.getLogger(TeaserInternalModel.class);
63  
64      protected final static String PROPERTY_NAME_PAGE_TITLE = "title";
65      protected final static String PROPERTY_NAME_PAGE_ABSTRACT = "abstract";
66  
67      @Inject
68      public TeaserInternalModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions, DamTemplatingFunctions damTemplatingFunctions) {
69          super(content, definition, parent, templatingFunctions, damTemplatingFunctions);
70      }
71  
72      /**
73       * Return the {@link Node} referenced by the content.
74       */
75      protected Node getTarget() {
76          final String targetNodeIdentifier = PropertyUtil.getString(content, PROPERTY_NAME_LINK);
77  
78          if (targetNodeIdentifier != null) {
79              final String path = NodeUtil.getPathIfPossible(content);
80              try {
81                  final Node target = templatingFunctions.nodeById(targetNodeIdentifier, getRepository());
82                  if (target != null) {
83                      if (target.isNodeType(NodeTypes.Deleted.NAME)) {
84                          return null;
85                      }
86  
87                      final Node targetWrappedEncoded = templatingFunctions.encode(templatingFunctions.wrapForI18n(target));
88  
89                      return targetWrappedEncoded;
90                  } else {
91                      log.warn("Could not retrieve item for source node '{}' referenced by '{}' property", path, PROPERTY_NAME_LINK);
92                  }
93              } catch (RepositoryException e) {
94                  log.warn("Could not retrieve item for source node '{}' referenced by '{}' property", path, PROPERTY_NAME_LINK, e);
95              }
96          }
97  
98          return null;
99      }
100 
101     /**
102      * Returns the rendition of image defined in the target intro/header. If none is defined,
103      * then it returns the rendition of the first image found in the content of the target.
104      */
105     @Override
106     public AssetRendition getImage() {
107         final AssetRendition rendition = super.getImage();
108         if (rendition != null) {
109             return rendition;
110         }
111 
112         final String targetNodeIdentifier = PropertyUtil.getString(content, PROPERTY_NAME_LINK);
113         if (targetNodeIdentifier != null) {
114             final Node target = templatingFunctions.nodeById(targetNodeIdentifier, getRepository());
115             if (target != null) {
116                 final Asset image = findImage(target);
117                 if (image != null) {
118                     return damTemplatingFunctions.getRendition(image, getImageVariationName());
119                 }
120             }
121         }
122 
123         return null;
124     }
125 
126     /**
127      * Returns the image defined in the target intro/header. If none is defined,
128      * then it returns the first image found in the content of the target.
129      */
130     protected Asset findImage(Node target) {
131         if (target != null) {
132             final String assetIdentifier = PropertyUtil.getString(target, getImageName());
133             if (StringUtils.isNotBlank(assetIdentifier)) {
134                 return damTemplatingFunctions.getAsset(assetIdentifier);
135             }
136 
137             try {
138                 for (final Node child : NodeUtil.getNodes(target)) {
139                     return findImage(child);
140                 }
141             } catch (RepositoryException e) {
142                 log.error("Could not get nodes for target node [{}]", target, e);
143             }
144         }
145 
146         return null;
147     }
148 
149     /**
150      * Returns a link to the target page.
151      *
152      * @see info.magnolia.templating.functions.TemplatingFunctions#link(String, String)
153      */
154     @Override
155     public String getTeaserLink() {
156         final String targetNodeIdentifier = PropertyUtil.getString(content, PROPERTY_NAME_LINK);
157         return templatingFunctions.link(getRepository(), targetNodeIdentifier);
158     }
159 
160     /**
161      * Returns the title of the teaser. If not set, it tries to determine the title or node name of the target page.
162      */
163     @Override
164     public String getTeaserTitle() {
165         String teaserTitle = super.getTeaserTitle();
166 
167         if (StringUtils.isBlank(teaserTitle)) {
168             final Node target = getTarget();
169             if (target != null) {
170                 teaserTitle = PropertyUtil.getString(target, PROPERTY_NAME_PAGE_TITLE);
171                 if (StringUtils.isBlank(teaserTitle)) {
172                     teaserTitle = NodeUtil.getName(target);
173                 }
174             }
175         }
176 
177         return teaserTitle;
178     }
179 
180     /**
181      * Returns the teaser text and if not specified will try to determine an <code>abstract</code> of the target page.
182      */
183     @Override
184     public String getTeaserText() {
185         String teaserText = super.getTeaserText();
186 
187         if (StringUtils.isBlank(teaserText)) {
188             final Node target = getTarget();
189             if (target != null) {
190                 teaserText = PropertyUtil.getString(target, PROPERTY_NAME_PAGE_ABSTRACT);
191             }
192         }
193 
194         return teaserText;
195     }
196 
197     /**
198      * Internal links should always open in same window.
199      */
200     @Override
201     public boolean isTargetBlank() {
202         return false;
203     }
204 
205     /**
206      * Returns the repository name of the target {@link Node}.
207      */
208     protected static String getRepository() {
209         return RepositoryConstants.WEBSITE;
210     }
211 
212 }