|
| 1 | +package com.marklogic.client.impl; |
| 2 | + |
| 3 | +import com.marklogic.client.pojo.PojoQueryBuilder; |
| 4 | +import com.marklogic.client.query.StructuredQueryBuilder; |
| 5 | +import com.marklogic.client.query.StructuredQueryBuilder.TextIndex; |
| 6 | +import com.marklogic.client.query.StructuredQueryDefinition; |
| 7 | + |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.lang.reflect.Modifier; |
| 10 | +import java.util.Date; |
| 11 | +import java.util.HashMap; |
| 12 | + |
| 13 | +public class PojoQueryBuilderImpl<T> extends StructuredQueryBuilder implements PojoQueryBuilder<T> { |
| 14 | + private HashMap<String, Class> types = new HashMap<String, Class>(); |
| 15 | + private HashMap<String, String> rangeIndextypes = new HashMap<String, String>(); |
| 16 | + private Class<?> clazz; |
| 17 | + private String classWrapper; |
| 18 | + |
| 19 | + public PojoQueryBuilderImpl(Class<T> clazz) { |
| 20 | + super(); |
| 21 | + if ( clazz == null ) throw new IllegalArgumentException("clazz cannot be null"); |
| 22 | + this.clazz = clazz; |
| 23 | + this.classWrapper = clazz.getName(); |
| 24 | + } |
| 25 | + |
| 26 | + private StructuredQueryBuilder.PathIndex pojoFieldPath(String pojoField) { |
| 27 | + return pathIndex(classWrapper + "/" + pojoField); |
| 28 | + } |
| 29 | + |
| 30 | + public StructuredQueryDefinition containerQuery(String pojoField, StructuredQueryDefinition query) { |
| 31 | + return containerQuery(jsonProperty(pojoField), query); |
| 32 | + } |
| 33 | + @Override |
| 34 | + public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query) { |
| 35 | + return containerQuery(jsonProperty(classWrapper), query); |
| 36 | + } |
| 37 | + public PojoQueryBuilder containerQuery(String pojoField) { |
| 38 | + return new PojoQueryBuilderImpl(getType(pojoField)); |
| 39 | + } |
| 40 | + @Override |
| 41 | + public StructuredQueryBuilder.GeospatialIndex |
| 42 | + geoPair(String latitudeFieldName, String longitudeFieldName) |
| 43 | + { |
| 44 | + return geoElementPair(jsonProperty(classWrapper), jsonProperty(latitudeFieldName), jsonProperty(longitudeFieldName)); |
| 45 | + } |
| 46 | + public StructuredQueryBuilder.GeospatialIndex geoField(String pojoField) { |
| 47 | + return geoElement(jsonProperty(pojoField)); |
| 48 | + } |
| 49 | + public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoField) { |
| 50 | + return geoPath(pojoFieldPath(pojoField)); |
| 51 | + } |
| 52 | + public StructuredQueryDefinition range(String pojoField, |
| 53 | + StructuredQueryBuilder.Operator operator, Object... values) { |
| 54 | + return range(pojoFieldPath(pojoField), getRangeIndexType(pojoField), operator, values); |
| 55 | + } |
| 56 | + public StructuredQueryDefinition range(String pojoField, String[] options, |
| 57 | + StructuredQueryBuilder.Operator operator, Object... values) |
| 58 | + { |
| 59 | + return range(pojoFieldPath(pojoField), getRangeIndexType(pojoField), options, |
| 60 | + operator, values); |
| 61 | + } |
| 62 | + public StructuredQueryDefinition value(String pojoField, String... values) { |
| 63 | + return value(jsonProperty(pojoField), values); |
| 64 | + } |
| 65 | + public StructuredQueryDefinition value(String pojoField, String[] options, |
| 66 | + double weight, String... values) |
| 67 | + { |
| 68 | + return value(jsonProperty(pojoField), null, options, weight, values); |
| 69 | + } |
| 70 | + public StructuredQueryDefinition word(String pojoField, String... words) { |
| 71 | + return word(jsonProperty(pojoField), words); |
| 72 | + } |
| 73 | + public StructuredQueryDefinition word(String pojoField, String[] options, |
| 74 | + double weight, String... words) |
| 75 | + { |
| 76 | + return word(jsonProperty(pojoField), null, options, weight, words); |
| 77 | + } |
| 78 | + public StructuredQueryDefinition word(String... words) { |
| 79 | + return word(words); |
| 80 | + } |
| 81 | + public StructuredQueryDefinition word(String[] options, double weight, String... words) { |
| 82 | + return word(options, weight, words); |
| 83 | + } |
| 84 | + |
| 85 | + public String getRangeIndexType(String fieldName) { |
| 86 | + // map java types to acceptable Range Index types |
| 87 | + String type = rangeIndextypes.get(fieldName); |
| 88 | + if ( type == null ) { |
| 89 | + Class fieldClass = getType(fieldName); |
| 90 | + 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"; |
| 100 | + } else if ( Number.class.isAssignableFrom(fieldClass) ) { |
| 101 | + type = "decimal"; |
| 102 | + } else if ( Date.class.isAssignableFrom(fieldClass) ) { |
| 103 | + type = "dateTime"; |
| 104 | + } |
| 105 | + if ( type == null ) { |
| 106 | + throw new IllegalArgumentException("Field " + fieldName + " is not a native Java type"); |
| 107 | + } |
| 108 | + rangeIndextypes.put(fieldName, type); |
| 109 | + } |
| 110 | + return type; |
| 111 | + } |
| 112 | + |
| 113 | + public Class getType(String fieldName) { |
| 114 | + Class fieldClass = types.get(fieldName); |
| 115 | + if ( fieldClass == null ) { |
| 116 | + // figure out the type of the java field |
| 117 | + String initCapPojoField = fieldName.substring(1,2).toUpperCase() + |
| 118 | + fieldName.substring(2); |
| 119 | + try { |
| 120 | + fieldClass = clazz.getField(fieldName).getType(); |
| 121 | + } catch(NoSuchFieldException e) { |
| 122 | + Method getMethod = null; |
| 123 | + try { |
| 124 | + getMethod = clazz.getMethod("get" + initCapPojoField); |
| 125 | + } catch (NoSuchMethodException e2) { |
| 126 | + try { |
| 127 | + getMethod = clazz.getMethod("is" + initCapPojoField); |
| 128 | + if ( ! Boolean.class.isAssignableFrom(getMethod.getReturnType()) ) { |
| 129 | + getMethod = null; |
| 130 | + } |
| 131 | + } catch (NoSuchMethodException e3) {} |
| 132 | + } |
| 133 | + if ( getMethod != null ) { |
| 134 | + if ( Modifier.isStatic(getMethod.getModifiers()) ) { |
| 135 | + throw new IllegalArgumentException("get" + initCapPojoField + |
| 136 | + " cannot be static"); |
| 137 | + } |
| 138 | + fieldClass = getMethod.getReturnType(); |
| 139 | + if ( fieldClass == Void.TYPE ) { |
| 140 | + throw new IllegalArgumentException("get" + initCapPojoField + |
| 141 | + " must not have return type void"); |
| 142 | + } |
| 143 | + } else { |
| 144 | + String setMethodName = "set" + initCapPojoField; |
| 145 | + for ( Method method : clazz.getMethods() ) { |
| 146 | + if ( setMethodName.equals(method.getName()) ) { |
| 147 | + Class[] parameters = method.getParameterTypes(); |
| 148 | + if ( parameters != null && parameters.length == 1 ) { |
| 149 | + fieldClass = parameters[0]; |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + if ( fieldClass == null ) { |
| 157 | + throw new IllegalArgumentException("field " + fieldName + " not found, get" + initCapPojoField + |
| 158 | + " not found, and set" + initCapPojoField + " not found in class " + classWrapper); |
| 159 | + } |
| 160 | + types.put(fieldName, fieldClass); |
| 161 | + } |
| 162 | + return fieldClass; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | + |
0 commit comments