View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.freemarker.models;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.cms.util.ContentUtil;
39  
40  import java.util.Collection;
41  import java.util.Iterator;
42  
43  import javax.jcr.RepositoryException;
44  
45  import org.apache.commons.collections4.IteratorUtils;
46  import org.apache.commons.collections4.Transformer;
47  
48  import freemarker.template.AdapterTemplateModel;
49  import freemarker.template.ObjectWrapper;
50  import freemarker.template.SimpleCollection;
51  import freemarker.template.TemplateCollectionModel;
52  import freemarker.template.TemplateHashModelEx;
53  import freemarker.template.TemplateModel;
54  import freemarker.template.TemplateModelException;
55  import freemarker.template.TemplateNodeModel;
56  import freemarker.template.TemplateScalarModel;
57  import freemarker.template.TemplateSequenceModel;
58  
59  /**
60   * A wrapper for Content nodes. Exposes properties using an HashModel (.property, ?size, ...)
61   * a hierarchy (TemplateNodeModel: ?children, ?parent, ...)
62   * and as a scalar (returns the node name)
63   */
64  public class ContentModel implements TemplateHashModelEx, TemplateNodeModel, TemplateScalarModel, AdapterTemplateModel {
65      static final MagnoliaModelFactory FACTORY = new MagnoliaModelFactory() {
66          @Override
67          public Class factoryFor() {
68              return Content.class;
69          }
70  
71          @Override
72          public AdapterTemplateModel create(Object object, ObjectWrapper wrapper) {
73              return new ContentModel((Content) object, (MagnoliaObjectWrapper) wrapper);
74          }
75      };
76  
77      private final Content content;
78      private final MagnoliaObjectWrapper wrapper;
79  
80      ContentModel(Content content, MagnoliaObjectWrapper wrapper) {
81          this.content = content;
82          this.wrapper = wrapper;
83      }
84  
85      @Override
86      public String getAsString() {
87          return content.getName();
88      }
89  
90      @Override
91      public TemplateModel get(String key) throws TemplateModelException {
92          final Object result;
93  
94          try {
95              if (key.equals("@handle")) {
96                  result = content.getHandle();
97              } else if (key.equals("@uuid")) {
98                  result = content.getUUID();
99              } else if (key.equals("@name")) {
100                 result = content.getName();
101             } else if (key.equals("@level") || key.equals("@depth")) {
102                 result = content.getLevel();
103             } else if (key.equalsIgnoreCase("metaData")) {
104                 result = content.getMetaData();
105             } else {
106                 // try for node data or child node
107                 if (content.hasNodeData(key)) {
108                     result = content.getNodeData(key);
109                 } else {
110                     if (content.hasContent(key)) {
111                         result = content.getContent(key);
112                     } else {
113                         result = null;
114                     }
115                 }
116             }
117         } catch (RepositoryException e) {
118             throw new TemplateModelException(e);
119         }
120         return wrapper.wrap(result);
121     }
122 
123     @Override
124     public boolean isEmpty() throws TemplateModelException {
125         return (size() == 0);
126     }
127 
128     @Override
129     public int size() throws TemplateModelException {
130         return content.getNodeDataCollection().size();
131     }
132 
133     @Override
134     public TemplateCollectionModel keys() throws TemplateModelException {
135         final Iterator it = IteratorUtils.transformedIterator(content.getNodeDataCollection().iterator(), new Transformer() {
136             @Override
137             public Object transform(Object input) {
138                 return ((NodeData) input).getName();
139             }
140         });
141         return new SimpleCollection(it);
142     }
143 
144     @Override
145     public TemplateCollectionModel values() throws TemplateModelException {
146         return (TemplateCollectionModel) wrapper.wrap(content.getNodeDataCollection().iterator());
147     }
148 
149     @Override
150     public TemplateNodeModel getParentNode() throws TemplateModelException {
151         try {
152             // todo : check if this is the root?
153             // content.getLevel() == 0;
154             final Content parent = content.getParent();
155             return (TemplateNodeModel) wrapper.wrap(parent);
156         } catch (RepositoryException e) {
157             throw new TemplateModelException("Can't get parent of " + content + ":" + e.getMessage(), e);
158         }
159     }
160 
161     /**
162      * This returns all children, except nodes or jcr: types and mgnl:metaData.
163      */
164     @Override
165     public TemplateSequenceModel getChildNodes() throws TemplateModelException {
166         final Collection<Content> children = ContentUtil.getAllChildren(content);
167         return (TemplateSequenceModel) wrapper.wrap(children);
168     }
169 
170     @Override
171     public String getNodeName() throws TemplateModelException {
172         return content.getName();
173     }
174 
175     @Override
176     public String getNodeType() throws TemplateModelException {
177         try {
178             return content.getNodeTypeName();
179         } catch (RepositoryException e) {
180             throw new TemplateModelException("Can't get node type of " + content + ":" + e.getMessage(), e);
181         }
182     }
183 
184     @Override
185     public String getNodeNamespace() throws TemplateModelException {
186         return null; // non XML implementation
187     }
188 
189     public Content asContent() {
190         return this.content;
191     }
192 
193     @Override
194     public Object getAdaptedObject(Class hint) {
195         return asContent();
196     }
197 }