View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.cms.core;
35  
36  import info.magnolia.cms.security.AccessManager;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.PropertyUtil;
39  import info.magnolia.repository.RepositoryConstants;
40  
41  import java.util.Calendar;
42  import java.util.GregorianCalendar;
43  import java.util.Map;
44  import java.util.TimeZone;
45  import java.util.concurrent.ConcurrentHashMap;
46  
47  import javax.jcr.Node;
48  import javax.jcr.PathNotFoundException;
49  import javax.jcr.Property;
50  import javax.jcr.RepositoryException;
51  
52  import org.apache.commons.lang.StringUtils;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  /**
57   * Represents the meta data of a node, its creation date, modification date, assigned template etc.
58   *
59   * As of 5.0 the meta data is stored directly on the node itself using mixins rather than in a subnode named MetaData.
60   * With this change this class was deprecated and replaced with corresponding methods in
61   * {@link info.magnolia.jcr.util.NodeUtil}.
62   *
63   * @deprecated since 5.0 - use instead the corresponding methods in NodeUtil
64   */
65  public class MetaData {
66      private static final Logger log = LoggerFactory.getLogger(MetaData.class);
67  
68      /**
69       * Top level atoms viewed as metadata of the specified content these must be set by the authoring system itself, but
70       * could be changed via custom templates if necessary.
71       */
72  
73      /**
74       * @deprecated since 5.0 - no longer supported
75       */
76      public static final String TITLE = "title";
77  
78      /**
79       * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Created#CREATED} instead
80       */
81      public static final String CREATION_DATE = "creationdate";
82  
83      /**
84       * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.LastModified#LAST_MODIFIED} instead
85       */
86      public static final String LAST_MODIFIED = "lastmodified";
87  
88      /**
89       * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#LAST_ACTIVATED} instead
90       */
91      public static final String LAST_ACTION = "lastaction";
92  
93      /**
94       * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.LastModified#LAST_MODIFIED_BY} instead
95       */
96      public static final String AUTHOR_ID = "authorid";
97  
98      /**
99       * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#LAST_ACTIVATED_BY} instead
100      */
101     public static final String ACTIVATOR_ID = "activatorid";
102 
103     /**
104      * Template assigned to the node.
105      *
106      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Renderable#TEMPLATE} instead
107      */
108     public static final String TEMPLATE = "template";
109 
110     /**
111      * @deprecated since 5.0 - no longer supported
112      */
113     public static final String TEMPLATE_TYPE = "templatetype";
114 
115     /**
116      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#ACTIVATION_STATUS} instead
117      */
118     public static final String ACTIVATED = "activated";
119 
120     /**
121      * Name of the node hosting the MetaData.
122      *
123      * @deprecated since 5.0 - there's no longer such a subnode
124      */
125     public static final String DEFAULT_META_NODE = "MetaData";
126 
127     /**
128      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#ACTIVATION_STATUS_NOT_ACTIVATED} instead
129      */
130     public static final int ACTIVATION_STATUS_NOT_ACTIVATED = NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
131 
132     /**
133      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#ACTIVATION_STATUS_MODIFIED} instead
134      */
135     public static final int ACTIVATION_STATUS_MODIFIED = NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED;
136 
137     /**
138      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#ACTIVATION_STATUS_ACTIVATED} instead
139      */
140     public static final int ACTIVATION_STATUS_ACTIVATED = NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED;
141 
142     /**
143      * Since 5.0 this is the working node itself.
144      */
145     private Node node;
146 
147     /**
148      * @param workingNode current <code>Node</code> on which <code>MetaData</code> is requested
149      * @param ignoredAccessManager no longer required hence use other constructor.
150      *
151      * @deprecated since 4.5 use MetaData(Node) instead.
152      */
153     protected MetaData(Node workingNode, AccessManager ignoredAccessManager) {
154         this(workingNode);
155     }
156 
157     /**
158      * @param workingNode current <code>Node</code> on which <code>MetaData</code> is requested
159      */
160     public MetaData(Node workingNode) {
161         this.node = workingNode;
162     }
163 
164     /**
165      * Maps property names from the names used when we had a MetaData sub node to their replacements on mixins on the
166      * working node itself.
167      */
168     private static Map<String, String> propertyMappings = new ConcurrentHashMap<String, String>();
169 
170     static {
171         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + CREATION_DATE, NodeTypes.Created.CREATED);
172         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + LAST_MODIFIED, NodeTypes.LastModified.LAST_MODIFIED);
173         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + LAST_ACTION, NodeTypes.Activatable.LAST_ACTIVATED);
174         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + AUTHOR_ID, NodeTypes.LastModified.LAST_MODIFIED_BY);
175         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + ACTIVATOR_ID, NodeTypes.Activatable.LAST_ACTIVATED_BY);
176         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + TEMPLATE, NodeTypes.Renderable.TEMPLATE);
177         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + ACTIVATED, NodeTypes.Activatable.ACTIVATION_STATUS);
178         propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":comment", NodeTypes.Versionable.COMMENT);
179     }
180 
181     /**
182      * Returns the property name to use including its prefix.
183      *
184      * @return name with namespace prefix
185      */
186     private String getInternalPropertyName(String name) {
187         if (StringUtils.indexOf(name, ":") < 0) {
188             name = RepositoryConstants.NAMESPACE_PREFIX + ":" + name;
189         }
190 
191         String newName = propertyMappings.get(name);
192 
193         if (newName == null) {
194             throw new IllegalArgumentException("Unsupported meta data property: " + name);
195         }
196 
197         return newName;
198     }
199 
200     /**
201      * @return value of property TITLE if it's around on working node
202      *
203      * @deprecated since 5.0 - only for backwards compatibility.
204      */
205     public String getTitle() {
206         return getStringProperty(TITLE);
207     }
208 
209     /**
210      * Will set value of property TITLE on working node.
211      *
212      * @deprecated since 5.0 - only for backwards compatibility.
213      */
214     public void setTitle(String value) {
215         setProperty(TITLE, value);
216     }
217 
218     /**
219      * Part of metadata, adds creation date of the current node.
220      *
221      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Created#set(Node)}
222      */
223     public void setCreationDate() {
224         Calendar value = new GregorianCalendar(TimeZone.getDefault());
225         setProperty(CREATION_DATE, value);
226     }
227 
228     /**
229      * Part of metadata, get creation date of the current node.
230      *
231      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Created#getCreated(Node)}
232      */
233     public Calendar getCreationDate() {
234         return this.getDateProperty(CREATION_DATE);
235     }
236 
237     /**
238      * Part of metadata, adds activated status of the current node.
239      *
240      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#update(javax.jcr.Node, String, boolean)}
241      */
242     public void setActivated() {
243         setProperty(ACTIVATED, true);
244     }
245 
246     /**
247      * Part of metadata, adds activated status of the current node.
248      *
249      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#update(javax.jcr.Node, String, boolean)}
250      */
251     public void setUnActivated() {
252         setProperty(ACTIVATED, false);
253     }
254 
255     /**
256      * Part of metadata, get last activated status of the current node.
257      *
258      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#isActivated(javax.jcr.Node)}
259      */
260     public boolean getIsActivated() {
261         return getBooleanProperty(ACTIVATED);
262     }
263 
264     /**
265      * Returns one of the ACTIVATION_STATUS_* constants.
266      *
267      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#getActivationStatus(javax.jcr.Node)}
268      */
269     public int getActivationStatus() {
270         if (getIsActivated()) {
271             if (getModificationDate() != null && getModificationDate().after(getLastActionDate())) {
272                 // node has been modified after last activation
273                 return ACTIVATION_STATUS_MODIFIED;
274             }
275             // activated and not modified ever since
276             return ACTIVATION_STATUS_ACTIVATED;
277         }
278         // never activated or deactivated
279         return ACTIVATION_STATUS_NOT_ACTIVATED;
280     }
281 
282     /**
283      * Part of metadata, adds activated date of the current node.
284      *
285      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#update(javax.jcr.Node, String, boolean)}
286      */
287     public void setLastActivationActionDate() {
288         Calendar value = new GregorianCalendar(TimeZone.getDefault());
289         setProperty(LAST_ACTION, value);
290     }
291 
292     /**
293      * Part of metadata, get last activated/de- date of the current node.
294      *
295      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#getLastActivated(javax.jcr.Node)}
296      */
297     public Calendar getLastActionDate() {
298         return getDateProperty(LAST_ACTION);
299     }
300 
301     /**
302      * Part of metadata, adds modification date of the current node.
303      *
304      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.LastModified#update(javax.jcr.Node)}
305      */
306     public void setModificationDate() {
307         Calendar value = new GregorianCalendar(TimeZone.getDefault());
308         setProperty(LAST_MODIFIED, value);
309     }
310 
311     /**
312      * Get last modified date of the node to which this meta data belongs or creation date in case content was not
313      * modified since.
314      *
315      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.LastModified#getLastModified(Node)}
316      */
317     public Calendar getModificationDate() {
318         Calendar modDate = getDateProperty(LAST_MODIFIED);
319         if (modDate == null) {
320             modDate = getCreationDate();
321         }
322         return modDate;
323     }
324 
325     /**
326      * Part of metadata, last known author of this node.
327      *
328      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.LastModified#getLastModifiedBy(javax.jcr.Node)}
329      */
330     public String getAuthorId() {
331         return getStringProperty(AUTHOR_ID);
332     }
333 
334     /**
335      * Part of metadata, current logged-in author who did some action on this page.
336      *
337      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.LastModified#update(javax.jcr.Node, String, java.util.Calendar)}
338      */
339     public void setAuthorId(String value) {
340         setProperty(AUTHOR_ID, value);
341     }
342 
343     /**
344      * Part of metadata, last known activator of this node.
345      *
346      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#getLastActivatedBy(javax.jcr.Node)}
347      */
348     public String getActivatorId() {
349         return getStringProperty(ACTIVATOR_ID);
350     }
351 
352     /**
353      * Part of metadata, current logged-in author who last activated this page.
354      *
355      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Activatable#update(javax.jcr.Node, String, boolean)} to directly set userName and true false.
356      */
357     public void setActivatorId(String value) {
358         setProperty(ACTIVATOR_ID, value);
359     }
360 
361     /**
362      * Part of metadata, template which will be used to render content of this node.
363      *
364      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Renderable#getTemplate(javax.jcr.Node)}
365      */
366     public String getTemplate() {
367         return getStringProperty(TEMPLATE);
368     }
369 
370     /**
371      * Part of metadata, template which will be used to render content of this node.
372      *
373      * @deprecated since 5.0 - use {@link info.magnolia.jcr.util.NodeTypes.Renderable#set(javax.jcr.Node, String)}
374      */
375     public void setTemplate(String value) {
376         setProperty(TEMPLATE, value);
377     }
378 
379     public void setProperty(String name, String value) {
380         setJCRProperty(name, value);
381     }
382 
383     public void setProperty(String name, long value) {
384         setJCRProperty(name, value);
385     }
386 
387     public void setProperty(String name, double value) {
388         setJCRProperty(name, value);
389     }
390 
391     public void setProperty(String name, boolean value) {
392         setJCRProperty(name, value);
393     }
394 
395     public void setProperty(String name, Calendar value) {
396         setJCRProperty(name, value);
397     }
398 
399     private void setJCRProperty(String name, Object value) {
400         final String propName = this.getInternalPropertyName(name);
401         try {
402             PropertyUtil.setProperty(node, propName, value);
403         } catch (RepositoryException re) {
404             log.error(re.getMessage(), re);
405         }
406     }
407 
408     public boolean getBooleanProperty(String name) {
409         try {
410             final Property property = getJCRProperty(name);
411             if (property != null) {
412                 return property.getBoolean();
413             }
414         } catch (RepositoryException re) {
415             log.error(re.getMessage(), re);
416         }
417         return false;
418     }
419 
420     public double getDoubleProperty(String name) {
421         try {
422             final Property property = getJCRProperty(name);
423             if (property != null) {
424                 return property.getDouble();
425             }
426         } catch (RepositoryException re) {
427             log.error(re.getMessage(), re);
428         }
429         return 0d;
430     }
431 
432     public long getLongProperty(String name) {
433         try {
434             final Property property = getJCRProperty(name);
435             if (property != null) {
436                 return property.getLong();
437             }
438         } catch (RepositoryException re) {
439             log.error(re.getMessage(), re);
440         }
441         return 0L;
442     }
443 
444     public String getStringProperty(String name) {
445         try {
446             final Property property = getJCRProperty(name);
447             if (property != null) {
448                 return property.getString();
449             }
450         } catch (RepositoryException re) {
451             log.error(re.getMessage(), re);
452         }
453         return StringUtils.EMPTY;
454     }
455 
456     public Calendar getDateProperty(String name) {
457         try {
458             final Property property = getJCRProperty(name);
459             if (property != null) {
460                 return property.getDate();
461             }
462         } catch (RepositoryException re) {
463             log.error(re.getMessage(), re);
464         }
465         return null;
466     }
467 
468     /**
469      * remove specified property.
470      *
471      * @param name of the property to be removed
472      * @throws PathNotFoundException if property does not exist
473      * @throws RepositoryException if unable to remove
474      */
475     public void removeProperty(String name) throws PathNotFoundException, RepositoryException {
476         this.node.getProperty(this.getInternalPropertyName(name)).remove();
477     }
478 
479     private Property getJCRProperty(String name) throws RepositoryException {
480         final String propName = this.getInternalPropertyName(name);
481         try {
482             return node.getProperty(propName);
483         } catch (PathNotFoundException re) {
484             log.debug("PathNotFoundException for property [{}] in node {}", propName, node);
485         }
486         return null;
487     }
488 }