View Javadoc
1   /**
2    * This file Copyright (c) 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.module.templatingkit.setup.for2_9;
35  
36  import info.magnolia.module.InstallContext;
37  import info.magnolia.module.delta.AbstractRepositoryTask;
38  import info.magnolia.module.delta.TaskExecutionException;
39  import info.magnolia.module.templatingkit.templates.pages.STKPage;
40  
41  import javax.jcr.Node;
42  import javax.jcr.NodeIterator;
43  import javax.jcr.RepositoryException;
44  import javax.jcr.Session;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * {@link info.magnolia.module.delta.NodeVisitorTask} that migrates legacy template categories to new template types.
51   */
52  public class MigrateTemplateTypesTask extends AbstractRepositoryTask {
53  
54      private final static Logger log = LoggerFactory.getLogger(MigrateTemplateTypesTask.class);
55  
56      protected final static String MESSAGE_FORMAT = "Checked %d STK page definitions modified %d and copied %d category and %d subcategory properties";
57  
58      protected final static String PROPERTY_NAME_CLASS = "class";
59      protected final static String PROPERTY_VALUE_STK_PAGE_CLASS = STKPage.class.getName();
60  
61      protected final static String PROPERTY_NAME_CATEGORY = "category";
62      protected final static String PROPERTY_NAME_SUBCATEGORY = "subcategory";
63  
64      protected final static String PROPERTY_NAME_TYPE = "type";
65      protected final static String PROPERTY_NAME_SUBTYPE = "subtype";
66  
67      protected static final String PROPERTY_NAME_EXTENDS = "extends";
68  
69      private Session session;
70      private int templates = 0;
71      private int templatesModified = 0;
72      private int categories = 0;
73      private int subcategories = 0;
74  
75      public MigrateTemplateTypesTask() {
76          super("Migrate template categories to template types", "This task will copy template properties 'category' and 'subcategory' to new template types");
77      }
78  
79      @Override
80      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
81          session = installContext.getConfigJCRSession();
82          final Node startNode = session.getNode("/modules");
83          final NodeIterator nodeIterator = startNode.getNodes();
84          while (nodeIterator.hasNext()) {
85              Node moduleNode = nodeIterator.nextNode();
86              if (moduleNode.hasNode("templates")) {
87                  operateOnNode(moduleNode.getNode("templates"));
88              }
89          }
90          // Output some migration information
91          final String message = String.format(MESSAGE_FORMAT, templates, templatesModified, categories, subcategories);
92          log.info(message);
93          installContext.info(message);
94      }
95  
96      private void operateOnNode(Node templatesNode) throws RepositoryException {
97          if (isSTKPage(templatesNode) || extendsSTKPage(templatesNode)) {
98              templates++;
99              boolean modified = false;
100             // Migrate category value, but do not override existing value
101             if (templatesNode.hasProperty(PROPERTY_NAME_CATEGORY) && !templatesNode.hasProperty(PROPERTY_NAME_TYPE)) {
102                 final String category = templatesNode.getProperty(PROPERTY_NAME_CATEGORY).getString();
103                 templatesNode.setProperty(PROPERTY_NAME_TYPE, category);
104                 categories++;
105                 modified = true;
106             }
107             // Migrate subcategory value, but do not override existing value
108             if (templatesNode.hasProperty(PROPERTY_NAME_SUBCATEGORY) && !templatesNode.hasProperty(PROPERTY_NAME_SUBTYPE)) {
109                 final String category = templatesNode.getProperty(PROPERTY_NAME_SUBCATEGORY).getString();
110                 templatesNode.setProperty(PROPERTY_NAME_SUBTYPE, category);
111                 subcategories++;
112                 modified = true;
113             }
114             if (modified)
115                 templatesModified++;
116         } else {
117             if (templatesNode.hasNodes()) {
118                 for (final NodeIterator nodeIterator = templatesNode.getNodes(); nodeIterator.hasNext(); ) {
119                     operateOnNode(nodeIterator.nextNode());
120                 }
121             }
122         }
123     }
124 
125     /**
126      * Checks if a {@link javax.jcr.Node} is a {@link info.magnolia.module.templatingkit.templates.pages.STKPage}.
127      */
128     private boolean isSTKPage(Node node) {
129         try {
130             return node.hasProperty(PROPERTY_NAME_CLASS) && PROPERTY_VALUE_STK_PAGE_CLASS.equals(node.getProperty(PROPERTY_NAME_CLASS).getString());
131         } catch (RepositoryException e) {
132             return false;
133         }
134     }
135 
136     /**
137      * Checks if a {@link javax.jcr.Node} extends a {@link info.magnolia.module.templatingkit.templates.pages.STKPage}.
138      */
139     private boolean extendsSTKPage(Node node) {
140         try {
141             return node.hasProperty(PROPERTY_NAME_EXTENDS) && isSTKPage(session.getNode(node.getProperty(PROPERTY_NAME_EXTENDS).getString()));
142         } catch (RepositoryException e) {
143             return false;
144         }
145     }
146 
147 }