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   * @deprecated since 5.6 - use {@link ContentMapModel} instead.
65   */
66  @Deprecated
67  public class ContentModel implements TemplateHashModelEx, TemplateNodeModel, TemplateScalarModel, AdapterTemplateModel {
68      static final MagnoliaModelFactory FACTORY = new MagnoliaModelFactory() {
69          @Override
70          public Class factoryFor() {
71              return Content.class;
72          }
73  
74          @Override
75          public AdapterTemplateModel create(Object object, ObjectWrapper wrapper) {
76              return new ContentModel((Content) object, (MagnoliaObjectWrapper) wrapper);
77          }
78      };
79  
80      private final Content content;
81      private final MagnoliaObjectWrapper wrapper;
82  
83      ContentModel(Content content, MagnoliaObjectWrapper wrapper) {
84          this.content = content;
85          this.wrapper = wrapper;
86      }
87  
88      @Override
89      public String getAsString() {
90          return content.getName();
91      }
92  
93      @Override
94      public TemplateModel get(String key) throws TemplateModelException {
95          final Object result;
96  
97          try {
98              if (key.equals("@handle")) {
99                  result = content.getHandle();
100             } else if (key.equals("@uuid")) {
101                 result = content.getUUID();
102             } else if (key.equals("@name")) {
103                 result = content.getName();
104             } else if (key.equals("@level") || key.equals("@depth")) {
105                 result = content.getLevel();
106             } else if (key.equalsIgnoreCase("metaData")) {
107                 result = content.getMetaData();
108             } else {
109                 // try for node data or child node
110                 if (content.hasNodeData(key)) {
111                     result = content.getNodeData(key);
112                 } else {
113                     if (content.hasContent(key)) {
114                         result = content.getContent(key);
115                     } else {
116                         result = null;
117                     }
118                 }
119             }
120         } catch (RepositoryException e) {
121             throw new TemplateModelException(e);
122         }
123         return wrapper.wrap(result);
124     }
125 
126     @Override
127     public boolean isEmpty() throws TemplateModelException {
128         return (size() == 0);
129     }
130 
131     @Override
132     public int size() throws TemplateModelException {
133         return content.getNodeDataCollection().size();
134     }
135 
136     @Override
137     public TemplateCollectionModel keys() throws TemplateModelException {
138         final Iterator it = IteratorUtils.transformedIterator(content.getNodeDataCollection().iterator(), new Transformer() {
139             @Override
140             public Object transform(Object input) {
141                 return ((NodeData) input).getName();
142             }
143         });
144         return new SimpleCollection(it);
145     }
146 
147     @Override
148     public TemplateCollectionModel values() throws TemplateModelException {
149         return (TemplateCollectionModel) wrapper.wrap(content.getNodeDataCollection().iterator());
150     }
151 
152     @Override
153     public TemplateNodeModel getParentNode() throws TemplateModelException {
154         try {
155             // todo : check if this is the root?
156             // content.getLevel() == 0;
157             final Content parent = content.getParent();
158             return (TemplateNodeModel) wrapper.wrap(parent);
159         } catch (RepositoryException e) {
160             throw new TemplateModelException("Can't get parent of " + content + ":" + e.getMessage(), e);
161         }
162     }
163 
164     /**
165      * This returns all children, except nodes or jcr: types and mgnl:metaData.
166      */
167     @Override
168     public TemplateSequenceModel getChildNodes() throws TemplateModelException {
169         final Collection<Content> children = ContentUtil.getAllChildren(content);
170         return (TemplateSequenceModel) wrapper.wrap(children);
171     }
172 
173     @Override
174     public String getNodeName() throws TemplateModelException {
175         return content.getName();
176     }
177 
178     @Override
179     public String getNodeType() throws TemplateModelException {
180         try {
181             return content.getNodeTypeName();
182         } catch (RepositoryException e) {
183             throw new TemplateModelException("Can't get node type of " + content + ":" + e.getMessage(), e);
184         }
185     }
186 
187     @Override
188     public String getNodeNamespace() throws TemplateModelException {
189         return null; // non XML implementation
190     }
191 
192     public Content asContent() {
193         return this.content;
194     }
195 
196     @Override
197     public Object getAdaptedObject(Class hint) {
198         return asContent();
199     }
200 }