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 javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.cms.properties.section.technical.AbstractTechnicalItem;
028import org.ametys.core.right.RightManager;
029import org.ametys.core.right.RightManager.RightResult;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.jcr.JCRAmetysObject;
032
033/**
034 * Technical section item to display content id.
035 */
036public class AmetysObjectIdItem extends AbstractTechnicalItem implements Serviceable
037{
038    private RightManager _rightManager;
039
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
043    }
044    
045    public boolean supports(AmetysObject ametysObject)
046    {
047        return true;
048    }
049    
050    public Map<String, Object> buildData(AmetysObject ametysObject)
051    {
052        Map<String, Object> resultMap = new LinkedHashMap<>();
053        
054        resultMap.put("hasRepoAccess", _rightManager.currentUserHasRight("REPOSITORY_Rights_Access", "/repository") == RightResult.RIGHT_ALLOW);
055        
056        AmetysObject targetedAO = _getTargetedAO(ametysObject);
057        
058        resultMap.put("id", targetedAO.getId());
059        
060        if (targetedAO instanceof JCRAmetysObject jcrAO)
061        {
062            try
063            {
064                resultMap.put("jcrId", jcrAO.getNode().getIdentifier());
065                resultMap.put("jcrPath", jcrAO.getNode().getPath());
066            }
067            catch (RepositoryException e)
068            {
069                // Should not happen
070            }
071        }
072        
073        return resultMap;
074    }
075    
076    /**
077     * Returns the object on which we want to display identifier
078     * @param ametysObject The selected object
079     * @return The object to target
080     */
081    protected AmetysObject _getTargetedAO(AmetysObject ametysObject)
082    {
083        return ametysObject;
084    }
085}