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.mobileapp.action; 017 018import java.util.Comparator; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.environment.Request; 026 027import org.ametys.plugins.workspaces.project.ProjectsCatalogueManager; 028import org.ametys.runtime.plugin.PluginsManager; 029 030/** 031 * Returns the list of projects for a user 032 */ 033public class GetProjectsAction extends AbstractLoggedAction 034{ 035 /** The project catalogue manager component */ 036 private ProjectsCatalogueManager _projectsCatalogueManager; 037 038 @Override 039 public void service(ServiceManager smanager) throws ServiceException 040 { 041 super.service(smanager); 042 if (PluginsManager.getInstance().isPluginActive("workspaces")) 043 { 044 _projectsCatalogueManager = (ProjectsCatalogueManager) smanager.lookup(ProjectsCatalogueManager.ROLE); 045 } 046 } 047 048 @Override 049 protected Map<String, Object> doLoggedInAction(Request request, Map<String, Object> jsonParams) 050 { 051 Map<String, Object> result = new HashMap<>(); 052 053 if (_projectsCatalogueManager != null) 054 { 055 List<Map<String, Object>> userProjects = _projectsCatalogueManager.getUserProjects(); 056 057 List<Map<String, Object>> feedList = userProjects.stream() 058 .map(up -> _mapResult(up)) 059 .sorted(Comparator.comparing(project -> ((String) project.get("name")).toLowerCase())) 060 .toList(); 061 result.put("feed_list", feedList); 062 } 063 064 return result; 065 } 066 067 /** 068 * Map a json used for project list to a map used by the mobile app 069 * @param userProject a map representing a project 070 * @return a map used by the mobile app 071 */ 072 private Map<String, Object> _mapResult(Map<String, Object> userProject) 073 { 074 Map<String, Object> result = new HashMap<>(); 075 076 result.put("feed_id", userProject.get("id")); 077 result.put("name", userProject.get("title")); 078 result.put("url", userProject.get("url")); 079 result.put("illustration", userProject.get("illustration")); 080 result.put("language", userProject.get("language")); 081 if (userProject.containsKey("category")) 082 { 083 result.put("color", ((Map) userProject.get("category")).get("color")); 084 } 085 086 return result; 087 } 088}