001/* 002 * Copyright 2020 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.plugins.workspaces.events.activitystream; 017 018import java.util.Collection; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.context.Context; 023import org.apache.avalon.framework.context.ContextException; 024import org.apache.avalon.framework.context.Contextualizable; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028import org.apache.cocoon.ProcessingException; 029import org.apache.cocoon.components.ContextHelper; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.xml.AttributesImpl; 032import org.apache.cocoon.xml.XMLUtils; 033import org.xml.sax.ContentHandler; 034import org.xml.sax.SAXException; 035 036import org.ametys.plugins.workspaces.project.ProjectManager; 037import org.ametys.plugins.workspaces.project.ProjectWorkspaceSiteType; 038import org.ametys.runtime.plugin.component.AbstractLogEnabled; 039import org.ametys.web.WebConstants; 040import org.ametys.web.inputdata.InputData; 041import org.ametys.web.repository.page.Page; 042import org.ametys.web.repository.site.Site; 043import org.ametys.web.repository.site.SiteManager; 044 045/** 046 * This input data saxes the user's events on projects 047 * 048 */ 049public class ActivityStreamInputData extends AbstractLogEnabled implements InputData, Serviceable, Contextualizable 050{ 051 private static final int __RESULTS_DEFAULT_LIMIT = 100; 052 053 private ActivityStreamClientInteraction _activityStream; 054 private SiteManager _siteManager; 055 private ProjectManager _projectManager; 056 057 private Context _context; 058 059 @Override 060 public void service(ServiceManager smanager) throws ServiceException 061 { 062 _activityStream = (ActivityStreamClientInteraction) smanager.lookup(ActivityStreamClientInteraction.ROLE); 063 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 064 _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE); 065 } 066 067 @Override 068 public void contextualize(Context context) throws ContextException 069 { 070 _context = context; 071 } 072 073 @Override 074 public boolean isCacheable(Site site, Page page) 075 { 076 return !_isActive(site); 077 } 078 079 @Override 080 public void toSAX(ContentHandler contentHandler) throws SAXException, ProcessingException 081 { 082 if (_isActive()) 083 { 084 contentHandler.startDocument(); 085 XMLUtils.startElement(contentHandler, "projects-activities"); 086 087 List<Map<String, Object>> events = _activityStream.getEventsForCurrentUser(__RESULTS_DEFAULT_LIMIT); 088 089 for (Map<String, Object> event : events) 090 { 091 XMLUtils.startElement(contentHandler, "events"); 092 for (String key : event.keySet()) 093 { 094 _saxObject(contentHandler, event.get(key), key); 095 } 096 XMLUtils.endElement(contentHandler, "events"); 097 } 098 099 XMLUtils.endElement(contentHandler, "projects-activities"); 100 contentHandler.endDocument(); 101 } 102 } 103 104 private boolean _isActive() 105 { 106 Request request = ContextHelper.getRequest(_context); 107 108 String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 109 Site site = _siteManager.getSite(siteName); 110 111 return _isActive(site); 112 } 113 114 private boolean _isActive(Site site) 115 { 116 return site.getName().equals(_projectManager.getCatalogSiteName()) || ProjectWorkspaceSiteType.TYPE_ID.equals(site.getType()); 117 } 118 119 @SuppressWarnings("unchecked") 120 private void _saxObject(ContentHandler contentHandler, Object item, String tag) throws SAXException 121 { 122 if (item instanceof Collection) 123 { 124 _saxCollection(contentHandler, (Collection<Object>) item, tag); 125 } 126 else if (item instanceof Map) 127 { 128 _saxMap(contentHandler, (Map<String, Object>) item, tag); 129 } 130 else 131 { 132 XMLUtils.createElement(contentHandler, tag, String.valueOf(item)); 133 } 134 } 135 136 private void _saxCollection(ContentHandler contentHandler, Collection<Object> list, String tag) throws SAXException 137 { 138 XMLUtils.startElement(contentHandler, tag); 139 for (Object item : list) 140 { 141 _saxObject(contentHandler, item, tag); 142 } 143 XMLUtils.endElement(contentHandler, tag); 144 } 145 146 private void _saxMap(ContentHandler contentHandler, Map<String, Object> map, String tag) throws SAXException 147 { 148 _saxMap(contentHandler, map, tag, new AttributesImpl()); 149 } 150 151 private void _saxMap(ContentHandler contentHandler, Map<String, Object> map, String tag, AttributesImpl attrs) throws SAXException 152 { 153 XMLUtils.startElement(contentHandler, tag, attrs); 154 for (String key : map.keySet()) 155 { 156 _saxObject(contentHandler, map.get(key), key); 157 } 158 XMLUtils.endElement(contentHandler, tag); 159 } 160 161}