001/* 002 * Copyright 2017 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.odfweb.actions; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021import java.util.Map.Entry; 022import java.util.Set; 023import java.util.stream.Collectors; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.acting.ServiceableAction; 029import org.apache.cocoon.environment.ObjectModelHelper; 030import org.apache.cocoon.environment.Redirector; 031import org.apache.cocoon.environment.Request; 032import org.apache.cocoon.environment.SourceResolver; 033import org.apache.commons.lang3.StringUtils; 034 035import org.ametys.core.cocoon.JSonReader; 036import org.ametys.plugins.odfweb.repository.OdfPageHandler; 037import org.ametys.plugins.repository.AmetysObjectResolver; 038import org.ametys.runtime.i18n.I18nizableText; 039import org.ametys.web.repository.page.Page; 040 041/** 042 * Action providing the list of existing catalogs 043 */ 044public class GetCatalogList extends ServiceableAction 045{ 046 /** The ODF root page handler. */ 047 protected OdfPageHandler _odfRootHandler; 048 049 /** Ametys object resolver. */ 050 protected AmetysObjectResolver _resolver; 051 052 @Override 053 public void service(ServiceManager serviceManager) throws ServiceException 054 { 055 super.service(serviceManager); 056 _odfRootHandler = (OdfPageHandler) serviceManager.lookup(OdfPageHandler.ROLE); 057 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 058 } 059 060 @Override 061 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 062 { 063 Request request = ObjectModelHelper.getRequest(objectModel); 064 065 String id = request.getParameter("id"); 066 Page page = _resolver.resolveById(id); 067 068 // List of catalogs used by other ODF pages 069 Set<String> unavailableCatalogs = 070 _odfRootHandler.getOdfRootPages(page.getSiteName(), page.getSitemapName()).stream() 071 .filter(odfRootpage -> !page.equals(odfRootpage)) 072 .map(_odfRootHandler::getCatalog) 073 .filter(StringUtils::isNotEmpty) 074 .collect(Collectors.toSet()); 075 076 // Retrieve the list of available catalogs 077 List<Map<String, Object>> catalogList = _odfRootHandler.getCatalogs().entrySet().stream() 078 .filter(catalogEntry -> !unavailableCatalogs.contains(catalogEntry.getKey())) 079 .map(this::getCatalogData) 080 .collect(Collectors.toList()); 081 082 request.setAttribute(JSonReader.OBJECT_TO_READ, catalogList); 083 084 return EMPTY_MAP; 085 } 086 087 /** 088 * Generate a catalog object from its entry 089 * @param catalogEntry The catalog entry 090 * @return Catalog object containing its data 091 */ 092 protected Map<String, Object> getCatalogData(Entry<String, I18nizableText> catalogEntry) 093 { 094 Map<String, Object> data = new HashMap<>(); 095 data.put("code", catalogEntry.getKey()); 096 data.put("label", catalogEntry.getValue()); 097 return data; 098 } 099}