001/* 002 * Copyright 2015 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.site; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import javax.jcr.RepositoryException; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.environment.ObjectModelHelper; 029import org.apache.cocoon.environment.Redirector; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.environment.SourceResolver; 032 033import org.ametys.cms.repository.Content; 034import org.ametys.cms.repository.ContentQueryHelper; 035import org.ametys.core.cocoon.JSonReader; 036import org.ametys.plugins.repository.AmetysObjectIterable; 037import org.ametys.plugins.repository.AmetysObjectResolver; 038import org.ametys.runtime.i18n.I18nizableText; 039import org.ametys.web.repository.site.Site; 040import org.ametys.web.repository.sitemap.Sitemap; 041 042/** 043 * Provides some statistics about all the sites, such as:<br> 044 * <ul> 045 * <li>Number of sites 046 * <li>Number of live contents 047 * <li>Number of orphaned contents 048 * <li>Number of external contents 049 * </ul> 050 */ 051public class GlobalStatisticsAction extends SiteStatisticsAction 052{ 053 /** The ametys object resolver. */ 054 protected AmetysObjectResolver _ametysResolver; 055 056 @Override 057 public void service(ServiceManager serviceManager) throws ServiceException 058 { 059 super.service(serviceManager); 060 _ametysResolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 061 } 062 063 @Override 064 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 065 { 066 Request request = ObjectModelHelper.getRequest(objectModel); 067 Map<String, Object> result = new HashMap<>(); 068 069 List<Map<String, Object>> statistics = new ArrayList<>(); 070 071 // Global statistics 072 statistics.add(_global2json()); 073 074 // Contents 075 statistics.add(_contents2json()); 076 077 // Resources 078 statistics.add(_resources2json()); 079 080 // Sitemap 081 statistics.add(_sitemaps2json()); 082 083 result.put("values", statistics); 084 085 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 086 087 return EMPTY_MAP; 088 } 089 090 private Map<String, Object> _global2json () 091 { 092 Map<String, Object> global2json = new HashMap<>(); 093 global2json.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_GLOBAL")); 094 global2json.put("type", "global"); 095 096 List<Map<String, Object>> values = new ArrayList<>(); 097 098 // Number of contents 099 String query = ContentQueryHelper.getContentXPathQuery(null); 100 AmetysObjectIterable<Content> contents = _ametysResolver.query(query); 101 Map<String, Object> nbContents = new HashMap<>(); 102 nbContents.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_VALUE_GLOBAL_CONTENTCOUNT")); 103 nbContents.put("value", contents.getSize()); 104 values.add(nbContents); 105 106 // Number of sites 107 AmetysObjectIterable<Site> sites = _siteManager.getSites(); 108 Map<String, Object> nbSites = new HashMap<>(); 109 nbSites.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_VALUE_GLOBAL_SITECOUNT")); 110 nbSites.put("value", sites.getSize()); 111 values.add(nbSites); 112 113 global2json.put("values", values); 114 115 return global2json; 116 } 117 118 private Map<String, Object> _contents2json () throws RepositoryException 119 { 120 Map<String, Object> contents2json = new HashMap<>(); 121 122 contents2json.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_CONTENTS")); 123 contents2json.put("type", "contents"); 124 125 HashMap<String, Long> values = new HashMap<>(); 126 values.put("COUNT", 0L); 127 values.put("ORPHANED", 0L); 128 values.put("EXTERNAL", 0L); 129 130 AmetysObjectIterable<Site> sites = _siteManager.getSites(); 131 132 for (Site site : sites) 133 { 134 _processContents (site, values); 135 } 136 137 List<Map<String, Object>> values2json = new ArrayList<>(); 138 139 Map<String, Object> nbContents = new HashMap<>(); 140 nbContents.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_VALUE_CONTENTS_SITECONTENTS")); 141 nbContents.put("value", values.get("COUNT")); 142 nbContents.put("values", new ArrayList<>()); 143 values2json.add(nbContents); 144 145 Map<String, Object> nbOrphans = new HashMap<>(); 146 nbOrphans.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_VALUE_CONTENTS_ORPHANED")); 147 nbOrphans.put("value", values.get("ORPHANED")); 148 nbOrphans.put("values", new ArrayList<>()); 149 values2json.add(nbOrphans); 150 151 contents2json.put("values", values2json); 152 153 return contents2json; 154 } 155 156 /** 157 * Convert resources to JSON 158 * @return the resources in JSON 159 */ 160 protected Map<String, Object> _resources2json() 161 { 162 Map<String, Object> resources2json = new HashMap<>(); 163 164 resources2json.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_RESOURCES")); 165 resources2json.put("type", "resources"); 166 167 HashMap<String, Long> values = new HashMap<>(); 168 values.put("FOLDERCOUNT", 0L); 169 values.put("RESOURCECOUNT", 0L); 170 values.put("TOTALSIZE", 0L); 171 172 AmetysObjectIterable<Site> sites = _siteManager.getSites(); 173 for (Site site : sites) 174 { 175 _processResources (site.getRootResources(), values); 176 } 177 178 List<Map<String, Object>> values2json = new ArrayList<>(); 179 180 for (String name : values.keySet()) 181 { 182 Map<String, Object> stat = new HashMap<>(); 183 stat.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITE_STATISTICS_VALUE_RESOURCES_" + name)); 184 stat.put("value", values.get(name)); 185 stat.put("values", new ArrayList<>()); 186 values2json.add(stat); 187 } 188 189 resources2json.put("values", values2json); 190 191 return resources2json; 192 } 193 194 /** 195 * Convert sitemaps to JSON 196 * @return the sitemaps in JSON 197 */ 198 protected Map<String, Object> _sitemaps2json() 199 { 200 Map<String, Object> sitemap2json = new HashMap<>(); 201 202 sitemap2json.put("type", "sitemap"); 203 sitemap2json.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_GLOBAL_STATISTICS_PAGES")); 204 205 Map<String, Long> values = new HashMap<>(); 206 values.put("NBPAGES", 0L); // Number of pages 207 values.put("NBCACHEABLE", 0L); // Number of cacheable pages 208 values.put("SERVICES", 0L); // Number of services 209 values.put("CONTENTS", 0L); // Number of contents 210 211 AmetysObjectIterable<Site> sites = _siteManager.getSites(); 212 for (Site site : sites) 213 { 214 for (Sitemap sitemap : site.getSitemaps()) 215 { 216 _processPages(sitemap, values); 217 } 218 } 219 220 List<Map<String, Object>> values2json = new ArrayList<>(); 221 222 for (String name : values.keySet()) 223 { 224 Map<String, Object> stat = new HashMap<>(); 225 stat.put("label", new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITE_STATISTICS_VALUE_SITEMAP_" + name)); 226 stat.put("value", values.get(name)); 227 stat.put("values", new ArrayList<>()); 228 values2json.add(stat); 229 230 } 231 232 sitemap2json.put("values", values2json); 233 234 return sitemap2json; 235 } 236 237}