001/*
002 *  Copyright 2025 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.task;
017
018import java.time.LocalDate;
019import java.time.ZoneId;
020import java.time.ZonedDateTime;
021import java.util.List;
022import java.util.Optional;
023import java.util.Set;
024
025import org.ametys.core.user.UserIdentity;
026import org.ametys.core.util.DateUtils;
027import org.ametys.plugins.messagingconnector.EventRecurrenceTypeEnum;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.workspaces.calendars.Calendar;
031import org.ametys.plugins.workspaces.calendars.events.CalendarEvent;
032import org.ametys.plugins.workspaces.calendars.events.CalendarEventOccurrence;
033import org.ametys.plugins.workspaces.tasks.Task;
034
035/**
036 * Object for a task calendar event
037 */
038public class TaskCalendarEvent implements CalendarEvent
039{
040    private Calendar _calendar;
041    private Task _task;
042    
043    /**
044     * The constructor
045     * @param calendar the calendar
046     * @param task the task
047     */
048    public TaskCalendarEvent(Calendar calendar, Task task)
049    {
050        _calendar = calendar;
051        _task = task;
052    }
053    
054    /**
055     * Return the task link to the calendar event
056     * @return the task
057     */
058    public Task getTask()
059    {
060        return _task;
061    }
062    
063    public void tag(String tag) throws AmetysRepositoryException
064    {
065        throw new UnsupportedOperationException("Can't call method #tag for a TaskCalendarEvent");
066    }
067
068    public void untag(String tag) throws AmetysRepositoryException
069    {
070        throw new UnsupportedOperationException("Can't call method #untag for a TaskCalendarEvent");
071    }
072
073    public Set<String> getTags() throws AmetysRepositoryException
074    {
075        return _task.getTags();
076    }
077
078    public String getName() throws AmetysRepositoryException
079    {
080        return _task.getName();
081    }
082
083    public String getPath() throws AmetysRepositoryException
084    {
085        return _task.getPath();
086    }
087
088    public String getId() throws AmetysRepositoryException
089    {
090        return _task.getId();
091    }
092
093    public <A extends AmetysObject> A getParent() throws AmetysRepositoryException
094    {
095        return _task.getParent();
096    }
097
098    public String getParentPath() throws AmetysRepositoryException
099    {
100        return _task.getParentPath();
101    }
102
103    public Calendar getCalendar()
104    {
105        return _calendar;
106    }
107    
108    public String getTitle()
109    {
110        return _task.getLabel();
111    }
112
113    public String getDescription()
114    {
115        return _task.getDescription();
116    }
117
118    public String getLocation()
119    {
120        return null;
121    }
122
123    public ZonedDateTime getStartDate()
124    {
125        LocalDate date = _task.getStartDate() != null 
126                ? _task.getStartDate()
127                : _task.getDueDate();
128        return DateUtils.asZonedDateTime(date, getZone());
129    }
130
131    public ZonedDateTime getEndDate()
132    {
133        LocalDate date = _task.getDueDate() != null 
134                ? _task.getDueDate()
135                : _task.getStartDate();
136        return DateUtils.asZonedDateTime(date, getZone());
137    }
138
139    public ZoneId getZone()
140    {
141        return ZoneId.systemDefault();
142    }
143
144    public Boolean getFullDay()
145    {
146        return true;
147    }
148
149    public UserIdentity getCreator()
150    {
151        return _task.getAuthor();
152    }
153
154    public ZonedDateTime getCreationDate()
155    {
156        return _task.getCreationDate();
157    }
158
159    public UserIdentity getLastContributor()
160    {
161        return _task.getAuthor();
162    }
163
164    public ZonedDateTime getLastModified()
165    {
166        return _task.getLastModified();
167    }
168
169    public EventRecurrenceTypeEnum getRecurrenceType()
170    {
171        return EventRecurrenceTypeEnum.NEVER;
172    }
173
174    public Boolean isRecurrent()
175    {
176        return false;
177    }
178
179    public ZonedDateTime getRepeatUntil()
180    {
181        return null;
182    }
183
184    public List<ZonedDateTime> getExcludedOccurences()
185    {
186        return List.of();
187    }
188
189    public List<CalendarEventOccurrence> getOccurrences(ZonedDateTime startDate, ZonedDateTime endDate)
190    {
191        Optional<CalendarEventOccurrence> optionalEvent = getFirstOccurrence(startDate);
192        return (optionalEvent.isPresent() && optionalEvent.get().getStartDate().isBefore(endDate))
193            ? List.of(optionalEvent.get())
194            : List.of();
195    }
196
197    public Optional<CalendarEventOccurrence> getFirstOccurrence(ZonedDateTime date)
198    {
199        ZonedDateTime eventStartDate = getStartDate();
200        ZonedDateTime eventEndDate = getEndDate();
201        
202        if (getFullDay())
203        {
204            eventEndDate = eventEndDate.plusDays(1);
205        }
206        
207        return (eventEndDate.isAfter(date) || eventEndDate.isEqual(date))
208            ? Optional.of(new CalendarEventOccurrence(this, eventStartDate))
209            : Optional.empty();
210    }
211
212    public Optional<CalendarEventOccurrence> getNextOccurrence(CalendarEventOccurrence occurrence)
213    {
214        return Optional.empty();
215    }
216
217    public UserIdentity getOrganiser()
218    {
219        return _task.getAuthor();
220    }
221
222    public List<String> getResources()
223    {
224        return List.of(_calendar.getId());
225    }
226}