View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.delta;
35  
36  import info.magnolia.cms.util.RequestDispatchUtil;
37  import info.magnolia.module.InstallContext;
38  import info.magnolia.repository.RepositoryConstants;
39  
40  import javax.jcr.Node;
41  import javax.jcr.NodeIterator;
42  import javax.jcr.RepositoryException;
43  import javax.jcr.Session;
44  import javax.jcr.query.Query;
45  import javax.jcr.query.QueryManager;
46  import javax.jcr.query.QueryResult;
47  
48  import org.apache.commons.lang.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * Task for changing Virtual URI mappings - replaces old toURI and oldURI values with new ones.
54   */
55  public class ChangeVirtualUriMappingTask extends AbstractRepositoryTask {
56  
57      private static final Logger log = LoggerFactory.getLogger(ChangeVirtualUriMappingTask.class);
58  
59      protected static final String VIRTUAL_URI_MAPPING = "virtualURIMapping";
60      protected static final String FROM_URI = "fromURI";
61      protected static final String TO_URI = "toURI";
62  
63      private String currentValue;
64      private String newValue;
65      private String query;
66  
67      public ChangeVirtualUriMappingTask(String name, String description, String currentValue, String newValue) {
68          this(name, description, currentValue, newValue, new String[] { RequestDispatchUtil.FORWARD_PREFIX, RequestDispatchUtil.REDIRECT_PREFIX, RequestDispatchUtil.PERMANENT_PREFIX });
69      }
70  
71      public ChangeVirtualUriMappingTask(String name, String description, String currentValue, String newValue, String[] includedPrefixes) {
72  
73          super(name, description);
74          this.currentValue = currentValue;
75          this.newValue = newValue;
76  
77          query = "select * from [nt:base] as t WHERE ISDESCENDANTNODE([/modules])  AND (contains(t.*,'" + this.currentValue + "')";
78          for (String prefix : includedPrefixes) {
79              query += " OR contains(t.*,'" + prefix + this.currentValue + "')";
80          }
81          query += ")";
82      }
83  
84      @Override
85      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
86  
87          Session config = installContext.getJCRSession(RepositoryConstants.CONFIG);
88          QueryManager qm = config.getWorkspace().getQueryManager();
89          Query q = qm.createQuery(query, Query.JCR_SQL2);
90          QueryResult queryResult = q.execute();
91          NodeIterator nodeIterator = queryResult.getNodes();
92  
93          while (nodeIterator.hasNext()) {
94              Node node = nodeIterator.nextNode();
95              if (!VIRTUAL_URI_MAPPING.equals(node.getParent().getName())) {
96                  continue;
97              }
98  
99              if (node.hasProperty(FROM_URI)) {
100                 String fromUri = node.getProperty(FROM_URI).getString();
101 
102                 if (fromUri.equals(currentValue)) {
103                     node.setProperty(FROM_URI, newValue);
104                     log.info("'{}/{}' changed from '{}' to '{}'", node.getPath(), FROM_URI, fromUri, newValue);
105                 }
106             }
107 
108             if (node.hasProperty(TO_URI)) {
109                 String oldToUri = node.getProperty(TO_URI).getString();
110                 String prefix = StringUtils.substringBefore(oldToUri, ":");
111 
112                 if (oldToUri.equals(prefix + ":" + currentValue)) {
113                     String newToUri = prefix + ":" + newValue;
114                     node.setProperty(TO_URI, newToUri);
115                     log.info("'{}/{}' changed from '{}' to '{}'", node.getPath(), TO_URI, oldToUri, newToUri);
116                 }
117             }
118         }
119     }
120 }