001/*
002 *  Copyright 2025 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.cms.properties.section.technical.impl;
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.plugins.core.user.UserHelper;
029import org.ametys.plugins.repository.AmetysObject;
030
031/**
032 * Technical section item to display date of a content.
033 */
034public class ContentDateItem extends AbstractTechnicalItem implements Serviceable
035{
036    /** The user helper */
037    protected UserHelper _userHelper;
038    
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE);
042    }
043    
044    public boolean supports(AmetysObject ametysObject)
045    {
046        return ametysObject instanceof Content;
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        
055        Map<String, Object> dateCreator = new HashMap<>();
056        dateCreator.put("date", content.getCreationDate());
057        dateCreator.put("user", _userHelper.user2json(content.getCreator(), true));
058        resultMap.put("creator", dateCreator);
059        
060        Map<String, Object> dateContributor = new HashMap<>();
061        dateContributor.put("date", content.getLastModified());
062        dateContributor.put("user", _userHelper.user2json(content.getLastContributor(), true));
063        resultMap.put("contributor", dateContributor);
064        
065        return resultMap;
066    }
067}