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.core.Content;
37 import info.magnolia.jcr.util.PropertyUtil;
38 import info.magnolia.module.InstallContext;
39
40 import java.text.MessageFormat;
41 import java.util.Arrays;
42 import java.util.Collection;
43
44 import javax.jcr.Node;
45 import javax.jcr.Property;
46 import javax.jcr.RepositoryException;
47
48 import org.apache.commons.lang3.StringUtils;
49
50
51
52
53 public abstract class PropertyValuesTask extends AbstractTask {
54
55 public PropertyValuesTask(String name, String description) {
56 super(name, description);
57 }
58
59
60
61
62
63
64 protected void checkAndModifyPropertyValue(InstallContext ctx, Content node, String propertyName, String expectedCurrentValue, String newValue) throws RepositoryException {
65 checkAndModifyPropertyValue(ctx, node.getJCRNode(), propertyName, Arrays.asList(expectedCurrentValue), newValue);
66 }
67
68
69
70
71 protected void checkAndModifyPropertyValue(InstallContext ctx, Node node, String propertyName, String expectedCurrentValue, String newValue) throws RepositoryException {
72 checkAndModifyPropertyValue(ctx, node, propertyName, Arrays.asList(expectedCurrentValue), newValue);
73 }
74
75
76
77
78 protected void checkAndModifyPropertyValue(InstallContext ctx, Node node, String propertyName, Collection<String> expectedCurrentValues, String newValue) throws RepositoryException {
79 if (node.hasProperty(propertyName)) {
80 final Property prop = node.getProperty(propertyName);
81 final String currentvalue = prop.getString();
82 if (expectedCurrentValues.contains(currentvalue)) {
83 prop.setValue(newValue);
84 } else {
85 String msg;
86 if (expectedCurrentValues.size() == 1) {
87 msg = format("Property \"{0}\" was expected to exist at {1} with value \"{2}\" but has the value \"{3}\" instead.",
88 propertyName, node.getPath(), expectedCurrentValues.iterator().next(), currentvalue);
89 } else {
90 msg = format("Property \"{0}\" was expected to exist at {1} with one of values {2} but has the value \"{3}\" instead.",
91 propertyName, node.getPath(), expectedCurrentValues.toString(), currentvalue);
92 }
93 ctx.warn(msg);
94 }
95 } else {
96 String msg;
97 if (expectedCurrentValues.size() == 1) {
98 msg = format("Property \"{0}\" was expected to exist at {1} with value \"{2}\" but does not exist.",
99 propertyName, node.getPath(), expectedCurrentValues.iterator().next());
100 } else {
101 msg = format("Property \"{0}\" was expected to exist at {1} with one of values {2} but does not exist.",
102 propertyName, node.getPath(), expectedCurrentValues.toString());
103 }
104 ctx.warn(msg);
105 }
106 }
107
108
109
110
111 protected void checkAndModifyPartOfPropertyValue(InstallContext ctx, Node node, String propertyName, String expectedValue, String newValue) throws RepositoryException {
112 if (node.hasProperty(propertyName)) {
113 final Property prop = node.getProperty(propertyName);
114 final String currentvalue = prop.getString();
115 if (currentvalue.contains(expectedValue)) {
116 prop.setValue(StringUtils.replace(currentvalue, expectedValue, newValue));
117 } else {
118 final String msg = format("Property \"{0}\" was expected to exist at {1} with part string \"{2}\" but does not contain this string.",
119 propertyName, node.getPath(), expectedValue);
120 ctx.warn(msg);
121 }
122 } else {
123 final String msg = format("Property \"{0}\" was expected to exist at {1} with part string \"{2}\" but does not exist.",
124 propertyName, node.getPath(), expectedValue);
125 ctx.warn(msg);
126 }
127 }
128
129 protected void checkOrCreateProperty(InstallContext ctx, Node node, String propertyName, String expectedValue) throws RepositoryException {
130
131 if (node.hasProperty(propertyName)) {
132 final Property prop = node.getProperty(propertyName);
133 final String currentvalue = prop.getString();
134
135 if (!currentvalue.equals(expectedValue)) {
136 final String msg = format("Property \"{0}\" was expected to exist at {1} with value \"{2}\" but {3,choice,0#does not exist|1#has the value \"{4}\" instead}.",
137 propertyName, node.getPath(), expectedValue, 1, currentvalue);
138 ctx.warn(msg);
139 }
140 } else {
141 node.setProperty(propertyName, expectedValue);
142 }
143 }
144
145
146
147
148 protected void checkOrCreateProperty(InstallContext ctx, Content node, String propertyName, String expectedValue) throws RepositoryException {
149 this.checkOrCreateProperty(ctx, node.getJCRNode(), propertyName, expectedValue);
150 }
151
152
153
154
155 @Deprecated
156 protected void newProperty(InstallContext ctx, Content node, String propertyName, String value) throws RepositoryException {
157 newProperty(ctx, node.getJCRNode(), propertyName, value);
158 }
159
160
161
162
163 protected void newProperty(InstallContext ctx, Node node, String propertyName, Object value) throws RepositoryException {
164 if (!node.hasProperty(propertyName)) {
165 PropertyUtil.setProperty(node, propertyName, value);
166 } else {
167 final String msg = format("Property \"{0}\" was expected not to exist at {1}, but exists with value \"{2}\" and was going to be created with value \"{3}\".",
168 propertyName, node.getPath(), node.getProperty(propertyName).getValue().getString(), value);
169 ctx.warn(msg);
170 }
171 }
172
173
174 protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) {
175 return MessageFormat.format(pattern, arg0, arg1, arg2, arg3, arg4);
176 }
177
178 protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3) {
179 return format(pattern, arg0, arg1, arg2, arg3, null);
180 }
181
182 protected String format(String pattern, Object arg0, Object arg1, Object arg2) {
183 return format(pattern, arg0, arg1, arg2, null);
184 }
185 }