View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr.query.qom;
25  
26  import java.io.InputStream;
27  import java.math.BigDecimal;
28  import java.util.Calendar;
29  import javax.jcr.Binary;
30  import javax.jcr.PropertyType;
31  import javax.jcr.RepositoryException;
32  import javax.jcr.Value;
33  import javax.jcr.ValueFormatException;
34  import javax.jcr.query.qom.Literal;
35  import javax.jcr.query.qom.StaticOperand;
36  import org.modeshape.graph.query.model.FullTextSearch;
37  import org.modeshape.graph.query.model.SelectorName;
38  
39  /**
40   * Implementation of the full-text search constraint for the JCR Query Object Model and the Graph API.
41   */
42  public class JcrFullTextSearch extends FullTextSearch implements javax.jcr.query.qom.FullTextSearch, JcrConstraint {
43  
44      private static final long serialVersionUID = 1L;
45  
46      private final StaticOperand fullTextSearchOperand;
47  
48      protected static String toString( StaticOperand operand ) throws RepositoryException {
49          if (operand instanceof Literal) {
50              return ((Literal)operand).getLiteralValue().getString();
51          }
52          return operand.toString();
53      }
54  
55      /**
56       * Create a constraint defining a full-text search against the property values on node within the search scope.
57       * 
58       * @param selectorName the name of the node selector defining the search scope
59       * @param propertyName the name of the property to be searched; may be null if all property values are to be searched
60       * @param fullTextSearchExpression the search expression
61       * @throws RepositoryException if there is an error converting the full text search expression to a string
62       */
63      public JcrFullTextSearch( SelectorName selectorName,
64                                String propertyName,
65                                StaticOperand fullTextSearchExpression ) throws RepositoryException {
66          super(selectorName, propertyName, toString(fullTextSearchExpression));
67          this.fullTextSearchOperand = fullTextSearchExpression;
68      }
69  
70      /**
71       * Create a constraint defining a full-text search against the property values on node within the search scope.
72       * 
73       * @param selectorName the name of the node selector defining the search scope
74       * @param propertyName the name of the property to be searched; may be null if all property values are to be searched
75       * @param fullTextSearchExpression the search expression
76       * @param term the term, if known; may be null if the term representation is to be computed
77       */
78      public JcrFullTextSearch( SelectorName selectorName,
79                                String propertyName,
80                                final String fullTextSearchExpression,
81                                Term term ) {
82          super(selectorName, propertyName, fullTextSearchExpression, term);
83          this.fullTextSearchOperand = new JcrLiteral(new Value() {
84  
85              @Override
86              public int getType() {
87                  return PropertyType.STRING;
88              }
89  
90              @Override
91              public String getString() {
92                  return fullTextSearchExpression;
93              }
94  
95              @Override
96              public InputStream getStream() throws RepositoryException {
97                  throw new ValueFormatException();
98              }
99  
100             @Override
101             public long getLong() throws ValueFormatException, RepositoryException {
102                 throw new ValueFormatException();
103             }
104 
105             @Override
106             public double getDouble() throws ValueFormatException, RepositoryException {
107                 throw new ValueFormatException();
108             }
109 
110             @Override
111             public BigDecimal getDecimal() throws ValueFormatException, RepositoryException {
112                 throw new ValueFormatException();
113             }
114 
115             @Override
116             public Calendar getDate() throws ValueFormatException, RepositoryException {
117                 throw new ValueFormatException();
118             }
119 
120             @Override
121             public boolean getBoolean() throws ValueFormatException, RepositoryException {
122                 throw new ValueFormatException();
123             }
124 
125             @Override
126             public Binary getBinary() throws RepositoryException {
127                 throw new ValueFormatException();
128             }
129         }, fullTextSearchExpression);
130     }
131 
132     /**
133      * {@inheritDoc}
134      * 
135      * @see javax.jcr.query.qom.PropertyValue#getPropertyName()
136      */
137     @Override
138     public String getPropertyName() {
139         return propertyName();
140     }
141 
142     /**
143      * {@inheritDoc}
144      * 
145      * @see javax.jcr.query.qom.FullTextSearch#getFullTextSearchExpression()
146      */
147     @Override
148     public StaticOperand getFullTextSearchExpression() {
149         return fullTextSearchOperand;
150     }
151 
152     /**
153      * {@inheritDoc}
154      * 
155      * @see javax.jcr.query.qom.PropertyValue#getSelectorName()
156      */
157     @Override
158     public String getSelectorName() {
159         return selectorName().name();
160     }
161 }