KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > query > model > HasPart


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.query.model;
17
18 import org.outerj.daisy.query.QueryContext;
19 import org.outerj.daisy.repository.query.QueryException;
20 import org.outerj.daisy.repository.query.EvaluationContext;
21 import org.outerj.daisy.repository.Document;
22 import org.outerj.daisy.repository.RepositoryException;
23 import org.outerj.daisy.repository.Version;
24 import org.outerj.daisy.repository.schema.PartType;
25
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 public class HasPart extends AbstractPredicateExpr {
30     private final String JavaDoc partTypeName;
31     private PartType partType;
32
33     public HasPart(String JavaDoc partTypeName) {
34         this.partTypeName = partTypeName;
35     }
36
37     public void prepare(QueryContext context) throws QueryException {
38         try {
39             this.partType = context.getPartTypeByName(partTypeName);
40         } catch (RepositoryException e) {
41             throw new QueryException("Error retrieving PartType named \"" + partTypeName + "\".", e);
42         }
43     }
44
45     public boolean evaluate(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
46         if (version != null)
47             return version.hasPart(partType.getId());
48         else
49             return document.hasPart(partType.getId());
50     }
51
52     public void generateSql(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
53         String JavaDoc alias = context.getNewPartsTableAlias();
54
55         sql.append(alias);
56         sql.append('.');
57         sql.append(SqlGenerationContext.PartsTable.PARTTYPE_ID);
58         sql.append(" = ? ");
59     }
60
61     public int bindSql(PreparedStatement JavaDoc stmt, int bindPos, EvaluationContext evaluationContext) throws SQLException JavaDoc {
62         stmt.setLong(bindPos, partType.getId());
63         bindPos++;
64         return bindPos;
65     }
66
67     public AclConditionViolation isAclAllowed() {
68         return new AclConditionViolation("HasPart is not allowed in ACL conditions");
69     }
70
71     public Tristate appliesTo(Document document) {
72         throw new IllegalStateException JavaDoc();
73     }
74 }
75
Popular Tags