001/*
002 *  Copyright 2021 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.project.helper;
017
018import java.time.ZonedDateTime;
019import java.util.Date;
020import java.util.Optional;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.content.indexing.solr.SolrResourceGroupedMimeTypes;
027import org.ametys.core.util.DateUtils;
028import org.ametys.plugins.explorer.resources.jcr.JCRResource;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.plugins.workspaces.WorkspacesHelper.FileType;
032import org.ametys.plugins.workspaces.calendars.Calendar;
033import org.ametys.plugins.workspaces.calendars.CalendarColorsComponent;
034import org.ametys.plugins.workspaces.calendars.CalendarColorsComponent.CalendarColor;
035import org.ametys.plugins.workspaces.calendars.events.CalendarEvent;
036import org.ametys.plugins.workspaces.tasks.Task;
037
038/**
039 * Helper providing information required for mail notification
040 */
041public class MailXSLTHelper implements Serviceable
042{
043    private static CalendarColorsComponent _calendarColors;
044    private static AmetysObjectResolver _resolver;
045
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _calendarColors = (CalendarColorsComponent) manager.lookup(CalendarColorsComponent.ROLE);
050        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
051    }
052    
053    
054    // TODO there should be a better handling of what happens when the data is not available in all those method
055    /**
056     * Get a file type from a resourceId.
057     * @param resourceId the id of the resource we want to get the file type
058     * @return the name of the file type
059     */
060    public static String getFileTypeFromId(String resourceId)
061    {
062        try
063        {
064            JCRResource resource = _resolver.resolveById(resourceId);
065            return getFileType(resource.getMimeType());
066        }
067        catch (Exception e)
068        {
069            return FileType.UNKNOWN.name().toLowerCase();
070        }
071    }
072
073    
074    /**
075     * Get a file type from a mime type.
076     * @param mimeType the mime type to transform
077     * @return the name of the file type
078     */
079    public static String getFileType(String mimeType)
080    {
081        Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(mimeType);
082        
083        return group.map(groupMimeType -> groupMimeType.toLowerCase())
084                    .orElse(FileType.UNKNOWN.name().toLowerCase());
085    }
086    
087    /**
088     * Get the color of a {@link Calendar} from its id
089     * @param calendarId the calendar's id
090     * @return the color of the calendar
091     */
092    public static String getCalendarColor(String calendarId)
093    {
094        try
095        {
096            Calendar calendar = _resolver.resolveById(calendarId);
097            CalendarColor color = _calendarColors.getColor(calendar.getColor());
098            return color.getColor();
099        }
100        catch (UnknownAmetysObjectException e)
101        {
102            return _calendarColors.getColors().values().iterator().next().getColor();
103        }
104    }
105
106    /**
107     * Get the start date of a calendar event from its id
108     * @param eventId the event's id
109     * @return the {@link String} representation of the date
110     */
111    public static String getCalendarEventStartDate(String eventId)
112    {
113        try
114        {
115            CalendarEvent event = _resolver.resolveById(eventId);
116            return DateUtils.zonedDateTimeToString(event.getStartDate());
117        }
118        catch (UnknownAmetysObjectException e)
119        {
120            return DateUtils.dateToString(new Date(0));
121        }
122    }
123    
124    /**
125     * Get the end date of a calendar event from its id
126     * @param eventId the event's id
127     * @return the {@link String} representation of the date
128     */
129    public static String getCalendarEventEndDate(String eventId)
130    {
131        try
132        {
133            CalendarEvent event = _resolver.resolveById(eventId);
134            return DateUtils.zonedDateTimeToString(event.getEndDate());
135        }
136        catch (UnknownAmetysObjectException e)
137        {
138            return DateUtils.dateToString(new Date(0));
139        }
140    }
141    
142    /**
143     * Test if a calendar event last all day from its id
144     * @param eventId the event's id
145     * @return true if the calendar event last all day
146     */
147    public static boolean isCalendarEventFullDay(String eventId)
148    {
149        try
150        {
151            CalendarEvent event = _resolver.resolveById(eventId);
152            return event.getFullDay();
153        }
154        catch (UnknownAmetysObjectException e)
155        {
156            return true;
157        }
158    }
159    
160    /**
161     * Get the recurrence of a calendar event from its id
162     * @param eventId the event's id
163     * @return the recurrence of the calendar event
164     */
165    public static String getCalendarEventRecurrence(String eventId)
166    {
167        try
168        {
169            CalendarEvent event = _resolver.resolveById(eventId);
170            return event.getRecurrenceType().name().toUpperCase();
171        }
172        catch (UnknownAmetysObjectException e)
173        {
174            return "NEVER";
175        }
176    }
177    
178    /**
179     * Get the date of end of recurrence of a calendar event from its id
180     * @param eventId the event's id
181     * @return the {@link String} representation of the date of end of recurrence
182     */
183    public static String getCalendarEventRepeatUntil(String eventId)
184    {
185        try
186        {
187            CalendarEvent event = _resolver.resolveById(eventId);
188            ZonedDateTime repeatUntil = event.getRepeatUntil();
189            return repeatUntil != null ? DateUtils.zonedDateTimeToString(repeatUntil) : null;
190        }
191        catch (UnknownAmetysObjectException e)
192        {
193            return DateUtils.dateToString(new Date());
194        }
195    }
196
197    /**
198     * Get the ics export link of a calendar event from its id
199     * @param eventId the event's id
200     * @param projectURL the URL of the project
201     * @return the recurrence of the calendar event
202     */
203    public static String getCalendarEventICSExportLink(String eventId, String projectURL)
204    {
205        try
206        {
207            return projectURL + "/_plugins/workspaces/event.ics?eventId=" + eventId;
208        }
209        catch (UnknownAmetysObjectException e)
210        {
211            return "";
212        }
213    }
214    
215    /**
216     * Test if a task have a due date from its id
217     * @param taskId the task's id
218     * @return true if the task has a due date
219     */
220    public static boolean hasDueDate(String taskId)
221    {
222        try
223        {
224            Task task = _resolver.resolveById(taskId);
225            return task.getDueDate() != null;
226        }
227        catch (UnknownAmetysObjectException e)
228        {
229            return false;
230        }
231    }
232
233    /**
234     * Get the ics export link of a task from its id
235     * @param taskId the task's id
236     * @param projectURL the URL of the project
237     * @return the recurrence of the calendar event
238     */
239    public static String getTaskICSExportLink(String taskId, String projectURL)
240    {
241        return projectURL + "/_plugins/workspaces/task.ics?id=" + taskId;
242    }
243}