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.cms.trash.model;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.List;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
029import org.ametys.plugins.repository.trash.TrashElementTypeEnumerator;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.runtime.model.DefaultElementDefinition;
032import org.ametys.runtime.model.ElementDefinition;
033import org.ametys.runtime.model.Model;
034import org.ametys.runtime.model.ModelItem;
035import org.ametys.runtime.model.type.ModelItemTypeConstants;
036
037/**
038 * The model for trash elements.
039 */
040public class TrashElementModel implements Model, Component, Serviceable
041{
042    /** The avalon role */
043    public static final String ROLE = TrashElementModel.class.getName();
044    
045    /** Item name for the title of the element */
046    public static final String TITLE = "title";
047    /** Item name for the type of the element */
048    public static final String TRASH_TYPE = "trashType";
049    /** Item name for the author of the deletion */
050    public static final String AUTHOR = "author";
051    /** Item name for the deletion date */
052    public static final String DATE = "date";
053    /** Item name for the parent path of the object */
054    public static final String PARENT_PATH = "parentPath";
055    /** Item name for the reference to the deleted object */
056    public static final String DELETED_OBJECT = "deletedObject";
057    /** Item name for the reference to the deleted object */
058    public static final String HIDDEN = "hidden";
059    /** Item name for the reference to the linked objects */
060    public static final String LINKED_OBJECTS = "linkedObjects";
061    
062    /** The trash element type enumerator */
063    protected TrashElementTypeEnumerator _trashElementTypeEnum;
064    
065    private Collection<? extends ModelItem> _modelItems;
066    
067    public void service(ServiceManager manager) throws ServiceException
068    {
069        _trashElementTypeEnum = (TrashElementTypeEnumerator) manager.lookup(TrashElementTypeEnumerator.ROLE);
070    }
071    
072    public String getId()
073    {
074        return "TrashElement";
075    }
076
077    public String getFamilyId()
078    {
079        return "Trash";
080    }
081
082    public Collection<? extends ModelItem> getModelItems()
083    {
084        if (_modelItems == null)
085        {
086            try
087            {
088                _modelItems = List.copyOf(createModelItems());
089                
090                for (ModelItem modelItem : _modelItems)
091                {
092                    modelItem.setModel(this);
093                }
094            }
095            catch (Exception e)
096            {
097                throw new AmetysRepositoryException("An error occurred while create the trash element model", e);
098            }
099        }
100        return _modelItems;
101    }
102
103    /**
104     * Create the model items present in the trash element model
105     * @return the model items
106     * @throws Exception if an occurs when creating the model items
107     */
108    protected List<ElementDefinition> createModelItems() throws Exception
109    {
110        List<ElementDefinition> items = new ArrayList<>();
111        
112        items.add(_getDefinition(TITLE, ModelItemTypeConstants.STRING_TYPE_ID, false));
113        ElementDefinition<String> trashType = _getDefinition(TRASH_TYPE, ModelItemTypeConstants.STRING_TYPE_ID, false);
114        trashType.setEnumerator(_trashElementTypeEnum);
115        items.add(trashType);
116        items.add(_getDefinition(DATE, ModelItemTypeConstants.DATETIME_TYPE_ID, false));
117        items.add(_getDefinition(AUTHOR, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, false));
118        items.add(_getDefinition(PARENT_PATH, ModelItemTypeConstants.STRING_TYPE_ID, false));
119        items.add(_getDefinition(DELETED_OBJECT, ModelItemTypeConstants.STRING_TYPE_ID, false));
120        items.add(_getDefinition(HIDDEN, ModelItemTypeConstants.BOOLEAN_TYPE_ID, false));
121        items.add(_getDefinition(LINKED_OBJECTS, ModelItemTypeConstants.STRING_TYPE_ID, true));
122        
123        return items;
124    }
125    
126    private <T> ElementDefinition<T> _getDefinition(String name, String type, boolean isMultiple) throws ServiceException
127    {
128        DefaultElementDefinition<T> definition = DefaultElementDefinition.of(name, isMultiple, type, ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC);
129        definition.setLabel(new I18nizableText("plugin.cms", "PLUGINS_CMS_TRASH_MODEL_" + name.toUpperCase()));
130        definition.setDescription(new I18nizableText("plugin.cms", "PLUGINS_CMS_TRASH_MODEL_" + name.toUpperCase() + "_DESC"));
131        return definition;
132    }
133}