View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 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.module.templatingkit.templates.components;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.cms.util.ContentUtil;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.jcr.util.PropertyUtil;
40  import info.magnolia.jcr.wrapper.I18nNodeWrapper;
41  import info.magnolia.module.dms.DMSModule;
42  import info.magnolia.module.dms.beans.Document;
43  import info.magnolia.module.templatingkit.STKModule;
44  import info.magnolia.module.templatingkit.dam.Asset;
45  import info.magnolia.module.templatingkit.dam.DAMException;
46  import info.magnolia.module.templatingkit.dam.DAMHandler;
47  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
48  import info.magnolia.module.templatingkit.templates.AbstractSTKTemplateModel;
49  import info.magnolia.rendering.model.RenderingModel;
50  import info.magnolia.rendering.template.TemplateDefinition;
51  import info.magnolia.templating.functions.TemplatingFunctions;
52  
53  import java.util.ArrayList;
54  import java.util.List;
55  
56  import javax.inject.Inject;
57  import javax.jcr.ItemNotFoundException;
58  import javax.jcr.Node;
59  import javax.jcr.RepositoryException;
60  
61  import org.apache.commons.lang.ArrayUtils;
62  import org.slf4j.Logger;
63  import org.slf4j.LoggerFactory;
64  
65  /**
66   * STK Renderable Model definition dedicated to Image Gallery display.
67   * 
68   * @param <RD> the TemplateDefinition this model is operating on.
69   * @version $Id$
70   */
71  public class ImageGalleryParagraphModel<RD extends TemplateDefinition> extends AbstractSTKTemplateModel<TemplateDefinition> {
72  
73      private static Logger log = LoggerFactory.getLogger(ImageGalleryParagraphModel.class);
74  
75      protected static final String[] ALLOWED_IMAGE_TYPES = new String[] { "gif", "jpg", "jpeg", "png" };
76  
77      private STKModule stkModule;
78  
79      @Inject
80      public ImageGalleryParagraphModel(Node content, RD definition, RenderingModel<?> parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions, STKModule stkModule) {
81          super(content, definition, parent, stkFunctions, templatingFunctions);
82          this.stkModule = stkModule;
83      }
84  
85      public List<Asset> getImages() throws DAMException {
86          List<Asset> images = new ArrayList<Asset>();
87          List<String> keys = getKeys();
88  
89          DAMHandler handler = getDAMHandler();
90          for (String key : keys) {
91              Asset asset = handler.getAssetByKey(key);
92              images.add(asset);
93          }
94          return images;
95      }
96  
97      protected DAMHandler getDAMHandler() {
98          return stkModule.getDamSupport().getHandler(DMSModule.getInstance().getRepository());
99      }
100 
101     protected List<String> getKeys() {
102         // can't use former code because STKFunctions#getReferencedContent is throwing RuntimeRepositoryException which is hiding real cause
103         Node dmsFolder = null;
104         try {
105             if (content.hasProperty("link")) {
106                 String identifier = PropertyUtil.getString(content, "link");
107                 dmsFolder = NodeUtil.getNodeByIdentifier("dms", identifier);
108             }
109         } catch (ItemNotFoundException e) {
110             // ignore - referenced content doesn't exist - we will not print any images
111         } catch (RepositoryException e) {
112             log.error("Error while obtaining node referenced node", e);
113         }
114         List<String> keys = new ArrayList<String>();
115         if (dmsFolder == null) {
116             return keys;
117         }
118         dmsFolder = new I18nNodeWrapper(dmsFolder);
119         final Iterable<Node> imageChildren;
120         try {
121             imageChildren = NodeUtil.getNodes(dmsFolder, MgnlNodeType.NT_CONTENTNODE);
122         } catch (RepositoryException e) {
123             log.error("", e);
124             return keys;
125         }
126 
127         for (Node imageNode : imageChildren) {
128             if (showImage(new Document(ContentUtil.asContent(imageNode)))) {
129                 try {
130                     keys.add(imageNode.getIdentifier());
131                 } catch (RepositoryException e) {
132                     log.error("", e);
133                 }
134             }
135         }
136         return keys;
137     }
138 
139     protected boolean showImage(Document doc) {
140         return ArrayUtils.contains(ALLOWED_IMAGE_TYPES, doc.getFileExtension().toLowerCase());
141     }
142 }