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.QueryUtil;
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.Property;
43 import javax.jcr.PropertyIterator;
44 import javax.jcr.PropertyType;
45 import javax.jcr.RepositoryException;
46 import javax.jcr.query.Query;
47
48
49
50
51
52
53
54
55
56
57
58
59 public class ChangeAllPropertiesWithCertainValueTask extends AbstractRepositoryTask {
60
61 private final String workspaceName;
62 private final String currentValue;
63 private final String newValue;
64
65 public ChangeAllPropertiesWithCertainValueTask(String currentValue, String newValue) {
66 this(RepositoryConstants.CONFIG, currentValue, newValue);
67 }
68
69 public ChangeAllPropertiesWithCertainValueTask(String workspaceName, String currentValue, String newValue) {
70 this("Rename all properties with certain name.", String.format("Renames all '%s' properties in '%s' workspace to '%s'.", currentValue, workspaceName, newValue),
71 workspaceName, currentValue, newValue);
72 }
73
74 public ChangeAllPropertiesWithCertainValueTask(String name, String description, String workspaceName, String currentValue, String newValue) {
75 super(name, description);
76 this.workspaceName = workspaceName;
77 this.currentValue = currentValue;
78 this.newValue = newValue;
79 }
80
81 @Override
82 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
83
84 String query = "select * from [nt:base] as t where contains(t.*,'" + currentValue + "')";
85 NodeIterator nodeIterator = QueryUtil.search(workspaceName, query, Query.JCR_SQL2);
86 while (nodeIterator.hasNext()) {
87 Node node = nodeIterator.nextNode();
88 PropertyIterator iterator = node.getProperties();
89 while (iterator.hasNext()) {
90 Property property = iterator.nextProperty();
91 if (property.getType() == PropertyType.STRING) {
92 if (property.getString().equals(currentValue)) {
93 property.setValue(newValue);
94 }
95 }
96 }
97 }
98 }
99 }