1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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 }