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.net.URLDecoder;
019import java.util.ArrayList;
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;
032import org.apache.commons.lang.StringUtils;
033
034import org.ametys.core.cocoon.ActionResultGenerator;
035import org.ametys.plugins.explorer.calendars.Calendar;
036import org.ametys.plugins.explorer.calendars.helper.RecurrentEventHelper;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.workspaces.calendars.CalendarWorkspaceModule;
039import org.ametys.plugins.workspaces.calendars.WorkspaceCalendarDAO;
040import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint;
041
042/**
043 * Action providing events for the calendars of a project.
044 * The list of calendar can also be filtered.
045 */
046public class GetCalendarEvents extends ServiceableAction
047{
048    /** The object resolver */
049    protected AmetysObjectResolver _resolver;
050    
051    /** Calendar manager for workspaces */
052    protected WorkspaceCalendarDAO _calendarDAO;
053
054    private WorkspaceModuleExtensionPoint _workspaceModuleEP;
055
056    @Override
057    public void service(ServiceManager smanager) throws ServiceException
058    {
059        super.service(smanager);
060        _calendarDAO = (WorkspaceCalendarDAO) smanager.lookup(WorkspaceCalendarDAO.ROLE);
061        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
062        _workspaceModuleEP = (WorkspaceModuleExtensionPoint) smanager.lookup(WorkspaceModuleExtensionPoint.ROLE);
063    }
064
065    @Override
066    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
067    {
068        Map<String, Object> result = new HashMap<>();
069        Request request = ObjectModelHelper.getRequest(objectModel);
070        String calendarId = parameters.getParameter("calendarId");
071        
072        if (StringUtils.isNotEmpty(calendarId))
073        {
074            calendarId = URLDecoder.decode(calendarId, "UTF-8");
075        }
076        
077        Calendar calendar = (Calendar) _resolver.resolveById(calendarId);
078        
079        CalendarWorkspaceModule calendarModule = (CalendarWorkspaceModule) _workspaceModuleEP.getModule(CalendarWorkspaceModule.CALENDAR_MODULE_ID);
080        if (calendarModule.canView(calendar))
081        {
082            //get the working days
083            List<Integer> workingDayStrings = RecurrentEventHelper.getWorkingDays();
084            List<String> icsWorkingDays = new ArrayList<>();
085            for (Integer day : workingDayStrings)
086            {
087                switch (day)
088                {
089                    case java.util.Calendar.MONDAY:
090                        icsWorkingDays.add("MO");
091                        break;
092                    case java.util.Calendar.TUESDAY:
093                        icsWorkingDays.add("TU");
094                        break;
095                    case java.util.Calendar.WEDNESDAY:
096                        icsWorkingDays.add("WE");
097                        break;
098                    case java.util.Calendar.THURSDAY:
099                        icsWorkingDays.add("TH");
100                        break;
101                    case java.util.Calendar.FRIDAY:
102                        icsWorkingDays.add("FR");
103                        break;
104                    case java.util.Calendar.SATURDAY:
105                        icsWorkingDays.add("SA");
106                        break;
107                    case java.util.Calendar.SUNDAY:
108                        icsWorkingDays.add("SU");
109                        break;
110
111                    default:
112                        break;
113                }
114            }
115            Map<String, Object> calendarMap = _calendarDAO.getCalendarData(calendar, true, true);
116            result.put("calendar", calendarMap);
117            
118            result.put("workingDays", icsWorkingDays);
119        }
120
121        request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
122
123        return EMPTY_MAP;
124    }
125}