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.context.MgnlContext;
37
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Map.Entry;
41
42 import javax.jcr.Item;
43 import javax.jcr.RepositoryException;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import com.vaadin.data.Property;
49 import com.vaadin.data.Property.ValueChangeEvent;
50
51
52
53
54
55 public abstract class AbstractJcrAdapter implements Property.ValueChangeListener, JcrItemAdapter {
56
57 private static final Logger log = LoggerFactory.getLogger(AbstractJcrAdapter.class);
58
59 static final String UNIDENTIFIED = "?";
60
61 private boolean isNode;
62
63 private String workspace;
64
65 private String path;
66
67 private final Map<String, Property> changedProperties = new HashMap<String, Property>();
68
69 private final Map<String, Property> removedProperties = new HashMap<String, Property>();
70
71 public AbstractJcrAdapter(Item jcrItem) {
72 initCommonAttributes(jcrItem);
73 }
74
75
76
77
78 protected void initCommonAttributes(Item jcrItem) {
79 isNode = jcrItem.isNode();
80 try {
81 workspace = jcrItem.getSession().getWorkspace().getName();
82 path = jcrItem.getPath();
83 } catch (RepositoryException e) {
84 log.error("Could not retrieve workspace or path of JCR Item.", e);
85 path = UNIDENTIFIED;
86 workspace = UNIDENTIFIED;
87 }
88 }
89
90 @Override
91 public boolean isNode() {
92 return isNode;
93 }
94
95 @Override
96 public String getWorkspace() {
97 return workspace;
98 }
99
100 @Override
101 public String getPath() {
102 return path;
103 }
104
105 protected void setPath(String path) {
106 this.path = path;
107 }
108
109
110
111
112 @Override
113 public javax.jcr.Item getJcrItem() {
114 try {
115 return MgnlContext.getJCRSession(workspace).getItem(path);
116 } catch (RepositoryException re) {
117 log.warn("Not able to retrieve the JcrItem ", re.getMessage());
118 return null;
119 }
120 }
121
122
123
124 protected Map<String, Property> getChangedProperties() {
125 return changedProperties;
126 }
127
128 protected Map<String, Property> getRemovedProperties() {
129 return removedProperties;
130 }
131
132
133
134
135
136 @Override
137 public void valueChange(ValueChangeEvent event) {
138 Property property = event.getProperty();
139 if (property instanceof DefaultProperty) {
140 String propertyId = ((DefaultProperty) property).getPropertyName();
141 getChangedProperties().put(propertyId, property);
142 }
143 }
144
145
146
147
148
149 public void updateProperties() throws RepositoryException {
150 updateProperties(getJcrItem());
151 }
152
153
154
155
156
157 public void updateProperties(Item item) throws RepositoryException {
158 for (Entry<String, Property> entry : changedProperties.entrySet()) {
159 if (entry.getValue().isReadOnly()) {
160 continue;
161 }
162 updateProperty(item, entry.getKey(), entry.getValue());
163 }
164 }
165
166
167
168
169
170
171 abstract protected void updateProperty(Item item, String propertyId, Property property);
172
173 }