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.stream.Collectors; 023 024import org.apache.avalon.framework.parameters.Parameters; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.acting.ServiceableAction; 028import org.apache.cocoon.environment.ObjectModelHelper; 029import org.apache.cocoon.environment.Redirector; 030import org.apache.cocoon.environment.Request; 031import org.apache.cocoon.environment.SourceResolver; 032 033import org.ametys.core.cocoon.JSonReader; 034import org.ametys.plugins.odfweb.repository.OdfPageHandler; 035import org.ametys.runtime.model.ModelItem; 036 037/** 038 * Action providing the list of eligible enumerated metadata for ODF page level 039 */ 040public class GetEligibleMetadataList extends ServiceableAction 041{ 042 /** The ODF root page handler. */ 043 protected OdfPageHandler _odfRootHandler; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 super.service(serviceManager); 049 _odfRootHandler = (OdfPageHandler) serviceManager.lookup(OdfPageHandler.ROLE); 050 } 051 052 @Override 053 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 054 { 055 Request request = ObjectModelHelper.getRequest(objectModel); 056 057 // Retrieve the list of eligible metadata 058 List<Map<String, Object>> attributesInfo = _odfRootHandler.getEligibleAttributesForLevel().entrySet().stream() 059 .map(this::getAttributeInfo) 060 .collect(Collectors.toList()); 061 062 request.setAttribute(JSonReader.OBJECT_TO_READ, attributesInfo); 063 064 return EMPTY_MAP; 065 } 066 067 /** 068 * Generates a {@link Map} with information about the given attribute 069 * The returned information are the attribute's name and label 070 * @param attributeEntry The attribute's entry with its path as a key and its definition as a value 071 * @return a {@link Map} with the attribute's information 072 */ 073 protected Map<String, Object> getAttributeInfo(Entry<String, ModelItem> attributeEntry) 074 { 075 Map<String, Object> data = new HashMap<>(); 076 data.put("name", attributeEntry.getKey()); 077 data.put("label", attributeEntry.getValue().getLabel()); 078 return data; 079 } 080}