001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.repository.site; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.environment.ObjectModelHelper; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.generation.ServiceableGenerator; 026import org.apache.cocoon.xml.AttributesImpl; 027import org.apache.cocoon.xml.XMLUtils; 028import org.apache.commons.lang.StringUtils; 029import org.xml.sax.SAXException; 030 031import org.ametys.cms.repository.Content; 032import org.ametys.plugins.repository.AmetysObject; 033import org.ametys.plugins.repository.AmetysObjectIterable; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.plugins.repository.query.SortCriteria; 036import org.ametys.plugins.repository.query.expression.Expression; 037import org.ametys.web.filter.SharedContentsHelper; 038 039/** 040 * SAX sites with shared contents information 041 * 042 */ 043public class SharedSitesGenerator extends ServiceableGenerator 044{ 045 private SiteManager _siteManager; 046 private AmetysObjectResolver _resolver; 047 private SiteTypesExtensionPoint _siteTypeExtensionPoint; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 054 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 055 _siteTypeExtensionPoint = (SiteTypesExtensionPoint) smanager.lookup(SiteTypesExtensionPoint.ROLE); 056 } 057 058 @Override 059 public void generate() throws IOException, SAXException, ProcessingException 060 { 061 Request request = ObjectModelHelper.getRequest(objectModel); 062 String currentSiteName = request.getParameter("siteName"); 063 064 String id = request.getParameter("id"); 065 066 Site currentSite = null; 067 if (StringUtils.isNotBlank(currentSiteName)) 068 { 069 currentSite = _siteManager.getSite(currentSiteName); 070 } 071 072 contentHandler.startDocument(); 073 074 XMLUtils.startElement(contentHandler, "sites"); 075 076 if (StringUtils.isNotEmpty(id) && (id.startsWith("collection://") || id.startsWith("site://"))) 077 { 078 AmetysObject aObject = _resolver.resolveById(id); 079 080 try (AmetysObjectIterable<Site> rootSites = aObject instanceof Site ? ((Site) aObject).getChildrenSites() : _siteManager.getRootSites()) 081 { 082 for (Site site : rootSites) 083 { 084 _saxSite (site, currentSite); 085 } 086 } 087 } 088 else 089 { 090 AmetysObjectIterable<Site> rootSites = _siteManager.getRootSites(); 091 for (Site site : rootSites) 092 { 093 _saxSite (site, currentSite); 094 } 095 } 096 XMLUtils.endElement(contentHandler, "sites"); 097 contentHandler.endDocument(); 098 } 099 100 private void _saxSite (Site site, Site currentSite) throws SAXException 101 { 102 AttributesImpl attr = new AttributesImpl(); 103 attr.addAttribute("", "id", "id", "CDATA", site.getId()); 104 attr.addAttribute("", "type", "type", "CDATA", site.getType()); 105 attr.addAttribute("", "name", "name", "CDATA", site.getName()); 106 107 if (currentSite != null) 108 { 109 if (site.getId().equals(currentSite.getId())) 110 { 111 attr.addAttribute("", "current", "current", "CDATA", "true"); 112 } 113 long nbSharedContents = _sharedContentsSize (site, currentSite); 114 attr.addAttribute("", "shared-contents", "shared-contents", "CDATA", String.valueOf(nbSharedContents)); 115 } 116 117 XMLUtils.startElement(contentHandler, "site", attr); 118 119 XMLUtils.createElement(contentHandler, "name", site.getName()); 120 String title = site.getTitle() != null ? site.getTitle() : site.getName(); 121 XMLUtils.createElement(contentHandler, "title", title); 122 123 XMLUtils.createElement(contentHandler, "url", site.getUrl() != null ? site.getUrl() : ""); 124 125 SiteType siteType = _siteTypeExtensionPoint.getExtension(site.getType()); 126 127 String iconGlyph = siteType.getIconGlyph(); 128 if (iconGlyph != null) 129 { 130 XMLUtils.createElement(contentHandler, "iconGlyph", siteType.getIconGlyph()); 131 } 132 133 XMLUtils.createElement(contentHandler, "icon", siteType.getSmallIcon()); 134 XMLUtils.createElement(contentHandler, "iconLarge", siteType.getLargeIcon()); 135 136 XMLUtils.endElement(contentHandler, "site"); 137 } 138 139 private long _sharedContentsSize (Site site, Site currentSite) 140 { 141 Expression expr = SharedContentsHelper.getSharedContentsExpression(currentSite, site); 142 143 SortCriteria sortCriteria = new SortCriteria(); 144 sortCriteria.addCriterion("title", true, true); 145 String xPathQuery = org.ametys.plugins.repository.query.QueryHelper.getXPathQuery(null, "ametys:content", expr, sortCriteria); 146 147 AmetysObjectIterable<Content> contents = _resolver.query(xPathQuery); 148 return contents.getSize(); 149 } 150 151}