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.SessionUtil;
37 import info.magnolia.module.InstallContext;
38 import info.magnolia.repository.RepositoryConstants;
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.Node;
46 import javax.jcr.RepositoryException;
47 import javax.jcr.Session;
48 import javax.jcr.Value;
49
50
51
52
53 public class NewPropertyTask extends PropertyValuesTask {
54 private final String workspaceName;
55 private final String nodePath;
56 private final String propertyName;
57 private final Object value;
58
59 public NewPropertyTask(String name, String nodePath, String propertyName, Object value) {
60 this(name, String.format("Create property '%s:%s' with value '%s'.", RepositoryConstants.CONFIG, nodePath + "/" + propertyName, value),
61 RepositoryConstants.CONFIG, nodePath, propertyName, value);
62 }
63
64 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, String value) {
65 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
66 }
67
68 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Value value) {
69 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
70 }
71
72 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Node value) {
73 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
74 }
75
76 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Binary value) {
77 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
78 }
79
80 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Calendar value) {
81 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
82 }
83
84 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, BigDecimal value) {
85 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
86 }
87
88 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Long value) {
89 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
90 }
91
92 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Double value) {
93 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
94 }
95
96 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Boolean value) {
97 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
98 }
99
100 public NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, InputStream value) {
101 this(name, description, workspaceName, nodePath, propertyName, (Object) value);
102 }
103
104 private NewPropertyTask(String name, String description, String workspaceName, String nodePath, String propertyName, Object value) {
105 super(name, description);
106 this.workspaceName = workspaceName;
107 this.nodePath = nodePath;
108 this.propertyName = propertyName;
109 this.value = value;
110 }
111
112 @Override
113 public void execute(InstallContext ctx) {
114 try {
115 final Session session = ctx.getJCRSession(workspaceName);
116 final Node node = SessionUtil.getNode(session, nodePath);
117
118 newProperty(ctx, node, propertyName, value);
119 } catch (RepositoryException e) {
120 ctx.error(format("Could not create property {0} at {1}, please create it with value {2}.", propertyName, nodePath, value), e);
121 }
122 }
123 }