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 javax.jcr.RepositoryException;
41 import java.text.MessageFormat;
42
43
44
45
46
47
48
49 public abstract class PropertyValuesTask extends AbstractTask {
50
51 public PropertyValuesTask(String name, String description) {
52 super(name, description);
53 }
54
55
56
57
58 protected void checkAndModifyPropertyValue(InstallContext ctx, Content node, String propertyName, String expectedCurrentValue, String newValue) throws RepositoryException {
59 final NodeData prop = node.getNodeData(propertyName);
60 final String currentvalue = prop.getString();
61 if (prop.isExist() && currentvalue.equals(expectedCurrentValue)) {
62 prop.setValue(newValue);
63 } else {
64 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}.",
65 propertyName, node.getHandle(), expectedCurrentValue, new Integer(prop.isExist() ? 1 : 0), currentvalue);
66 ctx.warn(msg);
67 }
68 }
69
70 protected void checkOrCreateProperty(InstallContext ctx, Content node, String propertyName, String expectedValue) throws RepositoryException {
71 if (node.hasNodeData(propertyName)) {
72 final NodeData prop = node.getNodeData(propertyName);
73 final String currentvalue = prop.getString();
74 if (!currentvalue.equals(expectedValue)) {
75 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}.",
76 propertyName, node.getHandle(), expectedValue, new Integer(prop.isExist() ? 1 : 0), currentvalue);
77 ctx.warn(msg);
78 }
79 } else {
80 node.createNodeData(propertyName, expectedValue);
81 }
82 }
83
84
85
86
87 protected void newProperty(InstallContext ctx, Content node, String propertyName, String value) throws RepositoryException {
88 newProperty(ctx, node, propertyName, value, true);
89 }
90
91 protected void newProperty(InstallContext ctx, Content node, String propertyName, String value, boolean log) throws RepositoryException {
92 final NodeData prop = node.getNodeData(propertyName);
93 if (!prop.isExist()) {
94 node.createNodeData(propertyName, value);
95 } else if (log) {
96 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}\".",
97 propertyName, node.getHandle(), prop.getString(), value);
98 ctx.warn(msg);
99 }
100 }
101
102
103 protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) {
104 return MessageFormat.format(pattern, new Object[]{arg0, arg1, arg2, arg3, arg4});
105 }
106
107 protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3) {
108 return format(pattern, arg0, arg1, arg2, arg3, null);
109 }
110
111 protected String format(String pattern, Object arg0, Object arg1, Object arg2) {
112 return format(pattern, arg0, arg1, arg2, null);
113 }
114 }