KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Recurring Model
23  *
24  * @author Jorg Janke
25  * @version $Id: MRecurring.java,v 1.2 2003/08/12 17:58:17 jjanke Exp $
26  */

27 public class MRecurring extends X_C_Recurring
28 {
29     public MRecurring (Properties ctx, int C_Recurring_ID)
30     {
31         super (ctx, C_Recurring_ID);
32         if (C_Recurring_ID == 0)
33         {
34         // setC_Recurring_ID (0); // PK
35
setDateNextRun (new Timestamp(System.currentTimeMillis()));
36             setFrequencyType (FREQUENCYTYPE_Monthly);
37             setFrequency(1);
38         // setName (null);
39
// setRecurringType (null);
40
setRunsMax (1);
41             setRunsRemaining (0);
42         }
43     } // MRecurring
44

45     public MRecurring (Properties ctx, ResultSet rs)
46     {
47         super (ctx, rs);
48     } // MRecurring
49

50     /**
51      * String representation
52      * @return info
53      */

54     public String JavaDoc toString()
55     {
56         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MRecurring[")
57             .append(getID()).append(",").append(getName());
58         if (getRecurringType().equals(MRecurring.RECURRINGTYPE_Order))
59             sb.append(",C_Order_ID=").append(getC_Order_ID());
60         else if (getRecurringType().equals(MRecurring.RECURRINGTYPE_Invoice))
61             sb.append(",C_Invoice_ID=").append(getC_Invoice_ID());
62         else if (getRecurringType().equals(MRecurring.RECURRINGTYPE_Project))
63             sb.append(",C_Project_ID=").append(getC_Project_ID());
64         else if (getRecurringType().equals(MRecurring.RECURRINGTYPE_GLJournal))
65             sb.append(",GL_JournalBatch_ID=").append(getGL_JournalBatch_ID());
66         sb.append(",Fequency=").append(getFrequencyType()).append("*").append(getFrequency());
67         sb.append("]");
68         return sb.toString();
69     } // toString
70

71     /*************************************************************************/
72
73     /**
74      * Execute Run.
75      * @return clear text info
76      */

77     public String JavaDoc executeRun()
78     {
79         Timestamp dateDoc = getDateNextRun();
80         if (!calculateRuns())
81             throw new IllegalStateException JavaDoc ("No Runs Left");
82
83         // log
84
MRecurringRun run = new MRecurringRun (getCtx(), this);
85         String JavaDoc msg = "@Created@ ";
86
87
88         // Copy
89
if (getRecurringType().equals(MRecurring.RECURRINGTYPE_Order))
90         {
91             MOrder order = MOrder.copyFrom (getCtx(), getC_Order_ID(), dateDoc);
92             run.setC_Order_ID(order.getC_Order_ID());
93             msg += order.getDocumentNo();
94         }
95         else if (getRecurringType().equals(MRecurring.RECURRINGTYPE_Invoice))
96         {
97             MInvoice invoice = MInvoice.copyFrom (getCtx(), getC_Invoice_ID(), dateDoc);
98             run.setC_Invoice_ID(invoice.getC_Invoice_ID());
99             msg += invoice.getDocumentNo();
100         }
101         else if (getRecurringType().equals(MRecurring.RECURRINGTYPE_Project))
102         {
103             MProject project = MProject.copyFrom (getCtx(), getC_Project_ID(), dateDoc);
104             run.setC_Project_ID(project.getC_Project_ID());
105             msg += project.getValue();
106         }
107         else if (getRecurringType().equals(MRecurring.RECURRINGTYPE_GLJournal))
108         {
109             MJournalBatch journal = MJournalBatch.copyFrom (getCtx(), getGL_JournalBatch_ID(), dateDoc);
110             run.setGL_JournalBatch_ID(journal.getGL_JournalBatch_ID());
111             msg += journal.getDocumentNo();
112         }
113         else
114             return "Invalid @RecurringType@ = " + getRecurringType();
115         run.save();
116
117         //
118
setDateLastRun (run.getUpdated());
119         setRunsRemaining (getRunsRemaining()-1);
120         setDateNextRun();
121         save();
122         return msg;
123     } // execureRun
124

125     /**
126      * Calculate & set remaining Runs
127      * @return true if runs left
128      */

129     private boolean calculateRuns()
130     {
131         String JavaDoc sql = "SELECT COUNT(*) FROM C_Recurring_Run WHERE C_Recurring_ID=?";
132         int current = DB.getSQLValue(sql, getC_Recurring_ID());
133         int remaining = getRunsMax() - current;
134         setRunsRemaining(remaining);
135         save();
136         return remaining > 0;
137     } // calculateRuns
138

139     /**
140      * Calculate next run date
141      */

142     private void setDateNextRun()
143     {
144         if (getFrequency() < 1)
145             setFrequency(1);
146         int frequency = getFrequency();
147         Calendar cal = Calendar.getInstance();
148         cal.setTime(getDateNextRun());
149         //
150
if (getFrequencyType().equals(FREQUENCYTYPE_Daily))
151             cal.add(Calendar.DAY_OF_YEAR, frequency);
152         else if (getFrequencyType().equals(FREQUENCYTYPE_Weekly))
153             cal.add(Calendar.WEEK_OF_YEAR, frequency);
154         else if (getFrequencyType().equals(FREQUENCYTYPE_Monthly))
155             cal.add(Calendar.MONTH, frequency);
156         else if (getFrequencyType().equals(FREQUENCYTYPE_Quarterly))
157             cal.add(Calendar.MONTH, 3*frequency);
158         Timestamp next = new Timestamp (cal.getTimeInMillis());
159         setDateNextRun(next);
160     } // setDateNextRun
161

162 } // MRecurring
163
Popular Tags