001/*
002 *  Copyright 2018 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.cms.contenttype;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.commons.lang3.builder.HashCodeBuilder;
024
025import org.ametys.cms.content.references.RichTextOutgoingReferencesExtractor;
026import org.ametys.cms.transformation.RichTextTransformer;
027import org.ametys.runtime.parameter.Parameter;
028
029/**
030 * Metadata definition. The {@link #getId()} method returns the metadata path,
031 * separated by slashes.
032 * @deprecated Use {@link AttributeDefinition} instead
033 */
034@Deprecated
035public class MetadataDefinition extends Parameter<MetadataType> implements MetadataDefinitionHolder
036{
037    private String _name;
038
039    private boolean _isMultiple;
040
041    private String _contentType;
042
043    private String _invertRelationPath;
044
045    private boolean _forceInvert;
046
047    private RichTextTransformer _transformer;
048
049    private RichTextOutgoingReferencesExtractor _richTextOutgoingReferencesExtractor;
050
051    // private String[] _defaultValues;
052    private final Map<String, MetadataDefinition> _children = new LinkedHashMap<>();
053
054    private String _refContentTypeId;
055
056    private String _customEnumerator;
057
058    private Configuration _enumeratorConfiguration;
059
060    private String _customValidator;
061
062    private Configuration _validatorConfiguration;
063
064    /**
065     * Retrieves the metadata name.
066     * 
067     * @return the metadata name.
068     */
069    public String getName()
070    {
071        return _name;
072    }
073
074    /**
075     * Set the metadata name.
076     * 
077     * @param name the metadata name.
078     */
079    public void setName(String name)
080    {
081        _name = name;
082    }
083
084    /**
085     * Test if the metadata is multiple.
086     * 
087     * @return <code>true</code> if the metadata is multiple.
088     */
089    public boolean isMultiple()
090    {
091        return _isMultiple;
092    }
093
094    /**
095     * Set the metadata multiple status.
096     * 
097     * @param isMultiple the metadata multiple status.
098     */
099    public void setMultiple(boolean isMultiple)
100    {
101        _isMultiple = isMultiple;
102    }
103
104    /**
105     * Get the content type of reference (ie. the content type which has
106     * declared the metadata definition)
107     * 
108     * @return The id of reference content type
109     */
110    public String getReferenceContentType()
111    {
112        return _refContentTypeId;
113    }
114
115    /**
116     * Set the id of reference content type (ie. the content type which has
117     * declared the metadata definition)
118     * 
119     * @param cTypeId The id of reference content type
120     */
121    public void setReferenceContentType(String cTypeId)
122    {
123        _refContentTypeId = cTypeId;
124    }
125
126    /**
127     * Get the metadata's content type (only applicable for a content-typed
128     * metadata).
129     * 
130     * @return the metadata's content type, null if no restriction.
131     */
132    public String getContentType()
133    {
134        return _contentType;
135    }
136
137    /**
138     * Set the content type (only applicable for a content-typed metadata).
139     * 
140     * @param contentType the metadata's content type.
141     */
142    public void setContentType(String contentType)
143    {
144        _contentType = contentType;
145    }
146
147    /**
148     * Get the metadata's mutual relationship path (only applicable for a
149     * content-typed metadata).
150     * 
151     * @return the metadata's mutual relationship path, null if not applicable.
152     */
153    public String getInvertRelationPath()
154    {
155        return _invertRelationPath;
156    }
157
158    /**
159     * Set the metadata's mutual relationship path (only applicable for a
160     * content-typed metadata).
161     * 
162     * @param invertRelationPath the metadata's mutual relationship path,
163     *            separated by slashes.
164     */
165    public void setInvertRelationPath(String invertRelationPath)
166    {
167        _invertRelationPath = invertRelationPath;
168    }
169
170    /**
171     * Force mutual relationship regardless of user's rights (only applicable
172     * for a content-typed metadata with invert relation path).
173     * 
174     * @param force true to force mutual relationship regardless of user's
175     *            rights
176     */
177    public void setForceInvert(boolean force)
178    {
179        _forceInvert = force;
180    }
181
182    /**
183     * Returns true if mutual relationship should be set regardless of user's
184     * rights
185     * 
186     * @return true if mutual relationship should be set regardless of user's
187     *         rights
188     */
189    public boolean getForceInvert()
190    {
191        return _forceInvert;
192    }
193
194    /**
195     * Retrieves the rich text transformer to use.
196     * 
197     * @return the rich text transformer.
198     */
199    public RichTextTransformer getRichTextTransformer()
200    {
201        return _transformer;
202    }
203
204    /**
205     * Set the rich text transformer to use.
206     * 
207     * @param transformer the rich text transformer.
208     */
209    public void setRichTextTransformer(RichTextTransformer transformer)
210    {
211        _transformer = transformer;
212    }
213
214    /**
215     * Retrieves the rich text outgoing references extractor to use.
216     * 
217     * @return the rich text outgoing references extractor.
218     */
219    public RichTextOutgoingReferencesExtractor getRichTextOutgoingReferencesExtractor()
220    {
221        return _richTextOutgoingReferencesExtractor;
222    }
223
224    /**
225     * Set the rich text outgoing references extractor to use.
226     * 
227     * @param richTextOutgoingReferencesExtractor The rich text outgoing
228     *            references extractor
229     */
230    public void setRichTextOutgoingReferencesExtractor(RichTextOutgoingReferencesExtractor richTextOutgoingReferencesExtractor)
231    {
232        _richTextOutgoingReferencesExtractor = richTextOutgoingReferencesExtractor;
233    }
234
235    /**
236     * Retrieves the metadata default values.
237     * 
238     * @return the metadata default values.
239     */
240    /*
241     * public String[] getDefaultValues() { return _defaultValues; }
242     * 
243     * /** Set the metadata default values.
244     * 
245     * @param defaultValues the metadata default values.
246     */
247    /*
248     * public void setDefaultValues(String[] defaultValues) { _defaultValues =
249     * defaultValues; }
250     */
251
252    /**
253     * Retrieves the metadata names below this composite metadata.
254     * 
255     * @return the metadata names.
256     * @deprecated Use getMetadataNames instead
257     */
258    @Deprecated
259    public Set<String> getMetadataComponentsName()
260    {
261        return getMetadataNames();
262    }
263
264    @Override
265    public Set<String> getMetadataNames()
266    {
267        return _children.keySet();
268    }
269
270    @Override
271    public MetadataDefinition getMetadataDefinition(String metadataName)
272    {
273        return _children.get(metadataName);
274    }
275
276    /**
277     * Add a metadata definition.
278     * 
279     * @param metadataDef the metadata definition.
280     * @return <code>true</code> if the metadata has been added,
281     *         <code>false</code> if this metadata already exists.
282     */
283    public boolean addMetadata(MetadataDefinition metadataDef)
284    {
285        if (getType() != MetadataType.COMPOSITE)
286        {
287            throw new UnsupportedOperationException("Only composite metadata can add metadata");
288        }
289
290        if (_children.containsKey(metadataDef.getName()))
291        {
292            return false;
293        }
294
295        _children.put(metadataDef.getName(), metadataDef);
296        return true;
297    }
298
299    /**
300     * Get the configuration of enumerator
301     * 
302     * @return the configuration of enumerator
303     */
304    public Configuration getEnumeratorConfiguration()
305    {
306        return _enumeratorConfiguration;
307    }
308
309    /**
310     * Get the configuration of validator
311     * 
312     * @return the configuration of validator
313     */
314    public Configuration getValidatorConfiguration()
315    {
316        return _validatorConfiguration;
317    }
318
319    /**
320     * Get the custom enumerator
321     * 
322     * @return the custom enumerator
323     */
324    public String getCustomEnumerator()
325    {
326        return _customEnumerator;
327    }
328
329    /**
330     * Get the custom validator
331     * 
332     * @return the custom validator
333     */
334    public String getCustomValidator()
335    {
336        return _customValidator;
337    }
338
339    /**
340     * Set the configuration of enumerator
341     * 
342     * @param configuration the configuration of enumerator
343     */
344    public void setEnumeratorConfiguration(Configuration configuration)
345    {
346        this._enumeratorConfiguration = configuration;
347    }
348
349    /**
350     * Set the configuration of validator
351     * 
352     * @param configuration the configuration of validator
353     */
354    public void setValidatorConfiguration(Configuration configuration)
355    {
356        this._validatorConfiguration = configuration;
357    }
358
359    /**
360     * Set the custom enumerator
361     * 
362     * @param customEnumerator the custom enumerator to set
363     */
364    public void setCustomEnumerator(String customEnumerator)
365    {
366        this._customEnumerator = customEnumerator;
367    }
368
369    /**
370     * Set the custom validator
371     * 
372     * @param customValidator the custom validator to set
373     */
374    public void setCustomValidator(String customValidator)
375    {
376        this._customValidator = customValidator;
377    }
378
379    @Override
380    public String toString()
381    {
382        StringBuilder metadataDef = new StringBuilder();
383
384        metadataDef.append(_name);
385        metadataDef.append(" (");
386        metadataDef.append(getType());
387        metadataDef.append(_isMultiple ? " MULTIPLE" : "");
388
389        if (getValidator() != null)
390        {
391            metadataDef.append(" ");
392            metadataDef.append(getValidator());
393        }
394
395        metadataDef.append(")");
396
397        return metadataDef.toString();
398    }
399
400    @Override
401    public boolean equals(Object obj)
402    {
403        if (obj instanceof MetadataDefinition)
404        {
405            MetadataDefinition metaDef = (MetadataDefinition) obj;
406            return metaDef.getReferenceContentType().equals(getReferenceContentType()) && metaDef.getId().equals(getId());
407        }
408
409        return false;
410    }
411
412    @Override
413    public int hashCode()
414    {
415        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
416        hashCodeBuilder.append(getReferenceContentType());
417        hashCodeBuilder.append(getId());
418
419        return hashCodeBuilder.toHashCode();
420    }
421}