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