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