]> git.argeo.org Git - lgpl/argeo-commons.git/blob - widgets/JcrComposite.java
Prepare next development cycle
[lgpl/argeo-commons.git] / widgets / JcrComposite.java
1 package org.argeo.cms.ui.widgets;
2
3 import javax.jcr.Item;
4 import javax.jcr.ItemNotFoundException;
5 import javax.jcr.Node;
6 import javax.jcr.Property;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
10 import org.argeo.cms.ui.util.CmsUiUtils;
11 import org.argeo.jcr.JcrException;
12 import org.eclipse.swt.widgets.Composite;
13
14 /** A composite which can (optionally) manage a JCR Item. */
15 public class JcrComposite extends Composite {
16 private static final long serialVersionUID = -1447009015451153367L;
17
18 private Session session;
19
20 private String nodeId;
21 private String property = null;
22 private Node cache;
23
24 /** Regular composite constructor. No layout is set. */
25 public JcrComposite(Composite parent, int style) {
26 super(parent, style);
27 session = null;
28 nodeId = null;
29 }
30
31 public JcrComposite(Composite parent, int style, Item item) {
32 this(parent, style, item, false);
33 }
34
35 public JcrComposite(Composite parent, int style, Item item, boolean cacheImmediately) {
36 super(parent, style);
37 if (item != null)
38 try {
39 this.session = item.getSession();
40 // if (!cacheImmediately && (SWT.READ_ONLY == (style & SWT.READ_ONLY))) {
41 // // (useless?) optimization: we only save a pointer to the session,
42 // // not even a reference to the item
43 // this.nodeId = null;
44 // } else {
45 Node node;
46 Property property = null;
47 if (item instanceof Node) {
48 node = (Node) item;
49 } else {// Property
50 property = (Property) item;
51 if (property.isMultiple())// TODO manage property index
52 throw new UnsupportedOperationException("Multiple properties not supported yet.");
53 this.property = property.getName();
54 node = property.getParent();
55 }
56 this.nodeId = node.getIdentifier();
57 if (cacheImmediately)
58 this.cache = node;
59 // }
60 setLayout(CmsUiUtils.noSpaceGridLayout());
61 } catch (RepositoryException e) {
62 throw new IllegalStateException("Cannot create composite from " + item, e);
63 }
64 }
65
66 public synchronized Node getNode() {
67 try {
68 if (!itemIsNode())
69 throw new IllegalStateException("Item is not a Node");
70 return getNodeInternal();
71 } catch (RepositoryException e) {
72 throw new JcrException("Cannot get node " + nodeId, e);
73 }
74 }
75
76 private synchronized Node getNodeInternal() throws RepositoryException {
77 if (cache != null)
78 return cache;
79 else if (session != null)
80 if (nodeId != null)
81 return session.getNodeByIdentifier(nodeId);
82 else
83 return null;
84 else
85 return null;
86 }
87
88 public synchronized Property getProperty() {
89 try {
90 if (itemIsNode())
91 throw new IllegalStateException("Item is not a Property");
92 Node node = getNodeInternal();
93 if (!node.hasProperty(property))
94 throw new IllegalStateException("Property " + property + " is not set on " + node);
95 return node.getProperty(property);
96 } catch (RepositoryException e) {
97 throw new JcrException("Cannot get property " + property + " from node " + nodeId, e);
98 }
99 }
100
101 public synchronized boolean itemIsNode() {
102 return property == null;
103 }
104
105 public synchronized boolean itemExists() {
106 if (session == null)
107 return false;
108 try {
109 Node n = session.getNodeByIdentifier(nodeId);
110 if (!itemIsNode())
111 return n.hasProperty(property);
112 else
113 return true;
114 } catch (ItemNotFoundException e) {
115 return false;
116 } catch (RepositoryException e) {
117 throw new JcrException("Cannot check whether node exists", e);
118 }
119 }
120
121 /** Set/update the cache or change the node */
122 public synchronized void setNode(Node node) {
123 if (!itemIsNode())
124 throw new IllegalArgumentException("Cannot set a Node on a Property");
125
126 if (node == null) {// clear cache
127 this.cache = null;
128 return;
129 }
130
131 try {
132 // if (session != null || session != node.getSession())// check session
133 // throw new IllegalArgumentException("Uncompatible session");
134 // if (session == null)
135 session = node.getSession();
136 if (nodeId == null || !nodeId.equals(node.getIdentifier())) {
137 nodeId = node.getIdentifier();
138 cache = node;
139 itemUpdated();
140 } else {
141 cache = node;// set/update cache
142 }
143 } catch (RepositoryException e) {
144 throw new IllegalStateException(e);
145 }
146 }
147
148 /** Set/update the cache or change the property */
149 public synchronized void setProperty(Property prop) {
150 if (itemIsNode())
151 throw new IllegalArgumentException("Cannot set a Property on a Node");
152
153 if (prop == null) {// clear cache
154 this.cache = null;
155 return;
156 }
157
158 try {
159 if (session == null || session != prop.getSession())// check session
160 throw new IllegalArgumentException("Uncompatible session");
161
162 Node node = prop.getNode();
163 if (nodeId == null || !nodeId.equals(node.getIdentifier()) || !property.equals(prop.getName())) {
164 nodeId = node.getIdentifier();
165 property = prop.getName();
166 cache = node;
167 itemUpdated();
168 } else {
169 cache = node;// set/update cache
170 }
171 } catch (RepositoryException e) {
172 throw new IllegalStateException(e);
173 }
174 }
175
176 public synchronized String getNodeId() {
177 return nodeId;
178 }
179
180 /** Change the node, does nothing if same. */
181 public synchronized void setNodeId(String nodeId) throws RepositoryException {
182 if (this.nodeId != null && this.nodeId.equals(nodeId))
183 return;
184 this.nodeId = nodeId;
185 if (cache != null)
186 cache = session.getNodeByIdentifier(this.nodeId);
187 itemUpdated();
188 }
189
190 protected synchronized void itemUpdated() {
191 layout();
192 }
193
194 public Session getSession() {
195 return session;
196 }
197 }