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.odf.clientsideelement;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang3.tuple.Pair;
024
025import org.ametys.cms.contenttype.ContentTypesHelper;
026import org.ametys.cms.repository.Content;
027import org.ametys.odf.skill.ODFSkillsHelper;
028import org.ametys.odf.skill.workflow.SkillEditionFunction;
029
030/**
031 * This element creates an action button to delete a ODF content
032 */
033public class DeleteSkillClientSideElement extends org.ametys.cms.clientsideelement.DeleteContentClientSideElement
034{
035    /** The content types helper */
036    protected ContentTypesHelper _contentTypesHelper;
037    
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        super.service(manager);
042        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
043    }
044
045    @Override
046    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
047    {
048        if (!ODFSkillsHelper.isSkillsEnabled())
049        {
050            return List.of();
051        }
052        
053        return super.getScripts(ignoreRights, contextParameters);
054    }
055    
056    @Override
057    protected boolean _hasRight(Content content)
058    {
059        // Check the right of the button on configuration context instead of content context
060        return hasRight(_rights);
061    }
062    
063    @Override
064    protected boolean _isContentReferenced(Content content)
065    {
066        if (_contentTypesHelper.isInstanceOf(content, SkillEditionFunction.ASBTRACT_MACRO_SKILL_TYPE))
067        {
068            for (Pair<String, Content> refPair : _contentHelper.getReferencingContents(content))
069            {
070                // The ref content is not ignored
071                if (!"parentMacroSkill".equals(refPair.getKey()))
072                {
073                    return true;
074                }
075            }
076            
077            return false;
078        }
079        
080        return content.hasReferencingContents();
081    }
082}