KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > Shop


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.hipergate;
34
35 import com.knowgate.debug.DebugFile;
36 import com.knowgate.jdc.JDCConnection;
37 import com.knowgate.acl.ACL;
38 import com.knowgate.acl.ACLDomain;
39 import com.knowgate.dataobjs.DB;
40 import com.knowgate.dataobjs.DBBind;
41 import com.knowgate.dataobjs.DBPersist;
42 import com.knowgate.misc.Gadgets;
43
44 import java.sql.SQLException JavaDoc;
45 import java.sql.Statement JavaDoc;
46
47 import java.io.IOException JavaDoc;
48
49 /**
50  * @author Sergio Montoro
51  * @version 2.2
52  */

53
54 public class Shop extends DBPersist {
55   public Shop() {
56      super(DB.k_shops, "Shop");
57   }
58
59   public Shop(JDCConnection oConn, String JavaDoc sShopId) throws SQLException JavaDoc {
60      super(DB.k_shops, "Shop");
61
62      load (oConn, new Object JavaDoc[]{sShopId});
63   }
64
65   //----------------------------------------------------------------------------
66

67   /**
68    * Delete Shop including all its orders, categories and products.
69    * @param oConn Database Connection
70    * @throws SQLException
71    */

72   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
73
74     Statement JavaDoc oStmt;
75     String JavaDoc sRootCat;
76     boolean bRetVal;
77     String JavaDoc sSQL;
78
79     if (DebugFile.trace) {
80       DebugFile.writeln("Begin Shop.delete([Connection], " + getStringNull(DB.gu_root_cat, "null") + ")");
81       DebugFile.incIdent();
82     }
83
84     oStmt = oConn.createStatement();
85
86     if (DBBind.exists(oConn, DB.k_returned_invoices, "U")) {
87       sSQL = "DELETE FROM " + DB.k_invoice_lines + " WHERE " + DB.gu_invoice + " IN (SELECT "+DB.gu_returned+" FROM " + DB.k_returned_invoices + " WHERE " + DB.gu_shop + "='" + getString(DB.gu_shop) + "')";
88       if (DebugFile.trace) DebugFile.writeln("Statement.execute(" + sSQL + ")");
89       oStmt.executeUpdate(sSQL);
90       sSQL = "DELETE FROM " + DB.k_returned_invoices + " WHERE " + DB.gu_shop + "='" + getString(DB.gu_shop) + "'";
91       if (DebugFile.trace) DebugFile.writeln("Statement.execute(" + sSQL + ")");
92       oStmt.executeUpdate(sSQL);
93     }
94
95     if (DBBind.exists(oConn, DB.k_invoices, "U")) {
96       sSQL = "DELETE FROM " + DB.k_invoice_lines + " WHERE " + DB.gu_invoice + " IN (SELECT "+DB.gu_invoice+" FROM " + DB.k_invoices + " WHERE " + DB.gu_shop + "='" + getString(DB.gu_shop) + "')";
97       if (DebugFile.trace) DebugFile.writeln("Statement.execute(" + sSQL + ")");
98       oStmt.executeUpdate(sSQL);
99       sSQL = "DELETE FROM " + DB.k_invoices + " WHERE " + DB.gu_shop + "='" + getString(DB.gu_shop) + "'";
100       if (DebugFile.trace) DebugFile.writeln("Statement.execute(" + sSQL + ")");
101       oStmt.executeUpdate(sSQL);
102     }
103
104     sSQL = "DELETE FROM " + DB.k_order_lines + " WHERE " + DB.gu_order + " IN (SELECT "+DB.gu_order+" FROM " + DB.k_orders + " WHERE " + DB.gu_shop + "='" + getString(DB.gu_shop) + "')";
105     if (DebugFile.trace) DebugFile.writeln("Statement.execute(" + sSQL + ")");
106     oStmt.executeUpdate(sSQL);
107
108     sSQL = "DELETE FROM " + DB.k_orders + " WHERE " + DB.gu_shop + "='" + getString(DB.gu_shop) + "'";
109     if (DebugFile.trace) DebugFile.writeln("Statement.execute(" + sSQL + ")");
110     oStmt.executeUpdate(sSQL);
111
112     oStmt.close();
113
114     sRootCat = getString(DB.gu_root_cat);
115
116     bRetVal = super.delete(oConn);
117
118     if (bRetVal)
119       try {
120         bRetVal = Category.delete(oConn, sRootCat);
121       } catch (IOException JavaDoc ioe) {
122         bRetVal = false;
123         throw new SQLException JavaDoc ("IOException " + ioe.getMessage());
124       }
125     if (DebugFile.trace) {
126       DebugFile.writeln("End Shop.delete() : " + String.valueOf(bRetVal));
127       DebugFile.incIdent();
128     }
129
130     return bRetVal;
131   }
132
133   //----------------------------------------------------------------------------
134

135    public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
136
137      if (!AllVals.containsKey(DB.gu_shop))
138       put(DB.gu_shop, Gadgets.generateUUID());
139
140     if (!AllVals.containsKey(DB.bo_active))
141       put(DB.bo_active, (short)1);
142
143      return super.store(oConn);
144    } // store
145

146    //----------------------------------------------------------------------------
147

148     public boolean store(JDCConnection oConn, String JavaDoc sParentCategoryId) throws SQLException JavaDoc {
149       boolean bRetVal;
150       Category oRootCat;
151       ACLDomain oDomain;
152
153       if (DebugFile.trace) {
154         DebugFile.writeln("Begin Shop.store([Connection], " + sParentCategoryId + ")");
155         DebugFile.incIdent();
156       }
157
158       if (sParentCategoryId==null)
159         throw new java.lang.IllegalArgumentException JavaDoc("Parent category Identifier cannot be null");
160
161       if (sParentCategoryId.length()==0)
162         throw new java.lang.IllegalArgumentException JavaDoc("Parent category Identifier cannot be empty");
163
164       if (!AllVals.containsKey(DB.gu_shop))
165         put(DB.gu_shop, Gadgets.generateUUID());
166
167       if (!AllVals.containsKey(DB.bo_active))
168         put(DB.bo_active, (short)1);
169
170       if (!AllVals.containsKey(DB.gu_root_cat)) {
171         if (DebugFile.trace) DebugFile.writeln("creating shops category for domain " + String.valueOf(getInt(DB.id_domain)));
172
173         oDomain = new ACLDomain(oConn, getInt(DB.id_domain));
174         oRootCat = new Category(Category.store(oConn, null, sParentCategoryId, Category.makeName(oConn, getString(DB.nm_shop)), getShort(DB.bo_active), (short)0, oDomain.getString(DB.gu_owner), null, null));
175         oRootCat.setUserPermissions(oConn, oDomain.getString(DB.gu_owner), ACL.PERMISSION_FULL_CONTROL, (short)0, (short)0);
176         oRootCat.setGroupPermissions(oConn, oDomain.getString(DB.gu_admins), ACL.PERMISSION_FULL_CONTROL, (short)0, (short)0);
177
178         if (sAuditUsr.length()>0 && !sAuditUsr.equals(oDomain.getString(DB.gu_owner))) {
179           oRootCat.setUserPermissions(oConn, sAuditUsr, ACL.PERMISSION_MODIFY, (short)0, (short)0);
180         }
181
182         put(DB.gu_root_cat, oRootCat.getString(DB.gu_category));
183       }
184       else {
185         oRootCat = new Category(getString(DB.gu_root_cat));
186         if (!oRootCat.isChildOf(oConn, sParentCategoryId));
187           throw new IllegalArgumentException JavaDoc("Root Category is not a child of specified Parent Category");
188       }
189
190       if (DebugFile.trace) {
191         DebugFile.decIdent();
192         DebugFile.writeln("End Shop.store()");
193       }
194
195       return super.store(oConn);
196     } // store
197

198     // ----------------------------------------------------------
199

200     public void expandCategories(JDCConnection oConn) throws SQLException JavaDoc {
201       Category oRootCat = new Category(DB.gu_root_cat);
202       oRootCat.expand(oConn);
203     } // expandCategories
204

205    // **********************************************************
206
// Constantes Publicas
207

208    public static final short ClassId = 40;
209
210 }
211
Popular Tags