Clover icon

Google Sitemap Module 2.4.3

  1. Project Clover database Thu May 11 2017 16:52:32 CEST
  2. Package info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.util

File SiteMapEntryContainer.java

 

Coverage histogram

../../../../../../../../img/srcFileCovDistChart6.png
55% of files have more coverage

Code metrics

14
50
12
1
178
117
21
0.42
4.17
12
1.75

Classes

Class Line # Actions
SiteMapEntryContainer 58 50 0% 21 34
0.5526315655.3%
 

Contributing tests

This file is covered by 8 tests. .

Source view

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.data.Container;
53    import com.vaadin.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  8 toggle public SiteMapEntryContainer(String siteMapPath, boolean isVirtualUris, SiteMapService siteMapService) {
73  8 super(SiteMapEntry.class);
74  8 this.siteMapPath = siteMapPath;
75  8 virtualUris = isVirtualUris;
76  8 this.siteMapService = siteMapService;
77  8 refresh();
78    }
79   
 
80  1 toggle @Override
81    public Collection<?> getChildren(Object itemId) {
82  1 SiteMapEntry entry = (SiteMapEntry)itemId;
83   
84  1 int maxLevel = entry.getLevel() + 1;
85  1 List<SiteMapEntry> children = new ArrayList<SiteMapEntry>();
86  1 int index = indexOfId(entry) + 1;
87  12 while (index < size()) {
88  11 SiteMapEntry childEntry = getIdByIndex(index++);
89  11 if (childEntry.getLevel() > maxLevel) {
90  9 continue;
91    }
92  2 if (childEntry.getLevel() < maxLevel) {
93  0 break;
94    }
95  2 children.add(childEntry);
96    }
97  1 return children;
98    }
99   
 
100  0 toggle @Override
101    public Object getParent(Object itemId) {
102  0 SiteMapEntry parent = null;
103  0 SiteMapEntry child = (SiteMapEntry)itemId;
104  0 int index = indexOfId(itemId);
105  0 while (index > 0 && parent == null) {
106  0 SiteMapEntry id = getIdByIndex(--index);
107  0 if (id.getLevel() < child.getLevel()) {
108  0 parent = id;
109    }
110    }
111  0 return parent;
112    }
113   
 
114  0 toggle @Override
115    public Collection<?> rootItemIds() {
116  0 List<SiteMapEntry> roots = new ArrayList<SiteMapEntry>();
117  0 for (SiteMapEntry entry : getItemIds()) {
118  0 if (entry.getLevel() == minLevel) {
119  0 roots.add(entry);
120    }
121    }
122  0 return roots;
123    }
124   
 
125  0 toggle @Override
126    public boolean areChildrenAllowed(Object itemId) {
127  0 return getChildren(itemId).size() > 0;
128    }
129   
 
130  0 toggle @Override
131    public boolean isRoot(Object itemId) {
132  0 return ((SiteMapEntry)itemId).getLevel() == minLevel;
133    }
134   
 
135  0 toggle @Override
136    public boolean hasChildren(Object itemId) {
137  0 return ((SiteMapEntry)itemId).getLevel() < maxLevel;
138    }
139   
 
140  0 toggle @Override
141    public boolean setParent(Object itemId, Object newParentId) throws UnsupportedOperationException {
142  0 throw new UnsupportedOperationException();
143    }
144   
 
145  0 toggle @Override
146    public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) throws UnsupportedOperationException {
147  0 throw new UnsupportedOperationException();
148    }
149   
 
150  10 toggle private void calculateBoundLevels() {
151  10 Collection<SiteMapEntry> ids = getItemIds();
152  10 for (SiteMapEntry id : ids) {
153  57 int level = id.getLevel();
154  57 maxLevel = Math.max(maxLevel, level);
155  57 minLevel = Math.min(minLevel, level);
156    }
157    }
158   
 
159  10 toggle protected List<SiteMapEntry> fetchSiteMapEntries() {
160  10 List<SiteMapEntry> entries = new ArrayList<SiteMapEntry>();
161  10 try {
162  10 Node sitemapNode = SessionUtil.getNode(GoogleSiteMapConfiguration.WORKSPACE, siteMapPath);
163  10 if (sitemapNode != null) {
164  9 entries.addAll(siteMapService.getSiteMapBeans(sitemapNode, virtualUris, true));
165    }
166    } catch (RepositoryException e) {
167  0 log.error("Failed to accumulate site map beans for pages", e);
168    }
169  10 return entries;
170    }
171   
 
172  10 toggle @Override
173    public void refresh() {
174  10 removeAllItems();
175  10 addAll(fetchSiteMapEntries());
176  10 calculateBoundLevels();
177    }
178    }