001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (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.containerpage.inherited; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsPropertyDefinition; 032import org.opencms.file.CmsResource; 033import org.opencms.file.CmsVfsResourceNotFoundException; 034import org.opencms.main.CmsException; 035import org.opencms.main.OpenCms; 036import org.opencms.xml.containerpage.CmsContainerElementBean; 037 038import java.util.HashSet; 039import java.util.Iterator; 040import java.util.List; 041import java.util.Locale; 042import java.util.Map; 043import java.util.Set; 044 045/** 046 * Utility methods for inheritance groups which don't fit anywhere else.<p> 047 */ 048public final class CmsInheritanceGroupUtils { 049 050 /** 051 * Private constructor to prevent instantiation.<p> 052 */ 053 private CmsInheritanceGroupUtils() { 054 055 // do nothing 056 } 057 058 /** 059 * Finds the inheritance group content with a given internal name.<p> 060 * 061 * Currently this is implemented as a property search, which may be potentially slow.<p> 062 * 063 * @param cms the current CMS context 064 * @param name the name to search 065 * 066 * @return the inheritance group resource 067 * 068 * @throws CmsException if something goes wrong 069 */ 070 public static CmsResource getInheritanceGroupContentByName(CmsObject cms, String name) throws CmsException { 071 072 String oldSiteRoot = cms.getRequestContext().getSiteRoot(); 073 try { 074 cms.getRequestContext().setSiteRoot(""); 075 List<CmsResource> resources = cms.readResourcesWithProperty( 076 "/", 077 CmsPropertyDefinition.PROPERTY_KEYWORDS, 078 name); 079 Iterator<CmsResource> resourceIter = resources.iterator(); 080 while (resourceIter.hasNext()) { 081 CmsResource currentRes = resourceIter.next(); 082 if (!OpenCms.getResourceManager().getResourceType(currentRes).getTypeName().equals( 083 "inheritance_group")) { 084 resourceIter.remove(); 085 } 086 } 087 if (resources.isEmpty()) { 088 throw new CmsVfsResourceNotFoundException( 089 org.opencms.gwt.Messages.get().container( 090 org.opencms.gwt.Messages.ERR_INHERITANCE_GROUP_NOT_FOUND_1, 091 name)); 092 } 093 return resources.get(0); 094 } finally { 095 cms.getRequestContext().setSiteRoot(oldSiteRoot); 096 } 097 } 098 099 /** 100 * Parses an inheritance group configuration C and returns the names of inheritance groups in C in which a given resource 101 * is defined as a new element.<p> 102 * 103 * @param cms the current CMS context 104 * @param inheritanceConfig the inheritance configuration resource 105 * @param target the resource to search in the inheritance configuration 106 * 107 * @return the names of the inheritance groups in which the target resource is defined as a new element 108 * 109 * @throws CmsException if something goes wrong 110 */ 111 public static Set<String> getNamesOfGroupsContainingResource( 112 CmsObject cms, 113 CmsResource inheritanceConfig, 114 CmsResource target) throws CmsException { 115 116 Set<String> names = new HashSet<String>(); 117 CmsContainerConfigurationParser parser = new CmsContainerConfigurationParser(cms); 118 parser.parse(inheritanceConfig); 119 Map<Locale, Map<String, CmsContainerConfiguration>> contents = parser.getParsedResults(); 120 for (Map<String, CmsContainerConfiguration> mapForLocale : contents.values()) { 121 for (Map.Entry<String, CmsContainerConfiguration> entry : mapForLocale.entrySet()) { 122 String key = entry.getKey(); 123 CmsContainerConfiguration config = entry.getValue(); 124 for (CmsContainerElementBean element : config.getNewElements().values()) { 125 if (element.getId().equals(target.getStructureId())) { 126 names.add(key); 127 } 128 } 129 } 130 } 131 return names; 132 } 133 134}