001/* 002 * Copyright 2016 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.calendars.actions; 017 018import java.util.ArrayList; 019import java.util.Date; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.parameters.Parameters; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.acting.ServiceableAction; 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.core.cocoon.JSonReader; 034import org.ametys.core.user.UserManager; 035import org.ametys.plugins.explorer.calendars.Calendar; 036import org.ametys.plugins.explorer.calendars.CalendarEvent; 037import org.ametys.plugins.repository.AmetysObjectIterable; 038import org.ametys.plugins.repository.AmetysObjectResolver; 039import org.ametys.plugins.workflow.support.WorkflowHelper; 040import org.ametys.plugins.workflow.support.WorkflowProvider; 041import org.ametys.plugins.workspaces.calendars.CalendarWorkspaceModule; 042import org.ametys.plugins.workspaces.calendars.WorkspaceCalendarDAO; 043import org.ametys.plugins.workspaces.project.ProjectManager; 044import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint; 045import org.ametys.plugins.workspaces.project.objects.Project; 046import org.ametys.runtime.parameter.ParameterHelper; 047import org.ametys.runtime.parameter.ParameterHelper.ParameterType; 048 049/** 050 * Action providing events for the calendars of a project. 051 * The list of calendar can also be filtered. 052 */ 053public class GetProjectCalendarEvents extends ServiceableAction 054{ 055 /** Workspaces project manager */ 056 protected ProjectManager _projectManager; 057 058 /** The object resolver */ 059 protected AmetysObjectResolver _resolver; 060 061 /** The ametys users manager */ 062 protected UserManager _userManager; 063 064 /** The workflow provider */ 065 protected WorkflowProvider _workflowProvider; 066 067 /** The workflow helper */ 068 protected WorkflowHelper _workflowHelper; 069 070 private WorkspaceCalendarDAO _calendarDAO; 071 072 private WorkspaceModuleExtensionPoint _workspaceModuleEP; 073 074 @Override 075 public void service(ServiceManager serviceManager) throws ServiceException 076 { 077 super.service(serviceManager); 078 _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE); 079 _workspaceModuleEP = (WorkspaceModuleExtensionPoint) serviceManager.lookup(WorkspaceModuleExtensionPoint.ROLE); 080 _calendarDAO = (WorkspaceCalendarDAO) serviceManager.lookup(WorkspaceCalendarDAO.ROLE); 081 082 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 083 _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE); 084 _workflowProvider = (WorkflowProvider) serviceManager.lookup(WorkflowProvider.ROLE); 085 _workflowHelper = (WorkflowHelper) serviceManager.lookup(WorkflowHelper.ROLE); 086 } 087 088 @Override 089 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 090 { 091 Map<String, Object> result = new HashMap<>(); 092 093 Request request = ObjectModelHelper.getRequest(objectModel); 094 String projectName = (String) request.getAttribute("projectName"); 095 096 String startDateString = request.getParameter("start"); 097 Date startDate = (Date) ParameterHelper.castValue(startDateString, ParameterType.DATE); 098 099 String endDateString = request.getParameter("end"); 100 Date endDate = (Date) ParameterHelper.castValue(endDateString, ParameterType.DATE); 101 102 CalendarWorkspaceModule calendarModule = (CalendarWorkspaceModule) _workspaceModuleEP.getModule(CalendarWorkspaceModule.CALENDAR_MODULE_ID); 103 Project project = _projectManager.getProject(projectName); 104 AmetysObjectIterable<Calendar> calendars = calendarModule.getCalendars(project); 105 106 List<Object> calendarDataList = new ArrayList<>(); 107 if (calendars != null) 108 { 109 for (Calendar calendar : calendars) 110 { 111 if (calendarModule.canView(calendar)) 112 { 113 Map<String, Object> calendarData = _calendarDAO.getCalendarData(calendar, false, false); 114 calendarDataList.add(calendarData); 115 116 List<Object> eventDataList = new ArrayList<>(); 117 calendarData.put("events", eventDataList); 118 119 for (Map.Entry<CalendarEvent, List<Date>> entry : calendar.getEvents(startDate, endDate).entrySet()) 120 { 121 CalendarEvent event = entry.getKey(); 122 Map<String, Object> eventData = _calendarDAO.getEventData(event, false); 123 eventDataList.add(eventData); 124 125 List<Object> occurrencesDataList = new ArrayList<>(); 126 eventData.put("occurrences", occurrencesDataList); 127 128 List<Date> occurrences = entry.getValue(); 129 for (Date date : occurrences) 130 { 131 Map<String, Object> eventOccurrenceData = _calendarDAO.getEventOccurrenceData(event, date); 132 occurrencesDataList.add(eventOccurrenceData); 133 } 134 } 135 } 136 } 137 } 138 139 result.put("calendars", calendarDataList); 140 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 141 142 return EMPTY_MAP; 143 } 144}