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.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.core.cocoon.ActionResultGenerator;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.workspaces.calendars.Calendar;
035import org.ametys.plugins.workspaces.calendars.CalendarDAO;
036import org.ametys.plugins.workspaces.calendars.helper.RecurrentEventHelper;
037
038/**
039 * Action providing events for the calendars of a project.
040 * The list of calendar can also be filtered.
041 */
042public class GetCalendarEvents extends ServiceableAction
043{
044    /** The object resolver */
045    protected AmetysObjectResolver _resolver;
046    
047    /** Calendar manager for workspaces */
048    protected CalendarDAO _calendarDAO;
049
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _calendarDAO = (CalendarDAO) smanager.lookup(CalendarDAO.ROLE);
055        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
056    }
057
058    @Override
059    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
060    {
061        Map<String, Object> result = new HashMap<>();
062        Request request = ObjectModelHelper.getRequest(objectModel);
063        String icsToken = parameters.getParameter("icsToken");
064        
065        Calendar calendar = _calendarDAO.getCalendarFromIcsToken(icsToken);
066        
067        if (calendar != null)
068        {
069            //get the working days
070            List<Integer> workingDayStrings = RecurrentEventHelper.getWorkingDays();
071            List<String> icsWorkingDays = new ArrayList<>();
072            for (Integer day : workingDayStrings)
073            {
074                switch (day)
075                {
076                    case java.util.Calendar.MONDAY:
077                        icsWorkingDays.add("MO");
078                        break;
079                    case java.util.Calendar.TUESDAY:
080                        icsWorkingDays.add("TU");
081                        break;
082                    case java.util.Calendar.WEDNESDAY:
083                        icsWorkingDays.add("WE");
084                        break;
085                    case java.util.Calendar.THURSDAY:
086                        icsWorkingDays.add("TH");
087                        break;
088                    case java.util.Calendar.FRIDAY:
089                        icsWorkingDays.add("FR");
090                        break;
091                    case java.util.Calendar.SATURDAY:
092                        icsWorkingDays.add("SA");
093                        break;
094                    case java.util.Calendar.SUNDAY:
095                        icsWorkingDays.add("SU");
096                        break;
097
098                    default:
099                        break;
100                }
101            }
102            Map<String, Object> calendarMap = _calendarDAO.getCalendarData(calendar, true, true, true);
103            result.put("calendar", calendarMap);
104            
105            result.put("workingDays", icsWorkingDays);
106        }
107
108        request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
109
110        return EMPTY_MAP;
111    }
112}