View Javadoc
1   /**
2    * This file Copyright (c) 2015 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      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      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       * 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      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       * This method is extracting the parent node of a given item,
98       * Returns null if the parent node couldn't be found.
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      * 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     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 }