KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > metainfo > RdbFilter


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2004 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm@objectweb.org
21  *
22  */

23 package org.objectweb.jorm.mapper.rdb.metainfo;
24
25 import org.objectweb.jorm.api.PException;
26 import org.objectweb.jorm.metainfo.api.MetaObject;
27 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
28 import org.objectweb.jorm.metainfo.lib.BasicMetaObject;
29 import org.objectweb.medor.expression.api.Expression;
30 import org.objectweb.medor.expression.api.Operand;
31 import org.objectweb.medor.expression.api.Operator;
32 import org.objectweb.medor.expression.lib.BasicOperand;
33 import org.objectweb.medor.expression.lib.BasicParameterOperand;
34 import org.objectweb.medor.expression.lib.Equal;
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 import java.util.HashMap JavaDoc;
38
39 public class RdbFilter extends BasicMetaObject {
40
41     /**
42      * This map associates a list of Predicate objects to a filter column.
43      * key: the name of the filter column
44      * value: a list of Predicate objects
45      */

46     //HashMap filtercol2predicates = null;
47

48     /**
49      * This list stores the names of PrimitiveElements which take part in the
50      * filter.
51      */

52     HashMap JavaDoc filteringElements = null;
53
54     /**
55      * The initial string parsed to construct the Expression
56      */

57     private String JavaDoc stringExp = null;
58
59     /**
60      * The MEDOR Expression constructed using the input String
61      */

62     private Expression exp = null;
63
64     /**
65      * Builds a new RdbFilter object.
66      *
67      * This object encapsulates information about filter columns.
68      * The parent object is a RdbClassMapping object.
69      *
70      * @param parent the parent of the current object.
71      */

72     public RdbFilter(MetaObject parent) {
73         super(parent);
74         filteringElements = new HashMap JavaDoc();
75     }
76
77     /**
78      * Indicates if a PrimitiveElement is part of the filter.
79      * @param pe a PrimitiveElement.
80      * @return true, if pe is a part of the filter, else false.
81      */

82     public boolean isFilter(PrimitiveElement pe) {
83         logger.log(BasicLevel.DEBUG, "Looking whether PrimitiveElement " + pe.getName() + " is in the filter");
84         return filteringElements.containsKey(pe.getName());
85     }
86
87     /**
88      * Returns the predicate value associated with the filter PrimitiveElement
89      * name.
90      * If the predicate does not correspond to an equal operator, a PException is raised.
91      * @param elementName a filter column name.
92      * @return the predicate value associated with the filter PrimitiveElement.
93      */

94     public String JavaDoc getEqualPredicateValue(String JavaDoc elementName) throws PException {
95         String JavaDoc predValue = (String JavaDoc) filteringElements.get(elementName);
96         if (predValue == null) {
97             throw new PException("No fixed value can be given for this PrimitiveElement " +
98                     elementName + " although it is a hidden filter field!");
99         }
100         return predValue;
101     }
102
103     public void setExpression(Expression e) {
104         exp = e;
105         /* parse the expression to add the PrimitiveElement to the list of
106           filtering elements
107         */

108         addElementsToFilter(e);
109     }
110
111     private void addElementsToFilter(Expression e) {
112         if (e instanceof BasicParameterOperand) {
113             filteringElements.put(((BasicParameterOperand) e).getName(), null);
114         } else if (e instanceof Operand) {
115             //do nothing
116
} else if (e instanceof Equal) {
117             Equal eq = (Equal) e;
118             Expression eq0 = eq.getExpression(0);
119             Expression eq1 = eq.getExpression(1);
120             if (eq0 instanceof BasicParameterOperand) {
121                 if (eq1 instanceof BasicOperand) {
122                     filteringElements.put(
123                             ((BasicParameterOperand) eq0).getName(),
124                             ((BasicOperand) eq1).getObject()
125                     );
126                 } else {
127                     filteringElements.put(((BasicParameterOperand) eq0).getName(), null);
128                     addElementsToFilter(eq1);
129                 }
130             } else if (eq1 instanceof BasicParameterOperand) {
131                 if (eq0 instanceof BasicOperand) {
132                     filteringElements.put(
133                             ((BasicParameterOperand) eq1).getName(),
134                             ((BasicOperand) eq0).getObject()
135                     );
136                 } else {
137                     filteringElements.put(((BasicParameterOperand) eq1).getName(), null);
138                     addElementsToFilter(eq0);
139                 }
140             } else {
141                 addElementsToFilter(eq.getExpression(0));
142                 addElementsToFilter(eq.getExpression(1));
143             }
144         } else if (e instanceof Operator) {
145             Operator op = (Operator) e;
146             for (int i = 0; i < op.getOperandNumber(); i++) {
147                 addElementsToFilter(op.getExpression(i));
148             }
149         }
150     }
151
152     public Expression getExpression() {
153         return exp;
154     }
155
156     public void setStringExpression(String JavaDoc e) {
157         stringExp = e;
158     }
159
160     public String JavaDoc getStringExpression() {
161         return stringExp;
162     }
163 }
164
Popular Tags