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