1 24 package org.objectweb.jalisto.se.query.constraint; 25 26 import org.objectweb.jalisto.se.api.ClassDescription; 27 import org.objectweb.jalisto.se.api.Session; 28 import org.objectweb.jalisto.se.api.query.FielComparator; 29 import org.objectweb.jalisto.se.api.query.Constraint; 30 import org.objectweb.jalisto.se.api.query.Operator; 31 import org.objectweb.jalisto.se.query.operator.OperatorFactory; 32 import org.objectweb.jalisto.se.query.parameter.QueryValue; 33 import org.objectweb.jalisto.se.query.result.WrapperSet; 34 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 38 public class ValueConstraintImpl extends ConstraintImpl { 39 40 public ValueConstraintImpl(String className, ArrayList subFieldNames, QueryValue value) { 41 this.className = className; 42 this.fieldNames = new String [subFieldNames.size()]; 43 for (int i = 0; i < fieldNames.length; i++) { 44 fieldNames[i] = (String ) subFieldNames.get(i); 45 } 46 this.value = value; 47 this.resolvedValues = new WrapperSet(); 48 this.isResolved = false; 49 } 50 51 public Constraint contains() { 52 operator = OperatorFactory.getContainsOperator(); 53 return this; 54 } 55 56 public Constraint equal() { 57 operator = OperatorFactory.getEqualOperator(); 58 return this; 59 } 60 61 public Object getObject() { 62 return value.getValue(); 63 } 64 65 public Constraint greater() { 66 operator = OperatorFactory.getGreaterOperator(); 67 return this; 68 } 69 70 public Constraint identity() { 71 operator = OperatorFactory.getIdentityOperator(); 72 return this; 73 } 74 75 public Constraint like() { 76 throw new UnsupportedOperationException (); 77 } 78 79 public Constraint not() { 80 not = !not; 81 return this; 82 } 83 84 public Constraint smaller() { 85 operator = OperatorFactory.getSmallerOperator(); 86 return this; 87 } 88 89 public boolean execute(Session session, FielComparator comparator, Object candidate) { 90 ArrayList candidates = new ArrayList (); 91 candidates.add(candidate); 92 if (fieldNames.length > 1) { 93 candidates.clear(); 94 if (Collection .class.isAssignableFrom(candidate.getClass())) { 95 candidates.addAll((Collection ) candidate); 96 } else { 97 candidates.add(candidate); 98 } 99 for (short i = 1; i < fieldNames.length; i++) { 100 String currentClassName = session.getClassNameFor(candidates.get(0)); 102 ClassDescription meta = session.getMetaRepository().getMetaData(currentClassName); 103 int index = meta.getIndex(fieldNames[i]); 104 105 ArrayList newCandidates = new ArrayList (); 106 for (int j = 0; j < candidates.size(); j++) { 107 newCandidates.add(session.readObjectByOid(candidates.get(j))[index]); 108 } 109 candidates.clear(); 110 if (Collection .class.isAssignableFrom(newCandidates.get(0).getClass())) { 111 for (int j = 0; j < newCandidates.size(); j++) { 112 candidates.addAll((Collection ) newCandidates.get(i)); 113 } 114 } else { 115 candidates.addAll(newCandidates); 116 } 117 } 118 } 119 if (not) { 120 return (!operator.execute(comparator, candidates, value.getValue())); 121 } else { 122 return operator.execute(comparator, candidates, value.getValue()); 123 } 124 } 125 126 public String getFieldName() { 127 return fieldNames[0]; 128 } 129 130 public String [] getFieldNames() { 131 return fieldNames; 132 } 133 134 public String getClassName() { 135 return className; 136 } 137 138 public boolean isResolved() { 139 return isResolved; 140 } 141 142 public void setResolved(boolean resolved) { 143 isResolved = resolved; 144 } 145 146 public WrapperSet getResolvedValues() { 147 return resolvedValues; 148 } 149 150 public void clean() { 151 resolvedValues.clear(); 152 isResolved = false; 153 } 154 155 public String toString() { 156 StringBuffer sb = new StringBuffer (); 157 sb.append("VCon").append(fieldNames[0]).append(operator).append(value.getValue()); 158 return sb.toString(); 159 } 160 161 162 private String className; 163 private String [] fieldNames; 164 private QueryValue value; 165 private Operator operator; 166 private boolean not = false; 167 168 protected WrapperSet resolvedValues; 169 protected boolean isResolved; 170 } 171 | Popular Tags |