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.helper.DeleteODFContentHelper;
028import org.ametys.odf.skill.ODFSkillsHelper;
029import org.ametys.odf.skill.workflow.SkillEditionFunction;
030
031/**
032 * This element creates an action button to delete a ODF content
033 */
034public class DeleteSkillClientSideElement extends org.ametys.cms.clientsideelement.DeleteContentClientSideElement
035{
036    /** The delete ODF content helper */
037    protected DeleteODFContentHelper _deleteODFContentHelper;
038    /** The content types helper */
039    protected ContentTypesHelper _contentTypesHelper;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _deleteODFContentHelper = (DeleteODFContentHelper) manager.lookup(DeleteODFContentHelper.ROLE);
046        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
047    }
048
049    @Override
050    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
051    {
052        if (!ODFSkillsHelper.isSkillsEnabled())
053        {
054            return List.of();
055        }
056        
057        return super.getScripts(ignoreRights, contextParameters);
058    }
059    
060    @Override
061    protected boolean _isContentReferenced(Content content)
062    {
063        if (_contentTypesHelper.isInstanceOf(content, SkillEditionFunction.MACRO_SKILL_TYPE))
064        {
065            for (Pair<String, Content> refPair : _contentHelper.getReferencingContents(content))
066            {
067                // The ref content is not ignored
068                if (!"parentMacroSkill".equals(refPair.getKey()))
069                {
070                    return true;
071                }
072            }
073            
074            return false;
075        }
076        
077        return content.hasReferencingContents();
078    }
079}