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