~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/schema/UUIDField.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.solr.schema;
2
 
 
3
 
/**
4
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
5
 
 * contributor license agreements.  See the NOTICE file distributed with
6
 
 * this work for additional information regarding copyright ownership.
7
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
8
 
 * (the "License"); you may not use this file except in compliance with
9
 
 * the License.  You may obtain a copy of the License at
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
13
 
 * Unless required by applicable law or agreed to in writing, software
14
 
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 * See the License for the specific language governing permissions and
17
 
 * limitations under the License.
18
 
 */
19
 
 
20
 
import java.io.IOException;
21
 
import java.util.Locale;
22
 
import java.util.Map;
23
 
import java.util.UUID;
24
 
 
25
 
import org.apache.lucene.document.Fieldable;
26
 
import org.apache.lucene.search.SortField;
27
 
import org.apache.solr.common.SolrException;
28
 
import org.apache.solr.response.TextResponseWriter;
29
 
import org.apache.solr.response.XMLWriter;
30
 
 
31
 
/**
32
 
 * This FieldType accepts UUID string values, as well as the special value 
33
 
 * of "NEW" which triggers generation of a new random UUID.
34
 
 *
35
 
 * @see UUID#toString
36
 
 * @see UUID#randomUUID
37
 
 * @version $Id: UUIDField.java 945270 2010-05-17 17:45:18Z rmuir $
38
 
 */
39
 
public class UUIDField extends FieldType {
40
 
  private static final String NEW = "NEW";
41
 
  private static final char DASH='-';
42
 
 
43
 
  @Override
44
 
  protected void init(IndexSchema schema, Map<String, String> args) {
45
 
    super.init(schema, args);
46
 
 
47
 
    // Tokenizing makes no sense
48
 
    restrictProps(TOKENIZED);
49
 
  }
50
 
 
51
 
  @Override
52
 
  public SortField getSortField(SchemaField field, boolean reverse) {
53
 
    return getStringSort(field, reverse);
54
 
  }
55
 
 
56
 
  @Override
57
 
  public void write(XMLWriter xmlWriter, String name, Fieldable f)
58
 
      throws IOException {
59
 
    xmlWriter.writeStr(name, f.stringValue());
60
 
  }
61
 
 
62
 
  @Override
63
 
  public void write(TextResponseWriter writer, String name, Fieldable f)
64
 
      throws IOException {
65
 
    writer.writeStr(name, f.stringValue(), false);
66
 
  }
67
 
 
68
 
  /**
69
 
   * Generates a UUID if val is either null, empty or "NEW".
70
 
   * 
71
 
   * Otherwise it behaves much like a StrField but checks that the value given
72
 
   * is indeed a valid UUID.
73
 
   * 
74
 
   * @param val The value of the field
75
 
   * @see org.apache.solr.schema.FieldType#toInternal(java.lang.String)
76
 
   */
77
 
  @Override
78
 
  public String toInternal(String val) {
79
 
    if (val == null || 0==val.length() || NEW.equals(val)) {
80
 
      return UUID.randomUUID().toString().toLowerCase(Locale.ENGLISH);
81
 
    } else {
82
 
      // we do some basic validation if 'val' looks like an UUID
83
 
      if (val.length() != 36 || val.charAt(8) != DASH || val.charAt(13) != DASH
84
 
          || val.charAt(18) != DASH || val.charAt(23) != DASH) {
85
 
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
86
 
            "Invalid UUID String: '" + val + "'");
87
 
      }
88
 
 
89
 
      return val.toLowerCase(Locale.ENGLISH);
90
 
    }
91
 
  }
92
 
 
93
 
  public String toInternal(UUID uuid) {
94
 
    return uuid.toString().toLowerCase(Locale.ENGLISH);
95
 
  }
96
 
 
97
 
  @Override
98
 
  public UUID toObject(Fieldable f) {
99
 
    return UUID.fromString(f.stringValue());
100
 
  }
101
 
}