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