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;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
027import org.ametys.core.ui.Callable;
028
029/**
030 * This component allow to know state of content type in the content type editor
031 * tool
032 */
033public class ContentTypeStateComponent implements Component, Serviceable
034{
035    /** The Avalon role name */
036    public static final String ROLE = ContentTypeStateComponent.class.getName();
037
038    /** The content type extension point instance */
039    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
040
041    /** New content types */
042    private Set<String> _contentTypesMarkedAsNew = new HashSet<>();
043
044    /** Save content types */
045    private Set<String> _contentTypesMarkedAsSaved = new HashSet<>();
046
047    /** Edit content types */
048    private Set<String> _contentTypesMarkedAsEdited = new HashSet<>();
049
050    /** States of a content type */
051    public enum ContentTypeState
052    {
053        /** Means that a content type is in edition in the content type editor tool */
054        EDIT,
055
056        /** Means that a content type is saved (the xml file is saved) but the server has not been restarted in the content type editor tool */
057        SAVE,
058
059        /** Means that a content type has not been modified in the content type editor tool */
060        RESTART
061    }
062
063    @Override
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
067    }
068
069    /**
070     * Get the state of content type according to its id
071     * 
072     * @param id The id of content type
073     * @return the state of content type
074     */
075    @Callable (right = "CMS_Rights_SeeContentTypeHierarchy", context = "/cms")
076    public String getStateContentType(String id)
077    {
078        if (_contentTypesMarkedAsEdited.contains(id))
079        {
080            return ContentTypeState.EDIT.name().toLowerCase();
081        }
082        else if (_contentTypesMarkedAsSaved.contains(id))
083        {
084            return ContentTypeState.SAVE.name().toLowerCase();
085        }
086        else if (_contentTypeExtensionPoint.getExtension(id) != null)
087        {
088            return ContentTypeState.RESTART.name().toLowerCase();
089        }
090        else
091        {
092            return ContentTypeState.EDIT.name().toLowerCase();
093        }
094    }
095
096    /**
097     * Add a content type id to the list of saved content types
098     * 
099     * @param id The id of content type
100     */
101    @Callable(right = "CMS_Rights_EditContentType", context = "/cms")
102    public void markContentTypeAsSaved(String id)
103    {
104        _contentTypesMarkedAsEdited.remove(id);
105        _contentTypesMarkedAsSaved.add(id);
106    }
107
108    /**
109     * Add a content type id to the list of edit content types
110     * 
111     * @param id The id of content type
112     * @param isNew True if the content type is new
113     */
114    @Callable(right = "CMS_Rights_EditContentType", context = "/cms")
115    public void markContentTypeAsEdited(String id, boolean isNew)
116    {
117        _contentTypesMarkedAsEdited.add(id);
118        if (isNew)
119        {
120            _contentTypesMarkedAsNew.add(id);
121        }
122    }
123
124    /**
125     * Remove content type from edit list
126     * 
127     * @param id The id of content type
128     */
129    @Callable(right = "CMS_Rights_EditContentType", context = "/cms")
130    public void removeContentTypeMarkedAsEdited(String id)
131    {
132        _contentTypesMarkedAsEdited.remove(id);
133    }
134
135    /**
136     * Get the new content type which are local of application
137     * 
138     * @return The new content type
139     */
140    public Set<String> getContentTypeMarkedAsNew()
141    {
142        return _contentTypesMarkedAsNew;
143    }
144}