1 10 package org.mmbase.bridge.implementation; 11 12 import java.util.*; 13 14 import org.mmbase.bridge.*; 15 import org.mmbase.storage.search.*; 16 import org.mmbase.storage.search.implementation.*; 17 import org.mmbase.util.logging.*; 18 19 38 public class BasicNodeQuery extends BasicQuery implements NodeQuery { 39 40 private static final Logger log = Logging.getLoggerInstance(BasicNodeQuery.class); 41 protected Step step = null; 42 43 BasicNodeQuery(Cloud c) { 44 super(c); 45 } 46 47 50 BasicNodeQuery(BasicNodeManager nodeManager) { 51 super(nodeManager.cloud); 52 query = new NodeSearchQuery(nodeManager.getMMObjectBuilder()); 53 this.step = (Step) getSteps().get(0); } 55 BasicNodeQuery(BasicNodeManager nodeManager, NodeSearchQuery q) { 56 super(nodeManager.cloud); 57 query = q; 58 this.step = (Step) getSteps().get(0); } 60 61 65 BasicNodeQuery(Cloud cloud, SearchQuery q) { 66 super(cloud); 67 query = new BasicSearchQuery(q); 68 List steps = query.getSteps(); 69 if (steps.size() > 0) { 70 setNodeStep((Step) (steps.get(steps.size() -1 ))); 71 } 72 } 73 74 public NodeManager getNodeManager() { 75 if (step == null) return null; 76 if (step instanceof RelationStep) { 77 RelationStep rs = (RelationStep) step; 78 int role = -1; 79 Integer roleValue = rs.getRole(); 80 if (roleValue != null) { 81 role = roleValue.intValue(); 82 } 83 String roleName = role > 0 ? cloud.getNode(role).getStringValue("sname") : null; 84 NodeManager previous = cloud.getNodeManager(rs.getPrevious().getTableName()); 85 NodeManager next = cloud.getNodeManager(rs.getNext().getTableName()); 86 if (cloud.hasRelationManager(previous, next, roleName)) { 87 return cloud.getRelationManager(previous, next, roleName); 88 } else { 89 if (roleName == null) { 90 return cloud.getNodeManager("insrel"); } else { 92 return cloud.getRelationManager(roleName); 93 } 94 } 95 } else { 96 return cloud.getNodeManager(step.getTableName()); 97 } 98 } 99 100 public Step getNodeStep() { 101 return step; 102 } 103 104 public StepField createStepField(String fieldName) { 106 if (fieldName.indexOf('.') == -1) { 107 BasicStepField stepField = (BasicStepField) getStepField(getNodeManager().getField(fieldName)); 108 if (stepField == null) throw new NotFoundException("No field '" + fieldName + "' found in " + getSteps()); 110 return stepField; 111 } else { 112 return super.createStepField(fieldName); 114 } 115 } 116 117 public StepField getStepField(Field field) { 118 if (query instanceof NodeSearchQuery) { 119 BasicStepField stepField = ((NodeSearchQuery) query).getField(((BasicField)field).coreField); 120 return stepField; 121 } else { 122 Iterator fields = query.getFields().iterator(); 123 while(fields.hasNext()) { 124 StepField stepField = (StepField) fields.next(); 125 if (stepField.getStep().equals(step)) { 126 if (stepField.getFieldName().equals(field.getName())) { 127 return stepField; 128 } 129 } 130 131 } 132 } 133 return null; } 136 137 138 public void removeFields() { 139 explicitFields.clear(); 140 setNodeStep(step); 141 } 142 143 144 public List getExtraFields() { 145 return Collections.unmodifiableList(explicitFields); 146 } 147 148 149 152 protected void addFields(Collection c) { 153 Iterator i = c.iterator(); 154 while (i.hasNext()) { 155 BasicStepField sf = (BasicStepField) i.next(); 156 Step addedStep = sf.getStep(); 157 if (addedStep.equals(step)) continue; query.addField(addedStep, sf.getField()); 159 } 160 } 161 162 public Query setDistinct(boolean distinct) { 164 if (used) throw new BridgeException("Query was used already"); 165 query.setDistinct(distinct); 166 if (distinct) { query.removeFields(); 168 query.addFields(step); 169 implicitFields.clear(); 170 addFields(explicitFields); 171 } 172 return this; 173 } 174 175 public Step setNodeStep(Step step) { 176 assert query.getSteps().contains(step); 177 if (this.step == null && step == null) return null; 178 if (this.step != null && this.step.equals(step)) return this.step; query.removeFields(); 182 query.addFields(step); 183 addFields(explicitFields); 184 Step prevStep = this.step; 185 this.step = step; 186 if (! isDistinct() ) { 187 addFields(implicitFields); 188 } 189 return prevStep; 190 } 191 192 public Query cloneWithoutFields() { 193 BasicSearchQuery bsq = new BasicSearchQuery(query, BasicSearchQuery.COPY_WITHOUTFIELDS); 194 if (queryCheck != null) { 195 removeSecurityConstraintFromClone(bsq); 196 } 197 BasicNodeQuery clone = new BasicNodeQuery(cloud, bsq); 198 clone.used = false; 199 clone.aggregating = false; 200 return clone; 201 } 202 203 public NodeList getList() { 204 return getNodeManager().getList(this); 205 } 206 207 208 } 209 | Popular Tags |