KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > add > server > XincoAddAttributeServer


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: XincoAddAttributeServer
23  *
24  * Description: additional attributes of a 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.add.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 import com.bluecubs.xinco.core.server.*;
46 import com.bluecubs.xinco.add.*;
47
48 public class XincoAddAttributeServer extends XincoAddAttribute {
49     
50     //create add attribute object for data structures
51
public XincoAddAttributeServer(int attrID1, int attrID2, XincoDBManager DBM) throws XincoException {
52         
53         try {
54             Statement stmt = DBM.con.createStatement();
55             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_add_attribute WHERE xinco_core_data_id=" + attrID1 + " AND attribute_id=" + attrID2);
56
57             while (rs.next()) {
58                 setXinco_core_data_id(rs.getInt("xinco_core_data_id"));
59                 setAttribute_id(rs.getInt("attribute_id"));
60                 setAttrib_int(rs.getInt("attrib_int"));
61                 setAttrib_unsignedint(rs.getInt("attrib_unsignedint"));
62                 setAttrib_double(rs.getInt("attrib_double"));
63                 setAttrib_varchar(rs.getString("attrib_varchar"));
64                 setAttrib_text(rs.getString("attrib_text"));
65                 setAttrib_datetime(new GregorianCalendar JavaDoc());
66                 getAttrib_datetime().setTime(rs.getDate("attrib_datetime"));
67             }
68
69             stmt.close();
70         } catch (Exception JavaDoc e) {
71             throw new XincoException();
72         }
73         
74     }
75
76     //create add attribute object for data structures
77
public XincoAddAttributeServer(int attrID1, int attrID2, int attrI, long attrUI, double attrD, String JavaDoc attrVC, String JavaDoc attrT, Calendar JavaDoc attrDT) throws XincoException {
78         
79         setXinco_core_data_id(attrID1);
80         setAttribute_id(attrID2);
81         setAttrib_int(attrI);
82         setAttrib_unsignedint(attrUI);
83         setAttrib_double(attrD);
84         setAttrib_varchar(attrVC);
85         setAttrib_text(attrT);
86         setAttrib_datetime(attrDT);
87         
88     }
89     
90     //write to db
91
public int write2DB(XincoDBManager DBM) throws XincoException {
92
93         try {
94
95             Statement stmt;
96             String JavaDoc attrT = "";
97             String JavaDoc attrVC = "";
98             String JavaDoc attrDT = "";
99             if (getAttrib_text() != null) {
100                 attrT = getAttrib_text();
101                 attrT = attrT.replaceAll("'","\\\\'");
102             } else {
103                 attrT = "NULL";
104             }
105             if (getAttrib_varchar() != null) {
106                 attrVC = getAttrib_varchar();
107                 attrVC = attrVC.replaceAll("'","\\\\'");
108             } else {
109                 attrVC = "NULL";
110             }
111             if (getAttrib_datetime() != null) {
112                 //convert clone from remote time to local time
113
Calendar JavaDoc cal = (Calendar JavaDoc)getAttrib_datetime().clone();
114                 Calendar JavaDoc ngc = new GregorianCalendar JavaDoc();
115                 cal.add(Calendar.MILLISECOND, (ngc.get(Calendar.ZONE_OFFSET) - getAttrib_datetime().get(Calendar.ZONE_OFFSET)) - (ngc.get(Calendar.DST_OFFSET) + getAttrib_datetime().get(Calendar.DST_OFFSET)) );
116                 attrDT = "" + cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
117             } else {
118                 attrDT = "NULL";
119             }
120
121             stmt = DBM.con.createStatement();
122             stmt.executeUpdate("INSERT INTO xinco_add_attribute VALUES (" + getXinco_core_data_id() + ", " + getAttribute_id() + ", " + getAttrib_int() + ", " + getAttrib_unsignedint() + ", " + getAttrib_double() + ", '" + attrVC + "', '" + attrT + "', '" + attrDT + "')");
123             stmt.close();
124             
125         } catch (Exception JavaDoc e) {
126             //no commit or rollback -> CoreData manages exceptions!
127
throw new XincoException();
128         }
129         
130         return 1;
131         
132     }
133     
134     //create complete list of add attributes
135
public static Vector JavaDoc getXincoAddAttributes(int attrID, XincoDBManager DBM) {
136         
137         Vector JavaDoc addAttributes = new Vector JavaDoc();
138         
139         try {
140             Statement stmt = DBM.con.createStatement();
141             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_add_attribute WHERE xinco_core_data_id =" + attrID + " ORDER BY attribute_id");
142
143             GregorianCalendar JavaDoc cal;
144             while (rs.next()) {
145                 cal = new GregorianCalendar JavaDoc();
146                 if (rs.getDate("attrib_datetime") != null) {
147                     cal.setTime(rs.getDate("attrib_datetime"));
148                 }
149                 addAttributes.addElement(new XincoAddAttributeServer(rs.getInt("xinco_core_data_id"), rs.getInt("attribute_id"), rs.getInt("attrib_int"), rs.getLong("attrib_unsignedint"), rs.getDouble("attrib_double"), rs.getString("attrib_varchar"), rs.getString("attrib_text"), cal));
150             }
151
152             stmt.close();
153         } catch (Exception JavaDoc e) {
154             addAttributes.removeAllElements();
155         }
156
157         return addAttributes;
158     }
159
160 }
161
Popular Tags