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.jcr.util.PropertyUtil;
37 import info.magnolia.jcr.util.SessionUtil;
38 import info.magnolia.module.InstallContext;
39
40 import java.io.InputStream;
41 import java.math.BigDecimal;
42 import java.util.Calendar;
43
44 import javax.jcr.Binary;
45 import javax.jcr.RepositoryException;
46 import javax.jcr.Node;
47 import javax.jcr.Session;
48 import javax.jcr.Value;
49
50
51
52
53
54
55
56
57
58 public class SetPropertyTask extends PropertyValuesTask {
59
60 private final String workspaceName;
61
62 private final String nodePath;
63
64 private final String propertyName;
65
66 private final Object newValue;
67
68 public SetPropertyTask(String workspaceName, String nodePath, String propertyName, String newValue) {
69 this("Set " + nodePath + "/" + propertyName, workspaceName, nodePath, propertyName, newValue);
70 }
71
72 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, String newValue) {
73 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
74 }
75
76 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Value newValue) {
77 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
78 }
79
80 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Node newValue) {
81 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
82 }
83
84 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Binary newValue) {
85 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
86 }
87
88 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Calendar newValue) {
89 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
90 }
91
92 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, BigDecimal newValue) {
93 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
94 }
95
96 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Long newValue) {
97 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
98 }
99
100 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Double newValue) {
101 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
102 }
103
104 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Boolean newValue) {
105 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
106 }
107
108 public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, InputStream newValue) {
109 this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
110 }
111
112 private SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Object newValue) {
113 super(taskName, "Sets " + nodePath + "/" + propertyName + " to " + newValue + ".");
114 this.workspaceName = workspaceName;
115 this.nodePath = nodePath;
116 this.propertyName = propertyName;
117 this.newValue = newValue;
118 }
119
120
121
122
123 @Override
124 public void execute(InstallContext installContext) {
125 try {
126 final Session session = installContext.getJCRSession(workspaceName);
127 final Node node = SessionUtil.getNode(session, nodePath);
128
129 PropertyUtil.setProperty(node, propertyName, this.newValue);
130 } catch (RepositoryException e) {
131 final String msg = format("Unable to set property \"{0}\" to value \"{1}\" on node \"{2}\".", propertyName, newValue, nodePath);
132 installContext.error(msg, e);
133 }
134 }
135 }