1+ /*
2+ * Copyright 2012-2014 MarkLogic Corporation
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
116package com .marklogic .client .impl ;
217
318import com .marklogic .client .pojo .PojoQueryBuilder ;
@@ -15,6 +30,7 @@ public class PojoQueryBuilderImpl<T> extends StructuredQueryBuilder implements P
1530 private HashMap <String , String > rangeIndextypes = new HashMap <String , String >();
1631 private Class <?> clazz ;
1732 private String classWrapper ;
33+ private boolean wrapQueries = false ;
1834
1935 public PojoQueryBuilderImpl (Class <T > clazz ) {
2036 super ();
@@ -23,19 +39,30 @@ public PojoQueryBuilderImpl(Class<T> clazz) {
2339 this .classWrapper = clazz .getName ();
2440 }
2541
42+ public PojoQueryBuilderImpl (Class <T > clazz , boolean wrapQueries ) {
43+ this (clazz );
44+ this .wrapQueries = wrapQueries ;
45+ }
46+
2647 private StructuredQueryBuilder .PathIndex pojoFieldPath (String pojoField ) {
48+ //return pathIndex("*[local-name()=\"" + classWrapper + "\"]/" + pojoField);
2749 return pathIndex (classWrapper + "/" + pojoField );
2850 }
2951
3052 public StructuredQueryDefinition containerQuery (String pojoField , StructuredQueryDefinition query ) {
31- return containerQuery (jsonProperty (pojoField ), query );
53+ if ( wrapQueries ) {
54+ return super .containerQuery (jsonProperty (classWrapper ),
55+ super .containerQuery (jsonProperty (pojoField ), query ));
56+ } else {
57+ return super .containerQuery (jsonProperty (pojoField ), query );
58+ }
3259 }
3360 @ Override
3461 public StructuredQueryDefinition containerQuery (StructuredQueryDefinition query ) {
35- return containerQuery (jsonProperty (classWrapper ), query );
62+ return super . containerQuery (jsonProperty (classWrapper ), query );
3663 }
3764 public PojoQueryBuilder containerQuery (String pojoField ) {
38- return new PojoQueryBuilderImpl (getType (pojoField ));
65+ return new PojoQueryBuilderImpl (getType (pojoField ), true );
3966 }
4067 @ Override
4168 public StructuredQueryBuilder .GeospatialIndex
@@ -50,7 +77,8 @@ public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoField) {
5077 return geoPath (pojoFieldPath (pojoField ));
5178 }
5279 public StructuredQueryDefinition range (String pojoField ,
53- StructuredQueryBuilder .Operator operator , Object ... values ) {
80+ StructuredQueryBuilder .Operator operator , Object ... values )
81+ {
5482 return range (pojoFieldPath (pojoField ), getRangeIndexType (pojoField ), operator , values );
5583 }
5684 public StructuredQueryDefinition range (String pojoField , String [] options ,
@@ -60,20 +88,40 @@ public StructuredQueryDefinition range(String pojoField, String[] options,
6088 operator , values );
6189 }
6290 public StructuredQueryDefinition value (String pojoField , String ... values ) {
63- return value (jsonProperty (pojoField ), values );
91+ if ( wrapQueries ) {
92+ return super .containerQuery (jsonProperty (classWrapper ),
93+ value (jsonProperty (pojoField ), values ));
94+ } else {
95+ return value (jsonProperty (pojoField ), values );
96+ }
6497 }
6598 public StructuredQueryDefinition value (String pojoField , String [] options ,
6699 double weight , String ... values )
67100 {
68- return value (jsonProperty (pojoField ), null , options , weight , values );
101+ if ( wrapQueries ) {
102+ return super .containerQuery (jsonProperty (classWrapper ),
103+ value (jsonProperty (pojoField ), null , options , weight , values ));
104+ } else {
105+ return value (jsonProperty (pojoField ), null , options , weight , values );
106+ }
69107 }
70108 public StructuredQueryDefinition word (String pojoField , String [] words ) {
71- return super .word (jsonProperty (pojoField ), words );
109+ if ( wrapQueries ) {
110+ return super .containerQuery (jsonProperty (classWrapper ),
111+ super .word (jsonProperty (pojoField ), words ));
112+ } else {
113+ return super .word (jsonProperty (pojoField ), words );
114+ }
72115 }
73116 public StructuredQueryDefinition word (String pojoField , String [] options ,
74117 double weight , String ... words )
75118 {
76- return super .word (jsonProperty (pojoField ), null , options , weight , words );
119+ if ( wrapQueries ) {
120+ return super .containerQuery (jsonProperty (classWrapper ),
121+ super .word (jsonProperty (pojoField ), null , options , weight , words ));
122+ } else {
123+ return super .word (jsonProperty (pojoField ), null , options , weight , words );
124+ }
77125 }
78126 public StructuredQueryDefinition word (String ... words ) {
79127 return super .word (jsonProperty (classWrapper ), words );
@@ -88,19 +136,19 @@ public String getRangeIndexType(String fieldName) {
88136 if ( type == null ) {
89137 Class fieldClass = getType (fieldName );
90138 if ( String .class .isAssignableFrom (fieldClass ) ) {
91- type = "string" ;
92- } else if ( Integer .class . isAssignableFrom (fieldClass ) ) {
93- type = "int" ;
94- } else if ( Long .class . isAssignableFrom (fieldClass ) ) {
95- type = "long" ;
96- } else if ( Float .class . isAssignableFrom (fieldClass ) ) {
97- type = "float" ;
98- } else if ( Double .class . isAssignableFrom (fieldClass ) ) {
99- type = "double" ;
139+ type = "xs: string" ;
140+ } else if ( Integer .TYPE . equals (fieldClass ) ) {
141+ type = "xs: int" ;
142+ } else if ( Long .TYPE . equals (fieldClass ) ) {
143+ type = "xs: long" ;
144+ } else if ( Float .TYPE . equals (fieldClass ) ) {
145+ type = "xs: float" ;
146+ } else if ( Double .TYPE . equals (fieldClass ) ) {
147+ type = "xs: double" ;
100148 } else if ( Number .class .isAssignableFrom (fieldClass ) ) {
101- type = "decimal" ;
149+ type = "xs: decimal" ;
102150 } else if ( Date .class .isAssignableFrom (fieldClass ) ) {
103- type = "dateTime" ;
151+ type = "xs: dateTime" ;
104152 }
105153 if ( type == null ) {
106154 throw new IllegalArgumentException ("Field " + fieldName + " is not a native Java type" );
@@ -114,8 +162,8 @@ public Class getType(String fieldName) {
114162 Class fieldClass = types .get (fieldName );
115163 if ( fieldClass == null ) {
116164 // figure out the type of the java field
117- String initCapPojoField = fieldName .substring (1 , 2 ).toUpperCase () +
118- fieldName .substring (2 );
165+ String initCapPojoField = fieldName .substring (0 , 1 ).toUpperCase () +
166+ fieldName .substring (1 );
119167 try {
120168 fieldClass = clazz .getField (fieldName ).getType ();
121169 } catch (NoSuchFieldException e ) {
@@ -162,5 +210,3 @@ public Class getType(String fieldName) {
162210 return fieldClass ;
163211 }
164212}
165-
166-
0 commit comments