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.ui.vaadin.integration.jcr;
35
36 import info.magnolia.cms.core.Path;
37 import info.magnolia.jcr.RuntimeRepositoryException;
38 import info.magnolia.jcr.util.PropertyUtil;
39
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.HashSet;
43 import java.util.Set;
44
45 import javax.jcr.Item;
46 import javax.jcr.Node;
47 import javax.jcr.PropertyType;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.Value;
50 import javax.jcr.ValueFactory;
51
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import com.vaadin.data.Property;
56
57
58
59
60 public class JcrPropertyAdapter extends AbstractJcrAdapter {
61
62 public static final String VALUE_PROPERTY = "value";
63
64 public static final String TYPE_PROPERTY = "type";
65
66 public static final Set<String> PROPERTY_IDS = Collections.unmodifiableSet(new HashSet<String>() {{
67 add(ModelConstants.JCR_NAME);
68 add(TYPE_PROPERTY);
69 add(VALUE_PROPERTY);
70 }});
71
72 private static final Logger log = LoggerFactory.getLogger(JcrPropertyAdapter.class);
73
74 public JcrPropertyAdapter(javax.jcr.Property jcrProperty) {
75 super(jcrProperty);
76 }
77
78 @Override
79 protected void initCommonAttributes(Item jcrItem) {
80 if (jcrItem instanceof javax.jcr.Property) {
81 try {
82 Node parent = jcrItem.getParent();
83 setItemId(new JcrPropertyItemId(parent.getIdentifier(), jcrItem.getSession().getWorkspace().getName(), jcrItem.getName()));
84 } catch (RepositoryException e) {
85 setItemId(new JcrPropertyItemId(UNIDENTIFIED, UNIDENTIFIED, UNIDENTIFIED));
86 }
87 }
88 }
89
90 @Override
91 public boolean isNode() {
92 return false;
93 }
94
95 @Override
96 public JcrPropertyItemId getItemId() {
97 return (JcrPropertyItemId) super.getItemId();
98 }
99
100 @Override
101 public javax.jcr.Property getJcrItem() {
102 return (javax.jcr.Property)super.getJcrItem();
103 }
104
105 @Override
106 public javax.jcr.Property applyChanges() throws RepositoryException {
107 javax.jcr.Property jcrItem = getJcrItem();
108 super.updateProperties(jcrItem);
109
110 return getJcrItem();
111 }
112
113 @Override
114 public Property getItemProperty(Object id) {
115 if (getChangedProperties().containsKey(id)) {
116 return getChangedProperties().get(id);
117 }
118 Object value;
119 int type = PropertyType.STRING;
120 try {
121 javax.jcr.Property jcrProperty = getJcrItem();
122 if (ModelConstants.JCR_NAME.equals(id)) {
123 value = jcrProperty.getName();
124 } else if (VALUE_PROPERTY.equals(id)) {
125 value = PropertyUtil.getPropertyValueObject(jcrProperty.getParent(), String.valueOf(jcrProperty.getName()));
126 type = jcrProperty.getType();
127 } else if (TYPE_PROPERTY.equals(id)) {
128 value = PropertyType.nameFromValue(jcrProperty.getType());
129 } else {
130 return null;
131 }
132 } catch (RepositoryException re) {
133 log.error("Could not get property for " + id, re);
134 throw new RuntimeRepositoryException(re);
135 }
136
137 DefaultProperty property = new DefaultProperty(value.getClass(), value);
138 getChangedProperties().put((String) id, property);
139 return property;
140 }
141
142 @Override
143 public Collection<?> getItemPropertyIds() {
144 return PROPERTY_IDS;
145 }
146
147
148
149
150 @Override
151 protected void updateProperty(Item item, String propertyId, Property property) {
152 if (!(item instanceof javax.jcr.Property)) {
153 return;
154 }
155 javax.jcr.Property jcrProperty = (javax.jcr.Property) item;
156
157 if (ModelConstants.JCR_NAME.equals(propertyId)) {
158 String jcrName = (String) property.getValue();
159 if (jcrName != null && !jcrName.isEmpty()) {
160 try {
161 if (!(jcrName.equals(jcrProperty.getName()))) {
162
163
164 jcrName = Path.getUniqueLabel(jcrProperty.getSession(), jcrProperty.getParent().getPath(), jcrName);
165
166
167 javax.jcr.Property newProperty = PropertyUtil.renameProperty(jcrProperty, jcrName);
168
169
170 setItemId(new JcrPropertyItemId(newProperty.getParent().getIdentifier(), getWorkspace(), newProperty.getName()));
171 }
172 } catch (RepositoryException e) {
173 log.error("Could not rename JCR property", e);
174 }
175 }
176 } else if (VALUE_PROPERTY.equals(propertyId)) {
177 if (property.getValue() != null) {
178 try {
179 ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
180
181 Value newValue = PropertyUtil.createValue(property.getValue(), valueFactory);
182 jcrProperty.setValue(newValue);
183 } catch (RepositoryException e) {
184 log.error("Could not set JCR property", e);
185 }
186 }
187 } else if (TYPE_PROPERTY.equals(propertyId)) {
188 if (property.getValue() != null) {
189
190
191 int newType;
192 try {
193 newType = PropertyType.valueFromName(property.getValue().toString());
194 } catch (IllegalArgumentException e) {
195 log.warn("Could not set new type for JCR property, unknown type string", e);
196 return;
197 }
198
199
200 try {
201 String valueString = jcrProperty.getValue().getString();
202 ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
203
204 Value newValue = PropertyUtil.createValue(valueString, newType, valueFactory);
205 jcrProperty.setValue(newValue);
206 } catch (RepositoryException e) {
207 log.error("Could not set new type for JCR property", e);
208 }
209 }
210 }
211 }
212
213 @Override
214 public boolean addItemProperty(Object id, Property property) {
215 throw new UnsupportedOperationException("addItemProperty");
216 }
217
218 @Override
219 public boolean removeItemProperty(Object id) throws UnsupportedOperationException {
220 throw new UnsupportedOperationException("removeItemProperty");
221 }
222
223 @Override
224 public boolean isNew() {
225 return false;
226 }
227 }