KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > core > server > XincoCoreLanguageServer


1 /**
2  *Copyright 2004 blueCubs.com
3  *
4  *Licensed under the Apache License, Version 2.0 (the "License");
5  *you may not use this file except in compliance with the License.
6  *You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *Unless required by applicable law or agreed to in writing, software
11  *distributed under the License is distributed on an "AS IS" BASIS,
12  *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *See the License for the specific language governing permissions and
14  *limitations under the License.
15  *
16  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoCoreLanguageServer
23  *
24  * Description: language
25  *
26  * Original Author: Alexander Manes
27  * Date: 2004
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.core.server;
38
39 import java.sql.*;
40 import java.util.Vector JavaDoc;
41
42 import com.bluecubs.xinco.core.*;
43
44 public class XincoCoreLanguageServer extends XincoCoreLanguage {
45     
46     //create language object for data structures
47
public XincoCoreLanguageServer(int attrID, XincoDBManager DBM) throws XincoException {
48         
49         try {
50             
51             Statement stmt = DBM.con.createStatement();
52             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_language WHERE id=" + attrID);
53
54             //throw exception if no result found
55
int RowCount = 0;
56             while (rs.next()) {
57                 RowCount++;
58                 setId(rs.getInt("id"));
59                 setSign(rs.getString("sign"));
60                 setDesignation(rs.getString("designation"));
61             }
62             if (RowCount < 1) {
63                 throw new XincoException();
64             }
65
66             stmt.close();
67             
68         } catch (Exception JavaDoc e) {
69             throw new XincoException();
70         }
71         
72     }
73     
74     //create language object for data structures
75
public XincoCoreLanguageServer(int attrID, String JavaDoc attrS, String JavaDoc attrD) throws XincoException {
76         
77         setId(attrID);
78         setSign(attrS);
79         setDesignation(attrD);
80         
81     }
82     
83     //write to db
84
public int write2DB(XincoDBManager DBM) throws XincoException {
85
86         try {
87
88             Statement stmt;
89
90             if (getId() > 0) {
91                 stmt = DBM.con.createStatement();
92                 stmt.executeUpdate("UPDATE xinco_core_language SET sign='" + getSign().replaceAll("'","\\\\'") + "', designation='" + getDesignation().replaceAll("'","\\\\'") + "' WHERE id=" + getId());
93                 stmt.close();
94             } else {
95                 setId(DBM.getNewID("xinco_core_language"));
96             
97                 stmt = DBM.con.createStatement();
98                 stmt.executeUpdate("INSERT INTO xinco_core_language VALUES (" + getId() + ", '" + getSign().replaceAll("'","\\\\'") + "', '" + getDesignation().replaceAll("'","\\\\'") + "')");
99                 stmt.close();
100             }
101             
102             DBM.con.commit();
103             
104         } catch (Exception JavaDoc e) {
105             try {
106                 DBM.con.rollback();
107             } catch (Exception JavaDoc erollback) {
108             }
109             throw new XincoException();
110         }
111         
112         return getId();
113         
114     }
115     
116     //delete from db
117
public static int deleteFromDB(XincoCoreLanguage attrCL, XincoDBManager DBM) throws XincoException {
118
119         try {
120             
121             Statement stmt = null;
122             
123             stmt = DBM.con.createStatement();
124             stmt.executeUpdate("DELETE FROM xinco_core_language WHERE id=" + attrCL.getId());
125             stmt.close();
126
127             DBM.con.commit();
128             
129         } catch (Exception JavaDoc e) {
130             try {
131                 DBM.con.rollback();
132             } catch (Exception JavaDoc erollback) {
133             }
134             throw new XincoException();
135         }
136         
137         return 0;
138             
139     }
140     
141     //create complete list of languages
142
public static Vector JavaDoc getXincoCoreLanguages(XincoDBManager DBM) {
143         
144         Vector JavaDoc coreLanguages = new Vector JavaDoc();
145         
146         try {
147             
148             Statement stmt = DBM.con.createStatement();
149             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_language ORDER BY designation");
150
151             while (rs.next()) {
152                 coreLanguages.addElement(new XincoCoreLanguageServer(rs.getInt("id"), rs.getString("sign"), rs.getString("designation")));
153             }
154
155             stmt.close();
156             
157         } catch (Exception JavaDoc e) {
158             coreLanguages.removeAllElements();
159         }
160
161         return coreLanguages;
162     }
163
164     //check if language is in use by other objects
165
public static boolean isLanguageUsed(XincoCoreLanguage xcl, XincoDBManager DBM) {
166         
167         boolean is_used = false;
168         
169         try {
170             
171             Statement stmt = null;
172             ResultSet rs = null;
173             
174             stmt = DBM.con.createStatement();
175             rs = stmt.executeQuery("SELECT 1 FROM xinco_core_node WHERE xinco_core_language_id = " + xcl.getId());
176             while (rs.next()) {
177                 is_used = true;
178             }
179             stmt.close();
180             
181             if (!is_used) {
182                 stmt = DBM.con.createStatement();
183                 rs = stmt.executeQuery("SELECT 1 FROM xinco_core_data WHERE xinco_core_language_id = " + xcl.getId());
184                 while (rs.next()) {
185                     is_used = true;
186                 }
187                 stmt.close();
188             }
189             
190         } catch (Exception JavaDoc e) {
191             is_used = true; // rather lock language in case of error!
192
}
193
194         return is_used;
195     }
196
197 }
198
Popular Tags