KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > DataNodeFile


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;
21
22 import java.io.PrintWriter JavaDoc;
23
24 import org.apache.cayenne.access.DataNode;
25 import org.apache.cayenne.conf.ConfigSaver;
26 import org.apache.cayenne.conf.DriverDataSourceFactory;
27
28 /**
29  * DataNodeFile is a ProjectFile abstraction of the
30  * DataNode file in a Cayenne project.
31  *
32  * @author Andrus Adamchik
33  */

34 public class DataNodeFile extends ProjectFile {
35     public static final String JavaDoc LOCATION_SUFFIX = ".driver.xml";
36
37     protected DataNode nodeObj;
38
39     public DataNodeFile() {}
40
41     /**
42      * Constructor for DataNodeFile.
43      */

44     public DataNodeFile(Project project, DataNode node) {
45         super(project, node.getDataSourceLocation());
46         this.nodeObj = node;
47     }
48
49     /**
50      * @see ProjectFile#getObject()
51      */

52     public Object JavaDoc getObject() {
53         return nodeObj;
54     }
55
56     /**
57      * @see ProjectFile#getObjectName()
58      */

59     public String JavaDoc getObjectName() {
60         return nodeObj.getName();
61     }
62
63     public void save(PrintWriter JavaDoc out) throws Exception JavaDoc {
64         ProjectDataSource src = (ProjectDataSource) nodeObj.getDataSource();
65         new ConfigSaver().storeDataNode(out, getProject(), src.getDataSourceInfo());
66     }
67
68     /**
69      * @see org.apache.cayenne.project.ProjectFile#canHandle(Object)
70      */

71     public boolean canHandle(Object JavaDoc obj) {
72         if (obj instanceof DataNode) {
73             DataNode node = (DataNode) obj;
74
75             // only driver datasource factory requires a file
76
if (DriverDataSourceFactory
77                 .class
78                 .getName()
79                 .equals(node.getDataSourceFactory())) {
80                 return true;
81             }
82         }
83
84         return false;
85     }
86
87     /**
88      * Updates node location to match the name before save.
89      */

90     public void willSave() {
91         super.willSave();
92
93         if (nodeObj != null && canHandle(nodeObj)) {
94             nodeObj.setDataSourceLocation(getLocation());
95         }
96     }
97
98     /**
99      * Returns ".driver.xml" that should be used as a file suffix
100      * for DataNode driver files.
101      */

102     public String JavaDoc getLocationSuffix() {
103         return LOCATION_SUFFIX;
104     }
105 }
106
Popular Tags