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.ArrayList;
019import java.util.LinkedHashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.ametys.cms.contenttype.ContentType;
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.repository.WorkflowAwareContent;
026import org.ametys.odf.ProgramItem;
027import org.ametys.runtime.i18n.I18nizableText;
028
029/**
030 * Set the metadata of type 'content' of a content, with another content.
031 * If content is a {@link ProgramItem}, additional check will be done on catalog and content language
032 */
033public class SetContentMetadataClientSideElement extends org.ametys.cms.clientsideelement.relations.SetContentMetadataClientSideElement
034{
035
036    @Override
037    protected void _setContentMetatada(List<String> contentIdsToReference, Map<WorkflowAwareContent, Integer> contentToEdit, ContentType contentType, String metadataPath,
038            List<String> workflowActionIds, List<I18nizableText> errorMessages, List<String> errorIds)
039    {
040        
041        Map<WorkflowAwareContent, Integer> validContents = new LinkedHashMap<>();
042        
043        @SuppressWarnings("unchecked")
044        List<Content> refContents = (List<Content>) _resolve(contentIdsToReference);
045        
046        for (Map.Entry<WorkflowAwareContent, Integer> contentEntry : contentToEdit.entrySet())
047        {
048            WorkflowAwareContent content = contentEntry.getKey();
049            boolean isValid = true;
050            if (content instanceof ProgramItem)
051            {
052                String catalogName = ((ProgramItem) content).getCatalog();
053                
054                for (Content refContent : refContents)
055                {
056                    if (refContent instanceof ProgramItem)
057                    {
058                        if (!catalogName.equals(((ProgramItem) refContent).getCatalog()))
059                        {
060                            // Catalog is invalid
061                            List<String> parameters = new ArrayList<>();
062                            parameters.add(content.getTitle());
063                            parameters.add(content.getName());
064                            parameters.add(content.getId());
065                            errorMessages.add(new I18nizableText("plugin.odf", "PLUGINS_ODF_RELATIONS_SETCONTENTMETADATA_REFERENCE_ERROR_CATALOG", parameters));
066                            errorIds.add(content.getId());
067                            
068                            isValid = false;
069                        }
070                        else if (!content.getLanguage().equals(content.getLanguage()))
071                        {
072                            // Language is invalid
073                            List<String> parameters = new ArrayList<>();
074                            parameters.add(content.getTitle());
075                            parameters.add(content.getName());
076                            parameters.add(content.getId());
077                            errorMessages.add(new I18nizableText("plugin.odf", "PLUGINS_ODF_RELATIONS_SETCONTENTMETADATA_REFERENCE_ERROR_LANGUAGE", parameters));
078                            errorIds.add(content.getId());
079                            
080                            isValid = false;
081                        }
082                    }
083                }
084            }
085            
086            if (isValid)
087            {
088                validContents.put(content, contentEntry.getValue());
089            }
090        }
091        
092        super._setContentMetatada(contentIdsToReference, validContents, contentType, metadataPath, workflowActionIds, errorMessages, errorIds);
093    }
094}