001/*
002 *  Copyright 2026 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.odf.enumeration;
017
018import java.util.Collection;
019import java.util.Map;
020import java.util.Optional;
021import java.util.stream.Collectors;
022import java.util.stream.Stream;
023
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.components.ContextHelper;
031
032import org.ametys.cms.data.ContentValue;
033import org.ametys.cms.repository.Content;
034import org.ametys.odf.ODFHelper;
035import org.ametys.odf.ProgramItem;
036import org.ametys.plugins.repository.UnknownAmetysObjectException;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.runtime.model.Enumerator;
039
040/**
041 * {@link Enumerator} for the parent programs
042 */
043public class ParentProgramsEnumerator implements Enumerator<ContentValue>, Serviceable, Contextualizable
044{
045    private Context _context;
046    private ODFHelper _odfHelper;
047    
048    public void contextualize(Context context) throws ContextException
049    {
050        _context = context;
051    }
052    
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
056    }
057
058    public I18nizableText getEntry(ContentValue value) throws Exception
059    {
060        try
061        {
062            return _getDisplayName((ProgramItem) value.getContent());
063        }
064        catch (UnknownAmetysObjectException e)
065        {
066            // Ignore.
067        }
068        
069        return null;
070    }
071    
072    public Map<ContentValue, I18nizableText> getEntries() throws Exception
073    {
074        return Optional.of(_context)
075            .map(ContextHelper::getRequest)
076            .map(r -> r.getAttribute(Content.class.getName()))
077            .filter(ProgramItem.class::isInstance)
078            .map(ProgramItem.class::cast)
079            .map(_odfHelper::getParentPrograms)
080            .map(Collection::stream)
081            .orElseGet(Stream::empty)
082            .collect(Collectors.toMap(ContentValue::new, this::_getDisplayName));
083    }
084
085    private I18nizableText _getDisplayName(ProgramItem programItem)
086    {
087        StringBuilder displayName = new StringBuilder();
088        
089        // Display code
090        displayName.append("[");
091        displayName.append(programItem.getDisplayCode());
092        displayName.append("] ");
093        
094        displayName.append(((Content) programItem).getTitle());
095        
096        return new I18nizableText(displayName.toString());
097    }
098}