001/*
002 *  Copyright 2016 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.search.solr.schema;
017
018import java.util.Map;
019
020/**
021 * Represents a field definition (simple field or dynamic field) in a solr schema.
022 */
023public class FieldDefinition implements SchemaDefinition
024{
025    
026    /** The field name, can contain a star character to represent a dynamic field (i.e. *_type). */
027    protected String _name;
028    
029    /** The field type. */
030    protected String _type;
031    
032    /** If the field is multi-valued. */
033    protected boolean _multiValued;
034    
035    /** If the field is stored as docValues. */
036    protected boolean _docValues;
037    
038    /** Whether the field is indexed. */
039    protected boolean _indexed;
040    
041    /** Whether the field is stored. */
042    protected boolean _stored;
043    
044    /**
045     * Build a field definition, indexed and stored.
046     * @param name The field name, can contain a star character to represent a dynamic field (i.e. *_type).
047     * @param type The field type.
048     * @param multiValued <code>true</code> if the field is multi-valued.
049     * @param docValues <code>true</code> if the field is stored as docValues.
050     */
051    public FieldDefinition(String name, String type, boolean multiValued, boolean docValues)
052    {
053        this(name, type, multiValued, docValues, true, true);
054    }
055    
056    /**
057     * Build a field definition.
058     * @param name The field name, can contain a star character to represent a dynamic field (i.e. *_type).
059     * @param type The field type.
060     * @param multiValued <code>true</code> if the field is multi-valued.
061     * @param docValues <code>true</code> if the field is stored as docValues.
062     * @param indexed <code>true</code> if the field is indexed.
063     * @param stored <code>true</code> if the field is stored.
064     */
065    public FieldDefinition(String name, String type, boolean multiValued, boolean docValues, boolean indexed, boolean stored)
066    {
067        this._name = name;
068        this._type = type;
069        this._multiValued = multiValued;
070        this._docValues = docValues;
071        this._indexed = indexed;
072        this._stored = stored;
073    }
074    
075    /**
076     * Build a field definition from a map of attributes.
077     * @param attributes the Map of attributes.
078     */
079    public FieldDefinition(Map<String, Object> attributes)
080    {
081        this._name = (String) attributes.get("name");
082        this._type = (String) attributes.get("type");
083        this._multiValued = getBoolean(attributes, "multiValued", false);
084        this._docValues = getBoolean(attributes, "docValues", false);
085        this._indexed = getBoolean(attributes, "indexed", false);
086        this._stored = getBoolean(attributes, "stored", false);
087    }
088    
089    private boolean getBoolean(Map<String, Object> attributes, String name, boolean defaultValue)
090    {
091        String value = (String) attributes.get(name);
092        return value != null ? Boolean.parseBoolean(value) : defaultValue;
093    }
094    
095    /**
096     * Get the field name.
097     * @return The field name.
098     */
099    public String getName()
100    {
101        return _name;
102    }
103    
104    /**
105     * Set the field name.
106     * @param name the field name.
107     */
108    public void setName(String name)
109    {
110        this._name = name;
111    }
112    
113    /**
114     * Get the field type.
115     * @return The field type.
116     */
117    public String getType()
118    {
119        return _type;
120    }
121    
122    /**
123     * Set the field type.
124     * @param type the field type.
125     */
126    public void setType(String type)
127    {
128        this._type = type;
129    }
130    
131    /**
132     * If the field is multi-valued.
133     * @return true if the field is multi-valued, false otherwise.
134     */
135    public boolean isMultiValued()
136    {
137        return _multiValued;
138    }
139    
140    /**
141     * Set if the field is multi-valued.
142     * @param multiValued <code>true</code> if the field is multi-valued.
143     */
144    public void setMultiValued(boolean multiValued)
145    {
146        this._multiValued = multiValued;
147    }
148    
149    /**
150     * True to store the field as docValues.
151     * @return true to store the field as docValues, false otherwise.
152     */
153    public boolean isDocValues()
154    {
155        return _docValues;
156    }
157    
158    /**
159     * Set if the field is stored as docValues.
160     * @param docValues <code>true</code> if the field is stored as docValues.
161     */
162    public void setDocValues(boolean docValues)
163    {
164        this._docValues = docValues;
165    }
166    
167    /**
168     * True to store the field as indexed.
169     * @return true to store the field as indexed, false otherwise.
170     */
171    public boolean isIndexed()
172    {
173        return _indexed;
174    }
175    
176    /**
177     * Set if the field is stored as indexed.
178     * @param indexed <code>true</code> if the field is stored as indexed.
179     */
180    public void setIndexed(boolean indexed)
181    {
182        this._indexed = indexed;
183    }
184    
185    /**
186     * True to store the field as stored.
187     * @return true to store the field as stored, false otherwise.
188     */
189    public boolean isStored()
190    {
191        return _stored;
192    }
193    
194    /**
195     * Set if the field is stored as stored.
196     * @param stored <code>true</code> if the field is stored as stored.
197     */
198    public void setStored(boolean stored)
199    {
200        this._stored = stored;
201    }
202    
203    /**
204     * Test if the field is dynamic.
205     * @return true if the field is dynamic.
206     */
207    public boolean isDynamic()
208    {
209        return _name.contains("*");
210    }
211    
212}