View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.dam.external.app.contentview;
35  
36  import info.magnolia.dam.api.Folder;
37  import info.magnolia.dam.api.ItemKey;
38  import info.magnolia.dam.external.app.contentconnector.AssetContentConnector;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.Collections;
43  import java.util.HashMap;
44  import java.util.Iterator;
45  import java.util.List;
46  import java.util.Map;
47  
48  import com.vaadin.data.Container;
49  
50  /**
51   * Hierarchical implementation of {@link AbstractAssetContainer}.
52   */
53  public class HierarchicalAssetContainer extends AbstractAssetContainer implements Container.Hierarchical {
54  
55      private List<ItemKey> rootItemKeys = new ArrayList<ItemKey>();
56  
57      private Map<ItemKey, List<ItemKey>> children = new HashMap<ItemKey, List<ItemKey>>();
58  
59      public HierarchicalAssetContainer(AssetContentConnector assetContentConnector) {
60          super(assetContentConnector);
61      }
62  
63      @Override
64      public void refresh() {
65          rootItemKeys.clear();
66          children.clear();
67          super.refresh();
68      }
69  
70      /* {@link Container.Hierarchical} interface */
71  
72      @Override
73      public Collection<ItemKey> getChildren(Object itemId) {
74          final List<ItemKey> result = new ArrayList<ItemKey>();
75  
76          if (itemId instanceof ItemKey) {
77              if (children.containsKey(itemId)) {
78                  result.addAll(children.get(itemId));
79              } else {
80                  info.magnolia.dam.api.Item item = getAssetItem(itemId);
81                  if (item != null && item.isFolder()) {
82                      Iterator<info.magnolia.dam.api.Item> iterator = ((Folder) item).getChildren();
83                      while (iterator.hasNext()) {
84                          info.magnolia.dam.api.Item childItem = iterator.next();
85                          result.add(childItem.getItemKey());
86                          getItem(childItem.getItemKey());
87                      }
88                      children.put((ItemKey) itemId, result);
89                  }
90              }
91          }
92          return Collections.unmodifiableCollection(result);
93      }
94  
95      @Override
96      public ItemKey getParent(Object itemId) {
97          if (!(itemId instanceof ItemKey)) {
98              return null;
99          }
100         if (isRoot(itemId)) {
101             return null;
102         }
103         info.magnolia.dam.api.Item item = getAssetItem(itemId);
104         if (item == null || (item.isFolder() && ((Folder) item).isRoot())) {
105             return null;
106         }
107         return item.getParent().getItemKey();
108     }
109 
110     @Override
111     public Collection<ItemKey> rootItemIds() {
112         if (rootItemKeys.isEmpty()) {
113             Iterator<info.magnolia.dam.api.Item> iterator = getAssetProvider().getRootFolder().getChildren();
114             while (iterator.hasNext()) {
115                 info.magnolia.dam.api.Item item = iterator.next();
116                 rootItemKeys.add(item.getItemKey());
117                 getItem(item.getItemKey());
118             }
119         }
120         return Collections.unmodifiableCollection(rootItemKeys);
121     }
122 
123     @Override
124     public boolean areChildrenAllowed(Object itemId) {
125         info.magnolia.dam.api.Item item = getAssetItem(itemId);
126         if (item != null && item.isFolder()) {
127             return true;
128         }
129         return false;
130     }
131 
132     @Override
133     public boolean isRoot(Object itemId) {
134         info.magnolia.dam.api.Item item = getAssetItem(itemId);
135         if (item == null) {
136             return true;
137         }
138         if (item.isAsset()) {
139             return false;
140         }
141         return ((Folder) item).isRoot();
142     }
143 
144     @Override
145     public boolean hasChildren(Object itemId) {
146         info.magnolia.dam.api.Item item = getAssetItem(itemId);
147         if (item == null || item.isAsset()) {
148             return false;
149         }
150         return ((Folder) item).getChildren().hasNext();
151     }
152 
153     @Override
154     public List<String> getSortableContainerPropertyIds() {
155         // at present tree view is not sortable
156         return Collections.emptyList();
157     }
158 
159     @Override
160     public boolean setParent(Object itemId, Object newParentId) throws UnsupportedOperationException {
161         throw new UnsupportedOperationException(
162                 "Asset container does not support 'setParent()' operation");
163     }
164 
165     @Override
166     public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) throws UnsupportedOperationException {
167         throw new UnsupportedOperationException(
168                 "Asset container does not support 'setChildrenAllowed()' operation");
169     }
170 }