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.resources.app.utils;
35
36 import info.magnolia.context.Context;
37 import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
38 import info.magnolia.resources.app.workbench.ResourcesContainer;
39 import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
40 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
41
42 import java.util.Collection;
43 import java.util.List;
44
45 import javax.jcr.Node;
46 import javax.jcr.RepositoryException;
47
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import com.google.common.collect.Lists;
52 import com.vaadin.data.Item;
53 import com.vaadin.data.Property;
54
55
56
57
58 public final class ResourceUtils {
59
60 private static final Logger log = LoggerFactory.getLogger(ResourceUtils.class);
61
62
63 private ResourceUtils() {
64 }
65
66
67
68
69
70 public static JcrNodeAdapter getJcrNodeAdapterFor(Context context, Item resourceItem) {
71 try {
72 Property resourcePathProperty = resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH);
73 if (resourcePathProperty != null) {
74 return new JcrNodeAdapter(context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE).getNode(resourcePathProperty.getValue().toString()));
75 } else {
76 throw new RepositoryException(String.format("Required [%s] property cannot be found", ResourcesContainer.RESOURCE_PATH));
77 }
78 } catch (RepositoryException e) {
79 throw new RuntimeException(e);
80 }
81 }
82
83
84
85
86
87 public static List<JcrItemAdapter> getJcrItemAdaptersFor(Context context, Collection<Item> resourceItems) {
88 List<JcrItemAdapter> adapters = Lists.newArrayList();
89
90 for (Item resourceItem : resourceItems) {
91 adapters.add(getJcrNodeAdapterFor(context, resourceItem));
92 }
93 return adapters;
94 }
95
96
97
98
99
100 public static Node extractParentItem(JcrItemAdapter jcrItemAdapter) {
101 try {
102 return jcrItemAdapter.getJcrItem().getParent();
103 } catch (RepositoryException ignored) {
104 log.info("Could not extract parent node of the given item {}", jcrItemAdapter);
105 }
106 return null;
107 }
108
109
110
111
112
113
114 public static boolean isDirectory(Item item) {
115 Property itemProperty = item.getItemProperty("directory");
116 return itemProperty != null && itemProperty.getValue() != null && Boolean.parseBoolean(itemProperty.getValue().toString());
117 }
118 }