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.odf.clientsideelement;
017
018import java.util.LinkedHashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.repository.WorkflowAwareContent;
027import org.ametys.odf.ODFHelper;
028import org.ametys.odf.ProgramItem;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Set the attribute of type 'content' of a content, with another content.
033 * If content is a {@link ProgramItem}, additional check will be done on catalog and content language
034 */
035public class SetContentAttributeClientSideElement extends org.ametys.cms.clientsideelement.relations.SetContentAttributeClientSideElement
036{
037    /** The ODF helper */
038    protected ODFHelper _odfHelper;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
045    }
046    
047    @Override
048    protected Map<WorkflowAwareContent, Integer> _filterContentsToEdit(Map<WorkflowAwareContent, Integer> contentsToEdit, List<String> contentIdsToReference, List<I18nizableText> errorMessages, List<String> errorIds, Map<String, Object> additionalParams)
049    {
050        Map<WorkflowAwareContent, Integer> validContents = new LinkedHashMap<>();
051        Map<WorkflowAwareContent, Integer> filteredContents = super._filterContentsToEdit(contentsToEdit, contentIdsToReference, errorMessages, errorIds, additionalParams);
052        
053        @SuppressWarnings("unchecked")
054        List<Content> refContents = (List<Content>) _resolve(contentIdsToReference);
055        
056        for (Map.Entry<WorkflowAwareContent, Integer> contentEntry : filteredContents.entrySet())
057        {
058            WorkflowAwareContent content = contentEntry.getKey();
059            
060            for (Content refContent : refContents)
061            {
062                if (!_odfHelper.isRelationCompatible(refContent, content, errorMessages, additionalParams))
063                {
064                    errorIds.add(content.getId());
065                }
066                else
067                {
068                    validContents.put(content, contentEntry.getValue());
069                }
070            }
071        }
072        
073        return validContents;
074    }
075    
076}