KickJava   Java API By Example, From Geeks To Geeks.

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


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.project.ProjectPath;
26 import org.apache.cayenne.query.Query;
27 import org.apache.cayenne.query.SQLTemplate;
28 import org.apache.cayenne.util.Util;
29
30 /**
31  * Validator for SQLTemplate queries.
32  *
33  * @author Andrus Adamchik
34  * @since 1.1
35  */

36 public class SQLTemplateValidator extends TreeNodeValidator {
37
38     public void validateObject(ProjectPath treeNodePath, Validator validator) {
39         SQLTemplate query = (SQLTemplate) treeNodePath.getObject();
40
41         validateName(query, treeNodePath, validator);
42         validateRoot(query, treeNodePath, validator);
43         validateDefaultSQL(query, treeNodePath, validator);
44     }
45
46     protected void validateDefaultSQL(
47             SQLTemplate query,
48             ProjectPath path,
49             Validator validator) {
50
51         if (Util.isEmptyString(query.getDefaultTemplate())) {
52             // see if there is at least one adapter-specific template...
53

54             Iterator JavaDoc it = query.getTemplateKeys().iterator();
55             while (it.hasNext()) {
56                 String JavaDoc key = (String JavaDoc) it.next();
57                 if (!Util.isEmptyString(query.getCustomTemplate(key))) {
58                     return;
59                 }
60             }
61
62             validator.registerWarning("Query has no default SQL template", path);
63         }
64     }
65
66     protected void validateRoot(SQLTemplate query, ProjectPath path, Validator validator) {
67         DataMap map = (DataMap) path.firstInstanceOf(DataMap.class);
68         if (query.getRoot() == null && map != null) {
69             validator.registerWarning("Query has no root", path);
70         }
71     }
72
73     protected void validateName(Query query, ProjectPath path, Validator validator) {
74         String JavaDoc name = query.getName();
75
76         // Must have name
77
if (Util.isEmptyString(name)) {
78             validator.registerError("Unnamed Query.", path);
79             return;
80         }
81
82         DataMap map = (DataMap) path.getObjectParent();
83         if (map == null) {
84             return;
85         }
86
87         // check for duplicate names in the parent context
88
Iterator JavaDoc it = map.getQueries().iterator();
89         while (it.hasNext()) {
90             Query otherQuery = (Query) it.next();
91             if (otherQuery == query) {
92                 continue;
93             }
94
95             if (name.equals(otherQuery.getName())) {
96                 validator.registerError("Duplicate Query name: " + name + ".", path);
97                 break;
98             }
99         }
100     }
101 }
102
Popular Tags