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.web.trash;
017
018import org.ametys.cms.model.properties.AbstractIndexableProperty;
019import org.ametys.cms.trash.element.DefaultTrashElement;
020import org.ametys.plugins.repository.AmetysObject;
021import org.ametys.plugins.repository.root.RootAmetysObject;
022import org.ametys.runtime.i18n.I18nizableText;
023import org.ametys.runtime.model.type.ModelItemTypeConstants;
024
025/**
026 * Indicate the site related to this trash element if any
027 */
028public final class TrashElementSiteProperty extends AbstractIndexableProperty<String, String, DefaultTrashElement>
029{
030    /** Item name for the site of the object */
031    public static final String NAME = "siteName";
032    
033    private TrashElementSiteProperty()
034    {
035        setName(NAME);
036    }
037    
038    /**
039     * Create a trash element site property
040     * @param availableTypesRole the role of the extension point containing all available types for this property
041     * @return the created property
042     * @throws Exception if an error occurs
043     */
044    public static TrashElementSiteProperty of(String availableTypesRole) throws Exception
045    {
046        TrashElementSiteProperty property = new TrashElementSiteProperty();
047        property.init(availableTypesRole);
048        return property;
049    }
050    
051    public String getValue(DefaultTrashElement trashElement)
052    {
053        // See WebTrashElementDAO.getOrCreateRoot for more details
054        // If the element is not linked to a site, the parent of the trash root is the Ametys root
055        AmetysObject parent = trashElement.getParent();
056        if (parent.getParent() instanceof RootAmetysObject)
057        {
058            return null;
059        }
060        // If an element is linked to a site, its in a specific root with the sitename as name
061        else
062        {
063            return parent.getName();
064        }
065    }
066
067    @Override
068    protected String getTypeId()
069    {
070        return ModelItemTypeConstants.STRING_TYPE_ID;
071    }
072    
073    @Override
074    public I18nizableText getLabel()
075    {
076        return new I18nizableText("plugin.web", "PLUGINS_WEB_TRASH_MODEL_SITENAME");
077    }
078
079    @Override
080    public I18nizableText getDescription()
081    {
082        return new I18nizableText("plugin.web", "PLUGINS_WEB_TRASH_MODEL_SITENAME_DESC");
083    }
084}