KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > RuntimeSaveDelegate


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.conf;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.commons.collections.Transformer;
30 import org.apache.commons.collections.iterators.TransformIterator;
31 import org.apache.cayenne.access.DataDomain;
32 import org.apache.cayenne.access.DataNode;
33 import org.apache.cayenne.dba.AutoAdapter;
34 import org.apache.cayenne.dba.DbAdapter;
35 import org.apache.cayenne.map.DataMap;
36 import org.apache.cayenne.util.Util;
37
38 /**
39  * Save delegate used for saving Cayenne access stack.
40  *
41  * @author Andrus Adamchik
42  */

43 public class RuntimeSaveDelegate implements ConfigSaverDelegate {
44
45     protected Configuration config;
46
47     public RuntimeSaveDelegate(Configuration config) {
48         this.config = config;
49     }
50
51     /**
52      * Constructor for RuntimeSaveDelegate.
53      */

54     public RuntimeSaveDelegate() {
55         super();
56     }
57
58     protected DataDomain findDomain(String JavaDoc domainName) {
59         DataDomain domain = config.getDomain(domainName);
60         if (domain == null) {
61             throw new IllegalArgumentException JavaDoc("Can't find DataDomain: " + domainName);
62         }
63
64         return domain;
65     }
66
67     protected DataNode findNode(String JavaDoc domainName, String JavaDoc nodeName) {
68         DataDomain domain = findDomain(domainName);
69         DataNode node = domain.getNode(nodeName);
70         if (node == null) {
71             throw new IllegalArgumentException JavaDoc("Can't find DataNode: "
72                     + domainName
73                     + "."
74                     + nodeName);
75         }
76
77         return node;
78     }
79
80     /**
81      * @since 1.1
82      */

83     public String JavaDoc projectVersion() {
84         return config.getProjectVersion();
85     }
86
87     public Iterator JavaDoc domainNames() {
88         Transformer tr = new Transformer() {
89
90             public Object JavaDoc transform(Object JavaDoc input) {
91                 return ((DataDomain) input).getName();
92             }
93         };
94         return new TransformIterator(config.getDomains().iterator(), tr);
95     }
96
97     /**
98      * @since 1.1
99      */

100     public Iterator JavaDoc viewNames() {
101         return config.getDataViewLocations().keySet().iterator();
102     }
103
104     /**
105      * @since 1.1
106      */

107     public String JavaDoc viewLocation(String JavaDoc dataViewName) {
108         return (String JavaDoc) config.getDataViewLocations().get(dataViewName);
109     }
110
111     public Iterator JavaDoc propertyNames(String JavaDoc domainName) {
112         return findDomain(domainName).getProperties().keySet().iterator();
113     }
114
115     public String JavaDoc propertyValue(String JavaDoc domainName, String JavaDoc propertyName) {
116         return (String JavaDoc) findDomain(domainName).getProperties().get(propertyName);
117     }
118
119     public String JavaDoc mapLocation(String JavaDoc domainName, String JavaDoc mapName) {
120         return findDomain(domainName).getMap(mapName).getLocation();
121     }
122
123     public Iterator JavaDoc mapNames(String JavaDoc domainName) {
124
125         // sort maps by name
126
List JavaDoc maps = new ArrayList JavaDoc(findDomain(domainName).getDataMaps());
127         Collections.sort(maps, new Comparator JavaDoc() {
128
129             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
130                 String JavaDoc name1 = (o1 != null) ? ((DataMap) o1).getName() : null;
131                 String JavaDoc name2 = (o1 != null) ? ((DataMap) o2).getName() : null;
132                 return Util.nullSafeCompare(true, name1, name2);
133             }
134         });
135
136         Transformer tr = new Transformer() {
137
138             public Object JavaDoc transform(Object JavaDoc input) {
139                 return ((DataMap) input).getName();
140             }
141         };
142         return new TransformIterator(maps.iterator(), tr);
143     }
144
145     public String JavaDoc nodeAdapterName(String JavaDoc domainName, String JavaDoc nodeName) {
146         DbAdapter adapter = findNode(domainName, nodeName).getAdapter();
147         return (adapter != null && adapter.getClass() != AutoAdapter.class) ? adapter
148                 .getClass()
149                 .getName() : null;
150     }
151
152     public String JavaDoc nodeDataSourceName(String JavaDoc domainName, String JavaDoc nodeName) {
153         return findNode(domainName, nodeName).getDataSourceLocation();
154     }
155
156     public String JavaDoc nodeFactoryName(String JavaDoc domainName, String JavaDoc nodeName) {
157         return findNode(domainName, nodeName).getDataSourceFactory();
158     }
159
160     public Iterator JavaDoc nodeNames(String JavaDoc domainName) {
161         Transformer tr = new Transformer() {
162
163             public Object JavaDoc transform(Object JavaDoc input) {
164                 return ((DataNode) input).getName();
165             }
166         };
167
168         // sort nodes by name
169
List JavaDoc nodes = new ArrayList JavaDoc(findDomain(domainName).getDataNodes());
170         Collections.sort(nodes, new Comparator JavaDoc() {
171
172             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
173                 String JavaDoc name1 = (o1 != null) ? ((DataNode) o1).getName() : null;
174                 String JavaDoc name2 = (o1 != null) ? ((DataNode) o2).getName() : null;
175                 return Util.nullSafeCompare(true, name1, name2);
176             }
177         });
178         
179         return new TransformIterator(nodes.iterator(), tr);
180     }
181
182     public Iterator JavaDoc linkedMapNames(String JavaDoc domainName, String JavaDoc nodeName) {
183         Transformer tr = new Transformer() {
184
185             public Object JavaDoc transform(Object JavaDoc input) {
186                 return ((DataMap) input).getName();
187             }
188         };
189
190         Collection JavaDoc maps = findNode(domainName, nodeName).getDataMaps();
191         return new TransformIterator(maps.iterator(), tr);
192     }
193 }
194
Popular Tags