View Javadoc
1   /**
2    * This file Copyright (c) 2013-2017 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.module.googlesitemap.app.subapp.sitemapdetail.util;
35  
36  import info.magnolia.jcr.util.SessionUtil;
37  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
38  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
39  import info.magnolia.module.googlesitemap.service.SiteMapService;
40  import info.magnolia.ui.workbench.container.Refreshable;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.List;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.vaadin.v7.data.Container;
53  import com.vaadin.v7.data.util.BeanItemContainer;
54  
55  /**
56   * Container capable of keeping objects of type {@link SiteMapEntry} and resolving their hierarchy.
57   */
58  public class SiteMapEntryContainer extends BeanItemContainer<SiteMapEntry> implements Container.Hierarchical, Refreshable {
59  
60      private static Logger log = LoggerFactory.getLogger(SiteMapEntryContainer.class);
61  
62      private int maxLevel = Integer.MIN_VALUE;
63  
64      private int minLevel = Integer.MAX_VALUE;
65  
66      private String siteMapPath;
67  
68      private boolean virtualUris;
69  
70      private SiteMapService siteMapService;
71  
72      public SiteMapEntryContainer(String siteMapPath, boolean isVirtualUris, SiteMapService siteMapService) {
73          super(SiteMapEntry.class);
74          this.siteMapPath = siteMapPath;
75          virtualUris = isVirtualUris;
76          this.siteMapService = siteMapService;
77          refresh();
78      }
79  
80      @Override
81      public Collection<?> getChildren(Object itemId) {
82          SiteMapEntry entry = (SiteMapEntry)itemId;
83  
84          int maxLevel = entry.getLevel() + 1;
85          List<SiteMapEntry> children = new ArrayList<SiteMapEntry>();
86          int index = indexOfId(entry) + 1;
87          while (index < size()) {
88              SiteMapEntry childEntry = getIdByIndex(index++);
89              if (childEntry.getLevel() > maxLevel) {
90                  continue;
91              }
92              if (childEntry.getLevel() < maxLevel) {
93                  break;
94              }
95              children.add(childEntry);
96          }
97          return children;
98      }
99  
100     @Override
101     public Object getParent(Object itemId) {
102         SiteMapEntry parent = null;
103         SiteMapEntry child = (SiteMapEntry)itemId;
104         int index = indexOfId(itemId);
105         while (index > 0 && parent == null) {
106             SiteMapEntry id = getIdByIndex(--index);
107             if (id.getLevel() < child.getLevel()) {
108                 parent = id;
109             }
110         }
111         return parent;
112     }
113 
114     @Override
115     public Collection<?> rootItemIds() {
116         List<SiteMapEntry> roots = new ArrayList<SiteMapEntry>();
117         for (SiteMapEntry entry : getItemIds()) {
118             if (entry.getLevel() == minLevel) {
119                 roots.add(entry);
120             }
121         }
122         return roots;
123     }
124 
125     @Override
126     public boolean areChildrenAllowed(Object itemId) {
127         return getChildren(itemId).size() > 0;
128     }
129 
130     @Override
131     public boolean isRoot(Object itemId) {
132         return ((SiteMapEntry)itemId).getLevel() == minLevel;
133     }
134 
135     @Override
136     public boolean hasChildren(Object itemId) {
137         return ((SiteMapEntry)itemId).getLevel() < maxLevel;
138     }
139 
140     @Override
141     public boolean setParent(Object itemId, Object newParentId) throws UnsupportedOperationException {
142         throw new UnsupportedOperationException();
143     }
144 
145     @Override
146     public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) throws UnsupportedOperationException {
147         throw new UnsupportedOperationException();
148     }
149 
150     private void calculateBoundLevels() {
151         Collection<SiteMapEntry> ids = getItemIds();
152         for (SiteMapEntry id : ids) {
153             int level = id.getLevel();
154             maxLevel = Math.max(maxLevel, level);
155             minLevel = Math.min(minLevel, level);
156         }
157     }
158 
159     protected List<SiteMapEntry> fetchSiteMapEntries() {
160         List<SiteMapEntry> entries = new ArrayList<SiteMapEntry>();
161         try {
162             Node sitemapNode = SessionUtil.getNode(GoogleSiteMapConfiguration.WORKSPACE, siteMapPath);
163             if (sitemapNode != null) {
164                 entries.addAll(siteMapService.getSiteMapBeans(sitemapNode, virtualUris, true));
165             }
166         } catch (RepositoryException e) {
167             log.error("Failed to accumulate site map beans for pages", e);
168         }
169         return entries;
170     }
171 
172     @Override
173     public void refresh() {
174         removeAllItems();
175         addAll(fetchSiteMapEntries());
176         calculateBoundLevels();
177     }
178 }