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