001/*
002 *  Copyright 2018 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.plugins.contenttypeseditor.edition.clientsideelement;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.contenttype.AutomaticContentType;
025import org.ametys.cms.contenttype.ContentType;
026import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
027import org.ametys.cms.contenttype.EditContentTypeHelper;
028import org.ametys.cms.contenttype.RemoveContentTypeException;
029import org.ametys.core.ui.Callable;
030import org.ametys.core.ui.StaticClientSideElement;
031import org.ametys.plugins.repository.AmetysObject;
032import org.ametys.plugins.repository.AmetysObjectIterable;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034
035/**
036 * Tool client side element for remove a content type
037 */
038public class RemoveContentTypeClientSideElement extends StaticClientSideElement
039{
040    /** The directory path of content types */
041    private static final String __CONTENT_TYPES_DIR = "WEB-INF/param/content-types/";
042    
043    /** The content type extension point instance */
044    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
045
046    /** The edit content type component instance */
047    protected EditContentTypeHelper _editContentTypeHelper;
048
049    /** The ametys object resolver instance */
050    protected AmetysObjectResolver _ametysObjectResolver;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _editContentTypeHelper = (EditContentTypeHelper) manager.lookup(EditContentTypeHelper.ROLE);
057        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
058        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
059    }
060
061    /**
062     * Remove a existing content type defined in WEB-INF/param/content-types
063     * directory.<br>
064     * This method archived the content type into the
065     * WEB-INF/param/content-types/_archive/pluginName folder.
066     * 
067     * @param contentTypeId the id of content type to remove
068     * @return True if the content type was removed
069     */
070    @Callable(right = "CMS_Rights_EditContentType", context = "/cms")
071    public boolean removeContentType(String contentTypeId)
072    {
073        boolean success = true;
074        try
075        {
076            _editContentTypeHelper.removeContentType(contentTypeId);
077        }
078        catch (RemoveContentTypeException e)
079        {
080            success = false;
081        }
082        return success;
083    }
084
085    /**
086     * Check if a content type is deletable
087     * 
088     * @param contentTypeId Id of content type
089     * @return True if the content type is deletable
090     */
091    @Callable(right = "CMS_Rights_EditContentType", context = "/cms")
092    public boolean isDeletableContentType(String contentTypeId)
093    {
094        return _isAutomaticContentType(contentTypeId) && isLeafContentType(contentTypeId);
095    }
096
097    /**
098     * Check if a content type is unused
099     * 
100     * @param contentTypeId Id of content type
101     * @return True if the content type is unused
102     */
103    @Callable(right = "CMS_Rights_EditContentType", context = "/cms")
104    public boolean isUnusedContentType(String contentTypeId)
105    {
106        AmetysObjectIterable<AmetysObject> query = _ametysObjectResolver.query("//element(*,ametys:content)[@ametys-internal:contentType='" + contentTypeId + "']");
107        return query.getSize() == 0;
108    }
109
110    /**
111     * Recover information about a content type
112     * 
113     * @param contentTypeId Id of content type
114     * @return Information about a content type
115     */
116    @Callable(right = "CMS_Rights_EditContentType", context = "/cms")
117    public Map<String, Object> getRemovedContentTypeInfo(String contentTypeId)
118    {
119        Map<String, Object> contentTypeInfo = new HashMap<>();
120        ContentType contentType = _contentTypeExtensionPoint.getExtension(contentTypeId);
121        String pluginContentType = contentType.getPluginName();
122        contentTypeInfo.put("id", contentTypeId);
123        contentTypeInfo.put("plugin", pluginContentType);
124        contentTypeInfo.put("label", contentType.getLabel());
125        contentTypeInfo.put("location", __CONTENT_TYPES_DIR + pluginContentType);
126        contentTypeInfo.put("destination", __CONTENT_TYPES_DIR + "_archives/" + pluginContentType);
127        return contentTypeInfo;
128    }
129
130    private boolean _isAutomaticContentType(String contentTypeId)
131    {
132        ContentType contentType = _contentTypeExtensionPoint.getExtension(contentTypeId);
133        return contentType instanceof AutomaticContentType;
134    }
135
136    private boolean isLeafContentType(String contentTypeId)
137    {
138        return _contentTypeExtensionPoint.getDirectSubTypes(contentTypeId).size() == 0;
139    }
140
141}