1 23 24 package org.objectweb.medor.query.lib; 25 26 import org.objectweb.medor.api.Field; 27 import org.objectweb.medor.api.MedorException; 28 import org.objectweb.medor.api.QueryNodeException; 29 import org.objectweb.medor.expression.api.Expression; 30 import org.objectweb.medor.query.api.NestQueryNode; 31 import org.objectweb.medor.query.api.NestedField; 32 import org.objectweb.medor.query.api.OperationType; 33 import org.objectweb.medor.query.api.PropagatedField; 34 import org.objectweb.medor.query.api.QueryTree; 35 import org.objectweb.medor.query.api.QueryTreeField; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 38 import java.util.Map ; 39 40 44 public class Nest extends BasicQueryNode implements NestQueryNode { 45 46 49 protected PropagatedField[] myGroupByFields = null; 50 protected QueryTreeField[] theGroupByFields = null; 51 protected NestedField nestedField = null; 52 53 public Nest() { 54 } 55 56 68 public Nest(QueryTreeField[] groupedFields, 69 String groupedFieldName, 70 QueryTreeField[] groupByFields, 71 String nodeName) throws MedorException{ 72 super(nodeName); 73 boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 74 if (debug) logger.log(BasicLevel.DEBUG, "Nest. Super() done. Entering core."); 75 77 this.myGroupByFields = new PropagatedField[groupByFields.length]; 78 this.theGroupByFields = new QueryTreeField[groupByFields.length]; 79 80 for (int i = 0; i < groupByFields.length; i++) { 81 89 this.theGroupByFields[i] = groupByFields[i]; 90 } 91 92 this.replaceNestedField(groupedFieldName, groupedFields); 94 if (debug) logger.log(BasicLevel.DEBUG, "Checking number of children for " + this); 95 QueryTree[] theChildren = this.getChildren(); 96 if (debug) logger.log(BasicLevel.DEBUG, "Found " + theChildren.length + " chid(ren) for Nest node"); 97 if (theChildren.length > 1) { 98 throw new QueryNodeException("Too many children for this Nest node"); 99 } 100 } 101 102 public Object clone(Object clone, 103 Map obj2clone) throws CloneNotSupportedException { 104 clone = super.clone(clone, obj2clone); 105 Nest n = (Nest) clone; 106 n.nestedField = (NestedField) getClone(nestedField, obj2clone); 107 if (myGroupByFields != null) { 108 n.myGroupByFields = new PropagatedField[myGroupByFields.length]; 109 for(int i=0; i<myGroupByFields.length; i++) { 110 n.myGroupByFields[i] = (PropagatedField) getClone(myGroupByFields[i], obj2clone); 111 } 112 } 113 return clone; 114 } 115 116 124 public NestedField replaceNestedField(String name, 125 Field[] grouped) 126 throws MedorException { 127 if (nestedField != null) { 129 this.removeField(nestedField.getName()); 130 } 131 NestedField newField = new BasicNestedField(name, this, grouped); 132 this.addField(newField); 133 nestedField = newField; 134 updateChildren(); 135 return newField; 136 } 137 138 public void setQueryFilter(Expression e) { 139 throw new UnsupportedOperationException ("Nest query nodes cannot be assigned a filter."); 140 } 141 142 public QueryTreeField[] getNestingFields() { 143 return theGroupByFields; 144 } 145 146 public QueryTreeField getNestingField(String fieldName) { 147 for (int i = 0; i < theGroupByFields.length; i++) { 148 if (theGroupByFields[i].getName().equals(fieldName)) { 149 return theGroupByFields[i]; 150 } 151 } 152 return null; 153 } 154 155 public NestedField getNestedField() { 156 return nestedField; 157 } 158 159 public QueryTreeField replaceNestingField(QueryTreeField old, QueryTreeField neo) { 160 boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 161 for (int i = 0; i < theGroupByFields.length; i++) { 162 if (theGroupByFields[i].equals(old)) { 163 theGroupByFields[i] = neo; 164 return neo; 165 } 166 } 167 logger.log(BasicLevel.DEBUG, "Field not found " + old); 168 return null; 169 } 170 171 public short getType() { 172 return OperationType.NEST; 173 } 174 } 175 | Popular Tags |