001/*
002 *  Copyright 2024 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.properties.section.technical;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.properties.section.technical.AbstractTechnicalItem;
026import org.ametys.cms.repository.Content;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
028import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.runtime.model.type.DataContext;
031
032/**
033 * Technical zone item to display synchronization informations.
034 */
035public class ContentSCCItem extends AbstractTechnicalItem implements Serviceable
036{
037    private SynchronizableContentsCollectionHelper _sccHelper;
038    
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
042    }
043    
044    public boolean supports(AmetysObject ametysObject)
045    {
046        return ametysObject instanceof Content content && !_sccHelper.getSynchronizableCollectionIds(content).isEmpty();
047    }
048    
049    public Map<String, Object> buildData(AmetysObject ametysObject)
050    {
051        Map<String, Object> resultMap = new LinkedHashMap<>();
052        
053        Content content = (Content) ametysObject;
054        DataContext dataContext = DataContext.newInstance().withEmptyValues(false);
055
056        resultMap.put("scc", content.dataToJSON(SynchronizableContentsCollection.COLLECTION_ID_DATA_NAME, dataContext));
057        resultMap.put("lastSynchronization", content.dataToJSON(SynchronizableContentsCollection.LAST_SYNCHRONIZATION_DATA_NAME, dataContext));
058        resultMap.put("lastSynchronizationUser", content.dataToJSON(SynchronizableContentsCollection.LAST_SYNCHRONIZATION_USER_DATA_NAME, dataContext));
059        
060        return resultMap;
061    }
062}