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