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.cms.contenttype.MetadataDefinition;
034import org.ametys.core.cocoon.JSonReader;
035import org.ametys.plugins.odfweb.repository.OdfPageHandler;
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>> metadataList = _odfRootHandler.getEligibleMetadataForLevel().entrySet().stream()
059            .map(this::getMetadataData)
060            .collect(Collectors.toList());
061        
062        request.setAttribute(JSonReader.OBJECT_TO_READ, metadataList);
063        
064        return EMPTY_MAP;
065    }
066    
067    /**
068     * Generate a metadata definition object from its entry
069     * @param metaEntry The metadata entry
070     * @return Metadata definition object containing its data
071     */
072    protected Map<String, Object> getMetadataData(Entry<String, MetadataDefinition> metaEntry)
073    {
074        Map<String, Object> data = new HashMap<>();
075        data.put("name", metaEntry.getKey());
076        data.put("label", metaEntry.getValue().getLabel());
077        return data;
078    }
079}