001/*
002 *  Copyright 2024 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.glossary.theme;
017
018import javax.jcr.Node;
019import javax.jcr.RepositoryException;
020
021import org.ametys.plugins.repository.AmetysObject;
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.RepositoryConstants;
024import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
025import org.ametys.web.repository.SiteAwareAmetysObject;
026import org.ametys.web.repository.site.Site;
027
028/**
029 * Repository implementation of a glossary theme.
030 */
031public class DefaultTheme extends SimpleAmetysObject<DefaultThemeFactory> implements Theme, SiteAwareAmetysObject
032{
033    
034    /** Constant for label attribute. */
035    public static final String ATTRIBUTE_LABEL = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":label";
036    
037    /**
038     * Create a {@link DefaultTheme}.
039     * 
040     * @param node the node backing this {@link AmetysObject}.
041     * @param parentPath the parent path in the Ametys hierarchy.
042     * @param factory the {@link DefaultThemeFactory} which creates the AmetysObject.
043     */
044    public DefaultTheme(Node node, String parentPath, DefaultThemeFactory factory)
045    {
046        super(node, parentPath, factory);
047    }
048    
049    @Override
050    public String getLabel() throws AmetysRepositoryException
051    {
052        try
053        {
054            return getNode().getProperty(ATTRIBUTE_LABEL).getString();
055        }
056        catch (RepositoryException e)
057        {
058            throw new AmetysRepositoryException("Error getting the label attribute.", e);
059        }
060    }
061    
062    @Override
063    public void setLabel(String label) throws AmetysRepositoryException
064    {
065        try
066        {
067            getNode().setProperty(ATTRIBUTE_LABEL, label);
068        }
069        catch (RepositoryException e)
070        {
071            throw new AmetysRepositoryException("Error setting the label attribute.", e);
072        }
073    }
074    
075    @Override
076    public Site getSite() throws AmetysRepositoryException
077    {
078        return getParent().getParent().getParent().getParent().getParent();
079    }
080    
081    @Override
082    public String getSiteName() throws AmetysRepositoryException
083    {
084        return getSite().getName();
085    }
086    
087    /**
088     * Get the theme language.
089     * @return the theme language.
090     */
091    public String getLanguage()
092    {
093        return getParent().getParent().getName();
094    }
095    
096}