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.cms.core.NodeData;
38 import info.magnolia.module.InstallContext;
39
40 import java.text.MessageFormat;
41
42 import javax.jcr.Node;
43 import javax.jcr.Property;
44 import javax.jcr.RepositoryException;
45
46 import org.apache.commons.lang.StringUtils;
47
48
49
50
51
52
53
54
55 public abstract class PropertyValuesTask extends AbstractTask {
56
57 public PropertyValuesTask(String name, String description) {
58 super(name, description);
59 }
60
61
62
63
64 protected void checkAndModifyPropertyValue(InstallContext ctx, Content node, String propertyName, String expectedCurrentValue, String newValue) throws RepositoryException {
65 final NodeData prop = node.getNodeData(propertyName);
66 final String currentvalue = prop.getString();
67 if (prop.isExist() && currentvalue.equals(expectedCurrentValue)) {
68 prop.setValue(newValue);
69 } else {
70 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}.",
71 propertyName, node.getHandle(), expectedCurrentValue,
72 Integer.valueOf(prop.isExist() ? 1 : 0), currentvalue);
73 ctx.warn(msg);
74 }
75 }
76
77
78
79
80 protected void checkAndModifyPartOfPropertyValue(InstallContext ctx, Node node, String propertyName, String expectedValue, String newValue) throws RepositoryException {
81 final Property prop = node.getProperty(propertyName);
82 final String currentvalue = prop.getString();
83 if (prop != null && currentvalue.contains(expectedValue)) {
84 prop.setValue(StringUtils.replace(currentvalue, expectedValue, newValue));
85 } else {
86 final String msg = format("Property \"{0}\" was expected to exist at {1} with part string \"{2}\" but {3,choice,0#does not exist|1#does not contain string}.",
87 propertyName, node.getPath(), expectedValue,
88 prop == null ? 1 : 0, currentvalue);
89 ctx.warn(msg);
90 }
91 }
92
93 protected void checkOrCreateProperty(InstallContext ctx, Content node, String propertyName, String expectedValue) throws RepositoryException {
94 if (node.hasNodeData(propertyName)) {
95 final NodeData prop = node.getNodeData(propertyName);
96 final String currentvalue = prop.getString();
97 if (!currentvalue.equals(expectedValue)) {
98 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}.",
99 propertyName, node.getHandle(), expectedValue, Integer.valueOf(prop.isExist() ? 1 : 0),
100 currentvalue);
101 ctx.warn(msg);
102 }
103 } else {
104 node.createNodeData(propertyName, expectedValue);
105 }
106 }
107
108
109
110
111 protected void newProperty(InstallContext ctx, Content node, String propertyName, String value) throws RepositoryException {
112 newProperty(ctx, node, propertyName, value, true);
113 }
114
115 protected void newProperty(InstallContext ctx, Content node, String propertyName, String value, boolean log) throws RepositoryException {
116 final NodeData prop = node.getNodeData(propertyName);
117 if (!prop.isExist()) {
118 node.createNodeData(propertyName, value);
119 } else if (log) {
120 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}\".",
121 propertyName, node.getHandle(), prop.getString(), value);
122 ctx.warn(msg);
123 }
124 }
125
126
127 protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) {
128 return MessageFormat.format(pattern, new Object[]{arg0, arg1, arg2, arg3, arg4});
129 }
130
131 protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3) {
132 return format(pattern, arg0, arg1, arg2, arg3, null);
133 }
134
135 protected String format(String pattern, Object arg0, Object arg1, Object arg2) {
136 return format(pattern, arg0, arg1, arg2, null);
137 }
138 }