001package io.ebeanservice.docstore.api.mapping;
002
003import io.ebean.annotation.DocMapping;
004import io.ebean.annotation.DocProperty;
005
006/**
007 * Options for mapping a property for document storage.
008 */
009public final class DocPropertyOptions {
010
011  private Boolean code;
012  private Boolean sortable;
013  private Boolean store;
014  private Float boost;
015  private String nullValue;
016  private Boolean includeInAll;
017  private Boolean enabled;
018  private Boolean norms;
019  private Boolean docValues;
020  private String analyzer;
021  private String searchAnalyzer;
022  private String copyTo;
023  private DocProperty.Option options;
024
025  /**
026   * Construct with no values set.
027   */
028  public DocPropertyOptions() {
029  }
030
031  /**
032   * Construct as a copy of the source options.
033   */
034  DocPropertyOptions(DocPropertyOptions source) {
035    this.code = source.code;
036    this.sortable = source.sortable;
037    this.store = source.store;
038    this.boost = source.boost;
039    this.nullValue = source.nullValue;
040    this.includeInAll = source.includeInAll;
041    this.analyzer = source.analyzer;
042    this.searchAnalyzer = source.searchAnalyzer;
043    this.options = source.options;
044    this.docValues = source.docValues;
045    this.norms = source.norms;
046    this.copyTo = source.copyTo;
047    this.enabled = source.enabled;
048  }
049
050  @Override
051  public String toString() {
052    StringBuilder sb = new StringBuilder();
053    if (code != null) {
054      sb.append("code:").append(code).append(" ");
055    }
056    if (sortable != null) {
057      sb.append("sortable:").append(sortable).append(" ");
058    }
059    if (store != null) {
060      sb.append("store:").append(store).append(" ");
061    }
062    if (boost != null) {
063      sb.append("boost:").append(boost).append(" ");
064    }
065    if (nullValue != null) {
066      sb.append("nullValue:").append(nullValue).append(" ");
067    }
068    return sb.toString();
069  }
070
071  public boolean isCode() {
072    return Boolean.TRUE.equals(code);
073  }
074
075  public Boolean code() {
076    return code;
077  }
078
079  public void code(Boolean code) {
080    this.code = code;
081  }
082
083  public boolean isSortable() {
084    return Boolean.TRUE.equals(sortable);
085  }
086
087  public Boolean sortable() {
088    return sortable;
089  }
090
091  public void sortable(Boolean sortable) {
092    this.sortable = sortable;
093  }
094
095  public Float boost() {
096    return boost;
097  }
098
099  public void boost(Float boost) {
100    this.boost = boost;
101  }
102
103  public String nullValue() {
104    return nullValue;
105  }
106
107  public void nullValue(String nullValue) {
108    this.nullValue = nullValue;
109  }
110
111  public Boolean store() {
112    return store;
113  }
114
115  public void store(Boolean store) {
116    this.store = store;
117  }
118
119  public Boolean includeInAll() {
120    return includeInAll;
121  }
122
123  public void includeInAll(Boolean includeInAll) {
124    this.includeInAll = includeInAll;
125  }
126
127  public Boolean docValues() {
128    return docValues;
129  }
130
131  public void docValues(Boolean docValues) {
132    this.docValues = docValues;
133  }
134
135  public String analyzer() {
136    return analyzer;
137  }
138
139  public void analyzer(String analyzer) {
140    this.analyzer = analyzer;
141  }
142
143  public String searchAnalyzer() {
144    return searchAnalyzer;
145  }
146
147  public void searchAnalyzer(String searchAnalyzer) {
148    this.searchAnalyzer = searchAnalyzer;
149  }
150
151  public String copyTo() {
152    return copyTo;
153  }
154
155  public void copyTo(String copyTo) {
156    this.copyTo = copyTo;
157  }
158
159  public Boolean enabled() {
160    return enabled;
161  }
162
163  public void enabled(Boolean enabled) {
164    this.enabled = enabled;
165  }
166
167  public Boolean norms() {
168    return norms;
169  }
170
171  public void norms(Boolean norms) {
172    this.norms = norms;
173  }
174
175  /**
176   * Return true if the index options is set to a non-default value.
177   */
178  public boolean isOptionsSet() {
179    return options != null && options != DocProperty.Option.DEFAULT;
180  }
181
182  public DocProperty.Option options() {
183    return options;
184  }
185
186  public void options(DocProperty.Option options) {
187    this.options = options;
188  }
189
190  /**
191   * Create a copy of this such that it can be overridden on a per index basis.
192   */
193  public DocPropertyOptions copy() {
194    return new DocPropertyOptions(this);
195  }
196
197  /**
198   * Apply override mapping from the document level or embedded property level.
199   */
200  public void apply(DocMapping docMapping) {
201    apply(docMapping.options());
202  }
203
204  /**
205   * Apply the property level mapping options.
206   */
207  public void apply(DocProperty docMapping) {
208    options = docMapping.options();
209    if (docMapping.code()) {
210      code = true;
211    }
212    if (docMapping.sortable()) {
213      sortable = true;
214    }
215    if (docMapping.store()) {
216      store = true;
217    }
218    if (Float.compare(docMapping.boost(), 1.0F) != 0) {
219      boost = docMapping.boost();
220    }
221    if (!"".equals(docMapping.nullValue())) {
222      nullValue = docMapping.nullValue();
223    }
224    if (!docMapping.includeInAll()) {
225      includeInAll = false;
226    }
227    if (!docMapping.docValues()) {
228      docValues = false;
229    }
230    if (!docMapping.enabled()) {
231      enabled = false;
232    }
233    if (!docMapping.norms()) {
234      norms = false;
235    }
236    if (!"".equals(docMapping.analyzer())) {
237      analyzer = docMapping.analyzer();
238    }
239    if (!"".equals(docMapping.searchAnalyzer())) {
240      searchAnalyzer = docMapping.searchAnalyzer();
241    }
242    if (!"".equals(docMapping.copyTo())) {
243      copyTo = docMapping.copyTo();
244    }
245  }
246
247}