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 org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.commons.lang3.tuple.Pair;
021
022import org.ametys.cms.contenttype.ContentTypesHelper;
023import org.ametys.cms.repository.Content;
024import org.ametys.odf.helper.DeleteODFContentHelper;
025import org.ametys.odf.skill.workflow.SkillEditionFunction;
026
027/**
028 * This element creates an action button to delete a ODF content
029 */
030public class DeleteSkillClientSideElement extends org.ametys.cms.clientsideelement.DeleteContentClientSideElement
031{
032    /** The delete ODF content helper */
033    protected DeleteODFContentHelper _deleteODFContentHelper;
034    /** The content types helper */
035    protected ContentTypesHelper _contentTypesHelper;
036    
037    @Override
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        super.service(manager);
041        _deleteODFContentHelper = (DeleteODFContentHelper) manager.lookup(DeleteODFContentHelper.ROLE);
042        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
043    }
044
045    @Override
046    protected boolean _isContentReferenced(Content content)
047    {
048        if (_contentTypesHelper.isInstanceOf(content, SkillEditionFunction.MACRO_SKILL_TYPE))
049        {
050            for (Pair<String, Content> refPair : _contentHelper.getReferencingContents(content))
051            {
052                // The ref content is not ignored
053                if (!"parentMacroSkill".equals(refPair.getKey()))
054                {
055                    return true;
056                }
057            }
058            
059            return false;
060        }
061        
062        return content.hasReferencingContents();
063    }
064}