001/*
002 *  Copyright 2023 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.CalendarDAO;
035import org.ametys.plugins.workspaces.calendars.events.CalendarEvent;
036import org.ametys.plugins.workspaces.calendars.events.CalendarEventJSONHelper;
037import org.ametys.plugins.workspaces.calendars.helper.RecurrentEventHelper;
038
039/**
040 * Action providing an event of a project.
041 */
042public class GetEvent extends ServiceableAction
043{
044    /** The object resolver */
045    protected AmetysObjectResolver _resolver;
046
047    /** Calendar manager for workspaces */
048    protected CalendarDAO _calendarDAO;
049    
050    /** Calendar event JSON helper */
051    protected CalendarEventJSONHelper _calendarEventJSONHelper;
052
053    @Override
054    public void service(ServiceManager smanager) throws ServiceException
055    {
056        super.service(smanager);
057        _calendarDAO = (CalendarDAO) smanager.lookup(CalendarDAO.ROLE);
058        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
059        _calendarEventJSONHelper = (CalendarEventJSONHelper) smanager.lookup(CalendarEventJSONHelper.ROLE);
060    }
061
062    @Override
063    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
064    {
065        Request request = ObjectModelHelper.getRequest(objectModel);
066        
067        Map<String, Object> result = new HashMap<>();
068        request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
069        
070        String eventId = request.getParameter("eventId");
071        if (eventId == null)
072        {
073            return EMPTY_MAP;
074        }
075        
076        CalendarEvent event = _resolver.resolveById(eventId);
077        if (event != null)
078        {
079            //get the working days
080            List<Integer> workingDayStrings = RecurrentEventHelper.getWorkingDays();
081            List<String> icsWorkingDays = new ArrayList<>();
082            for (Integer day : workingDayStrings)
083            {
084                switch (day)
085                {
086                    case java.util.Calendar.MONDAY:
087                        icsWorkingDays.add("MO");
088                        break;
089                    case java.util.Calendar.TUESDAY:
090                        icsWorkingDays.add("TU");
091                        break;
092                    case java.util.Calendar.WEDNESDAY:
093                        icsWorkingDays.add("WE");
094                        break;
095                    case java.util.Calendar.THURSDAY:
096                        icsWorkingDays.add("TH");
097                        break;
098                    case java.util.Calendar.FRIDAY:
099                        icsWorkingDays.add("FR");
100                        break;
101                    case java.util.Calendar.SATURDAY:
102                        icsWorkingDays.add("SA");
103                        break;
104                    case java.util.Calendar.SUNDAY:
105                        icsWorkingDays.add("SU");
106                        break;
107                    default:
108                        break;
109                }
110            }
111            
112            result.put("events", _calendarEventJSONHelper.eventAsJson(event, false, true));
113            result.put("workingDays", icsWorkingDays);
114        }
115
116        return EMPTY_MAP;
117    }
118}