KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > crm > Attachment


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.crm;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38
39 import com.knowgate.debug.DebugFile;
40 import com.knowgate.jdc.JDCConnection;
41 import com.knowgate.dataobjs.DB;
42 import com.knowgate.dataobjs.DBBind;
43 import com.knowgate.dataobjs.DBPersist;
44 import com.knowgate.hipergate.Product;
45
46 /**
47  * <p>Contact Attachment</p>
48  * <p>Copyright: Copyright (c) KnowGate 2003</p>
49  * @author Sergio Montoro Ten
50  * @version 1.0
51  */

52
53 public class Attachment extends DBPersist {
54
55   public Attachment() {
56     super(DB.k_contact_attachs, "Attachment");
57   }
58
59   // ----------------------------------------------------------
60

61   /**
62    * <p>Store Product as Contact Attachment.</p>
63    * <p>A Product object must have been stored at database prior to storing the
64    * Attachment object.<br>
65    * The link between Product and Attachment is done just by setting the
66    * gu_product field at Attachment object.<br>
67    * An Attachment progressive numeric identifier is automatically computed
68    * for field pg_product of table k_contact_attachs if one is not explicitly set.<br>
69    * Automatically generates dt_modified DATE if not explicitly set.<br></p>
70    * k_contact.nu_attachs fields is incremented by 1
71    * @param oConn Database Connection
72    * @throws SQLException
73    */

74   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
75     Statement JavaDoc oStmt;
76     ResultSet JavaDoc oRSet;
77     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
78     boolean bRetVal;
79
80     if (DebugFile.trace) {
81       DebugFile.writeln("Begin Attachment.store([Connection])");
82       DebugFile.incIdent();
83     }
84
85     replace(DB.dt_modified, dtNow);
86
87     if (!AllVals.containsKey(DB.pg_product)) {
88
89       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
90
91       if (DebugFile.trace)
92         DebugFile.writeln("Statement.executeQuery(SELECT " + DBBind.Functions.ISNULL + "(MAX(" + DB.pg_product + "),0)+1 FROM " + DB.k_contact_attachs + " WHERE " + DB.gu_contact + "='" + getStringNull(DB.gu_contact,"null") + "')");
93
94       oRSet = oStmt.executeQuery("SELECT " + DBBind.Functions.ISNULL + "(MAX(" + DB.pg_product + "),0)+1 FROM " + DB.k_contact_attachs + " WHERE " + DB.gu_contact + "='" + getString(DB.gu_contact) + "'");
95       oRSet.next();
96       put (DB.pg_product, oRSet.getObject(1));
97       oRSet.close();
98       oStmt.close();
99     }
100
101     bRetVal = super.store(oConn);
102
103     oStmt = oConn.createStatement();
104
105     if (DebugFile.trace)
106       DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_contacts + " SET " + DB.nu_attachs + "=" + DB.nu_attachs + "+1 WHERE gu_contact='" + getStringNull(DB.gu_contact,"null") + "')");
107
108     oStmt.executeUpdate("UPDATE " + DB.k_contacts + " SET " + DB.nu_attachs + "=" + DB.nu_attachs + "+1 WHERE gu_contact='" + getString(DB.gu_contact) + "'");
109     oStmt.close();
110
111     if (DebugFile.trace) {
112       DebugFile.decIdent();
113       DebugFile.writeln("End Attachment.store() : " + String.valueOf(getInt(DB.pg_product)));
114     }
115
116     return bRetVal;
117   } // store
118

119   // ----------------------------------------------------------
120

121   /**
122    * <p>Delete Attachment</p>
123    * <p>The associated Product and physical files are automatically deleted as well.<br></p>
124    * k_contact.nu_attachs fields is decremented by 1
125    * @param oConn Database Connection
126    * @throws SQLException
127    */

128   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
129     Statement JavaDoc oStmt;
130     boolean bRetVal;
131
132     Product oProd = new Product(oConn, getString(DB.gu_product));
133     bRetVal = oProd.delete(oConn);
134
135     if (bRetVal) bRetVal = super.delete(oConn);
136
137     oStmt = oConn.createStatement();
138     oStmt.executeUpdate("UPDATE " + DB.k_contacts + " SET " + DB.nu_attachs + "=" + DB.nu_attachs + "-1 WHERE gu_contact='" + getString(DB.gu_contact) + "'");
139     oStmt.close();
140
141     return bRetVal;
142   }
143
144   // **********************************************************
145
// Public Constants
146

147   public static final short ClassId = 94;
148 }
149
Popular Tags