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.contenttype.ContentType;
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.repository.WorkflowAwareContent;
028import org.ametys.odf.ODFHelper;
029import org.ametys.odf.ProgramItem;
030import org.ametys.runtime.i18n.I18nizableText;
031
032/**
033 * Set the metadata of type 'content' of a content, with another content.
034 * If content is a {@link ProgramItem}, additional check will be done on catalog and content language
035 */
036public class SetContentMetadataClientSideElement extends org.ametys.cms.clientsideelement.relations.SetContentMetadataClientSideElement
037{
038    /** The ODF helper */
039    protected ODFHelper _odfHelper;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
046    }
047    
048    @Override
049    protected void _setContentMetatada(List<String> contentIdsToReference, Map<WorkflowAwareContent, Integer> contentToEdit, ContentType contentType, String metadataPath, List<String> workflowActionIds, List<I18nizableText> errorMessages, List<String> errorIds, Map<String, Object> additionalParams)
050    {
051        Map<WorkflowAwareContent, Integer> validContents = new LinkedHashMap<>();
052        
053        @SuppressWarnings("unchecked")
054        List<Content> refContents = (List<Content>) _resolve(contentIdsToReference);
055        
056        for (Map.Entry<WorkflowAwareContent, Integer> contentEntry : contentToEdit.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        super._setContentMetatada(contentIdsToReference, validContents, contentType, metadataPath, workflowActionIds, errorMessages, errorIds, additionalParams);
074    }
075}