KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > query > constraint > ValueConstraintImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.query.constraint;
25
26 import org.objectweb.jalisto.se.api.ClassDescription;
27 import org.objectweb.jalisto.se.api.Session;
28 import org.objectweb.jalisto.se.api.query.FielComparator;
29 import org.objectweb.jalisto.se.api.query.Constraint;
30 import org.objectweb.jalisto.se.api.query.Operator;
31 import org.objectweb.jalisto.se.query.operator.OperatorFactory;
32 import org.objectweb.jalisto.se.query.parameter.QueryValue;
33 import org.objectweb.jalisto.se.query.result.WrapperSet;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37
38 public class ValueConstraintImpl extends ConstraintImpl {
39
40     public ValueConstraintImpl(String JavaDoc className, ArrayList JavaDoc subFieldNames, QueryValue value) {
41         this.className = className;
42         this.fieldNames = new String JavaDoc[subFieldNames.size()];
43         for (int i = 0; i < fieldNames.length; i++) {
44             fieldNames[i] = (String JavaDoc) subFieldNames.get(i);
45         }
46         this.value = value;
47         this.resolvedValues = new WrapperSet();
48         this.isResolved = false;
49     }
50
51     public Constraint contains() {
52         operator = OperatorFactory.getContainsOperator();
53         return this;
54     }
55
56     public Constraint equal() {
57         operator = OperatorFactory.getEqualOperator();
58         return this;
59     }
60
61     public Object JavaDoc getObject() {
62         return value.getValue();
63     }
64
65     public Constraint greater() {
66         operator = OperatorFactory.getGreaterOperator();
67         return this;
68     }
69
70     public Constraint identity() {
71         operator = OperatorFactory.getIdentityOperator();
72         return this;
73     }
74
75     public Constraint like() {
76         throw new UnsupportedOperationException JavaDoc();
77     }
78
79     public Constraint not() {
80         not = !not;
81         return this;
82     }
83
84     public Constraint smaller() {
85         operator = OperatorFactory.getSmallerOperator();
86         return this;
87     }
88
89     public boolean execute(Session session, FielComparator comparator, Object JavaDoc candidate) {
90         ArrayList JavaDoc candidates = new ArrayList JavaDoc();
91         candidates.add(candidate);
92         if (fieldNames.length > 1) {
93             candidates.clear();
94             if (Collection JavaDoc.class.isAssignableFrom(candidate.getClass())) {
95                 candidates.addAll((Collection JavaDoc) candidate);
96             } else {
97                 candidates.add(candidate);
98             }
99             for (short i = 1; i < fieldNames.length; i++) {
100                 // all object in collection are of the same class
101
String JavaDoc currentClassName = session.getClassNameFor(candidates.get(0));
102                 ClassDescription meta = session.getMetaRepository().getMetaData(currentClassName);
103                 int index = meta.getIndex(fieldNames[i]);
104
105                 ArrayList JavaDoc newCandidates = new ArrayList JavaDoc();
106                 for (int j = 0; j < candidates.size(); j++) {
107                     newCandidates.add(session.readObjectByOid(candidates.get(j))[index]);
108                 }
109                 candidates.clear();
110                 if (Collection JavaDoc.class.isAssignableFrom(newCandidates.get(0).getClass())) {
111                     for (int j = 0; j < newCandidates.size(); j++) {
112                         candidates.addAll((Collection JavaDoc) newCandidates.get(i));
113                     }
114                 } else {
115                     candidates.addAll(newCandidates);
116                 }
117             }
118         }
119         if (not) {
120             return (!operator.execute(comparator, candidates, value.getValue()));
121         } else {
122             return operator.execute(comparator, candidates, value.getValue());
123         }
124     }
125
126     public String JavaDoc getFieldName() {
127         return fieldNames[0];
128     }
129
130     public String JavaDoc[] getFieldNames() {
131         return fieldNames;
132     }
133
134     public String JavaDoc getClassName() {
135         return className;
136     }
137
138     public boolean isResolved() {
139         return isResolved;
140     }
141
142     public void setResolved(boolean resolved) {
143         isResolved = resolved;
144     }
145
146     public WrapperSet getResolvedValues() {
147         return resolvedValues;
148     }
149
150     public void clean() {
151         resolvedValues.clear();
152         isResolved = false;
153     }
154
155     public String JavaDoc toString() {
156         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
157         sb.append("VCon").append(fieldNames[0]).append(operator).append(value.getValue());
158         return sb.toString();
159     }
160
161
162     private String JavaDoc className;
163     private String JavaDoc[] fieldNames;
164     private QueryValue value;
165     private Operator operator;
166     private boolean not = false;
167
168     protected WrapperSet resolvedValues;
169     protected boolean isResolved;
170 }
171
Popular Tags