View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.importexport.postprocessors;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.HashSet;
39  import java.util.Map;
40  import java.util.Set;
41  import javax.jcr.Node;
42  import javax.jcr.NodeIterator;
43  import javax.jcr.Property;
44  import javax.jcr.PropertyIterator;
45  import javax.jcr.RepositoryException;
46  
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  import info.magnolia.cms.core.MetaData;
51  import info.magnolia.jcr.util.NodeTypes;
52  import info.magnolia.jcr.util.NodeUtil;
53  
54  /**
55   * Converts the MetaData sub node into properties on the mixins <code>mgnl:created</code>,
56   * <code>mgnl:lastModified</code>, <code>mgnl:renderable</code>, <code>mgnl:activatable</code> and
57   * <code>mgnl:activatable</code>. It also renames the property <code>mgnl:deletedOn</code> property on the mixin
58   * <code>mgnl:deleted</code> to <code>mgnl:deleted</code>. The MetaData node itself is optionally removed if there are
59   * no additional properties on it.
60   */
61  public class MetaDataAsMixinConversionHelper {
62  
63      private static final int PERIODIC_SAVE_FREQUENCY = 20;
64      private static final String DEPRECATED_DELETION_DATE_PROPERTY_NAME = "mgnl:deletedOn";
65  
66      private final Logger logger = LoggerFactory.getLogger(MetaDataAsMixinConversionHelper.class);
67  
68      private final HashMap<String, String> propertyNameMapping = new HashMap<String, String>();
69  
70      /**
71       * Names of properties that we expect to see in MetaData after conversion.
72       */
73      private final Set<String> propertyNamesExpectedInMetaDataAfterConversion = new HashSet<String>();
74  
75      private boolean deleteMetaDataIfEmptied = false;
76      private boolean periodicSaves = false;
77  
78      public MetaDataAsMixinConversionHelper() {
79          propertyNameMapping.put(NodeTypes.MGNL_PREFIX + MetaData.CREATION_DATE, NodeTypes.Created.CREATED);
80          propertyNameMapping.put(NodeTypes.MGNL_PREFIX + MetaData.LAST_ACTION, NodeTypes.Activatable.LAST_ACTIVATED);
81          propertyNameMapping.put(NodeTypes.MGNL_PREFIX + MetaData.ACTIVATOR_ID, NodeTypes.Activatable.LAST_ACTIVATED_BY);
82          propertyNameMapping.put(NodeTypes.MGNL_PREFIX + MetaData.ACTIVATED, NodeTypes.Activatable.ACTIVATION_STATUS);
83          propertyNameMapping.put(NodeTypes.MGNL_PREFIX + MetaData.TEMPLATE, NodeTypes.Renderable.TEMPLATE);
84          propertyNameMapping.put(NodeTypes.MGNL_PREFIX + MetaData.LAST_MODIFIED, NodeTypes.LastModified.LAST_MODIFIED);
85          propertyNameMapping.put(NodeTypes.MGNL_PREFIX + MetaData.AUTHOR_ID, NodeTypes.LastModified.LAST_MODIFIED_BY);
86          propertyNameMapping.put("mgnl:comment", NodeTypes.Versionable.COMMENT);
87  
88          // We're not transferring mgnl:title and mgnl:templatetype so we'll expect those to still be present
89          propertyNamesExpectedInMetaDataAfterConversion.add(NodeTypes.MGNL_PREFIX + MetaData.TITLE);
90          propertyNamesExpectedInMetaDataAfterConversion.add(NodeTypes.MGNL_PREFIX + MetaData.TEMPLATE_TYPE);
91  
92          // Since we don't transfer a property if it already exists on the content node we need to expect all of those
93          // we transfer to still be present afterwards
94          propertyNamesExpectedInMetaDataAfterConversion.addAll(propertyNameMapping.keySet());
95      }
96  
97      public boolean isDeleteMetaDataIfEmptied() {
98          return deleteMetaDataIfEmptied;
99      }
100 
101     public void setDeleteMetaDataIfEmptied(boolean deleteMetaDataIfEmptied) {
102         this.deleteMetaDataIfEmptied = deleteMetaDataIfEmptied;
103     }
104 
105     public boolean isPeriodicSaves() {
106         return periodicSaves;
107     }
108 
109     /**
110      * Sets whether to save periodically as the sub tree is converted. This reduces the amount of memory required.
111      */
112     public void setPeriodicSaves(boolean periodicSaves) {
113         this.periodicSaves = periodicSaves;
114     }
115 
116     public void convertNodeAndChildren(Node startNode) throws RepositoryException {
117 
118         int nodesProcessed = 0;
119         ArrayList<Node> nodes = new ArrayList<Node>();
120         nodes.add(startNode);
121 
122         while (!nodes.isEmpty()) {
123             // Take the most recently added node creating a depth-first scan because it has smaller memory footprint
124             // than a breadth-first scan.
125             Node node = nodes.remove(nodes.size() - 1);
126 
127             processNode(node);
128 
129             // Save the session every x nodes if period saves is enabled
130             nodesProcessed++;
131             if (periodicSaves && nodesProcessed % PERIODIC_SAVE_FREQUENCY == 0) {
132                 node.getSession().save();
133             }
134 
135             // Queue child nodes
136             NodeIterator children = node.getNodes();
137             while (children.hasNext()) {
138                 Node child = children.nextNode();
139                 if (!(child.getName().equals(MetaData.DEFAULT_META_NODE) && NodeUtil.isNodeType(child, NodeTypes.MetaData.NAME))) {
140                     nodes.add(child);
141                 }
142             }
143         }
144     }
145 
146     private void processNode(Node node) throws RepositoryException {
147 
148         // Rename mgnl:deletedOn to mgnl:deleted for mixin mgnl:deleted
149         if (node.hasProperty(DEPRECATED_DELETION_DATE_PROPERTY_NAME)) {
150             moveProperty(node, DEPRECATED_DELETION_DATE_PROPERTY_NAME, node, NodeTypes.Deleted.DELETED);
151         }
152 
153         // Transfer properties from the MetaData node
154         if (node.hasNode(MetaData.DEFAULT_META_NODE)) {
155             Node metaDataNode = node.getNode(MetaData.DEFAULT_META_NODE);
156             if (NodeUtil.isNodeType(metaDataNode, NodeTypes.MetaData.NAME)) {
157                 moveProperties(node, metaDataNode, propertyNameMapping);
158 
159                 if (deleteMetaDataIfEmptied && isEmptyMetaDataNode(metaDataNode)) {
160                     metaDataNode.remove();
161                 }
162             }
163         }
164     }
165 
166     /**
167      * Returns true if the MetaData node is considered empty, i.e. has no sub nodes and no unexpected properties.
168      */
169     private boolean isEmptyMetaDataNode(Node node) throws RepositoryException {
170         if (node.getNodes().getSize() != 0) {
171             logger.warn("MetaData node not removed because it has sub nodes " + node.getPath());
172             return false;
173         }
174         PropertyIterator iterator = node.getProperties();
175         while (iterator.hasNext()) {
176             Property property = iterator.nextProperty();
177             if (!isExpectedMetaDataProperty(property)) {
178                 logger.warn("MetaData node not removed because of unrecognized property: " + property.getPath());
179                 return false;
180             }
181         }
182         return true;
183     }
184 
185     /**
186      * Returns true if the property is expected to be found on the MetaData node after conversion.
187      */
188     private boolean isExpectedMetaDataProperty(Property property) throws RepositoryException {
189         String propertyName = property.getName();
190 
191         // We expect there to be standard JCR properties like jcr:createdBy etc
192         if (propertyName.startsWith("jcr:")) {
193             return true;
194         }
195 
196         // Legacy property deprecated in Magnolia 3.0
197         if (propertyName.equals(NodeTypes.MGNL_PREFIX + "Data") && property.getString().equals("MetaData")) {
198             return true;
199         }
200 
201         return propertyNamesExpectedInMetaDataAfterConversion.contains(propertyName);
202     }
203 
204     /**
205      * Moves a set of properties from one node to another changing their names in the process.
206      *
207      * @param dstNode      node to move properties to
208      * @param srcNode      node to move properties from
209      * @param nameMappings maps current property names to their new names
210      */
211     private void moveProperties(Node dstNode, Node srcNode, Map<String, String> nameMappings) throws RepositoryException {
212         for (Map.Entry<String, String> entry : nameMappings.entrySet()) {
213             String srcPropertyName = entry.getKey();
214             String dstPropertyName = entry.getValue();
215             if (!dstNode.hasProperty(dstPropertyName) && srcNode.hasProperty(srcPropertyName)) {
216                 moveProperty(srcNode, srcPropertyName, dstNode, dstPropertyName);
217             }
218         }
219     }
220 
221     /**
222      * Moves a property from a node to another node and changes its name in the process. If a property already exists on
223      * the destination node it will be overwritten.
224      *
225      * @param srcNode         node containing the property
226      * @param srcPropertyName name of the property
227      * @param dstNode         node to which the property should be moved
228      * @param dstPropertyName new name after the move
229      *
230      * @throws javax.jcr.PathNotFoundException
231      *                             if the source property does not exist
232      */
233     private void moveProperty(Node srcNode, String srcPropertyName, Node dstNode, String dstPropertyName) throws RepositoryException {
234         Property srcProperty = srcNode.getProperty(srcPropertyName);
235         if (srcProperty.isMultiple()) {
236             dstNode.setProperty(dstPropertyName, srcProperty.getValues());
237         } else {
238             dstNode.setProperty(dstPropertyName, srcProperty.getValue());
239         }
240         srcProperty.remove();
241     }
242 }