001/*
002 *  Copyright 2010 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 */
016
017package org.ametys.plugins.blog.repository;
018
019import java.util.Date;
020import java.util.HashMap;
021import java.util.Locale;
022import java.util.Map;
023
024import org.ametys.core.user.UserIdentity;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.repository.TraversableAmetysObject;
027import org.ametys.plugins.repository.metadata.BinaryMetadata;
028import org.ametys.plugins.repository.metadata.CompositeMetadata;
029import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
030import org.ametys.plugins.repository.metadata.MultilingualString;
031import org.ametys.plugins.repository.metadata.RichText;
032import org.ametys.plugins.repository.metadata.UnknownMetadataException;
033
034/**
035 * Implementation of {@link CompositeMetadata} containing no metadata.<br>
036 * <ul>
037 * <li> getMetadataNames returns an empty array
038 * <li> getXXX(name) and getXXXArray(name) return null (or 0)
039 * <li> getXXX(name, defaultValue) and getXXXArray(name, defaultValues) return default values.
040 * </ul>
041 */
042public class StaticCompositeMetadata implements CompositeMetadata
043{
044    private Map<String, Object> _metadata;
045    
046    /**
047     * Constructor
048     */
049    public StaticCompositeMetadata()
050    {
051        _metadata = new HashMap<>();
052    }
053    
054    /**
055     * Constructor
056     * @param metadata the composite metadata
057     */
058    public StaticCompositeMetadata(Map<String, Object> metadata)
059    {
060        _metadata = metadata;
061    }
062    
063    @Override
064    public BinaryMetadata getBinaryMetadata(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
065    {
066        if (_metadata.containsKey(metadataName))
067        {
068            return (BinaryMetadata) _metadata.get(metadataName);
069        }
070        return null;
071    }
072
073    @Override
074    public boolean getBoolean(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
075    {
076        if (_metadata.containsKey(metadataName))
077        {
078            return (Boolean) _metadata.get(metadataName);
079        }
080        
081        return false;
082    }
083
084    @Override
085    public boolean getBoolean(String metadataName, boolean defaultValue) throws AmetysRepositoryException
086    {
087        if (_metadata.containsKey(metadataName))
088        {
089            return (Boolean) _metadata.get(metadataName);
090        }
091        
092        return defaultValue;
093    }
094
095    @Override
096    public boolean[] getBooleanArray(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
097    {
098        if (_metadata.containsKey(metadataName))
099        {
100            return (boolean[]) _metadata.get(metadataName);
101        }
102        
103        return null;
104    }
105
106    @Override
107    public boolean[] getBooleanArray(String metadataName, boolean[] defaultValues) throws AmetysRepositoryException
108    {
109        return defaultValues;
110    }
111
112    @Override
113    public CompositeMetadata getCompositeMetadata(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
114    {
115        return null;
116    }
117
118    @Override
119    public Date getDate(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
120    {
121        if (_metadata.containsKey(metadataName))
122        {
123            return (Date) _metadata.get(metadataName);
124        }
125        
126        return null;
127    }
128
129    @Override
130    public Date getDate(String metadataName, Date defaultValue) throws AmetysRepositoryException
131    {
132        if (_metadata.containsKey(metadataName))
133        {
134            return (Date) _metadata.get(metadataName);
135        }
136        
137        return defaultValue;
138    }
139
140    @Override
141    public Date[] getDateArray(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
142    {
143        if (_metadata.containsKey(metadataName))
144        {
145            return (Date[]) _metadata.get(metadataName);
146        }
147        
148        return null;
149    }
150
151    @Override
152    public Date[] getDateArray(String metadataName, Date[] defaultValues) throws AmetysRepositoryException
153    {
154        if (_metadata.containsKey(metadataName))
155        {
156            return (Date[]) _metadata.get(metadataName);
157        }
158        
159        return defaultValues;
160    }
161
162    @Override
163    public double getDouble(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
164    {
165        if (_metadata.containsKey(metadataName))
166        {
167            return (Double) _metadata.get(metadataName);
168        }
169        
170        return 0;
171    }
172
173    @Override
174    public double getDouble(String metadataName, double defaultValue) throws AmetysRepositoryException
175    {
176        if (_metadata.containsKey(metadataName))
177        {
178            return (Double) _metadata.get(metadataName);
179        }
180        
181        return defaultValue;
182    }
183
184    @Override
185    public double[] getDoubleArray(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
186    {
187        return null;
188    }
189
190    @Override
191    public double[] getDoubleArray(String metadataName, double[] defaultValues) throws AmetysRepositoryException
192    {
193        return defaultValues;
194    }
195
196    @Override
197    public long getLong(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
198    {
199        if (_metadata.containsKey(metadataName))
200        {
201            return (Long) _metadata.get(metadataName);
202        }
203        
204        return 0;
205    }
206
207    @Override
208    public long getLong(String metadataName, long defaultValue) throws AmetysRepositoryException
209    {
210        if (_metadata.containsKey(metadataName))
211        {
212            return (Long) _metadata.get(metadataName);
213        }
214        
215        return defaultValue;
216    }
217
218    @Override
219    public long[] getLongArray(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
220    {
221        return null;
222    }
223
224    @Override
225    public long[] getLongArray(String metadataName, long[] defaultValues) throws AmetysRepositoryException
226    {
227        return defaultValues;
228    }
229
230    @Override
231    public String[] getMetadataNames() throws AmetysRepositoryException
232    {
233        return _metadata.keySet().toArray(new String[_metadata.size()]);
234    }
235
236    @Override
237    public RichText getRichText(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
238    {
239        return null;
240    }
241
242    @Override
243    public String getString(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
244    {
245        if (_metadata.containsKey(metadataName))
246        {
247            return String.valueOf(_metadata.get(metadataName));
248        }
249        return null;
250    }
251
252    @Override
253    public String getString(String metadataName, String defaultValue) throws AmetysRepositoryException
254    {
255        if (_metadata.containsKey(metadataName))
256        {
257            return (String) _metadata.get(metadataName);
258        }
259        return defaultValue;
260    }
261
262    @Override
263    public String[] getStringArray(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
264    {
265        if (_metadata.containsKey(metadataName))
266        {
267            if (isMultiple(metadataName))
268            {
269                return (String[]) _metadata.get(metadataName);
270            }
271            else
272            {
273                return new String[] {(String) _metadata.get(metadataName)};
274            }
275        }
276        return null;
277    }
278
279    @Override
280    public String[] getStringArray(String metadataName, String[] defaultValues) throws AmetysRepositoryException
281    {
282        if (_metadata.containsKey(metadataName))
283        {
284            return (String[]) _metadata.get(metadataName);
285        }
286        return defaultValues;
287    }
288    
289    @Override
290    public String getLocalizedString(String metadataName, Locale locale) throws UnknownMetadataException, AmetysRepositoryException
291    {
292        return getLocalizedString(metadataName, locale, null);
293    }
294    
295    @Override
296    public String getLocalizedString(String metadataName, Locale locale, String defaultValue) throws AmetysRepositoryException
297    {
298        if (_metadata.containsKey(metadataName))
299        {
300            MultilingualString multilingualString = (MultilingualString) _metadata.get(metadataName);
301            if (multilingualString.hasLocale(locale))
302            {
303                return multilingualString.getValue(locale);
304            }
305        }
306        return defaultValue;
307    }
308    
309    public MultilingualString getMultilingualString(String metadataName) throws AmetysRepositoryException
310    {
311        if (_metadata.containsKey(metadataName))
312        {
313            return (MultilingualString) _metadata.get(metadataName);
314        }
315        return null;
316    }
317    
318    @Override
319    public UserIdentity getUser(String metadataName) throws AmetysRepositoryException
320    {
321        if (_metadata.containsKey(metadataName))
322        {
323            return (UserIdentity) _metadata.get(metadataName);
324        }
325        
326        return null;
327    }
328    
329    @Override
330    public UserIdentity getUser(String metadataName, UserIdentity defaultValue) throws AmetysRepositoryException
331    {
332        if (_metadata.containsKey(metadataName))
333        {
334            return (UserIdentity) _metadata.get(metadataName);
335        }
336        
337        return defaultValue;
338    }
339    
340    @Override
341    public UserIdentity[] getUserArray(String metadataName) throws AmetysRepositoryException
342    {
343        return null;
344    }
345    
346    @Override
347    public UserIdentity[] getUserArray(String metadataName, UserIdentity[] defaultValues) throws AmetysRepositoryException
348    {
349        return defaultValues;
350    }
351
352    @Override
353    public MetadataType getType(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
354    {
355        if (!_metadata.containsKey(metadataName))
356        {
357            return null;
358        }
359        
360        Object value = _metadata.get(metadataName);
361        if (value instanceof String)
362        {
363            return MetadataType.STRING;
364        }
365        else if (value instanceof Long || value instanceof Integer)
366        {
367            return MetadataType.LONG;
368        }
369        else if (value instanceof Double)
370        {
371            return MetadataType.DOUBLE;
372        }
373        else if (value instanceof Date)
374        {
375            return MetadataType.DATE;
376        }
377        else if (value instanceof Boolean)
378        {
379            return MetadataType.BOOLEAN;
380        }
381        else if (value instanceof BinaryMetadata)
382        {
383            return MetadataType.BINARY;
384        }
385        else if (value instanceof MultilingualString)
386        {
387            return MetadataType.MULTILINGUAL_STRING;
388        }
389        
390        return null;
391    }
392
393    @Override
394    public boolean hasMetadata(String metadataName)
395    {
396        return _metadata.containsKey(metadataName);
397    }
398
399    @Override
400    public boolean isMultiple(String metadataName) throws UnknownMetadataException, AmetysRepositoryException
401    {
402        return false;
403    }
404    
405    @Override
406    public void copyTo(ModifiableCompositeMetadata metadata) throws AmetysRepositoryException
407    {
408        throw new UnsupportedOperationException("copyTo is not supported for a StaticCompositeMetadata");
409    }
410
411    @Override
412    public TraversableAmetysObject getObjectCollection(String metadataName) throws AmetysRepositoryException
413    {
414        throw new UnsupportedOperationException("Object collection is not supported on a static composite metadata");
415    }
416    
417}