KickJava   Java API By Example, From Geeks To Geeks.

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


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: XincoCoreDataServer
23  *
24  * Description: data object
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 import java.io.File JavaDoc;
42 import com.bluecubs.xinco.core.*;
43 import com.bluecubs.xinco.add.*;
44 import com.bluecubs.xinco.add.server.*;
45
46 public class XincoCoreDataServer extends XincoCoreData {
47     
48     //create data object for data structures
49
public XincoCoreDataServer(int attrID, XincoDBManager DBM) throws XincoException {
50
51         try {
52             
53             Statement stmt = DBM.con.createStatement();
54             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_data WHERE id=" + attrID);
55
56             //throw exception if no result found
57
int RowCount = 0;
58             while (rs.next()) {
59                 RowCount++;
60                 setId(rs.getInt("id"));
61                 setXinco_core_node_id(rs.getInt("xinco_core_node_id"));
62                 setXinco_core_language(new XincoCoreLanguageServer(rs.getInt("xinco_core_language_id"), DBM));
63                 setXinco_core_data_type(new XincoCoreDataTypeServer(rs.getInt("xinco_core_data_type_id"), DBM));
64                 //load logs
65
setXinco_core_logs(XincoCoreLogServer.getXincoCoreLogs(rs.getInt("id"), DBM));
66                 //load add attributes
67
setXinco_add_attributes(XincoAddAttributeServer.getXincoAddAttributes(rs.getInt("id"), DBM));
68                 setDesignation(rs.getString("designation"));
69                 setStatus_number(rs.getInt("status_number"));
70                 //load acl for this object
71
setXinco_core_acl(XincoCoreACEServer.getXincoCoreACL(rs.getInt("id"), "xinco_core_data_id", DBM));
72             }
73             if (RowCount < 1) {
74                 throw new XincoException();
75             }
76
77             stmt.close();
78             
79         } catch (Exception JavaDoc e) {
80             getXinco_core_acl().removeAllElements();
81             throw new XincoException();
82         }
83         
84     }
85     
86     //create data object for data structures
87
public XincoCoreDataServer(int attrID, int attrCNID, int attrLID, int attrDTID, String JavaDoc attrD, int attrSN, XincoDBManager DBM) throws XincoException {
88
89         setId(attrID);
90         setXinco_core_node_id(attrCNID);
91         setXinco_core_language(new XincoCoreLanguageServer(attrLID, DBM));
92         setXinco_core_data_type(new XincoCoreDataTypeServer(attrDTID, DBM));
93         //load logs
94
setXinco_core_logs(XincoCoreLogServer.getXincoCoreLogs(attrID, DBM));
95         //load add attributes
96
//setXinco_add_attributes(XincoAddAttributeServer.getXincoAddAttributes(attrID, DBM));
97
//security: don't load add attribute, force direct access to data including check of access rights!
98
setXinco_add_attributes(new Vector JavaDoc());
99         setDesignation(attrD);
100         setStatus_number(attrSN);
101         //load acl for this object
102
setXinco_core_acl(XincoCoreACEServer.getXincoCoreACL(getId(), "xinco_core_data_id", DBM));
103
104     }
105
106     //write to db
107
public int write2DB(XincoDBManager DBM) throws XincoException {
108
109         int i = 0;
110         
111         try {
112             
113             Statement stmt;
114             
115             if (getId() > 0) {
116                 stmt = DBM.con.createStatement();
117                 stmt.executeUpdate("UPDATE xinco_core_data SET xinco_core_node_id=" + getXinco_core_node_id() + ", xinco_core_language_id=" + getXinco_core_language().getId() + ", xinco_core_data_type_id=" + getXinco_core_data_type().getId() + ", designation='" + getDesignation().replaceAll("'","\\\\'") + "', status_number=" + getStatus_number() + " WHERE id =" + getId());
118                 stmt.close();
119             
120             } else {
121                 setId(DBM.getNewID("xinco_core_data"));
122
123                 stmt = DBM.con.createStatement();
124                 stmt.executeUpdate("INSERT INTO xinco_core_data VALUES (" + getId() + ", " + getXinco_core_node_id() + ", " + getXinco_core_language().getId() + ", " + getXinco_core_data_type().getId() + ", '" + getDesignation().replaceAll("'","\\\\'") + "', " + getStatus_number() + ")");
125                 stmt.close();
126             
127             }
128             
129             //write add attributes
130
stmt = DBM.con.createStatement();
131             stmt.executeUpdate("DELETE FROM xinco_add_attribute WHERE xinco_core_data_id=" + getId());
132             stmt.close();
133             XincoAddAttributeServer xaas;
134             for (i=0;i<getXinco_add_attributes().size();i++) {
135                 ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).setXinco_core_data_id(getId());
136                 //copy fields from XincoAddAttribute to XincoAddAttributeServer
137
xaas = new XincoAddAttributeServer(((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getXinco_core_data_id(), ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getAttribute_id(), ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getAttrib_int(), ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getAttrib_unsignedint(), ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getAttrib_double(), ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getAttrib_varchar(), ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getAttrib_text(), ((XincoAddAttribute)getXinco_add_attributes().elementAt(i)).getAttrib_datetime());
138                 xaas.write2DB(DBM);
139                 //((XincoAddAttributeServer)getXinco_add_attributes().elementAt(i)).write2DB(DBM);
140
}
141             
142             DBM.con.commit();
143             
144         } catch (Exception JavaDoc e) {
145             try {
146                 DBM.con.rollback();
147             } catch (Exception JavaDoc erollback) {
148             }
149             throw new XincoException();
150         }
151         
152         return getId();
153         
154     }
155
156     //delete from db
157
public void deleteFromDB(XincoDBManager DBM) throws XincoException {
158
159         int i=0;
160         
161         try {
162             
163             Statement stmt;
164             
165             stmt = DBM.con.createStatement();
166             stmt.executeUpdate("DELETE FROM xinco_core_log WHERE xinco_core_data_id=" + getId());
167             stmt.close();
168             stmt = DBM.con.createStatement();
169             stmt.executeUpdate("DELETE FROM xinco_core_ace WHERE xinco_core_data_id=" + getId());
170             stmt.close();
171             stmt = DBM.con.createStatement();
172             stmt.executeUpdate("DELETE FROM xinco_add_attribute WHERE xinco_core_data_id=" + getId());
173             stmt.close();
174             //delete file / file = 1
175
if (getXinco_core_data_type().getId() == 1) {
176                 try {
177                     (new File JavaDoc(DBM.config.FileRepositoryPath + getId())).delete();
178                 } catch (Exception JavaDoc dfe) {
179                     // continue, file might not exists
180
}
181                 // delete revisions created upon creation or checkin
182
for (i=0;i<this.getXinco_core_logs().size();i++) {
183                     if ((((XincoCoreLog)getXinco_core_logs().elementAt(i)).getOp_code() == 1) || (((XincoCoreLog)getXinco_core_logs().elementAt(i)).getOp_code() == 5))
184                     try {
185                         (new File JavaDoc(DBM.config.FileRepositoryPath + getId() + "-" + ((XincoCoreLog)getXinco_core_logs().elementAt(i)).getId())).delete();
186                     } catch (Exception JavaDoc drfe) {
187                         // continue, delete next revision
188
}
189                 }
190             }
191             stmt = DBM.con.createStatement();
192             stmt.executeUpdate("DELETE FROM xinco_core_data WHERE id=" + getId());
193             stmt.close();
194             DBM.con.commit();
195         } catch (Exception JavaDoc e) {
196             try {
197                 DBM.con.rollback();
198             } catch (Exception JavaDoc erollback) {
199             }
200             throw new XincoException();
201         }
202         
203     }
204     
205     public static byte[] loadBinaryData(XincoCoreData attrCD) {
206         
207         byte[] binary_data = null;
208
209         return binary_data;
210
211     }
212     
213     public static int saveBinaryData(XincoCoreData attrCD, byte[] attrBD) {
214         
215         return 0;
216
217     }
218     
219     public static Vector JavaDoc findXincoCoreData(String JavaDoc attrS, int attrLID, boolean attrSA, boolean attrSFD, XincoDBManager DBM) {
220
221         Vector JavaDoc data = new Vector JavaDoc();
222
223         try {
224             
225             Statement stmt = DBM.con.createStatement();
226             ResultSet rs;
227             String JavaDoc lang = "";
228             if (attrLID != 0) {
229                 lang = "AND (xinco_core_language_id = " + attrLID + ")";
230             }
231             if (attrSA) {
232                 rs = stmt.executeQuery("SELECT DISTINCT xinco_core_data.* FROM xinco_core_data, xinco_add_attribute WHERE (xinco_core_data.id = xinco_add_attribute.xinco_core_data_id) AND (xinco_core_data.designation LIKE '" + attrS + "%' OR xinco_add_attribute.attrib_varchar LIKE '" + attrS + "' OR xinco_add_attribute.attrib_text LIKE '" + attrS + "') " + lang + " ORDER BY xinco_core_data.designation, xinco_core_data.xinco_core_language_id");
233             } else {
234                 rs = stmt.executeQuery("SELECT DISTINCT xinco_core_data.* FROM xinco_core_data WHERE designation LIKE '" + attrS + "' " + lang + " ORDER BY designation, xinco_core_language_id");
235             }
236
237             int i = 0;
238             while (rs.next()) {
239                 //data.addElement(new XincoCoreDataServer(rs.getInt("id"), rs.getInt("xinco_core_node_id"), rs.getInt("xinco_core_language_id"), rs.getInt("xinco_core_data_type_id"), rs.getString("designation"), rs.getInt("status_number"), DBM));
240
data.addElement(new XincoCoreDataServer(rs.getInt("id"), DBM));
241                 i++;
242                 if (i >= DBM.config.MaxSearchResult) {
243                     break;
244                 }
245             }
246
247             stmt.close();
248             
249         } catch (Exception JavaDoc e) {
250             data.removeAllElements();
251         }
252
253         return data;
254
255     }
256     
257 }
258
Popular Tags