KickJava   Java API By Example, From Geeks To Geeks.

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


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: XincoCoreLogServer
23  *
24  * Description: log
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.util.Vector JavaDoc;
40 import java.util.Calendar JavaDoc;
41 import java.util.GregorianCalendar JavaDoc;
42 import java.sql.*;
43
44 import com.bluecubs.xinco.core.*;
45
46 public class XincoCoreLogServer extends XincoCoreLog {
47     
48     //create single log object for data structures
49
public XincoCoreLogServer(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_log 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_data_id(rs.getInt("xinco_core_data_id"));
62                 setXinco_core_user_id(rs.getInt("xinco_core_user_id"));
63                 setOp_code(rs.getInt("op_code"));
64                 setOp_datetime(new GregorianCalendar JavaDoc());
65                 getOp_datetime().setTime(rs.getDate("op_datetime"));
66                 setOp_description(rs.getString("op_description"));
67                 setVersion(new XincoVersion());
68                 getVersion().setVersion_high(rs.getInt("version_high"));
69                 getVersion().setVersion_mid(rs.getInt("version_mid"));
70                 getVersion().setVersion_low(rs.getInt("version_low"));
71                 getVersion().setVersion_postfix(rs.getString("version_postfix"));
72             }
73             if (RowCount < 1) {
74                 throw new XincoException();
75             }
76
77             stmt.close();
78             
79         } catch (Exception JavaDoc e) {
80             throw new XincoException();
81         }
82         
83     }
84     
85     //create single log object for data structures
86
public XincoCoreLogServer(int attrID, int attrCDID, int attrUID, int attrOC, Calendar JavaDoc attrODT, String JavaDoc attrOD, int attrVH, int attrVM, int attrVL, String JavaDoc attrVP) throws XincoException {
87         
88         setId(attrID);
89         setXinco_core_data_id(attrCDID);
90         setXinco_core_user_id(attrUID);
91         setOp_code(attrOC);
92         setOp_datetime(attrODT);
93         setOp_description(attrOD);
94         setVersion(new XincoVersion());
95         getVersion().setVersion_high(attrVH);
96         getVersion().setVersion_mid(attrVM);
97         getVersion().setVersion_low(attrVL);
98         getVersion().setVersion_postfix(attrVP);
99         
100     }
101     
102     //write to db
103
public int write2DB(XincoDBManager DBM) throws XincoException {
104
105         try {
106             
107             if (getId() > 0) {
108                 Statement stmt = DBM.con.createStatement();
109                 stmt.executeUpdate("UPDATE xinco_core_log SET xinco_core_data_id=" + getXinco_core_data_id() + ", xinco_core_user_id=" + getXinco_core_user_id() + ", op_code=" + getOp_code() + ", op_datetime=now(), op_description='" + getOp_description().replaceAll("'","\\\\'") + "', version_high=" + getVersion().getVersion_high() + ", version_mid=" + getVersion().getVersion_mid() + ", version_low=" + getVersion().getVersion_low() + ", version_postfix='" + getVersion().getVersion_postfix().replaceAll("'","\\\\'") + "' WHERE id=" + getId());
110                 stmt.close();
111             } else {
112                 setId(DBM.getNewID("xinco_core_log"));
113
114                 Statement stmt = DBM.con.createStatement();
115                 stmt.executeUpdate("INSERT INTO xinco_core_log VALUES (" + getId() + ", " + getXinco_core_data_id() + ", " + getXinco_core_user_id() + ", " + getOp_code() + ", now(), '" + getOp_description().replaceAll("'","\\\\'") + "', " + getVersion().getVersion_high() + ", " + getVersion().getVersion_mid() + ", " + getVersion().getVersion_low() + ", '" + getVersion().getVersion_postfix().replaceAll("'","\\\\'") + "')");
116                 stmt.close();
117             }
118
119             DBM.con.commit();
120             
121         } catch (Exception JavaDoc e) {
122             try {
123                 DBM.con.rollback();
124             } catch (Exception JavaDoc erollback) {
125             }
126             throw new XincoException();
127         }
128         
129         return getId();
130             
131     }
132     
133     //create complete log list for data
134
public static Vector JavaDoc getXincoCoreLogs(int attrID, XincoDBManager DBM) {
135         
136         Vector JavaDoc core_log = new Vector JavaDoc();
137         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
138         
139         try {
140             Statement stmt = DBM.con.createStatement();
141             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_log WHERE xinco_core_data_id=" + attrID);
142
143             while (rs.next()) {
144                 cal = new GregorianCalendar JavaDoc();
145                 cal.setTime( rs.getDate("op_datetime"));
146                 core_log.addElement(new XincoCoreLogServer(rs.getInt("id"), rs.getInt("xinco_core_data_id"), rs.getInt("xinco_core_user_id"), rs.getInt("op_code"), cal, rs.getString("op_description"), rs.getInt("version_high"), rs.getInt("version_mid"), rs.getInt("version_low"), rs.getString("version_postfix")));
147             }
148
149             stmt.close();
150         } catch (Exception JavaDoc e) {
151             core_log.removeAllElements();
152         }
153
154         return core_log;
155     }
156
157 }
158
Popular Tags