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