KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > validator > ProcedureQueryValidator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project.validator;
21
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.cayenne.map.DataMap;
25 import org.apache.cayenne.map.Procedure;
26 import org.apache.cayenne.project.ProjectPath;
27 import org.apache.cayenne.query.ProcedureQuery;
28 import org.apache.cayenne.query.Query;
29 import org.apache.cayenne.util.Util;
30
31 /**
32  * Validator for ProcedureQueries.
33  *
34  * @author Andrus Adamchik
35  * @since 1.1
36  */

37 public class ProcedureQueryValidator extends TreeNodeValidator {
38
39     public void validateObject(ProjectPath treeNodePath, Validator validator) {
40         ProcedureQuery query = (ProcedureQuery) treeNodePath.getObject();
41
42         validateName(query, treeNodePath, validator);
43         validateRoot(query, treeNodePath, validator);
44     }
45
46     protected void validateRoot(
47             ProcedureQuery query,
48             ProjectPath path,
49             Validator validator) {
50         DataMap map = (DataMap) path.firstInstanceOf(DataMap.class);
51         Object JavaDoc root = query.getRoot();
52
53         if (root == null && map != null) {
54             validator.registerWarning("Query has no root", path);
55         }
56
57         // procedure query only supports procedure root
58
if (root instanceof Procedure) {
59             Procedure procedure = (Procedure) root;
60
61             // procedure may have been deleted...
62
if (map != null && map.getProcedure(procedure.getName()) != procedure) {
63                 validator.registerWarning("Invalid Procedure Root - "
64                         + procedure.getName(), path);
65             }
66
67             return;
68         }
69
70         if (root instanceof String JavaDoc) {
71             if (map != null && map.getProcedure(root.toString()) == null) {
72                 validator.registerWarning("Invalid Procedure Root - " + root, path);
73             }
74         }
75     }
76
77     protected void validateName(Query query, ProjectPath path, Validator validator) {
78         String JavaDoc name = query.getName();
79
80         // Must have name
81
if (Util.isEmptyString(name)) {
82             validator.registerError("Unnamed Query.", path);
83             return;
84         }
85
86         DataMap map = (DataMap) path.getObjectParent();
87         if (map == null) {
88             return;
89         }
90
91         // check for duplicate names in the parent context
92
Iterator JavaDoc it = map.getQueries().iterator();
93         while (it.hasNext()) {
94             Query otherQuery = (Query) it.next();
95             if (otherQuery == query) {
96                 continue;
97             }
98
99             if (name.equals(otherQuery.getName())) {
100                 validator.registerError("Duplicate Query name: " + name + ".", path);
101                 break;
102             }
103         }
104     }
105
106 }
107
Popular Tags