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.version.VersionManager;
37 import info.magnolia.cms.core.version.VersionedNode;
38 import info.magnolia.cms.core.version.VersionedNodeChild;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.objectfactory.Components;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.jcr.Item;
46 import javax.jcr.ItemNotFoundException;
47 import javax.jcr.Node;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.Session;
50
51 import org.apache.commons.lang3.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59
60
61
62
63
64 public class JcrItemUtil {
65
66 private static final Logger log = LoggerFactory.getLogger(JcrItemUtil.class);
67
68
69
70
71 public static final String PROPERTY_NAME_AND_IDENTIFIER_SEPARATOR = "@";
72
73
74
75
76 public static String parseNodeIdentifier(final String itemId) {
77 return isPropertyItemId(itemId) ? itemId.substring(0, itemId.indexOf(PROPERTY_NAME_AND_IDENTIFIER_SEPARATOR)) : itemId;
78 }
79
80 public static String parsePropertyName(final String itemId) {
81 return itemId.substring(itemId.indexOf(PROPERTY_NAME_AND_IDENTIFIER_SEPARATOR) + 1);
82 }
83
84 public static boolean isPropertyItemId(final String itemId) {
85 return itemId != null && itemId.contains(PROPERTY_NAME_AND_IDENTIFIER_SEPARATOR);
86 }
87
88
89
90
91 public static Item getJcrItem(final JcrItemId itemId) throws RepositoryException {
92 if (itemId == null) {
93 return null;
94 }
95 Node node;
96 String workspaceName = itemId.getWorkspace();
97 try {
98 node = MgnlContext.getJCRSession(workspaceName).getNodeByIdentifier(itemId.getUuid());
99 if (itemId instanceof VersionedChildJcrItemId) {
100 Node parent = Components.getComponent(VersionManager.class).getVersion(node, ((VersionedChildJcrItemId) itemId).getParent().getVersionName());
101 node = parent.getNode(((VersionedChildJcrItemId) itemId).getRelPath());
102 } else if (itemId instanceof VersionedJcrItemId) {
103 node = Components.getComponent(VersionManager.class).getVersion(node, ((VersionedJcrItemId) itemId).getVersionName());
104 }
105 } catch (ItemNotFoundException e) {
106 log.debug("Couldn't find item with id {} in workspace {}.", itemId, workspaceName);
107 return null;
108 }
109
110 if (!(itemId instanceof JcrPropertyItemId)) {
111 return node;
112 }
113
114 final String propertyName = ((JcrPropertyItemId) itemId).getPropertyName();
115 if (node.hasProperty(propertyName)) {
116 return node.getProperty(propertyName);
117 }
118 return null;
119 }
120
121 public static boolean itemExists(JcrItemId itemId) throws RepositoryException {
122 return getJcrItem(itemId) != null;
123 }
124
125
126
127
128
129
130
131
132 public static JcrItemId getItemId(final Item jcrItem) throws RepositoryException {
133 if (jcrItem == null) {
134 return null;
135 }
136
137 if (jcrItem.isNode()) {
138 String identifier = ((Node) jcrItem).getIdentifier();
139 String workspace = jcrItem.getSession().getWorkspace().getName();
140 if (jcrItem instanceof VersionedNode) {
141 identifier = ((VersionedNode) jcrItem).getBaseNode().getIdentifier();
142 workspace = jcrItem.getSession().getWorkspace().getName();
143 return new VersionedJcrItemId(identifier, workspace, ((VersionedNode) jcrItem).unwrap().getName());
144 } else if (jcrItem instanceof VersionedNodeChild) {
145 Node parent = jcrItem.getParent();
146 String relpath = jcrItem.getName();
147
148 while (parent instanceof VersionedNodeChild) {
149 relpath = parent.getName() + "/" + relpath;
150 parent = parent.getParent();
151 }
152
153 identifier = ((VersionedNode) parent).getBaseNode().getIdentifier();
154 workspace = parent.getSession().getWorkspace().getName();
155 VersionedJcrItemId parentItemId = new VersionedJcrItemId(identifier, workspace, ((VersionedNode) parent).unwrap().getName());
156
157 return new VersionedChildJcrItemId(parentItemId, relpath);
158 }
159
160 return new JcrNodeItemId(identifier, workspace);
161 } else {
162 return new JcrPropertyItemId((jcrItem.getParent()).getIdentifier(), jcrItem.getSession().getWorkspace().getName(), jcrItem.getName());
163 }
164 }
165
166
167
168
169 public static JcrItemId getItemId(final String workspaceName, final String absPath) throws RepositoryException {
170
171 if (StringUtils.isEmpty(workspaceName) || StringUtils.isEmpty(absPath)) {
172 return null;
173 }
174
175 Session session = MgnlContext.getJCRSession(workspaceName);
176 if (!session.nodeExists(absPath)) {
177 return null;
178 }
179
180 return getItemId(session.getNode(absPath));
181 }
182
183 public static List<Item> getJcrItems(List<JcrItemId> ids) {
184
185 List<Item> items = new ArrayList<Item>();
186 for (JcrItemId id : ids) {
187 Item item;
188 try {
189 item = getJcrItem(id);
190 if (item != null) {
191 items.add(item);
192 }
193 } catch (RepositoryException e) {
194 log.debug("Cannot find item with id [{}] in workspace [{}].", id.getUuid(), id.getWorkspace());
195 } catch (IllegalArgumentException e1) {
196 log.debug("Workspace [{}] is not initialized.", id.getWorkspace());
197 }
198 }
199 return items;
200 }
201
202 public static String getItemPath(Item item) {
203 if (item == null) {
204 return null;
205 }
206 String path = "unknown";
207 try {
208 if (item.isNode()) {
209 path = item.getPath();
210 } else {
211 String parentPath = item.getParent().getPath();
212 String name = item.getName();
213 path = parentPath + PROPERTY_NAME_AND_IDENTIFIER_SEPARATOR + name;
214 }
215 } catch (RepositoryException re) {
216 log.error("Cannot get path for item: " + item);
217 }
218 return path;
219 }
220
221 }