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 java.util.HashMap;
37 import java.util.Map;
38 import java.util.Map.Entry;
39
40 import javax.jcr.Item;
41 import javax.jcr.RepositoryException;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.vaadin.data.Property;
47
48
49
50
51 public abstract class AbstractJcrAdapter implements JcrItemAdapter {
52
53 private static final Logger log = LoggerFactory.getLogger(AbstractJcrAdapter.class);
54
55 protected static final String UNIDENTIFIED = "?";
56
57 private JcrItemId itemId;
58
59 private final Map<String, Property> changedProperties = new HashMap<String, Property>();
60
61 private final Map<String, Property> removedProperties = new HashMap<String, Property>();
62
63 public AbstractJcrAdapter(Item jcrItem) {
64 initCommonAttributes(jcrItem);
65 }
66
67
68
69
70 protected void initCommonAttributes(Item jcrItem) {
71 try {
72 setItemId(JcrItemUtil.getItemId(jcrItem));
73 } catch (RepositoryException e) {
74 log.error("Could not retrieve workspace or path of JCR Item.", e);
75 setItemId(new JcrItemId(UNIDENTIFIED, UNIDENTIFIED));
76 }
77 }
78
79 @Override
80 public String getWorkspace() {
81 return itemId.getWorkspace();
82 }
83
84 @Override
85 public JcrItemId getItemId() {
86 return itemId;
87 }
88
89 public void setItemId(JcrItemId itemId) {
90 this.itemId = itemId;
91 }
92
93 @Override
94 public javax.jcr.Item getJcrItem() {
95 try {
96 return JcrItemUtil.getJcrItem(itemId);
97 } catch (RepositoryException re) {
98 log.warn("Not able to retrieve the JcrItem ", re.getMessage());
99 return null;
100 }
101 }
102
103 @Override
104 public boolean hasChangedProperties() {
105 return changedProperties.size() > 0;
106 }
107
108
109
110 protected Map<String, Property> getChangedProperties() {
111 return changedProperties;
112 }
113
114 protected Map<String, Property> getRemovedProperties() {
115 return removedProperties;
116 }
117
118
119
120
121 protected void updateProperties(Item item) throws RepositoryException {
122 for (Entry<String, Property> entry : changedProperties.entrySet()) {
123 if (entry.getValue().isReadOnly()) {
124 continue;
125 }
126 updateProperty(item, entry.getKey(), entry.getValue());
127 if (ModelConstants.JCR_NAME.equals(entry.getKey())) {
128
129 item = getJcrItem();
130 }
131 }
132 }
133
134
135
136
137
138
139 protected abstract void updateProperty(Item item, String propertyId, Property property);
140
141 }