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.Optional; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.avalon.framework.service.Serviceable; 023 024import org.ametys.cms.content.indexing.solr.SolrResourceGroupedMimeTypes; 025import org.ametys.plugins.explorer.resources.jcr.JCRResource; 026import org.ametys.plugins.repository.AmetysObjectResolver; 027import org.ametys.plugins.repository.UnknownAmetysObjectException; 028import org.ametys.plugins.workspaces.WorkspacesHelper.FileType; 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.tasks.Task; 033 034/** 035 * Helper providing information required for mail notification 036 */ 037public class MailXSLTHelper implements Serviceable 038{ 039 private static CalendarColorsComponent _calendarColors; 040 private static AmetysObjectResolver _resolver; 041 042 @Override 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 _calendarColors = (CalendarColorsComponent) manager.lookup(CalendarColorsComponent.ROLE); 046 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 047 } 048 049 050 // TODO there should be a better handling of what happens when the data is not available in all those method 051 /** 052 * Get a file type from a resourceId. 053 * @param resourceId the id of the resource we want to get the file type 054 * @return the name of the file type 055 */ 056 public static String getFileTypeFromId(String resourceId) 057 { 058 try 059 { 060 JCRResource resource = _resolver.resolveById(resourceId); 061 return getFileType(resource.getMimeType()); 062 } 063 catch (Exception e) 064 { 065 return FileType.UNKNOWN.name().toLowerCase(); 066 } 067 } 068 069 070 /** 071 * Get a file type from a mime type. 072 * @param mimeType the mime type to transform 073 * @return the name of the file type 074 */ 075 public static String getFileType(String mimeType) 076 { 077 Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(mimeType); 078 079 return group.map(groupMimeType -> groupMimeType.toLowerCase()) 080 .orElse(FileType.UNKNOWN.name().toLowerCase()); 081 } 082 083 /** 084 * Get the color of a {@link Calendar} from its id 085 * @param calendarId the calendar's id 086 * @return the color of the calendar 087 */ 088 public static String getCalendarColor(String calendarId) 089 { 090 try 091 { 092 Calendar calendar = _resolver.resolveById(calendarId); 093 CalendarColor color = _calendarColors.getColor(calendar.getColor()); 094 return color.getColor(); 095 } 096 catch (UnknownAmetysObjectException e) 097 { 098 return _calendarColors.getColors().values().iterator().next().getColor(); 099 } 100 } 101 102 /** 103 * Get the ics export link of a calendar event from its id 104 * @param eventId the event's id 105 * @param projectURL the URL of the project 106 * @return the recurrence of the calendar event 107 */ 108 public static String getCalendarEventICSExportLink(String eventId, String projectURL) 109 { 110 // Try to resolve the event id to check if the event is deleted or not 111 if (_resolver.hasAmetysObjectForId(eventId)) 112 { 113 return projectURL + "/_plugins/workspaces/event.ics?eventId=" + eventId; 114 } 115 else 116 { 117 return ""; 118 } 119 } 120 121 /** 122 * Test if a task have a due date from its id 123 * @param taskId the task's id 124 * @return true if the task has a due date 125 */ 126 public static boolean hasDueDate(String taskId) 127 { 128 try 129 { 130 Task task = _resolver.resolveById(taskId); 131 return task.getDueDate() != null; 132 } 133 catch (UnknownAmetysObjectException e) 134 { 135 return false; 136 } 137 } 138 139 /** 140 * Get the ics export link of a task from its id 141 * @param taskId the task's id 142 * @param projectURL the URL of the project 143 * @return the recurrence of the calendar event 144 */ 145 public static String getTaskICSExportLink(String taskId, String projectURL) 146 { 147 return projectURL + "/_plugins/workspaces/task.ics?id=" + taskId; 148 } 149}