001/*
002 *  Copyright 2014 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.explorer.resources.metadata.populate;
017
018import java.util.Collections;
019import java.util.Date;
020import java.util.HashSet;
021import java.util.Set;
022
023import org.apache.avalon.framework.configuration.Configurable;
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026import org.apache.avalon.framework.logger.AbstractLogEnabled;
027import org.apache.tika.metadata.DublinCore;
028import org.apache.tika.metadata.Metadata;
029
030import org.ametys.plugins.explorer.resources.ModifiableResource;
031
032/**
033 * {@link ResourceMetadataPopulator} which populates Dublin Core metadata.
034 */
035public class DublinCorePopulator extends AbstractLogEnabled implements ResourceMetadataPopulator, Configurable
036{
037    
038    private Set<String> _types;
039    
040    @Override
041    public void configure(Configuration configuration) throws ConfigurationException
042    {
043        _types = new HashSet<>();
044        
045        Configuration[] types = configuration.getChild("types").getChildren("type");
046        for (Configuration type : types)
047        {
048            _types.add(type.getValue());
049        }
050    }
051    
052    @Override
053    public Set<String> getTypes()
054    {
055        return Collections.unmodifiableSet(_types);
056    }
057
058    @Override
059    public void populate(ModifiableResource resource, Metadata metadata)
060    {
061        String creator = metadata.get(DublinCore.CREATOR);
062        String contributor = metadata.get(DublinCore.CONTRIBUTOR);
063        String coverage = metadata.get(DublinCore.COVERAGE);
064        Date date = metadata.getDate(DublinCore.DATE);
065        String description = metadata.get(DublinCore.DESCRIPTION);
066        String format = metadata.get(DublinCore.FORMAT);
067        String identifier = metadata.get(DublinCore.IDENTIFIER);
068        String language = metadata.get(DublinCore.LANGUAGE);
069        String publisher = metadata.get(DublinCore.PUBLISHER);
070        String relation = metadata.get(DublinCore.RELATION);
071        String rights = metadata.get(DublinCore.RIGHTS);
072        String source = metadata.get(DublinCore.SOURCE);
073        String[] subject = metadata.getValues(DublinCore.SUBJECT);
074        String title = metadata.get(DublinCore.TITLE);
075        String type = metadata.get(DublinCore.TYPE);
076        
077        _setDCCreator(resource, creator);
078        _setDCContributor(resource, contributor);
079        _setDCCoverage(resource, coverage);
080        _setDCDate(resource, date);
081        _setDCDescription(resource, description);
082        _setDCFormat(resource, format);
083        _setDCIdentifier(resource, identifier);
084        _setDCLanguage(resource, language);
085        _setDCPublisher(resource, publisher);
086        _setDCRelation(resource, relation);
087        _setDCRights(resource, rights);
088        _setDCSource(resource, source);
089        _setDCSubject(resource, subject);
090        _setDCTitle(resource, title);
091        _setDCType(resource, type);
092    }
093    
094    /**
095     * Set DC Creator
096     * @param resource The resource to edit
097     * @param creator The creator
098     */
099    protected void _setDCCreator(ModifiableResource resource, String creator)
100    {
101        if (creator != null)
102        {
103            resource.setDCCreator(creator);
104        }
105    }
106    
107    /**
108     * Set DC Contributor
109     * @param resource The resource to edit
110     * @param contributor The contributor login
111     */
112    protected void _setDCContributor(ModifiableResource resource, String contributor)
113    {
114        if (contributor != null)
115        {
116            resource.setDCContributor(contributor);
117        }
118    }
119    
120    /**
121     * Set DC Coverage
122     * @param resource The resource to edit
123     * @param coverage The coverage
124     */
125    protected void _setDCCoverage(ModifiableResource resource, String coverage)
126    {
127        if (coverage != null)
128        {
129            resource.setDCCoverage(coverage);
130        }
131    }
132    
133    /**
134     * Set DC Date
135     * @param resource The resource to edit
136     * @param date The date
137     */
138    protected void _setDCDate(ModifiableResource resource, Date date)
139    {
140        if (date != null)
141        {
142            resource.setDCDate(date);
143        }
144    }
145    
146    /**
147     * Set DC Description
148     * @param resource The resource to edit
149     * @param description The description
150     */
151    protected void _setDCDescription(ModifiableResource resource, String description)
152    {
153        if (description != null)
154        {
155            resource.setDCDescription(description);
156        }
157    }
158    
159    /**
160     * Set DC Format
161     * @param resource The resource to edit
162     * @param format The format
163     */
164    protected void _setDCFormat(ModifiableResource resource, String format)
165    {
166        if (format != null)
167        {
168            resource.setDCFormat(format);
169        }
170    }
171    
172    /**
173     * Set DC Identifier
174     * @param resource The resource to edit
175     * @param identifier The identifier
176     */
177    protected void _setDCIdentifier(ModifiableResource resource, String identifier)
178    {
179        if (identifier != null)
180        {
181            resource.setDCIdentifier(identifier);
182        }
183    }
184    
185    /**
186     * Set DC language
187     * @param resource The resource to edit
188     * @param language The language
189     */
190    protected void _setDCLanguage(ModifiableResource resource, String language)
191    {
192        if (language != null)
193        {
194            resource.setDCLanguage(language);
195        }
196    }
197    
198    /**
199     * Set DC Publisher
200     * @param resource The resource to edit
201     * @param publisher The publisher
202     */
203    protected void _setDCPublisher(ModifiableResource resource, String publisher)
204    {
205        if (publisher != null)
206        {
207            resource.setDCPublisher(publisher);
208        }
209    }
210    
211    /**
212     * Set DC Relation
213     * @param resource The resource to edit
214     * @param relation The relation
215     */
216    protected void _setDCRelation(ModifiableResource resource, String relation)
217    {
218        if (relation != null)
219        {
220            resource.setDCRelation(relation);
221        }
222    }
223    
224    /**
225     * Set DC rights
226     * @param resource The resource to edit
227     * @param rights The rights
228     */
229    protected void _setDCRights(ModifiableResource resource, String rights)
230    {
231        if (rights != null)
232        {
233            resource.setDCRights(rights);
234        }
235    }
236    
237    /**
238     * Set DC Source
239     * @param resource The resource to edit
240     * @param source The source
241     */
242    protected void _setDCSource(ModifiableResource resource, String source)
243    {
244        if (source != null)
245        {
246            resource.setDCSource(source);
247        }
248    }
249    
250    /**
251     * Set DC Subject
252     * @param resource The resource to edit
253     * @param subject The subjects
254     */
255    protected void _setDCSubject(ModifiableResource resource, String[] subject)
256    {
257        if (subject != null && subject.length > 0)
258        {
259            resource.setDCSubject(subject);
260        }
261    }
262    
263    /**
264     * Set DC Title
265     * @param resource The resource to edit
266     * @param title The title
267     */
268    protected void _setDCTitle(ModifiableResource resource, String title)
269    {
270        if (title != null)
271        {
272            resource.setDCTitle(title);
273        }
274    }
275    
276    /**
277     * Set DC Type
278     * @param resource The resource to edit
279     * @param type The type
280     */
281    protected void _setDCType(ModifiableResource resource, String type)
282    {
283        if (type != null)
284        {
285            resource.setDCType(type);
286        }
287    }
288}