001/*
002 *  Copyright 2011 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;
017
018import java.util.Collection;
019import java.util.HashSet;
020import java.util.LinkedHashSet;
021import java.util.Set;
022
023import javax.jcr.Node;
024import javax.jcr.RepositoryException;
025import javax.jcr.Value;
026
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.RepositoryConstants;
030import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
031import org.ametys.web.repository.SiteAwareAmetysObject;
032import org.ametys.web.repository.site.Site;
033
034/**
035 * Repository implementation of a glossary definition.
036 */
037public class DefaultDefinition extends SimpleAmetysObject<DefaultDefinitionFactory> implements Definition, SiteAwareAmetysObject
038{
039    
040    /** Constant for word metadata. */
041    public static final String METADATA_WORD = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":word";
042    /** Constant for variants metadata. */
043    public static final String METADATA_VARIANTS = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":variants";
044    /** Constant for content metadata. */
045    public static final String METADATA_CONTENT = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":content";
046    /** Constant for display metadata. */
047    public static final String METADATA_DISPLAY = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":display";
048    
049    /**
050     * Create a {@link DefaultDefinition}.
051     * 
052     * @param node the node backing this {@link AmetysObject}.
053     * @param parentPath the parent path in the Ametys hierarchy.
054     * @param factory the {@link DefaultDefinitionFactory} which creates the AmetysObject.
055     */
056    public DefaultDefinition(Node node, String parentPath, DefaultDefinitionFactory factory)
057    {
058        super(node, parentPath, factory);
059    }
060    
061    @Override
062    public String getWord() throws AmetysRepositoryException
063    {
064        try
065        {
066            return getNode().getProperty(METADATA_WORD).getString();
067        }
068        catch (RepositoryException e)
069        {
070            throw new AmetysRepositoryException("Error getting the word property.", e);
071        }
072    }
073
074    @Override
075    public void setWord(String word) throws AmetysRepositoryException
076    {
077        try
078        {
079            getNode().setProperty(METADATA_WORD, word);
080        }
081        catch (RepositoryException e)
082        {
083            throw new AmetysRepositoryException("Error setting the word property.", e);
084        }
085    }
086
087    @Override
088    public Set<String> getVariants() throws AmetysRepositoryException
089    {
090        try
091        {
092            Set<String> variants = new LinkedHashSet<>();
093            
094            Value[] values = getNode().getProperty(METADATA_VARIANTS).getValues();
095            for (Value value : values)
096            {
097                variants.add(value.getString());
098            }
099            
100            return variants;
101        }
102        catch (RepositoryException e)
103        {
104            throw new AmetysRepositoryException("Error getting the variants property.", e);
105        }
106
107    }
108
109    @Override
110    public void setVariants(Collection<String> variants) throws AmetysRepositoryException
111    {
112        try
113        {
114            String[] values = variants.toArray(new String[variants.size()]);
115            getNode().setProperty(METADATA_VARIANTS, values);
116        }
117        catch (RepositoryException e)
118        {
119            throw new AmetysRepositoryException("Error setting the variants property.", e);
120        }
121    }
122
123    @Override
124    public String getContent() throws AmetysRepositoryException
125    {
126        try
127        {
128            return getNode().getProperty(METADATA_CONTENT).getString();
129        }
130        catch (RepositoryException e)
131        {
132            throw new AmetysRepositoryException("Error getting the content property.", e);
133        }
134    }
135
136    @Override
137    public void setContent(String content) throws AmetysRepositoryException
138    {
139        try
140        {
141            getNode().setProperty(METADATA_CONTENT, content);
142        }
143        catch (RepositoryException e)
144        {
145            throw new AmetysRepositoryException("Error setting the content property.", e);
146        }
147    }
148
149    @Override
150    public boolean displayOnText() throws AmetysRepositoryException
151    {
152        try
153        {
154            return getNode().getProperty(METADATA_DISPLAY).getBoolean();
155        }
156        catch (RepositoryException e)
157        {
158            throw new AmetysRepositoryException("Error getting the display property.", e);
159        }
160    }
161
162    @Override
163    public void setDisplayOnText(boolean displayOnText) throws AmetysRepositoryException
164    {
165        try
166        {
167            getNode().setProperty(METADATA_DISPLAY, displayOnText);
168        }
169        catch (RepositoryException e)
170        {
171            throw new AmetysRepositoryException("Error setting the display property.", e);
172        }
173    }
174    
175    @Override
176    public Set<String> getAllForms() throws AmetysRepositoryException
177    {
178        Set<String> allForms = new HashSet<>();
179        
180        allForms.add(getWord());
181        allForms.addAll(getVariants());
182        
183        return allForms;
184    }
185    
186    @Override
187    public Site getSite() throws AmetysRepositoryException
188    {
189        return getParent().getParent().getParent().getParent().getParent();
190    }
191    
192    @Override
193    public String getSiteName() throws AmetysRepositoryException
194    {
195        return getSite().getName();
196    }
197    
198    /**
199     * Get the definition language.
200     * @return the definition language.
201     */
202    public String getLanguage()
203    {
204        return getParent().getParent().getName();
205    }
206    
207}