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