001/* 002 * Copyright 2020 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; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Map; 021import java.util.Optional; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.context.Context; 025import org.apache.avalon.framework.context.ContextException; 026import org.apache.avalon.framework.context.Contextualizable; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.avalon.framework.service.Serviceable; 030import org.apache.cocoon.Constants; 031import org.apache.cocoon.components.ContextHelper; 032import org.apache.cocoon.environment.Request; 033 034import org.ametys.cms.content.indexing.solr.SolrResourceGroupedMimeTypes; 035import org.ametys.core.user.UserIdentity; 036import org.ametys.plugins.explorer.resources.Resource; 037import org.ametys.plugins.repository.tag.TaggableAmetysObject; 038import org.ametys.plugins.workspaces.tags.ProjectTagsDAO; 039import org.ametys.runtime.plugin.component.AbstractLogEnabled; 040import org.ametys.web.URIPrefixHandler; 041import org.ametys.web.WebHelper; 042 043/** 044 * Utility classes for workspaces 045 * 046 */ 047public class WorkspacesHelper extends AbstractLogEnabled implements Component, Serviceable, Contextualizable 048{ 049 /** Avalon role */ 050 public static final String ROLE = WorkspacesHelper.class.getName(); 051 052 /** 053 * Enumeration for file type 054 */ 055 public enum FileType 056 { 057 /** Text document */ 058 TEXT, 059 /** PDF document */ 060 PDF, 061 /** Spreadsheet */ 062 SPREADSHEET, 063 /** Presentation */ 064 PRES, 065 /** Image */ 066 IMAGE, 067 /** Video */ 068 VIDEO, 069 /** Audio */ 070 AUDIO, 071 /** Archive */ 072 ARCHIVE, 073 /** Unknown type */ 074 UNKNOWN 075 } 076 077 private URIPrefixHandler _prefixHandler; 078 079 private Context _context; 080 081 private org.apache.cocoon.environment.Context _cocoonContext; 082 083 private ProjectTagsDAO _projectTagsDAO; 084 085 public void contextualize(Context context) throws ContextException 086 { 087 _context = context; 088 _cocoonContext = (org.apache.cocoon.environment.Context) _context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 089 } 090 091 public void service(ServiceManager smanager) throws ServiceException 092 { 093 _prefixHandler = (URIPrefixHandler) smanager.lookup(URIPrefixHandler.ROLE); 094 _projectTagsDAO = (ProjectTagsDAO) smanager.lookup(ProjectTagsDAO.ROLE); 095 } 096 097 /** 098 * Determines if the resource file is an image 099 * @param file the resource 100 * @return true if the resource file is an image 101 */ 102 public boolean isImage(Resource file) 103 { 104 return SolrResourceGroupedMimeTypes.getGroup(file.getMimeType()) 105 .map(groupMimeType -> FileType.IMAGE == FileType.valueOf(groupMimeType.toUpperCase())) 106 .orElse(false); 107 } 108 109 /** 110 * Get the file type of a resource file 111 * @param file the resource 112 * @return the {@link FileType} 113 */ 114 public FileType getFileType(Resource file) 115 { 116 Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(file.getMimeType()); 117 118 return group 119 .map(groupMimeType -> FileType.valueOf(groupMimeType.toUpperCase())) 120 .orElse(FileType.UNKNOWN); 121 } 122 123 /** 124 * Get the file type of a resource from its name 125 * @param filename the resource's name 126 * @return the {@link FileType} 127 */ 128 public FileType getFileType(String filename) 129 { 130 String mimeType = _cocoonContext.getMimeType(filename); 131 Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(mimeType); 132 133 return group 134 .map(groupMimeType -> FileType.valueOf(groupMimeType.toUpperCase())) 135 .orElse(FileType.UNKNOWN); 136 } 137 138 /** 139 * Get user avatar 140 * @param userIdentity the user identity 141 * @param lang the lang for user's content 142 * @param size the size in pixel of image 143 * @return the avatar url 144 */ 145 public String getAvatar(UserIdentity userIdentity, String lang, int size) 146 { 147 StringBuilder sb = new StringBuilder(); 148 149 String siteName = WebHelper.getSiteName(_getRequest()); 150 sb.append(_prefixHandler.getUriPrefix(siteName)) 151 .append("/_plugins/user-directory/user/") 152 .append(userIdentity.getPopulationId()) 153 .append("/") 154 .append(userIdentity.getLogin()) 155 .append("/image_") 156 .append(size) 157 .append("?lang=") 158 .append(lang); 159 160 return sb.toString(); 161 } 162 163 private Request _getRequest() 164 { 165 return ContextHelper.getRequest(_context); 166 } 167 168 /** 169 * Handle tags for the edition of a TaggableAmetysObject 170 * @param taggableAmetysObject the object to edit 171 * @param tags the tags 172 * @return the created tags 173 */ 174 public List<Map<String, Object>> handleTags(TaggableAmetysObject taggableAmetysObject, List<Object> tags) 175 { 176 List<String> createdTags = new ArrayList<>(); 177 List<Map<String, Object>> createdTagsJson = new ArrayList<>(); 178 // Tag new tags 179 for (Object tag : tags) 180 { 181 // Tag doesn't exist so create the tag 182 if (tag instanceof Map) 183 { 184 @SuppressWarnings("unchecked") 185 String tagText = (String) ((Map<String, Object>) tag).get("text"); 186 List<Map<String, Object>> newTags = _projectTagsDAO.addTags(new String[] {tagText}); 187 String newTag = (String) newTags.get(0).get("name"); 188 taggableAmetysObject.tag(newTag); 189 createdTags.add(newTag); 190 createdTagsJson.addAll(newTags); 191 } 192 else 193 { 194 taggableAmetysObject.tag((String) tag); 195 } 196 } 197 // Untag unused tags 198 for (String tag : taggableAmetysObject.getTags()) 199 { 200 if (!tags.contains(tag) && !createdTags.contains(tag)) 201 { 202 taggableAmetysObject.untag(tag); 203 } 204 } 205 return createdTagsJson; 206 } 207}