KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > report > MReportLine


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.report;
15
16 import java.util.*;
17 import java.sql.*;
18 import java.math.*;
19 import java.io.Serializable JavaDoc;
20
21 import org.compiere.model.*;
22 import org.compiere.util.*;
23
24
25 /**
26  * Report Line Model
27  *
28  * @author Jorg Janke
29  * @version $Id: MReportLine.java,v 1.10 2003/10/31 05:28:35 jjanke Exp $
30  */

31 public class MReportLine extends X_PA_ReportLine
32 {
33     /**
34      * Constructor
35      * @param ctx context
36      * @param PA_ReportLine_ID id
37      */

38     public MReportLine (Properties ctx, int PA_ReportLine_ID)
39     {
40         super (ctx, PA_ReportLine_ID);
41         if (PA_ReportLine_ID == 0)
42         {
43             setSeqNo (0);
44             setIsSummary (false);
45             setIsPrinted (false);
46         }
47         else
48             loadSources();
49     } // MReportLine
50

51     /**
52      * Constructor
53      * @param ctx context
54      * @param rs ResultSet to load from
55      */

56     public MReportLine (Properties ctx, ResultSet rs)
57     {
58         super (ctx, rs);
59         loadSources();
60     } // MReportLine
61

62     /** Containt Sources */
63     private MReportSource[] m_sources = null;
64     /** Cache result */
65     private String JavaDoc m_whereClause = null;
66
67     /**
68      * Load contained Sources
69      */

70     private void loadSources()
71     {
72         ArrayList list = new ArrayList();
73         String JavaDoc sql = "SELECT * FROM PA_ReportSource WHERE PA_ReportLine_ID=? AND IsActive='Y'";
74         PreparedStatement pstmt = null;
75         try
76         {
77             pstmt = DB.prepareStatement(sql);
78             pstmt.setInt(1, getPA_ReportLine_ID());
79             ResultSet rs = pstmt.executeQuery();
80             while (rs.next())
81                 list.add(new MReportSource (getCtx(), rs));
82             rs.close();
83             pstmt.close();
84             pstmt = null;
85         }
86         catch (Exception JavaDoc e)
87         {
88             Log.error("MReportLine.loadSources", e);
89         }
90         finally
91         {
92             try
93             {
94                 if (pstmt != null)
95                     pstmt.close ();
96             }
97             catch (Exception JavaDoc e)
98             {}
99             pstmt = null;
100         }
101         //
102
m_sources = new MReportSource[list.size()];
103         list.toArray(m_sources);
104         Log.trace(9, "MReportLine.loadSources ID=" + getPA_ReportLine_ID(),
105           "Size=" + list.size());
106     } // loadSources
107

108     /**
109      * Get Sources
110      * @return sources
111      */

112     public MReportSource[] getSources()
113     {
114         return m_sources;
115     } // getSources
116

117     /**
118      * List Info
119      */

120     public void list()
121     {
122         System.out.println("- " + toString());
123         if (m_sources == null)
124             return;
125         for (int i = 0; i < m_sources.length; i++)
126             System.out.println(" - " + m_sources[i].toString());
127     } // list
128

129
130     /**
131      * Get Source Column Name
132      * @return Source ColumnName
133      */

134     public String JavaDoc getSourceColumnName()
135     {
136         String JavaDoc ColumnName = null;
137         for (int i = 0; i < m_sources.length; i++)
138         {
139             String JavaDoc col = AcctSchemaElement.getColumnName (m_sources[i].getElementType());
140             if (ColumnName == null || ColumnName.length() == 0)
141                 ColumnName = col;
142             else if (!ColumnName.equals(col))
143             {
144                 Log.trace(Log.l4_Data, "MReportLine.getSourceColumnName - more than one: " + ColumnName + " - " + col);
145                 return null;
146             }
147         }
148         return ColumnName;
149     } // getColumnName
150

151     /**
152      * Get Value Query for Segment Type
153      * @return Query for first source element or null
154      */

155     public String JavaDoc getSourceValueQuery()
156     {
157         if (m_sources != null && m_sources.length > 0)
158             return AcctSchemaElement.getValueQuery(m_sources[0].getElementType());
159         return null;
160     } //
161

162
163     /**
164      * Get SQL Select Clause.
165      * @param withSum with SUM() function
166      * @return select clause - AmtAcctCR+AmtAcctDR/etc or "null" if not defined
167      */

168     public String JavaDoc getSelectClause (boolean withSum)
169     {
170         String JavaDoc at = getAmountType().substring(0,1);
171         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
172         if (withSum)
173             sb.append("SUM(");
174         if (AmountType_Balance.equals(at))
175             sb.append("AmtAcctDr-AmtAcctCr");
176         else if (AmountType_CR.equals(at))
177             sb.append("AmtAcctCr");
178         else if (AmountType_DR.equals(at))
179             sb.append("AmtAcctDr");
180         else if (AmountType_Qty.equals(at))
181             sb.append("Qty");
182         else
183         {
184             Log.error ("MReportLine.getSelectClause - AmountType=" + getAmountType () + ", at=" + at);
185             return "NULL";
186         }
187         if (withSum)
188             sb.append(")");
189         return sb.toString();
190     } // getSelectClause
191

192     /**
193      * Is it Period Balance?
194      * @return true if Period Balance
195      */

196     public boolean isPeriodBalance()
197     {
198         String JavaDoc at = getAmountType();
199         if (at == null)
200             return false;
201         return AMOUNTTYPE_PeriodBalance.equals(at);
202     } // isPeriodBalance
203

204     /**
205      * Is it Year Balance?
206      * @return true if Year Balance
207      */

208     public boolean isYearBalance()
209     {
210         String JavaDoc at = getAmountType();
211         if (at == null)
212             return false;
213         return AMOUNTTYPE_YearBalance.equals(at);
214     } // isYearBalance
215

216     /**
217      * Is it Total Balance?
218      * @return true if Year Balance
219      */

220     public boolean isTotalBalance()
221     {
222         String JavaDoc at = getAmountType();
223         if (at == null)
224             return false;
225         return AMOUNTTYPE_TotalBalance.equals(at);
226     } // isTotalBalance
227

228     /**
229      * Get SQL where clause
230      * @return where clause
231      */

232     public String JavaDoc getWhereClause()
233     {
234         if (m_sources == null)
235             return "";
236         if (m_whereClause == null)
237         {
238             // Only one
239
if (m_sources.length == 0)
240                 m_whereClause = "";
241             else if (m_sources.length == 1)
242                 m_whereClause = m_sources[0].getWhereClause ();
243             else
244             {
245                 // Multiple
246
StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("(");
247                 for (int i = 0; i < m_sources.length; i++)
248                 {
249                     if (i > 0)
250                         sb.append (" OR ");
251                     sb.append (m_sources[i].getWhereClause ());
252                 }
253                 sb.append (")");
254                 m_whereClause = sb.toString ();
255             }
256             // Posting Type
257
String JavaDoc PostingType = getPostingType();
258             if (PostingType != null && PostingType.length() > 0)
259             {
260                 if (m_whereClause.length() > 0)
261                     m_whereClause += " AND ";
262                 m_whereClause += "PostingType='" + PostingType + "'";
263             }
264             Log.trace(Log.l5_DData, "MReportLine.getWhereClause", m_whereClause);
265         }
266         return m_whereClause;
267     } // getWhereClause
268

269     /**
270      * String Representation
271      * @return info
272      */

273     public String JavaDoc toString ()
274     {
275         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MReportLine[")
276             .append(getID()).append(" - ").append(getName()).append(" - ").append(getDescription())
277             .append(", SeqNo=").append(getSeqNo()).append(", AmountType=").append(getAmountType())
278             .append(" - LineType=").append(getLineType());
279         if (isLineTypeCalculation())
280             sb.append(" - Calculation=").append(getCalculationType())
281                 .append(" - ").append(getOper_1_ID()).append(" - ").append(getOper_2_ID());
282         else // SegmentValue
283
sb.append(" - SegmentValue - PostingType=").append(getPostingType())
284                 .append(", AmountType=").append(getAmountType());
285         sb.append ("]");
286         return sb.toString ();
287     } // toString
288

289     public static final String JavaDoc AmountType_Balance = "B";
290     public static final String JavaDoc AmountType_CR = "C";
291     public static final String JavaDoc AmountType_DR = "D";
292     public static final String JavaDoc AmountType_Qty = "Q";
293     //
294
public static final String JavaDoc AmountType_Period = "P";
295     public static final String JavaDoc AmountType_Year = "Y";
296     public static final String JavaDoc AmountType_Total = "T";
297
298
299     public boolean isLineTypeCalculation()
300     {
301         return LINETYPE_Calculation.equals(getLineType());
302     }
303     public boolean isLineTypeSegmentValue()
304     {
305         return LINETYPE_SegmentValue.equals(getLineType());
306     }
307
308     public boolean isCalculationTypeRange()
309     {
310         return CALCULATIONTYPE_AddRangeOp1ToOp2.equals(getCalculationType());
311     }
312     public boolean isCalculationTypeAdd()
313     {
314         return CALCULATIONTYPE_AddOp1PlusOp2.equals(getCalculationType());
315     }
316     public boolean isCalculationTypeSubtract()
317     {
318         return CALCULATIONTYPE_SubtractOp1MinusOp2.equals(getCalculationType());
319     }
320     public boolean isCalculationTypePercent()
321     {
322         return CALCULATIONTYPE_PercentageOp2OfOp1.equals(getCalculationType());
323     }
324
325     /**
326      * Set Parent.
327      * @param PA_ReportLineSet_ID parent
328      */

329     private void setPA_ReportLineSet_ID (int PA_ReportLineSet_ID)
330     {
331         setValueNoCheck ("PA_ReportLineSet_ID", new Integer JavaDoc(PA_ReportLineSet_ID));
332     } // setPA_ReportLineSet_ID
333

334     /*************************************************************************/
335
336     /**
337      * Copy
338      * @param ctx context
339      * @param AD_Client_ID parent
340      * @param AD_Org_ID parent
341      * @param PA_ReportLineSet_ID parent
342      * @param source copy source
343      * @return Report Line
344      */

345     public static MReportLine copy (Properties ctx, int AD_Client_ID, int AD_Org_ID, int PA_ReportLineSet_ID, MReportLine source)
346     {
347         MReportLine retValue = new MReportLine (ctx, 0);
348         MReportLine.copyValues(source, retValue, AD_Client_ID, AD_Org_ID);
349         retValue.setPA_ReportLineSet_ID(PA_ReportLineSet_ID);
350         return retValue;
351     } // copy
352

353
354 } // MReportLine
355
Popular Tags