KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > query > lib > Nest


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2005 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, S. Chassande-Barrioz, A. Lefebvre
22  */

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 JavaDoc;
39
40 /**
41  *
42  * @author Sebastien Chassande-Barrioz
43  */

44 public class Nest extends BasicQueryNode implements NestQueryNode {
45
46     /**
47      * It lists the fields which have been grouped.
48      */

49     protected PropagatedField[] myGroupByFields = null;
50     protected QueryTreeField[] theGroupByFields = null;
51     protected NestedField nestedField = null;
52
53     public Nest() {
54     }
55
56     /**
57      * Creates a QueryNode corresponding to a nest operation (group by).
58      * The Fields that are grouped and those that are used for the grouping
59      * must come from one single (child) QueryTree.
60      * @param groupedFields is the array of Fields which are nested by the
61      * operator. These are Fields of the child QueryTree.
62      * @param groupedFieldName is the name of the Field corresponding to the
63      * nesting.
64      * @param groupByFields is the array of Fields which are used for defining
65      * the group to be nested (equivalent to SQL GROUP BY). These are Fields of
66      * the child QueryTree. They are also called NestingFields.
67      */

68     public Nest(QueryTreeField[] groupedFields,
69                 String JavaDoc groupedFieldName,
70                 QueryTreeField[] groupByFields,
71                 String JavaDoc 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         //adding all groupBy fields as PropagatedFields
76

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 /*
82             QueryTreeField[] anc = {groupByFields[i]};
83             if (debug) logger.log(BasicLevel.DEBUG, "Adding GroupByField " + groupByFields[i] + " of node " + groupByFields[i].getQueryTree());
84             this.myGroupByFields[i] = this.addPropagatedField(
85                 groupByFields[i].getName(),
86                 groupByFields[i].getType(),
87                 anc);
88                 */

89             this.theGroupByFields[i] = groupByFields[i];
90         }
91         
92         //creating and adding the NestedField
93
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 JavaDoc clone(Object JavaDoc clone,
103                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
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     /**
117      * Creates and adds a NestedField to the TupleStructure of the
118      * QueryNode.
119      * @param name is the name of the Field to be added.
120      * @param grouped are the grouped Fields for the NestedField to be created.
121      * @return the newly created NestedField
122      * @throws MedorException when a Field with the same name already exists.
123      */

124     public NestedField replaceNestedField(String JavaDoc name,
125                                       Field[] grouped)
126         throws MedorException {
127         //removing the old one it it exists
128
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 JavaDoc("Nest query nodes cannot be assigned a filter.");
140     }
141
142     public QueryTreeField[] getNestingFields() {
143         return theGroupByFields;
144     }
145
146     public QueryTreeField getNestingField(String JavaDoc 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