KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MProject


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.sql.*;
17 import java.util.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Project Model
23  *
24  * @author Jorg Janke
25  * @version $Id: MProject.java,v 1.7 2003/11/06 07:08:05 jjanke Exp $
26  */

27 public class MProject extends X_C_Project
28 {
29     public MProject (Properties ctx, int C_Project_ID)
30     {
31         super (ctx, C_Project_ID);
32         if (C_Project_ID == 0)
33         {
34         // setC_Project_ID(0);
35
// setValue (null);
36
// setC_Currency_ID (0);
37
setCommittedAmt (Env.ZERO);
38             setCommittedQty (Env.ZERO);
39             setInvoicedAmt (Env.ZERO);
40             setInvoicedQty (Env.ZERO);
41             setPlannedAmt (Env.ZERO);
42             setPlannedMarginAmt (Env.ZERO);
43             setPlannedQty (Env.ZERO);
44             setProjectBalanceAmt (Env.ZERO);
45         // setProjectCategory(PROJECTCATEGORY_General);
46
setIsCommitCeiling (false);
47             setIsCommitment (false);
48             setIsSummary (false);
49             setProcessed (false);
50         }
51     } // MProject
52

53     public MProject (Properties ctx, ResultSet rs)
54     {
55         super (ctx, rs);
56     } // MProject
57

58     /** Cached PL */
59     private int m_M_PriceList_ID = 0;
60
61     /**
62      * Get Project Type as Int (is Button).
63      * @return C_ProjectType_ID id
64      */

65     public int getC_ProjectType_IDasInt()
66     {
67         String JavaDoc pj = super.getC_ProjectType_ID();
68         if (pj == null)
69             return 0;
70         int C_ProjectType_ID = 0;
71         try
72         {
73             C_ProjectType_ID = Integer.parseInt (pj);
74         }
75         catch (Exception JavaDoc ex)
76         {
77             log.error("getC_ProjectType_IDasInt - " + pj, ex);
78         }
79         return C_ProjectType_ID;
80     } // setC_ProjectType_ID
81

82     /**
83      * Set Project Type (overwrite r/o)
84      * @param C_ProjectType_ID id
85      */

86     public void setC_ProjectType_ID (int C_ProjectType_ID)
87     {
88         if (C_ProjectType_ID == 0)
89             super.setC_ProjectType_ID (null);
90         else
91             super.setC_ProjectType_ID (String.valueOf(C_ProjectType_ID));
92     } // setC_ProjectType_ID
93

94     /**
95      * String Representation
96      * @return info
97      */

98     public String JavaDoc toString()
99     {
100         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MProject[").append(getID())
101             .append("-").append(getValue()).append(",ProjectCategory=").append(getProjectCategory())
102             .append("]");
103         return sb.toString();
104     } // toString
105

106     /**
107      * Get Price List from Price List Version
108      * @return price list or 0
109      */

110     public int getM_PriceList_ID()
111     {
112         if (getM_PriceList_Version_ID() == 0)
113             return 0;
114         if (m_M_PriceList_ID > 0)
115             return m_M_PriceList_ID;
116         //
117
String JavaDoc sql = "SELECT M_PriceList_ID FROM M_PriceList_Version WHERE M_PriceList_Version_ID=?";
118         m_M_PriceList_ID = DB.getSQLValue(sql, getM_PriceList_Version_ID());
119         return m_M_PriceList_ID;
120     } // getM_PriceList_ID
121

122     /**
123      * Set PL Version
124      * @param M_PriceList_Version_ID id
125      */

126     public void setM_PriceList_Version_ID (int M_PriceList_Version_ID)
127     {
128         super.setM_PriceList_Version_ID(M_PriceList_Version_ID);
129         m_M_PriceList_ID = 0; // reset
130
} // setM_PriceList_Version_ID
131

132
133     /*************************************************************************/
134
135     /**
136      * Get Project Lines
137      * @return Array of lines
138      */

139     public MProjectLine[] getLines()
140     {
141         ArrayList list = new ArrayList();
142         String JavaDoc sql = "SELECT * FROM C_ProjectLine WHERE C_Project_ID=? ORDER BY Line";
143         PreparedStatement pstmt = null;
144         try
145         {
146             pstmt = DB.prepareStatement(sql);
147             pstmt.setInt(1, getC_Project_ID());
148             ResultSet rs = pstmt.executeQuery();
149             while (rs.next())
150                 list.add(new MProjectLine (getCtx(), rs));
151             rs.close();
152             pstmt.close();
153             pstmt = null;
154         }
155         catch (SQLException ex)
156         {
157             log.error("getLines", ex);
158         }
159         try
160         {
161             if (pstmt != null)
162                 pstmt.close();
163         }
164         catch (SQLException ex1)
165         {
166         }
167         pstmt = null;
168         //
169
MProjectLine[] retValue = new MProjectLine[list.size()];
170         list.toArray(retValue);
171         return retValue;
172     } // getLines
173

174     /**
175      * Get Project Issues
176      * @return Array of issues
177      */

178     public MProjectIssue[] getIssues()
179     {
180         ArrayList list = new ArrayList();
181         String JavaDoc sql = "SELECT * FROM C_ProjectIssue WHERE C_Project_ID=? ORDER BY Line";
182         PreparedStatement pstmt = null;
183         try
184         {
185             pstmt = DB.prepareStatement(sql);
186             pstmt.setInt(1, getC_Project_ID());
187             ResultSet rs = pstmt.executeQuery();
188             while (rs.next())
189                 list.add(new MProjectIssue (getCtx(), rs));
190             rs.close();
191             pstmt.close();
192             pstmt = null;
193         }
194         catch (SQLException ex)
195         {
196             log.error("getIssues", ex);
197         }
198         try
199         {
200             if (pstmt != null)
201                 pstmt.close();
202         }
203         catch (SQLException ex1)
204         {
205         }
206         pstmt = null;
207         //
208
MProjectIssue[] retValue = new MProjectIssue[list.size()];
209         list.toArray(retValue);
210         return retValue;
211     } // getIssues
212

213     /**
214      * Get Project Phases
215      * @return Array of phases
216      */

217     public MProjectPhase[] getPhases()
218     {
219         ArrayList list = new ArrayList();
220         String JavaDoc sql = "SELECT * FROM C_ProjectPhase WHERE C_Project_ID=? ORDER BY SeqNo";
221         PreparedStatement pstmt = null;
222         try
223         {
224             pstmt = DB.prepareStatement(sql);
225             pstmt.setInt(1, getC_Project_ID());
226             ResultSet rs = pstmt.executeQuery();
227             while (rs.next())
228                 list.add(new MProjectPhase (getCtx(), rs));
229             rs.close();
230             pstmt.close();
231             pstmt = null;
232         }
233         catch (SQLException ex)
234         {
235             log.error("getPhases", ex);
236         }
237         try
238         {
239             if (pstmt != null)
240                 pstmt.close();
241         }
242         catch (SQLException ex1)
243         {
244         }
245         pstmt = null;
246         //
247
MProjectPhase[] retValue = new MProjectPhase[list.size()];
248         list.toArray(retValue);
249         return retValue;
250     } // getPhases
251

252     /*************************************************************************/
253
254     /**
255      * Copy Lines/Phase/Task from other Project
256      * @param project project
257      * @return number of total lines copied
258      */

259     public int copyDetailsFrom (MProject project)
260     {
261         if (isProcessed() || project == null)
262             return 0;
263         int count = copyLinesFrom(project)
264             + copyPhasesFrom(project);
265         return count;
266     } // copyDetailsFrom
267

268     /**
269      * Copy Lines From other Project
270      * @param project project
271      * @return number of lines copied
272      */

273     public int copyLinesFrom (MProject project)
274     {
275         if (isProcessed() || project == null)
276             return 0;
277         int count = 0;
278         MProjectLine[] fromLines = project.getLines();
279         for (int i = 0; i < fromLines.length; i++)
280         {
281             MProjectLine line = new MProjectLine (getCtx(), 0);
282             PO.copyValues(fromLines[i], line, getAD_Client_ID(), getAD_Org_ID());
283             line.setC_Project_ID(getC_Project_ID());
284             line.setInvoicedAmt(Env.ZERO);
285             line.setInvoicedQty(Env.ZERO);
286             line.setC_OrderPO_ID(0);
287             line.setC_Order_ID(0);
288             if (line.save())
289                 count++;
290         }
291         if (fromLines.length != count)
292             log.error("copyLinesFrom - Lines difference - Project=" + fromLines.length + " <> Saved=" + count);
293
294         return count;
295     } // copyLinesFrom
296

297     /**
298      * Copy Phases/Tasks from other Project
299      * @param fromProject project
300      * @return number of items copied
301      */

302     public int copyPhasesFrom (MProject fromProject)
303     {
304         if (isProcessed() || fromProject == null)
305             return 0;
306         int count = 0;
307         int taskCount = 0;
308         // Get Phases
309
MProjectPhase[] myPhases = getPhases();
310         MProjectPhase[] fromPhases = fromProject.getPhases();
311         // Copy Phases
312
for (int i = 0; i < fromPhases.length; i++)
313         {
314             // Check if Phase already exists
315
int C_Phase_ID = fromPhases[i].getC_Phase_ID();
316             boolean exists = false;
317             if (C_Phase_ID == 0)
318                 exists = false;
319             else
320             {
321                 for (int ii = 0; ii < myPhases.length; ii++)
322                 {
323                     if (myPhases[ii].getC_Phase_ID() == C_Phase_ID)
324                     {
325                         exists = true;
326                         break;
327                     }
328                 }
329             }
330             // Phase exist
331
if (exists)
332                 log.info("copyPhasesFrom - Phase already exists here, ignored - " + fromPhases[i]);
333             else
334             {
335                 MProjectPhase toPhase = new MProjectPhase (getCtx (), 0);
336                 PO.copyValues (fromPhases[i], toPhase, getAD_Client_ID (), getAD_Org_ID ());
337                 toPhase.setC_Project_ID (getC_Project_ID ());
338                 toPhase.setC_Order_ID (0);
339                 toPhase.setIsComplete (false);
340                 if (toPhase.save ())
341                 {
342                     count++;
343                     taskCount += toPhase.copyTasksFrom (fromPhases[i]);
344                 }
345             }
346         }
347         if (fromPhases.length != count)
348             log.warn("copyPhasesFrom - Count difference - Project=" + fromPhases.length + " <> Saved=" + count);
349
350         return count + taskCount;
351     } // copyPhasesFrom
352

353
354     /**
355      * Set Project Type and Category.
356      * If Service Project copy Projet Type Phase/Tasks
357      * @param type project type
358      */

359     public void setProjectType (MProjectType type)
360     {
361         if (type == null)
362             return;
363         setC_ProjectType_ID(type.getC_ProjectType_ID());
364         setProjectCategory(type.getProjectCategory());
365         if (PROJECTCATEGORY_ServiceChargeProject.equals(getProjectCategory()))
366             copyPhasesFrom(type);
367     } // setProjectType
368

369     /**
370      *
371      * @param type Project Type
372      * @return count
373      */

374     public int copyPhasesFrom (MProjectType type)
375     {
376         // create phases
377
int count = 0;
378         int taskCount = 0;
379         MProjectTypePhase[] typePhases = type.getPhases();
380         for (int i = 0; i < typePhases.length; i++)
381         {
382             MProjectPhase toPhase = new MProjectPhase (getCtx(), getC_Project_ID(), typePhases[i]);
383             if (toPhase.save())
384             {
385                 count++;
386                 taskCount += toPhase.copyTasksFrom(typePhases[i]);
387             }
388         }
389         if (typePhases.length != count)
390             log.error("copyPhasesFrom - Count difference - Type=" + typePhases.length + " <> Saved=" + count);
391         return count;
392     } // copyPhasesFrom
393

394     /*************************************************************************/
395
396     /**
397      * Create new Project by copying
398      * @param ctx context
399      * @param C_Project_ID project
400      * @param dateDoc date of the document date
401      * @return Project
402      */

403     public static MProject copyFrom (Properties ctx, int C_Project_ID, Timestamp dateDoc)
404     {
405         MProject from = new MProject (ctx, C_Project_ID);
406         if (from.getC_Project_ID() == 0)
407             throw new IllegalArgumentException JavaDoc ("From Project not found C_Project_ID=" + C_Project_ID);
408         //
409
MProject to = new MProject (ctx, 0);
410         PO.copyValues(from, to, from.getAD_Client_ID(), from.getAD_Org_ID());
411         to.setC_Project_ID(0);
412         // Set Value with Time
413
String JavaDoc Value = to.getValue() + " ";
414         String JavaDoc Time = dateDoc.toString();
415         int length = Value.length() + Time.length();
416         if (length <= 40)
417             Value += Time;
418         else
419             Value += Time.substring (length-40);
420         to.setValue(Value);
421         to.setInvoicedAmt(Env.ZERO);
422         to.setProjectBalanceAmt(Env.ZERO);
423         to.setProcessed(false);
424         //
425
if (!to.save())
426             throw new IllegalStateException JavaDoc("Could not create Project");
427
428         if (to.copyDetailsFrom(from) == 0)
429             throw new IllegalStateException JavaDoc("Could not create Project Details");
430
431         return to;
432     } // copyFrom
433

434 } // MProject
435
Popular Tags