KickJava   Java API By Example, From Geeks To Geeks.

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


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.access.DataDomain;
25 import org.apache.cayenne.access.DataNode;
26 import org.apache.cayenne.map.DataMap;
27 import org.apache.cayenne.project.ProjectPath;
28 import org.apache.cayenne.util.Util;
29
30 /**
31  * Validator for DataMaps.
32  *
33  * @author Andrus Adamchik
34  */

35 public class DataMapValidator extends TreeNodeValidator {
36
37     /**
38      * Constructor for DataMapValidator.
39      */

40     public DataMapValidator() {
41         super();
42     }
43
44     public void validateObject(ProjectPath path, Validator validator) {
45         DataMap map = (DataMap) path.getObject();
46         validateName(map, path, validator);
47
48         // check if data map is not attached to any nodes
49
validateNodeLinks(map, path, validator);
50     }
51
52     protected void validateNodeLinks(DataMap map, ProjectPath path, Validator validator) {
53         DataDomain domain = (DataDomain) path.getObjectParent();
54         if (domain == null) {
55             return;
56         }
57         
58         boolean unlinked = true;
59         int nodeCount = 0;
60         Iterator JavaDoc it = domain.getDataNodes().iterator();
61         while(it.hasNext()) {
62             DataNode node = (DataNode)it.next();
63             nodeCount++;
64             if(node.getDataMaps().contains(map)) {
65                 unlinked = false;
66                 break;
67             }
68         }
69         
70         if(unlinked && nodeCount > 0) {
71              validator.registerWarning("DataMap is not linked to any DataNodes.", path);
72         }
73     }
74
75     protected void validateName(DataMap map, ProjectPath path, Validator validator) {
76         String JavaDoc name = map.getName();
77
78         if (Util.isEmptyString(name)) {
79             validator.registerError("Unnamed DataMap.", path);
80             return;
81         }
82
83         DataDomain domain = (DataDomain) path.getObjectParent();
84         if (domain == null) {
85             return;
86         }
87
88         // check for duplicate names in the parent context
89
Iterator JavaDoc it = domain.getDataMaps().iterator();
90         while (it.hasNext()) {
91             DataMap otherMap = (DataMap) it.next();
92             if (otherMap == map) {
93                 continue;
94             }
95
96             if (name.equals(otherMap.getName())) {
97                 validator.registerError("Duplicate DataMap name: " + name + ".", path);
98                 return;
99             }
100         }
101     }
102 }
103
Popular Tags