1 24 package org.objectweb.jalisto.se.query; 25 26 import org.objectweb.jalisto.se.api.ClassDescription; 27 import org.objectweb.jalisto.se.impl.LogicalOid; 28 import org.objectweb.jalisto.se.api.query.QueryManager; 29 import org.objectweb.jalisto.se.api.query.Query; 30 import org.objectweb.jalisto.se.api.query.Constraint; 31 import org.objectweb.jalisto.se.api.internal.SessionInternal; 32 import org.objectweb.jalisto.se.query.exception.QueryEngineException; 33 import org.objectweb.jalisto.se.query.constraint.*; 34 import org.objectweb.jalisto.se.query.execution.ExecutionTree; 35 import org.objectweb.jalisto.se.query.parameter.QueryParameter; 36 import org.objectweb.jalisto.se.query.parameter.QueryValue; 37 import org.objectweb.jalisto.se.query.parameter.SubQuery; 38 import org.objectweb.jalisto.se.query.result.QueryResultWrapper; 39 import org.objectweb.jalisto.se.query.result.ObjectSetImpl; 40 import org.objectweb.jalisto.se.api.query.ObjectSet; 41 42 import java.util.*; 43 44 public class QueryImpl implements Query { 45 46 public QueryImpl(QueryManager queryManager, Object classConstraint) { 47 this.queryManager = queryManager; 48 this.session = (SessionInternal) queryManager.getSession(); 49 this.constraints = new HashMap(); 50 this.parameters = new HashMap(); 51 this.subQueries = new ArrayList(); 52 this.subFieldNames = new ArrayList(); 53 if (queriedClass != null) { 54 try { 55 this.queriedClass = (Class ) classConstraint; 56 } catch (ClassCastException e) { 57 this.queriedClass = classConstraint.getClass(); 58 } 59 } 60 } 61 62 public Constraint constrain(Object constraintObject) { 63 if (queriedClass == null) { 64 try { 65 this.queriedClass = (Class ) constraintObject; 66 } catch (ClassCastException e) { 67 this.queriedClass = constraintObject.getClass(); 68 } 69 return null; 70 } 71 Constraint constraint; 72 String key = getSubFieldNamesAsKey(); 73 ArrayList constraintList = (ArrayList) constraints.get(key); 74 if (constraintList == null) { 75 constraintList = new ArrayList(); 76 constraints.put(key, constraintList); 77 } 78 if (constraintObject instanceof Query) { 79 constraintObject = new SubQuery((Query) constraintObject); 80 subQueries.add(constraintObject); 81 constraint = new SubQueryConstraintImpl((SubQuery) constraintObject, queriedClass.getName(), subFieldNames); 82 } else { 83 if (constraintObject instanceof QueryParameter) { 84 QueryParameter parameter = (QueryParameter) constraintObject; 85 parameters.put(parameter.getParameterName(), parameter); 86 } else if (!(constraintObject instanceof QueryValue)) { 87 constraintObject = new QueryValue(constraintObject); 88 } 89 constraint = new ValueConstraintImpl(queriedClass.getName(), subFieldNames, (QueryValue) constraintObject); 90 } 91 constraintList.add(constraint); 92 subFieldNames.clear(); 93 return constraint; 94 } 95 96 private String getSubFieldNamesAsKey() { 97 StringBuffer sb = new StringBuffer (); 98 for (int i = 0; i < subFieldNames.size(); i++) { 99 sb.append(subFieldNames.get(i)); 100 if (i != subFieldNames.size() - 1) { 101 sb.append("@"); 102 } 103 } 104 return sb.toString(); 105 } 106 107 public List constraints() { 108 throw new QueryEngineException(); 109 } 110 111 public Query descend(String fieldName) { 112 subFieldNames.add(fieldName); 113 return this; 114 } 115 116 public Query orderAscending() { 117 throw new QueryEngineException(); 118 } 119 120 public Query orderDescending() { 121 throw new QueryEngineException(); 122 } 123 124 public Constraint getBinaryTree() { 125 HashSet roots = new HashSet(); 126 Iterator constraintLists = constraints.values().iterator(); 127 while (constraintLists.hasNext()) { 128 ArrayList constraintList = (ArrayList) constraintLists.next(); 129 for (int i = 0; i < constraintList.size(); i++) { 130 ConstraintImpl constraint = (ConstraintImpl) constraintList.get(i); 131 roots.addAll(constraint.getRootFathers()); 132 } 133 } 134 if (roots.size() > 0) { 136 Iterator rootsIterator = roots.iterator(); 137 Constraint left = (Constraint) rootsIterator.next(); 138 while (rootsIterator.hasNext()) { 139 Constraint right = (Constraint) rootsIterator.next(); 140 left = left.or(right); 141 } 142 return left; 143 } 144 return null; 145 } 146 147 public ObjectSet execute() { 148 if (!session.currentTransaction().isActive()) { 149 throw new QueryEngineException("transaction is not active during execution of a query"); 150 } 151 152 checkBindedParameters(); 153 checkSubQueries(); 154 155 if (tree == null) { 156 String queriedClass = getQueriedClassName(); 157 tree = new ExecutionTree(getBinaryTree(), session.getMetaRepository(), queriedClass); 158 } 159 160 Set resultSet = tree.resolve(session, queryManager.getIndexManager()); 161 162 tree.cleanLeaf(); 163 cleanSubQueries(); 164 165 ObjectSet result = new ObjectSetImpl(resultSet); 166 167 return result; 168 } 169 170 public Set getExtent(ClassDescription meta) { 171 session.currentTransaction().begin(); 172 Iterator extent = session.getExtent(meta.getClassName()).readFully().iterator(); 173 Set resultSet = new HashSet(); 174 while (extent.hasNext()) { 175 LogicalOid floid = (LogicalOid) extent.next(); 176 Object [] values = session.readObjectByOid(floid, true); 177 resultSet.add(new QueryResultWrapper(floid, values)); 178 } 179 session.currentTransaction().commit(); 180 return resultSet; 181 } 182 183 private void checkBindedParameters() { 184 Iterator parametersIt = parameters.values().iterator(); 185 while (parametersIt.hasNext()) { 186 QueryParameter parameter = (QueryParameter) parametersIt.next(); 187 if (!parameter.isBind()) { 188 throw new QueryEngineException("Parameter " + parameter.getParameterName() + 189 " is not bind during query execution"); 190 } 191 } 192 } 193 194 private void checkSubQueries() { 195 for (short i = 0; i < subQueries.size(); i++) { 196 SubQuery subQuery = (SubQuery) subQueries.get(i); 197 subQuery.getTree(session.getMetaRepository()); 198 } 199 } 200 201 private void cleanSubQueries() { 202 for (short i = 0; i < subQueries.size(); i++) { 203 SubQuery subQuery = (SubQuery) subQueries.get(i); 204 subQuery.deleteTree(); 205 } 206 } 207 208 public void bind(String parameterName, Object value) { 209 QueryParameter parameter = (QueryParameter) parameters.get(parameterName); 210 if (parameter == null) { 211 throw new QueryEngineException("Try to bind value to non existing parameter : " + parameterName); 212 } 213 parameter.bind(value); 214 } 215 216 public HashMap getParameters() { 217 return parameters; 218 } 219 220 public ArrayList getSubQueries() { 221 return subQueries; 222 } 223 224 public String getQueriedClassName() { 225 return queriedClass.getName(); 226 } 227 228 private SessionInternal session; 229 private QueryManager queryManager; 230 private Class queriedClass; 231 private HashMap constraints; 232 private HashMap parameters; 233 private ArrayList subQueries; 234 private ExecutionTree tree; 235 private ArrayList subFieldNames; 236 } 237 | Popular Tags |