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