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.cms.properties.section.technical.impl;
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.core.util.DateUtils;
027import org.ametys.plugins.core.user.UserHelper;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.repository.AmetysObject;
030
031/**
032 * Technical section item to display resource updates (creation and modification).
033 */
034public class ResourceUpdateItem extends AbstractTechnicalItem implements Serviceable
035{
036    private UserHelper _userHelper;
037    
038    @Override
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 Resource;
047    }
048    
049    public Map<String, Object> buildData(AmetysObject ametysObject)
050    {
051        Map<String, Object> resultMap = new LinkedHashMap<>();
052        
053        Resource resource = (Resource) ametysObject;
054        resultMap.put("creationDate", DateUtils.dateToString(resource.getCreationDate()));
055        resultMap.put("creator", _userHelper.user2json(resource.getCreator()));
056        resultMap.put("lastModified", DateUtils.dateToString(resource.getLastModified()));
057        resultMap.put("lastContributor", _userHelper.user2json(resource.getLastContributor()));
058        
059        return resultMap;
060    }
061}