001/*
002 *  Copyright 2022 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 */
016
017package org.ametys.plugins.workspaces.calendars.events;
018
019import java.time.ZonedDateTime;
020import java.time.temporal.ChronoUnit;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.ametys.core.util.DateUtils;
026
027/**
028 * Occurrence of a calendar event
029 */
030public class CalendarEventOccurrence
031{
032    private ZonedDateTime _startDate;
033    private ZonedDateTime _endDate;
034    private boolean _isFullDay;
035    private List<String> _resourceIds;
036    private CalendarEvent _event;
037
038    /**
039     * Creates a {@link CalendarEventOccurrence}.
040     * @param calendarEvent the calendar event
041     * @param occurrenceDate the occurrence date
042     */
043    public CalendarEventOccurrence(CalendarEvent calendarEvent, ZonedDateTime occurrenceDate)
044    {
045        this._startDate = occurrenceDate;
046
047        long diffInSeconds = ChronoUnit.SECONDS.between(calendarEvent.getStartDate(), calendarEvent.getEndDate());
048        this._endDate = occurrenceDate.plusSeconds(diffInSeconds);
049        this._isFullDay = calendarEvent.getFullDay();
050        this._resourceIds = calendarEvent.getResources();
051        this._event = calendarEvent;
052    }
053
054    /**
055     * Get the date of the begin of the occurrence
056     * @return the date of the occurrence
057     */
058    public ZonedDateTime getStartDate()
059    {
060        return _startDate;
061    }
062
063    /**
064     * Set the startDate of the occurrence
065     * @param startDate the start date
066     */
067    public void setStartDate(ZonedDateTime startDate)
068    {
069        this._startDate = startDate;
070    }
071
072    /**
073     * Get the date of the end of the occurrence
074     * @return the date of the occurrence
075     */
076    public ZonedDateTime getEndDate()
077    {
078        return _endDate;
079    }
080
081    /**
082     * Set the endDate of the occurrence
083     * @param endDate the end date
084     */
085    public void setEndDate(ZonedDateTime endDate)
086    {
087        this._endDate = endDate;
088    }
089
090    /**
091     * Get if the occurrence last all the day
092     * @return true if the occurrence last all the day
093     */
094    public boolean isFullDay()
095    {
096        return _isFullDay;
097    }
098
099    /**
100     * Set if the occurrence last all the day
101     * @param isFullDay true if the occurrence last all the day
102     */
103    public void setFullDay(boolean isFullDay)
104    {
105        this._isFullDay = isFullDay;
106    }
107
108    /**
109     * Retrieves the resource id's list.
110     * @return the resource id's list.
111     */
112    public List<String> getResourceIds()
113    {
114        return _resourceIds;
115    }
116
117    /**
118     * Set the resources ids
119     * @param resourceIds The list of ressource ids
120     */
121    public void setResourceIds(List<String> resourceIds)
122    {
123        this._resourceIds = resourceIds;
124    }
125
126    /**
127     * Check if the occurrence is before a given date
128     * @param date the date
129     * @return true if the occurrence is before a given date
130     */
131    public boolean before(ZonedDateTime date)
132    {
133        return this.getStartDate().isBefore(date);
134    }
135
136    /**
137     * Check if the occurrence is after a given date
138     * @param date the date
139     * @return true if the occurrence is after a given date
140     */
141    public boolean after(ZonedDateTime date)
142    {
143        return this.getStartDate().isAfter(date);
144    }
145    
146    /**
147     * Get the event
148     * @return the event
149     */
150    public CalendarEvent getEvent()
151    {
152        return _event;
153    }
154    
155    
156    /**
157     * occurrence info as a JSONizable map
158     * @return the map
159     */
160    public Map<String, Object> toJSON()
161    {
162        Map<String, Object> result = new HashMap<>();
163    
164        String occurrenceDateIso = DateUtils.zonedDateTimeToString(_startDate);
165        result.put("id", _event.getId() + "$" + occurrenceDateIso);
166        result.put("occurrenceDate", occurrenceDateIso);
167        
168        ZonedDateTime startDateEvent = _startDate;
169        ZonedDateTime endDateEvent = _endDate;
170        
171        if (_isFullDay)
172        {
173            result.put("endDateNextDay", DateUtils.zonedDateTimeToString(endDateEvent.plusDays(1)));
174        }
175        
176        result.put("startDate", DateUtils.zonedDateTimeToString(startDateEvent));
177        result.put("endDate", DateUtils.zonedDateTimeToString(endDateEvent));
178        
179        return result;
180    }
181}