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 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
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 }