001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ade.sitemap.shared; 029 030import org.opencms.gwt.shared.CmsCategoryTreeEntry; 031import org.opencms.util.CmsUUID; 032 033import java.util.Iterator; 034import java.util.List; 035import java.util.Set; 036 037import com.google.common.base.Function; 038import com.google.common.collect.ArrayListMultimap; 039import com.google.common.collect.Lists; 040import com.google.common.collect.Multimap; 041import com.google.common.collect.Multimaps; 042import com.google.common.collect.Sets; 043import com.google.gwt.user.client.rpc.IsSerializable; 044 045/** 046 * The category data for the current sitemap.<p> 047 */ 048public class CmsSitemapCategoryData implements IsSerializable { 049 050 /** The base path for the categories. */ 051 private String m_basePath; 052 053 /** A forest of categories. */ 054 private List<CmsCategoryTreeEntry> m_categoryEntries = Lists.newArrayList(); 055 056 /** 057 * Default constructor.<p> 058 */ 059 public CmsSitemapCategoryData() { 060 061 } 062 063 /** 064 * Adds a new category entry.<p> 065 * 066 * @param item the category entry 067 */ 068 public void add(CmsCategoryTreeEntry item) { 069 070 m_categoryEntries.add(item); 071 } 072 073 /** 074 * Gets a multimap of the top-level entries, indexed by whether they are local categories or not.<p> 075 * 076 * A category counts as local if all of its parent categories are defined in the current sitemap. 077 * 078 * @return the multimap of entries 079 */ 080 public Multimap<Boolean, CmsCategoryTreeEntry> getEntriesIndexedByLocality() { 081 082 return ArrayListMultimap.create( 083 Multimaps.index(m_categoryEntries, new Function<CmsCategoryTreeEntry, Boolean>() { 084 085 @SuppressWarnings("synthetic-access") 086 public Boolean apply(CmsCategoryTreeEntry entry) { 087 088 return Boolean.valueOf(entry.getBasePath().equals(m_basePath)); 089 } 090 })); 091 } 092 093 /** 094 * Gets the category bean by id.<p> 095 * 096 * @param id a structure id 097 * @return the entry with the given id, or null if no such entry was found 098 */ 099 public CmsCategoryTreeEntry getEntryById(CmsUUID id) { 100 101 for (CmsCategoryTreeEntry entry : getFlatList()) { 102 if (id.equals(entry.getId())) { 103 return entry; 104 } 105 } 106 return null; 107 } 108 109 /** 110 * Sets the base path.<p> 111 * 112 * @param basePath the base path 113 */ 114 public void setBasePath(String basePath) { 115 116 m_basePath = basePath; 117 } 118 119 /** 120 * Returns the category entries in a flat list instead of a tree structure.<p> 121 * 122 * @return the list of all tree entries 123 */ 124 List<CmsCategoryTreeEntry> getFlatList() { 125 126 List<CmsCategoryTreeEntry> toProcess = Lists.newArrayList(m_categoryEntries); 127 List<CmsCategoryTreeEntry> result = Lists.newArrayList(); 128 Set<CmsUUID> visited = Sets.newHashSet(); 129 while (!toProcess.isEmpty()) { 130 Iterator<CmsCategoryTreeEntry> iter = toProcess.iterator(); 131 CmsCategoryTreeEntry entry = iter.next(); 132 iter.remove(); 133 if (!visited.contains(entry.getId())) { 134 result.add(entry); 135 visited.add(entry.getId()); 136 toProcess.addAll(entry.getChildren()); 137 } 138 } 139 return result; 140 } 141 142 /** 143 * Gets the top-level entries.<p> 144 * 145 * @return the top-level entries 146 */ 147 List<CmsCategoryTreeEntry> getRootEntries() { 148 149 return m_categoryEntries; 150 } 151 152}