KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > projtrack > Project


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.projtrack;
34
35 import java.util.Stack JavaDoc;
36 import java.util.HashMap JavaDoc;
37
38 import com.knowgate.debug.DebugFile;
39
40 import com.knowgate.dataobjs.DB;
41 import com.knowgate.dataobjs.DBBind;
42 import com.knowgate.dataobjs.DBPersist;
43 import com.knowgate.dataobjs.DBSubset;
44
45 import com.knowgate.misc.Gadgets;
46 import com.knowgate.jdc.JDCConnection;
47
48 import java.sql.Connection JavaDoc;
49 import java.sql.SQLException JavaDoc;
50 import java.sql.CallableStatement JavaDoc;
51 import java.sql.Statement JavaDoc;
52 import java.sql.PreparedStatement JavaDoc;
53 import java.sql.ResultSet JavaDoc;
54
55 /**
56  * <p>Project</p>
57  * @author Sergio Montoro Ten
58  * @version 3.0
59  */

60 public class Project extends DBPersist {
61
62   /**
63    * Create empty an Project
64    */

65   public Project() {
66     super(DB.k_projects, "Project");
67   }
68
69   /**
70    * Create empty an Project and set gu_project.
71    * No data is loaded from database.
72    * @param sPrjId Project Unique Identifier.
73    */

74   public Project(String JavaDoc sPrjId) {
75     super(DB.k_projects, "Project");
76
77     put(DB.gu_project, sPrjId);
78   }
79
80   public Project(JDCConnection oConn, String JavaDoc sPrjId) throws SQLException JavaDoc {
81     super(DB.k_projects, "Project");
82
83     put (DB.gu_project, sPrjId);
84
85     load (oConn, new Object JavaDoc[]{sPrjId});
86   }
87
88   // ----------------------------------------------------------
89

90   /**
91    * Get Project Top Parent.
92    * Browse recursively k_projects table until finding the top most parent for Project.
93    * @param oConn Database Connection
94    * @return GUID of top most parent Project or <b>null</b> if this is a top Project.
95    * @throws SQLException
96    */

97   public String JavaDoc topParent(JDCConnection oConn) throws SQLException JavaDoc {
98     String JavaDoc sCurrent;
99     String JavaDoc sParent;
100     PreparedStatement JavaDoc oStmt;
101     ResultSet JavaDoc oRSet;
102
103     if (DebugFile.trace) {
104       DebugFile.writeln("Begin Project.topParent([Connection])");
105       DebugFile.incIdent();
106       DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.id_parent + " FROM " + DB.k_projects + " WHERE " + DB.gu_project + "='" + getStringNull(DB.gu_project, "null") + "')");
107     }
108
109     oStmt = oConn.prepareStatement("SELECT " + DB.id_parent + " FROM " + DB.k_projects + " WHERE " + DB.gu_project + "=?");
110
111     sParent = getString(DB.gu_project);
112     do {
113       sCurrent = sParent;
114
115       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setString(1, " + sCurrent + ")");
116
117       oStmt.setString(1, sCurrent);
118       oRSet = oStmt.executeQuery();
119       if (oRSet.next())
120         sParent = oRSet.getString(1);
121       else
122         sParent = null;
123       oRSet.close();
124     } while (sParent!=null);
125
126     oStmt.close();
127
128     if (DebugFile.trace) {
129       DebugFile.decIdent();
130       DebugFile.writeln("End Project.topParent() : " + sCurrent);
131     }
132
133     return sCurrent;
134   } // topParent();
135

136   // ----------------------------------------------------------
137

138   /**
139    * <P>Clone Project</P>
140    * When a project is cloned all its subprojects, duites and bugs are also cloned.
141    * @param oConn Database Connection
142    * @return GUID of new cloned Project
143    * @throws SQLException
144    * @throws IllegalAccessException
145    */

146   public String JavaDoc clone (JDCConnection oConn)
147     throws SQLException JavaDoc, IllegalAccessException JavaDoc {
148
149     if (DebugFile.trace) {
150       DebugFile.writeln("Begin Project.clone([Connection])");
151       DebugFile.incIdent();
152     }
153
154     Project oProj;
155     Duty oDuty = new Duty();
156     Bug oBug = new Bug();
157     HashMap JavaDoc oSubProjMap = new HashMap JavaDoc();
158     Object JavaDoc[] aChild = new Object JavaDoc[]{getString(DB.gu_project)};
159
160     oProj = new Project(oConn, getString(DB.gu_project));
161     oProj.replace(DB.gu_project, Gadgets.generateUUID());
162     oProj.store(oConn);
163
164     oSubProjMap.put(aChild[0], oProj.get(DB.gu_project));
165
166     DBSubset oChilds = new DBSubset(DB.k_projects, DB.gu_project, DB.id_parent + "=?", 10);
167     DBSubset oDuties = new DBSubset (DB.k_duties, oDuty.getTable(oConn).getColumnsStr(), DB.gu_project + "=?", 10);
168     DBSubset oBugs = new DBSubset (DB.k_bugs, oBug.getTable(oConn).getColumnsStr(), DB.gu_project + "=?", 10);
169
170     Stack JavaDoc oPending = new Stack JavaDoc();
171
172     int iChilds = oChilds.load(oConn, aChild);
173
174     if (DebugFile.trace) DebugFile.writeln(String.valueOf(iChilds) + " childs loaded for " + getString(DB.gu_project));
175
176     for (int c=0; c<iChilds;c++) oPending.push(oChilds.get(0,c));
177
178     while (!oPending.empty()) {
179       aChild[0] = oPending.pop();
180
181       iChilds = oChilds.load(oConn, aChild);
182
183       if (DebugFile.trace) DebugFile.writeln(String.valueOf(iChilds) + " childs loaded for " + aChild[0]);
184
185       oProj = new Project(oConn, (String JavaDoc) aChild[0]);
186       oProj.replace(DB.gu_project, Gadgets.generateUUID());
187       if (oSubProjMap.containsKey(oProj.get(DB.id_parent)))
188         oProj.replace(DB.id_parent, oSubProjMap.get(oProj.get(DB.id_parent)));
189       oProj.store(oConn);
190
191       int iDuties = oDuties.load (oConn, new Object JavaDoc[]{oProj.get(DB.gu_project)});
192
193       if (DebugFile.trace) DebugFile.writeln(String.valueOf(iDuties) + " duties loaded for " + oProj.getString(DB.gu_project));
194
195       for (int d=0; d<iDuties; d++) {
196         oDuties.setElementAt(Gadgets.generateUUID(), 0, d);
197         oDuties.setElementAt(oProj.get(DB.gu_project), 2, d);
198       }
199
200       try {
201         oDuties.store(oConn, oDuty.getClass(), true);
202       }
203       catch (java.lang.InstantiationException JavaDoc ignore) { /* never thrown*/ }
204
205       int iBugs = oBugs.load (oConn, new Object JavaDoc[]{oProj.get(DB.gu_project)});
206
207       if (DebugFile.trace) DebugFile.writeln(String.valueOf(iBugs) + " bugs loaded for " + oProj.getString(DB.gu_project));
208
209       for (int b=0; b<iBugs; b++) {
210         oBugs.setElementAt(Gadgets.generateUUID(), 0, b);
211         oBugs.setElementAt(oProj.get(DB.gu_project), 3, b);
212       }
213
214       try {
215         oBugs.store(oConn, oBug.getClass(), true);
216       }
217       catch (java.lang.InstantiationException JavaDoc ignore) { /* never thrown*/ }
218
219       oSubProjMap.put (aChild[0], oProj.getString(DB.gu_project));
220
221       for (int c=0; c<iChilds;c++)
222         oPending.push(oChilds.get(0,c));
223     } // wend
224

225     // Re-expandir todos los hijos del padre absoluto del clon
226
oProj = new Project((String JavaDoc) oSubProjMap.get(get(DB.gu_project)));
227     String JavaDoc sTopParent = oProj.topParent(oConn);
228
229     if (DebugFile.trace) DebugFile.writeln("topparent=" + (null!=sTopParent ? sTopParent : "null"));
230
231     if (null!=sTopParent)
232       oProj = new Project(sTopParent);
233
234     oProj.expand(oConn);
235
236     if (DebugFile.trace) {
237       DebugFile.decIdent();
238       DebugFile.writeln("End Project.topParent() : " + (String JavaDoc) oSubProjMap.get(get(DB.gu_project)));
239     }
240
241     return (String JavaDoc) oSubProjMap.get(get(DB.gu_project));
242   } // clone
243

244   // ----------------------------------------------------------
245

246   /**
247    * Load Project.
248    * If Project is assigned to a Company then Company Legal Name
249    * (k_companies.nm_legal) is loaded into property DB.tx_company.
250    * If Project is assigned to a Contact then Contact Full Name
251    * (tx_name+tx_surname) is loaded into property DB.tx_contact.
252    * @param oConn
253    * @param PKVals
254    * @return
255    * @throws SQLException
256    */

257   public boolean load(JDCConnection oConn, Object JavaDoc[] PKVals) throws SQLException JavaDoc {
258     boolean bRetVal = super.load(oConn, PKVals);
259     PreparedStatement JavaDoc oStmt;
260     ResultSet JavaDoc oRSet;
261
262     if (DebugFile.trace) {
263       DebugFile.writeln("Begin Project.load([Connection], Object[])");
264       DebugFile.incIdent();
265     }
266
267     if (bRetVal) {
268       if (!isNull(DB.gu_company) && DBBind.exists(oConn, DB.k_companies, "U")) {
269         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.nm_legal + " FROM " + DB.k_companies + " WHERE " + DB.gu_company + "='" + getStringNull(DB.gu_company, "null") + "'");
270
271         oStmt = oConn.prepareStatement("SELECT " + DB.nm_legal + " FROM " + DB.k_companies + " WHERE " + DB.gu_company + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
272         oStmt.setString(1, getString(DB.gu_company));
273         oRSet = oStmt.executeQuery();
274         if (oRSet.next())
275           replace(DB.tx_company, oRSet.getString(1));
276         else if (AllVals.containsKey(DB.tx_company))
277           remove(DB.tx_company);
278         oRSet.close();
279         oStmt.close();
280       } // fi (exists(k_companies))
281
else if (AllVals.containsKey(DB.tx_company))
282         remove(DB.tx_company);
283
284       if (!isNull(DB.gu_contact) && DBBind.exists(oConn, DB.k_contacts, "U")) {
285         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DBBind.Functions.ISNULL + "(" + DB.tx_name + ",'')" + DBBind.Functions.CONCAT + "' '" + DBBind.Functions.CONCAT + DBBind.Functions.ISNULL + "(" + DB.tx_surname + ",'') FROM " + DB.k_contacts + " WHERE " + DB.gu_contact + "='" + getStringNull(DB.gu_contact,"null") + "'");
286
287         oStmt = oConn.prepareStatement("SELECT " + DBBind.Functions.ISNULL + "(" + DB.tx_name + ",'')" + DBBind.Functions.CONCAT + "' '" + DBBind.Functions.CONCAT + DBBind.Functions.ISNULL + "(" + DB.tx_surname + ",'') FROM " + DB.k_contacts + " WHERE " + DB.gu_contact + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
288         oStmt.setString(1, getString(DB.gu_contact));
289         oRSet = oStmt.executeQuery();
290         if (oRSet.next())
291           replace(DB.tx_contact, oRSet.getString(1));
292         else if (AllVals.containsKey(DB.tx_contact))
293           remove(DB.tx_contact);
294         oRSet.close();
295         oStmt.close();
296       } // fi (exists(k_contacts))
297
else if (AllVals.containsKey(DB.tx_contact))
298         remove(DB.tx_contact);
299
300     } // fi (super.load)
301

302     if (DebugFile.trace) {
303       DebugFile.decIdent();
304       DebugFile.writeln("End Project.load() : " + String.valueOf(bRetVal));
305     }
306
307     return bRetVal;
308   } // load
309

310   // ----------------------------------------------------------
311

312   /**
313    * <p>Delete Project.</p>
314    * Calls k_sp_del_project stored procedure.<br>
315    * Deletion includes Project Childs, Duties and Bugs.
316    * @param oConn Database Connection
317    * @throws SQLException
318    */

319   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
320     return Project.delete(oConn, getString(DB.gu_project));
321   }
322
323   // ----------------------------------------------------------
324

325   /**
326    * Store Project
327    * If gu_project is null a new GUID is automatically assigned.<br>
328    * Calls internally to expand() method for re-expanding k_project_expand table.<br>
329    * @param oConn Database Connection
330    * @throws SQLException
331    */

332   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
333     boolean bRetVal;
334     String JavaDoc sTopParent;
335     Project oTopParent;
336     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
337
338     if (DebugFile.trace) {
339       DebugFile.writeln("Begin Project.store([Connection])");
340       DebugFile.incIdent();
341     }
342
343     if (!AllVals.containsKey(DB.gu_project))
344       put(DB.gu_project, Gadgets.generateUUID());
345
346     bRetVal = super.store(oConn);
347
348     // Re-expandir todos los hijos del padre absoluto de este proyecto
349
sTopParent = topParent(oConn);
350
351     if (DebugFile.trace) DebugFile.writeln("topparent=" + (null!=sTopParent ? sTopParent : "null"));
352
353     if (null!=sTopParent) {
354       oTopParent = new Project(sTopParent);
355       oTopParent.expand(oConn);
356       oTopParent = null;
357     } // fi (sTopParent)
358

359     if (DebugFile.trace) {
360       DebugFile.decIdent();
361       DebugFile.writeln("End Project.store() : " + String.valueOf(bRetVal));
362     }
363
364     return bRetVal;
365   } // store()
366

367   // ----------------------------------------------------------
368

369   /**
370    * <p>Compute total project cost</p>
371    * Total project cost is the sum of costs of all duties from the project<br>
372    * This method call stored procedure k_sp_prj_cost
373    * @param oConn Database Connection
374    * @return Sum of all duty costs for project
375    * @throws SQLException
376    * @throws NumberFormatException
377    */

378   public float cost (JDCConnection oConn) throws SQLException JavaDoc, NumberFormatException JavaDoc {
379     float fCost = 0;
380     Object JavaDoc oCost;
381     Statement JavaDoc oStmt;
382     ResultSet JavaDoc oRSet;
383     String JavaDoc sSQL;
384
385     if (DebugFile.trace) {
386       DebugFile.writeln("Begin Project.cost([Connection])");
387       DebugFile.incIdent();
388     }
389
390     switch (oConn.getDataBaseProduct()) {
391       case JDCConnection.DBMS_ORACLE:
392         sSQL = "SELECT k_sp_prj_cost ('" + getString(DB.gu_project) + "') FROM DUAL";
393         break;
394       case JDCConnection.DBMS_MSSQL:
395         String JavaDoc sSchema = oConn.getSchemaName();
396         if (null!=sSchema) {
397           if (sSchema.indexOf('\\')>0)
398             sSQL = "SELECT k_sp_prj_cost ('" + getString(DB.gu_project) + "')";
399           else
400             sSQL = "SELECT " + sSchema + ".k_sp_prj_cost ('" + getString(DB.gu_project) + "')";
401         } else {
402           sSQL = "SELECT k_sp_prj_cost ('" + getString(DB.gu_project) + "')";
403         }
404         break;
405       default:
406         sSQL = "SELECT k_sp_prj_cost ('" + getString(DB.gu_project) + "')";
407     }
408
409     oStmt = oConn.createStatement();
410
411     if (DebugFile.trace)
412       DebugFile.writeln("Statement.executeQuery(" + sSQL + ")");
413
414     oRSet = oStmt.executeQuery (sSQL);
415
416     oRSet.next();
417
418     oCost = oRSet.getObject(1);
419
420     oRSet.close();
421
422     oStmt.close();
423
424     fCost = Float.parseFloat(oCost.toString());
425
426     if (DebugFile.trace) {
427       DebugFile.decIdent();
428       DebugFile.writeln("End Project.store() : " + String.valueOf(fCost));
429     }
430
431     return fCost;
432   } // cost
433

434   // ----------------------------------------------------------
435

436   /**
437    * <p>Get all Project Childs as a DBSubset.</p>
438    * @param oConn Database Connection
439    * @return DBSubset with the following structure:<br>
440    * <table border=1 cellpadding=4>
441    * <tr><td><b>gu_project</b></td><td><b>nm_project</b></td><td><b>od_level</b></td><td><b>od_walk</b></td><td><b>id_parent</b></td></tr>
442    * <tr><td>Project GUID</td><td>Project Name</td><td>Depth Level</td><td>Walk order within level</td><td>Inmediate Parent</td></tr>
443    * </table>
444    * @throws SQLException
445    */

446   public DBSubset getAllChilds(JDCConnection oConn) throws SQLException JavaDoc {
447     if (DebugFile.trace) {
448       DebugFile.writeln("Begin Project.getAllChilds([Connection])");
449       DebugFile.incIdent();
450     }
451
452     DBSubset oTree = new DBSubset(DB.k_project_expand, DB.gu_project + "," + DB.nm_project + "," + DB.od_level + "," + DB.od_walk + "," + DB.gu_parent, DB.gu_rootprj + "='" + getString(DB.gu_project) + "' ORDER BY " + DB.od_walk, 50);
453
454     int iChildCount = oTree.load(oConn);
455
456     if (DebugFile.trace) {
457       for (int c=0; c<iChildCount; c++)
458         DebugFile.writeln(String.valueOf(oTree.getInt(3,c))+" lv="+String.valueOf(oTree.getInt(2,c))+",gu="+oTree.getString(0,c)+",nm="+oTree.getString(1,c));
459       DebugFile.decIdent();
460       DebugFile.writeln("End Project.getAllChilds() : "+String.valueOf(iChildCount));
461     }
462
463     return oTree;
464   }
465
466   // ----------------------------------------------------------
467

468   /**
469    * <p>Expand Project childs.</p>
470    * Calls k_sp_prj_expand stored procedure.<br>
471    * Expansion is stored at k_project_expand table.
472    * @param oConn Database Connection
473    * @throws SQLException
474    */

475   public void expand(JDCConnection oConn) throws SQLException JavaDoc {
476     CallableStatement JavaDoc oCall;
477     Statement JavaDoc oStmt;
478
479     if (DebugFile.trace) {
480       DebugFile.writeln("Begin Project.expand([Connection])");
481       DebugFile.incIdent();
482     }
483
484     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
485       if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery(SELECT k_sp_prj_expand ('" + getStringNull(DB.gu_project,"null") + "')");
486       oStmt = oConn.createStatement();
487       oStmt.executeQuery("SELECT k_sp_prj_expand ('" + getString(DB.gu_project) + "')");
488       oStmt.close();
489     } else {
490       if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_prj_expand ('" + getStringNull(DB.gu_project,"null") + "')}");
491       oCall = oConn.prepareCall("{ call k_sp_prj_expand ('" + getString(DB.gu_project) + "') }");
492       oCall.execute();
493       oCall.close();
494     }
495
496     if (DebugFile.trace) {
497       DebugFile.decIdent();
498       DebugFile.writeln("End Project.expand()");
499     }
500   } // expand()
501

502   // ----------------------------------------------------------
503

504   // **********************************************************
505
// Static Methods
506

507   /**
508    * <p>Delete Project.</p>
509    * Calls k_sp_del_project stored procedure.<br>
510    * Deletion includes Project Childs, Duties and Bugs.
511    * @param oConn Database Connection
512    * @param sProjectGUID GUID of project to be deleted.
513    * @throws NullPointerException if sProjectGUID is null
514    * @throws SQLException
515    */

516   public static boolean delete(JDCConnection oConn, String JavaDoc sProjectGUID)
517     throws SQLException JavaDoc,NullPointerException JavaDoc {
518     boolean bRetVal;
519
520     if (null==sProjectGUID)
521       throw new NullPointerException JavaDoc("Project.delete() GUID of project to be deleted may not be null");
522
523     if (DebugFile.trace) {
524       DebugFile.writeln("Begin Project.delete([Connection], " + sProjectGUID + ")");
525       DebugFile.incIdent();
526     }
527
528     String JavaDoc sTopParent = new Project(sProjectGUID).topParent(oConn);
529
530     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
531       if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery(SELECT k_sp_del_project ('" + sProjectGUID + "'))");
532       Statement JavaDoc oStmt = oConn.createStatement();
533       ResultSet JavaDoc oRSet = oStmt.executeQuery("SELECT k_sp_del_project ('" + sProjectGUID + "')");
534       oRSet.close();
535       oStmt.close();
536       bRetVal = true;
537     }
538     else {
539       if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_project ('" + sProjectGUID + "')})");
540       CallableStatement JavaDoc oCall = oConn.prepareCall("{ call k_sp_del_project ('" + sProjectGUID + "')}");
541       bRetVal = oCall.execute();
542       oCall.close();
543     }
544
545     if (DebugFile.trace) DebugFile.writeln("sTopParent="+sTopParent);
546
547     if (!sProjectGUID.equals(sTopParent)) new Project(sTopParent).expand(oConn);
548
549     if (DebugFile.trace) {
550       DebugFile.decIdent();
551       DebugFile.writeln("End Project.delete() : " + String.valueOf(bRetVal));
552     }
553
554     return bRetVal;
555   } // delete()
556

557   // **********************************************************
558
// Public Constants
559

560   public static final short ClassId = 80;
561
562 }
563
Popular Tags