Clover icon

Magnolia Resources App Module 2.4.7

  1. Project Clover database Fri Sep 9 2016 16:27:56 CEST
  2. Package info.magnolia.resources.app.utils

File ResourceUtils.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
52% of files have more coverage

Code metrics

2
16
5
1
118
51
8
0.5
3.2
5
1.6

Classes

Class Line # Actions
ResourceUtils 58 16 0% 8 6
0.7391304473.9%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Utilities for resources.
57    */
 
58    public final class ResourceUtils {
59   
60    private static final Logger log = LoggerFactory.getLogger(ResourceUtils.class);
61   
62    // Making sure this class cannot be instantiated.
 
63  0 toggle private ResourceUtils() {
64    }
65   
66    /**
67    * This method is assuming that given item has ResourcesContainer.RESOURCE_PATH property,
68    * In addition, it is also assuming that one who is using the method is searching for the given item in JcrResourceOrigin.RESOURCES_WORKSPACE.
69    */
 
70  23 toggle public static JcrNodeAdapter getJcrNodeAdapterFor(Context context, Item resourceItem) {
71  23 try {
72  23 Property resourcePathProperty = resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH);
73  23 if (resourcePathProperty != null) {
74  23 return new JcrNodeAdapter(context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE).getNode(resourcePathProperty.getValue().toString()));
75    } else {
76  0 throw new RepositoryException(String.format("Required [%s] property cannot be found", ResourcesContainer.RESOURCE_PATH));
77    }
78    } catch (RepositoryException e) {
79  0 throw new RuntimeException(e);
80    }
81    }
82   
83    /**
84    * This method is assuming that given list of items have ResourcesContainer.RESOURCE_PATH property,
85    * In addition, it is also assuming that one who is using the method is searching for the given items in JcrResourceOrigin.RESOURCES_WORKSPACE.
86    */
 
87  14 toggle public static List<JcrItemAdapter> getJcrItemAdaptersFor(Context context, Collection<Item> resourceItems) {
88  14 List<JcrItemAdapter> adapters = Lists.newArrayList();
89   
90  14 for (Item resourceItem : resourceItems) {
91  15 adapters.add(getJcrNodeAdapterFor(context, resourceItem));
92    }
93  14 return adapters;
94    }
95   
96    /**
97    * This method is extracting the parent node of a given item,
98    * Returns null if the parent node couldn't be found.
99    */
 
100  5 toggle public static Node extractParentItem(JcrItemAdapter jcrItemAdapter) {
101  5 try {
102  5 return jcrItemAdapter.getJcrItem().getParent();
103    } catch (RepositoryException ignored) {
104  0 log.info("Could not extract parent node of the given item {}", jcrItemAdapter);
105    }
106  0 return null;
107    }
108   
109    /**
110    * This method checks if given item is a directory.
111    * If item has property which identifies that is a directory, then this method returns {@code true}
112    * Otherwise {@code false}
113    */
 
114  4 toggle public static boolean isDirectory(Item item) {
115  4 Property itemProperty = item.getItemProperty("directory");
116  4 return itemProperty != null && itemProperty.getValue() != null && Boolean.parseBoolean(itemProperty.getValue().toString());
117    }
118    }