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 import info.magnolia.ui.api.ModelConstants;
40
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.HashSet;
44 import java.util.Set;
45
46 import javax.jcr.Item;
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 public boolean isNode() {
80 return false;
81 }
82
83 @Override
84 public javax.jcr.Property getJcrItem() {
85 return (javax.jcr.Property)super.getJcrItem();
86 }
87
88 @Override
89 public javax.jcr.Property applyChanges() throws RepositoryException {
90 javax.jcr.Property jcrItem = getJcrItem();
91 super.updateProperties(jcrItem);
92
93 return getJcrItem();
94 }
95
96 @Override
97 public Property getItemProperty(Object id) {
98 if (getChangedProperties().containsKey(id)) {
99 return getChangedProperties().get(id);
100 }
101 Object value;
102 int type = PropertyType.STRING;
103 try {
104 javax.jcr.Property jcrProperty = getJcrItem();
105 if (ModelConstants.JCR_NAME.equals(id)) {
106 value = jcrProperty.getName();
107 } else if (VALUE_PROPERTY.equals(id)) {
108 value = PropertyUtil.getPropertyValueObject(jcrProperty.getParent(), String.valueOf(jcrProperty.getName()));
109 type = jcrProperty.getType();
110 } else if (TYPE_PROPERTY.equals(id)) {
111 value = PropertyType.nameFromValue(jcrProperty.getType());
112 } else {
113 return null;
114 }
115 } catch (RepositoryException re) {
116 log.error("Could not get property for " + id, re);
117 throw new RuntimeRepositoryException(re);
118 }
119
120 DefaultProperty property = new DefaultProperty(value.getClass(), value);
121 getChangedProperties().put((String) id, property);
122 return property;
123 }
124
125 @Override
126 public Collection<?> getItemPropertyIds() {
127 return PROPERTY_IDS;
128 }
129
130
131
132
133 @Override
134 protected void updateProperty(Item item, String propertyId, Property property) {
135 if (!(item instanceof javax.jcr.Property)) {
136 return;
137 }
138 javax.jcr.Property jcrProperty = (javax.jcr.Property) item;
139
140 if (ModelConstants.JCR_NAME.equals(propertyId)) {
141 String jcrName = (String) property.getValue();
142 if (jcrName != null && !jcrName.isEmpty()) {
143 try {
144 if (!(jcrName.equals(jcrProperty.getName()))) {
145
146
147 jcrName = Path.getUniqueLabel(jcrProperty.getSession(), jcrProperty.getParent().getPath(), jcrName);
148
149
150 javax.jcr.Property newProperty = PropertyUtil.renameProperty(jcrProperty, jcrName);
151
152
153 setItemId(JcrItemUtil.getItemId(newProperty));
154 }
155 } catch (RepositoryException e) {
156 log.error("Could not rename JCR property", e);
157 }
158 }
159 } else if (VALUE_PROPERTY.equals(propertyId)) {
160 if (property.getValue() != null) {
161 try {
162 ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
163
164 Value newValue = PropertyUtil.createValue(property.getValue(), valueFactory);
165 jcrProperty.setValue(newValue);
166 } catch (RepositoryException e) {
167 log.error("Could not set JCR property", e);
168 }
169 }
170 } else if (TYPE_PROPERTY.equals(propertyId)) {
171 if (property.getValue() != null) {
172
173
174 int newType;
175 try {
176 newType = PropertyType.valueFromName(property.getValue().toString());
177 } catch (IllegalArgumentException e) {
178 log.warn("Could not set new type for JCR property, unknown type string", e);
179 return;
180 }
181
182
183 try {
184 String valueString = jcrProperty.getValue().getString();
185 ValueFactory valueFactory = jcrProperty.getSession().getValueFactory();
186
187 Value newValue = PropertyUtil.createValue(valueString, newType, valueFactory);
188 jcrProperty.setValue(newValue);
189 } catch (RepositoryException e) {
190 log.error("Could not set new type for JCR property", e);
191 }
192 }
193 }
194 }
195
196 @Override
197 public boolean addItemProperty(Object id, Property property) {
198 throw new UnsupportedOperationException("addItemProperty");
199 }
200
201 @Override
202 public boolean removeItemProperty(Object id) throws UnsupportedOperationException {
203 throw new UnsupportedOperationException("removeItemProperty");
204 }
205
206 @Override
207 public boolean isNew() {
208 return false;
209 }
210 }