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.apache.commons.lang3.ArrayUtils;
028
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.AmetysRepositoryException;
031import org.ametys.plugins.repository.RepositoryConstants;
032import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
033import org.ametys.web.repository.SiteAwareAmetysObject;
034import org.ametys.web.repository.site.Site;
035
036/**
037 * Repository implementation of a glossary definition.
038 */
039public class DefaultDefinition extends SimpleAmetysObject<DefaultDefinitionFactory> implements Definition, SiteAwareAmetysObject
040{
041    
042    /** Constant for word attribute. */
043    public static final String ATTRIBUTE_WORD = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":word";
044    /** Constant for variants attribute. */
045    public static final String ATTRIBUTE_VARIANTS = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":variants";
046    /** Constant for content attribute. */
047    public static final String ATTRIBUTE_CONTENT = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":content";
048    /** Constant for display attribute. */
049    public static final String ATTRIBUTE_DISPLAY = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":display";
050    /** Constant for themes attribute. */
051    public static final String ATTRIBUTE_THEMES = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":themes";
052    
053    /**
054     * Create a {@link DefaultDefinition}.
055     * 
056     * @param node the node backing this {@link AmetysObject}.
057     * @param parentPath the parent path in the Ametys hierarchy.
058     * @param factory the {@link DefaultDefinitionFactory} which creates the AmetysObject.
059     */
060    public DefaultDefinition(Node node, String parentPath, DefaultDefinitionFactory factory)
061    {
062        super(node, parentPath, factory);
063    }
064    
065    @Override
066    public String getWord() throws AmetysRepositoryException
067    {
068        try
069        {
070            return getNode().getProperty(ATTRIBUTE_WORD).getString();
071        }
072        catch (RepositoryException e)
073        {
074            throw new AmetysRepositoryException("Error getting the word property.", e);
075        }
076    }
077
078    @Override
079    public void setWord(String word) throws AmetysRepositoryException
080    {
081        try
082        {
083            getNode().setProperty(ATTRIBUTE_WORD, word);
084        }
085        catch (RepositoryException e)
086        {
087            throw new AmetysRepositoryException("Error setting the word property.", e);
088        }
089    }
090
091    @Override
092    public Set<String> getVariants() throws AmetysRepositoryException
093    {
094        try
095        {
096            Set<String> variants = new LinkedHashSet<>();
097            
098            Value[] values = getNode().getProperty(ATTRIBUTE_VARIANTS).getValues();
099            for (Value value : values)
100            {
101                variants.add(value.getString());
102            }
103            
104            return variants;
105        }
106        catch (RepositoryException e)
107        {
108            throw new AmetysRepositoryException("Error getting the variants property.", e);
109        }
110
111    }
112
113    @Override
114    public void setVariants(Collection<String> variants) throws AmetysRepositoryException
115    {
116        try
117        {
118            String[] values = variants.toArray(new String[variants.size()]);
119            getNode().setProperty(ATTRIBUTE_VARIANTS, values);
120        }
121        catch (RepositoryException e)
122        {
123            throw new AmetysRepositoryException("Error setting the variants property.", e);
124        }
125    }
126
127    @Override
128    public String getContent() throws AmetysRepositoryException
129    {
130        try
131        {
132            return getNode().getProperty(ATTRIBUTE_CONTENT).getString();
133        }
134        catch (RepositoryException e)
135        {
136            throw new AmetysRepositoryException("Error getting the content property.", e);
137        }
138    }
139
140    @Override
141    public void setContent(String content) throws AmetysRepositoryException
142    {
143        try
144        {
145            getNode().setProperty(ATTRIBUTE_CONTENT, content);
146        }
147        catch (RepositoryException e)
148        {
149            throw new AmetysRepositoryException("Error setting the content property.", e);
150        }
151    }
152
153    @Override
154    public boolean displayOnText() throws AmetysRepositoryException
155    {
156        try
157        {
158            return getNode().getProperty(ATTRIBUTE_DISPLAY).getBoolean();
159        }
160        catch (RepositoryException e)
161        {
162            throw new AmetysRepositoryException("Error getting the display property.", e);
163        }
164    }
165
166    @Override
167    public void setDisplayOnText(boolean displayOnText) throws AmetysRepositoryException
168    {
169        try
170        {
171            getNode().setProperty(ATTRIBUTE_DISPLAY, displayOnText);
172        }
173        catch (RepositoryException e)
174        {
175            throw new AmetysRepositoryException("Error setting the display property.", e);
176        }
177    }
178    
179    @Override
180    public Set<String> getAllForms() throws AmetysRepositoryException
181    {
182        Set<String> allForms = new HashSet<>();
183        
184        allForms.add(getWord());
185        allForms.addAll(getVariants());
186        
187        return allForms;
188    }
189    
190    @Override
191    public Site getSite() throws AmetysRepositoryException
192    {
193        return getParent().getParent().getParent().getParent().getParent();
194    }
195    
196    @Override
197    public String getSiteName() throws AmetysRepositoryException
198    {
199        return getSite().getName();
200    }
201    
202    /**
203     * Get the definition language.
204     * @return the definition language.
205     */
206    public String getLanguage()
207    {
208        return getParent().getParent().getName();
209    }
210    
211    @Override
212    public String[] getThemes() throws AmetysRepositoryException
213    {
214        return _getListFromAttributeName(ATTRIBUTE_THEMES);
215    }
216
217    @Override
218    public void setThemes(String[] themes) throws AmetysRepositoryException
219    {
220        try
221        {
222            getNode().setProperty(ATTRIBUTE_THEMES, themes);
223        }
224        catch (RepositoryException e)
225        {
226            throw new AmetysRepositoryException("An error occurred while trying to set the property '" + ATTRIBUTE_THEMES + "'.", e);
227        }
228    }
229
230    @Override
231    public void removeTheme(String themeId) throws AmetysRepositoryException
232    {
233        try
234        {
235            String[] themes = getThemes();
236            String[] updatedThemes = ArrayUtils.removeElement(themes, themeId);
237            getNode().setProperty(ATTRIBUTE_THEMES, updatedThemes);
238        }
239        catch (RepositoryException e)
240        {
241            throw new AmetysRepositoryException("Error removing theme of id " + themeId, e);
242        }
243    }
244    
245    /**
246     * Retrieves the list of values corresponding to the attribute name passed as
247     * parameter
248     * 
249     * @param attributeName the name of the attribute to retrieve
250     * @return the list corresponding to the attribute name
251     */
252    private String[] _getListFromAttributeName(String attributeName)
253    {
254        try
255        {
256            if (getNode().hasProperty(attributeName))
257            {
258                Value[] values = getNode().getProperty(attributeName).getValues();
259
260                String[] list = new String[values.length];
261
262                for (int i = 0; i < values.length; i++)
263                {
264                    list[i] = values[i].getString();
265                }
266
267                return list;
268            }
269            else
270            {
271                return new String[0];
272            }
273        }
274        catch (RepositoryException e)
275        {
276            throw new AmetysRepositoryException("An error occurred while trying to get the property '" + attributeName + "'.", e);
277        }
278    }
279    
280}