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