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