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.plugins.contentio.synchronize;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023
024/**
025 * Helps to retrieve the mappings as object for SCC
026 */
027public class SynchronizableContentsCollectionMappingHelper implements Component
028{
029    /** The avalon role */
030    public static final String ROLE = SynchronizableContentsCollectionMappingHelper.class.getName();
031    
032    /** Name of parameter into mapping holding the path of metadata */
033    public static final String _PARAM_MAPPING_ITEM_REF = "item-ref";
034    /** Name of parameter into mapping holding the remote attribute */
035    public static final String _PARAM_MAPPING_REMOTE_REF = "remote-ref";
036    /** Name of parameter into mapping holding the synchronized property */
037    public static final String _PARAM_MAPPING_SYNCHRO = "synchro";
038    
039    /**
040     * Get the mapping objects by item ref from the JSON
041     * @param mappings The mappings as a JSON List of entries
042     * @return The mapping object
043     */
044    public Map<String, MappingEntry> getMappingFromJSON(List<Object> mappings)
045    {
046        Map<String, MappingEntry> sccMapping = new HashMap<>();
047        for (Object object : mappings)
048        {
049            @SuppressWarnings("unchecked")
050            Map<String, Object> mapping = (Map<String, Object>) object;
051            
052            String itemRef = (String) mapping.get(_PARAM_MAPPING_ITEM_REF);
053            String remoteRef = (String) mapping.get(_PARAM_MAPPING_REMOTE_REF);
054            boolean isSynchronized = mapping.containsKey(_PARAM_MAPPING_SYNCHRO) ? (Boolean) mapping.get(_PARAM_MAPPING_SYNCHRO) : false;
055            
056            sccMapping.put(itemRef, new MappingEntry(remoteRef, isSynchronized));
057        }
058        
059        return sccMapping;
060    }
061    
062    /**
063     * A mapping entry
064     * @param remoteRef The remote ref
065     * @param synchro The synchro status of the item
066     */
067    public record MappingEntry(String remoteRef, Boolean synchro) {
068    }
069}