KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > businessUtility > AccountTree


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

17 package org.openbravo.erpCommon.businessUtility;
18
19 import java.util.Vector JavaDoc;
20 import org.apache.log4j.Logger ;
21 import org.openbravo.database.ConnectionProvider;
22 import org.openbravo.base.secureApp.VariablesSecureApp;
23 import org.openbravo.erpCommon.utility.*;
24 import javax.servlet.*;
25
26 public class AccountTree {
27   static Logger log4j = Logger.getLogger(AccountTree.class);
28   private VariablesSecureApp vars;
29   private ConnectionProvider conn;
30   private AccountTreeData[] accounts;
31   private AccountTreeData[] elements;
32   private AccountTreeData[] resultantAccounts;
33   private String JavaDoc[] elementValueParent;
34
35   public AccountTree(VariablesSecureApp _vars, ConnectionProvider _conn, AccountTreeData[] _elements, AccountTreeData[] _accounts, String JavaDoc _elementValueParent) throws ServletException {
36     if (log4j.isDebugEnabled()) log4j.debug("AccountTree []");
37     vars = _vars;
38     conn = _conn;
39     elements = _elements;
40     accounts = _accounts;
41     elementValueParent = new String JavaDoc[1];
42     elementValueParent[0] = _elementValueParent;
43     resultantAccounts = updateTreeQuantitiesSign(null, 0, "D");
44     //Calculating forms for every elements
45
if (resultantAccounts!=null && resultantAccounts.length>0) {
46       AccountTreeData[] forms = AccountTreeData.selectForms(conn, Utility.getContext(conn, vars, "#User_Org", "AccountTree"), Utility.getContext(conn, vars, "#User_Client", "AccountTree"));
47       resultantAccounts = calculateTree(forms, elementValueParent, new Vector JavaDoc<Object JavaDoc>());
48     }
49   }
50   
51   public AccountTree(VariablesSecureApp _vars, ConnectionProvider _conn, AccountTreeData[] _elements, AccountTreeData[] _accounts, String JavaDoc[] _elementValueParent) throws ServletException {
52      if (log4j.isDebugEnabled()) log4j.debug("AccountTree []");
53     vars = _vars;
54     conn = _conn;
55     elements = _elements;
56     accounts = _accounts;
57     elementValueParent = _elementValueParent;
58  
59     resultantAccounts = updateTreeQuantitiesSign(null, 0, "D");
60     
61     if (resultantAccounts!=null && resultantAccounts.length>0) {
62       AccountTreeData[] forms = AccountTreeData.selectForms(conn, Utility.getContext(conn, vars, "#User_Org", "AccountTree"), Utility.getContext(conn, vars, "#User_Client", "AccountTree"));
63       //resultantAccounts = calculateTree(forms, elementValueParent, new Vector<Object>());
64

65       
66       
67       Vector JavaDoc<Object JavaDoc> vec = new Vector JavaDoc<Object JavaDoc>();
68       AccountTreeData[] r;
69       
70       for (int i=0; i<elementValueParent.length; i++) {
71         r = calculateTree(forms, elementValueParent[i], new Vector JavaDoc<Object JavaDoc>());
72         for (int j=0; j<r.length; j++)
73           vec.addElement(r[j]);
74       }
75         
76       resultantAccounts = new AccountTreeData[vec.size()];
77       vec.copyInto(resultantAccounts);
78     }
79     
80   
81     /*//Calculating forms for every elements
82     if (resultantAccounts!=null && resultantAccounts.length>0) {
83       AccountTreeData[] forms = AccountTreeData.selectForms(conn, Utility.getContext(conn, vars, "#User_Org", "AccountTree"), Utility.getContext(conn, vars, "#User_Client", "AccountTree"));
84       
85       int totalAcct = 0;
86       AccountTreeData[][] accounts = new AccountTreeData[elementValueParent.length][];
87       for (int i=0; i<_elementValueParent.length; i++) {
88         //resultantAccounts = calculateTree(forms, elementValueParent, new Vector<Object>());
89         if (log4j.isDebugEnabled()) log4j.debug("calculating node: "+i+":"+elementValueParent[i]+" update quantities...");
90         accounts[i] = updateTreeQuantitiesSign(null, 0, "D");
91         if (log4j.isDebugEnabled()) log4j.debug("calculating node: "+i+":"+elementValueParent[i]+" calculate tree...");
92         accounts[i] = calculateTree(forms, elementValueParent[i], new Vector<Object>());
93         totalAcct += accounts[i].length;
94       }
95       
96       // Join all the trees
97       resultantAccounts = new AccountTreeData[totalAcct];
98       int k=0;
99       for (int i=0; i<elementValueParent.length; i++)
100         for (int j=0; i<accounts[i].length; j++)
101           resultantAccounts[k++] = accounts[i][j];
102       if (log4j.isDebugEnabled()) log4j.debug("AcctTree Created - Resultant Accounts " + totalAcct);
103     }*/

104   }
105
106   public AccountTreeData[] getAccounts() {
107     return resultantAccounts;
108   }
109
110   /*Apply the sign to the qty according to the showValueCond field*/
111   private double applySign(double qty, String JavaDoc sign, boolean isSummary) {
112     double total=0.0;
113     if (isSummary && !sign.equalsIgnoreCase("A")) {
114       if (sign.equalsIgnoreCase("P")) total=((qty>total)?qty:0.0);
115       else if (sign.equalsIgnoreCase("N")) total=((qty<total)?qty:0.0);
116       else total=qty;
117     } else total=qty;
118
119     return total;
120   }
121
122   /*Update the qty and the operation qty fields of the element*/
123   private AccountTreeData setDataQty(AccountTreeData element, String JavaDoc isDebitCredit) {
124     if (element==null || accounts==null || accounts.length==0) return element;
125     for (int i=0;i<accounts.length;i++) {
126       if (accounts[i].id.equals(element.id)) {
127         if (isDebitCredit.equals("C")) {
128           accounts[i].qty = accounts[i].qtycredit;
129           accounts[i].qtyRef = accounts[i].qtycreditRef;
130         }
131         element.qtyOperation = accounts[i].qty;
132         element.qtyOperationRef = accounts[i].qtyRef;
133         double dblQty = Double.valueOf(element.qtyOperation).doubleValue();
134         double dblQtyRef = Double.valueOf(element.qtyOperationRef).doubleValue();
135         element.qty = Double.toString(applySign(dblQty, element.showvaluecond, element.issummary.equals("Y")));
136         element.qtyRef = Double.toString(applySign(dblQtyRef, element.showvaluecond, element.issummary.equals("Y")));
137         break;
138       }
139     }
140     return element;
141   }
142
143   private AccountTreeData[] updateTreeQuantitiesSign(String JavaDoc indice, int level, String JavaDoc isDebitCredit) {
144     if (elements==null || elements.length==0) return elements;
145     AccountTreeData[] result = null;
146     Vector JavaDoc<Object JavaDoc> vec = new Vector JavaDoc<Object JavaDoc>();
147    // if (log4j.isDebugEnabled()) log4j.debug("AccountTree.updateTreeQuantitiesSign() - elements: " + elements.length);
148
if (indice == null) indice="0";
149     for (int i=0;i<elements.length;i++) {
150       if (elements[i].parentId.equals(indice)) {
151         if (level==0) isDebitCredit = elements[i].accountsign;
152         else if (isDebitCredit.equals("") || isDebitCredit.equalsIgnoreCase("N")) isDebitCredit = elements[i].accountsign;
153         AccountTreeData[] dataChilds = updateTreeQuantitiesSign(elements[i].nodeId, (level+1), isDebitCredit);
154         elements[i].elementLevel = Integer.toString(level);
155         elements[i] = setDataQty(elements[i], isDebitCredit);
156         vec.addElement(elements[i]);
157         if (dataChilds!=null && dataChilds.length>0) {
158           for (int j=0;j<dataChilds.length;j++) vec.addElement(dataChilds[j]);
159         }
160       }
161     }
162     result = new AccountTreeData[vec.size()];
163     vec.copyInto(result);
164     return result;
165   }
166
167   private boolean hasForm(String JavaDoc indice, AccountTreeData[] forms) {
168     if (indice == null) {
169       log4j.error("AccountTree.hasForm - Missing index");
170       return false;
171     }
172     for (int i=0;i<forms.length;i++) {
173       if (forms[i].id.equals(indice)) return true;
174     }
175     return false;
176   }
177
178   private void formsCalculate(Vector JavaDoc<Object JavaDoc> vecAll, AccountTreeData[] forms, String JavaDoc indice, Vector JavaDoc<Object JavaDoc> vecTotal) {
179     if (log4j.isDebugEnabled()) log4j.debug("AccountTree.formsCalculate");
180     if (resultantAccounts==null || resultantAccounts.length==0) return;
181     if (indice == null) {
182       log4j.error("AccountTree.formsCalculate - Missing index");
183       return;
184     }
185     if (vecTotal==null) vecTotal = new Vector JavaDoc<Object JavaDoc>();
186     if (vecTotal.size()==0) {
187       vecTotal.addElement("0");
188       vecTotal.addElement("0");
189     }
190     double total = Double.valueOf((String JavaDoc) vecTotal.elementAt(0)).doubleValue();
191     double totalRef = Double.valueOf((String JavaDoc) vecTotal.elementAt(1)).doubleValue();
192     boolean encontrado=false;
193     for (int i=0;i<forms.length;i++) {
194       if (forms[i].id.equals(indice)) {
195         encontrado=false;
196         for (int j=0;j<vecAll.size();j++) {
197           AccountTreeData actual = (AccountTreeData) vecAll.elementAt(j);
198           if (actual.nodeId.equals(forms[i].nodeId)) {
199             encontrado=true;
200             total += (Double.valueOf(actual.qtyOperation).doubleValue() * Double.valueOf(forms[i].accountsign).doubleValue());
201             totalRef += (Double.valueOf(actual.qtyOperationRef).doubleValue() * Double.valueOf(forms[i].accountsign).doubleValue());
202          // if (log4j.isDebugEnabled()) log4j.debug("AccountTree.formsCalculate - C_ElementValue_ID: " + actual.nodeId + " - total: " + total);
203
break;
204           }
205         }
206         if (!encontrado) {
207          // if (log4j.isDebugEnabled()) log4j.debug("AccountTree.formsCalculate - C_ElementValue_ID: " + forms[i].nodeId + " not found");
208
Vector JavaDoc<Object JavaDoc> vecParcial = new Vector JavaDoc<Object JavaDoc>();
209           vecParcial.addElement("0");
210           vecParcial.addElement("0");
211           calculateTree(forms, forms[i].nodeId, vecParcial, true);
212           double parcial = Double.valueOf((String JavaDoc) vecParcial.elementAt(0)).doubleValue();
213           double parcialRef = Double.valueOf((String JavaDoc) vecParcial.elementAt(1)).doubleValue();
214          // if (log4j.isDebugEnabled()) log4j.debug("AccountTree.formsCalculate - parcial: " + Double.toString(parcial));
215
parcial = (parcial * Double.valueOf(forms[i].accountsign).doubleValue());
216           parcialRef = (parcialRef * Double.valueOf(forms[i].accountsign).doubleValue());
217        // if (log4j.isDebugEnabled()) log4j.debug("AccountTree.formsCalculate - C_ElementValue_ID: " + forms[i].nodeId + " found with value: " + parcial + " account sign: " + forms[i].accountsign);
218
total += parcial;
219           totalRef += parcialRef;
220         }
221       }
222     }
223     vecTotal.set(0, Double.toString(total));
224     vecTotal.set(1, Double.toString(totalRef));
225   }
226
227   private AccountTreeData[] calculateTree(AccountTreeData[] forms, String JavaDoc[] indice, Vector JavaDoc<Object JavaDoc> vecTotal) {
228     return calculateTree(forms, indice, vecTotal, true);
229   }
230   
231   private AccountTreeData[] calculateTree(AccountTreeData[] forms, String JavaDoc indice, Vector JavaDoc<Object JavaDoc> vecTotal) {
232     String JavaDoc[] i=new String JavaDoc[1];
233     i[0]=indice;
234     return calculateTree(forms, indice, vecTotal, true);
235   }
236   private boolean nodeIn(String JavaDoc node, String JavaDoc[] listOfNodes) {
237     for (int i=0;i<listOfNodes.length;i++)
238       if (node.equals(listOfNodes[i])) return true;
239     return false;
240   }
241   
242   private AccountTreeData[] calculateTree(AccountTreeData[] forms, String JavaDoc indice, Vector JavaDoc<Object JavaDoc> vecTotal, boolean applysign) {
243     String JavaDoc[] i=new String JavaDoc[1];
244     i[0]=indice;
245  
246     return calculateTree(forms, i, vecTotal, applysign);
247   }
248   private AccountTreeData[] calculateTree(AccountTreeData[] forms, String JavaDoc[] indice, Vector JavaDoc<Object JavaDoc> vecTotal, boolean applysign) {
249     if (resultantAccounts==null || resultantAccounts.length==0) return resultantAccounts;
250     if (indice == null){ indice=new String JavaDoc[1]; indice[0]="0";}
251     AccountTreeData[] result = null;
252     Vector JavaDoc<Object JavaDoc> vec = new Vector JavaDoc<Object JavaDoc>();
253   // if (log4j.isDebugEnabled()) log4j.debug("AccountTree.calculateTree() - accounts: " + resultantAccounts.length);
254
if (vecTotal==null) vecTotal = new Vector JavaDoc<Object JavaDoc>();
255     if (vecTotal.size()==0) {
256       vecTotal.addElement("0");
257       vecTotal.addElement("0");
258     }
259     double total = Double.valueOf((String JavaDoc) vecTotal.elementAt(0)).doubleValue();
260     double totalRef = Double.valueOf((String JavaDoc) vecTotal.elementAt(1)).doubleValue();
261     for (int i=0;i<resultantAccounts.length;i++) {
262       //if (resultantAccounts[i].parentId.equals(indice)) {
263
if (resultantAccounts[i].nodeId.equals("1000510")&&resultantAccounts[i].parentId.equals(indice[0])) log4j.debug("CalculateTree Encontrado!!! indice"+indice[0]+" - parentId:"+resultantAccounts[i].parentId);
264       if (nodeIn(resultantAccounts[i].parentId, indice)) {
265       AccountTreeData[] dataChilds = null;
266       if (resultantAccounts[i].calculated.equals("N")) //this would work if it only passed here once, but it's passing more times... why????
267
{
268           Vector JavaDoc<Object JavaDoc> vecParcial = new Vector JavaDoc<Object JavaDoc>();
269           vecParcial.addElement("0");
270           vecParcial.addElement("0");
271           Vector JavaDoc<Object JavaDoc> vecAux = (Vector JavaDoc<Object JavaDoc>)vec.clone();
272           dataChilds = calculateTree(forms, resultantAccounts[i].nodeId, vecParcial);
273           if (dataChilds!=null && dataChilds.length>0) for (int h=0;h<dataChilds.length;h++) vecAux.addElement(dataChilds[h]);
274           if (!hasForm(resultantAccounts[i].nodeId, forms)) {
275             double parcial = Double.valueOf((String JavaDoc) vecParcial.elementAt(0)).doubleValue();
276             double parcialRef = Double.valueOf((String JavaDoc) vecParcial.elementAt(1)).doubleValue();
277             resultantAccounts[i].qtyOperation = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue() + parcial);
278             resultantAccounts[i].qtyOperationRef = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue() + parcialRef);
279           } else {
280             vecParcial.set(0, "0");
281             vecParcial.set(1, "0");
282             formsCalculate(vecAux, forms, resultantAccounts[i].nodeId, vecParcial);
283             double parcial = Double.valueOf((String JavaDoc) vecParcial.elementAt(0)).doubleValue();
284             double parcialRef = Double.valueOf((String JavaDoc) vecParcial.elementAt(1)).doubleValue();
285             resultantAccounts[i].qtyOperation = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue() + parcial);
286             resultantAccounts[i].qtyOperationRef = Double.toString(Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue() + parcialRef);
287           }
288
289           resultantAccounts[i].qty = Double.toString(applySign(Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue(), resultantAccounts[i].showvaluecond, resultantAccounts[i].issummary.equals("Y")));
290           resultantAccounts[i].qtyRef = Double.toString(applySign(Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue(), resultantAccounts[i].showvaluecond, resultantAccounts[i].issummary.equals("Y")));
291           resultantAccounts[i].calculated = "Y";
292       }
293           vec.addElement(resultantAccounts[i]);
294           if (dataChilds!=null && dataChilds.length>0) {
295             for (int j=0;j<dataChilds.length;j++) vec.addElement(dataChilds[j]);
296           }
297         // } This was for the culculated="N"
298

299         if (applysign) {
300           total += Double.valueOf(resultantAccounts[i].qty).doubleValue();
301           totalRef += Double.valueOf(resultantAccounts[i].qtyRef).doubleValue();
302         } else {
303           total += Double.valueOf(resultantAccounts[i].qtyOperation).doubleValue();
304           totalRef += Double.valueOf(resultantAccounts[i].qtyOperationRef).doubleValue();
305         }
306       }
307     }
308     vecTotal.set(0, Double.toString(total));
309     vecTotal.set(1, Double.toString(totalRef));
310     result = new AccountTreeData[vec.size()];
311     vec.copyInto(result);
312     return result;
313   }
314
315   private AccountTreeData[] levelFilter(String JavaDoc[] indice, boolean found, String JavaDoc strLevel) {
316     if (resultantAccounts==null || resultantAccounts.length==0 || strLevel==null || strLevel.equals("")) return resultantAccounts;
317     AccountTreeData[] result = null;
318     Vector JavaDoc<Object JavaDoc> vec = new Vector JavaDoc<Object JavaDoc>();
319     if (log4j.isDebugEnabled()) log4j.debug("AccountTree.levelFilter() - accounts: " + resultantAccounts.length);
320
321    // if (indice == null) indice="0";
322
if (indice == null){ indice=new String JavaDoc[1]; indice[0]="0";}
323     for (int i=0;i<resultantAccounts.length;i++) {
324       //if (resultantAccounts[i].parentId.equals(indice) && (!found || resultantAccounts[i].elementlevel.equalsIgnoreCase(strLevel))) {
325
if (nodeIn(resultantAccounts[i].parentId, indice)&& (!found || resultantAccounts[i].elementlevel.equalsIgnoreCase(strLevel))) {
326         AccountTreeData[] dataChilds = levelFilter(resultantAccounts[i].nodeId, (found || resultantAccounts[i].elementlevel.equals(strLevel)), strLevel);
327         vec.addElement(resultantAccounts[i]);
328         if (dataChilds!=null && dataChilds.length>0) for (int j=0;j<dataChilds.length;j++) vec.addElement(dataChilds[j]);
329       }
330     }
331     result = new AccountTreeData[vec.size()];
332     vec.copyInto(result);
333     vec.clear();
334     return result;
335   }
336   
337   private AccountTreeData[] levelFilter(String JavaDoc indice, boolean found, String JavaDoc strLevel) {
338     String JavaDoc[] i=new String JavaDoc[1];
339     i[0]=indice;
340     if (log4j.isDebugEnabled()) log4j.debug("AccountTree.levelFilter1");
341     return levelFilter(i, found, strLevel);
342   }
343
344   public AccountTreeData[] filterStructure(String JavaDoc[] indice, boolean notEmptyLines, String JavaDoc strLevel, boolean isLevel) {
345     if (log4j.isDebugEnabled()) log4j.debug("AccountTree.filterStructure() - accounts: " + resultantAccounts.length);
346     if (resultantAccounts==null || resultantAccounts.length==0) return resultantAccounts;
347     AccountTreeData[] result = null;
348     Vector JavaDoc<Object JavaDoc> vec = new Vector JavaDoc<Object JavaDoc>();
349     
350     
351     double ZERO = 0.0;
352
353     AccountTreeData[] r = levelFilter(indice, false, strLevel);
354
355     for (int i=0;i<r.length;i++) {
356       if (r[i].nodeId.equals("1000510")) log4j.debug("filterStructure qty:" + r[i].qty+" - qtyRef:"+r[i].qtyRef+" - show:"+r[i].showelement);
357       if (r[i].showelement.equals("Y")) {
358         if (r[i].nodeId.equals("1000510")) log4j.debug("Encontrado!!! qty:" + r[i].qty+" - qtyRef:"+r[i].qtyRef+" - show:"+r[i].showelement);
359         r[i].qty = Double.toString(applySign(Double.valueOf(r[i].qty).doubleValue(), r[i].showvaluecond, true));
360         r[i].qtyRef = Double.toString(applySign(Double.valueOf(r[i].qtyRef).doubleValue(), r[i].showvaluecond, true));
361         if (!notEmptyLines || (Double.valueOf(r[i].qty).doubleValue()!=ZERO || Double.valueOf(r[i].qtyRef).doubleValue()!=ZERO)) {
362           vec.addElement(r[i]);
363         }
364       }
365     }
366     result = new AccountTreeData[vec.size()];
367     vec.copyInto(result);
368     return result;
369   }
370   
371   /*
372   public AccountTreeData[] filterStructure(String[] parents, boolean notEmptyLines, String strLevel, boolean isLevel) {
373     if (log4j.isDebugEnabled()) log4j.debug("parents.length:" + parents.length+" - resultantAccounts"+resultantAccounts);
374     AccountTreeData[][] accounts = new AccountTreeData[parents.length][];
375     int totalAcct = 0;
376     for (int i=0;i<parents.length;i++) {
377       accounts[i] = filterStructure(parents[i], notEmptyLines, strLevel, isLevel);
378       totalAcct += accounts[i].length;
379     }
380     if (log4j.isDebugEnabled()) log4j.debug("totalAcct:" + totalAcct);
381     AccountTreeData[] r = new AccountTreeData[totalAcct];
382     int k =0;
383     for (int i=0; i<parents.length; i++)
384       for (int j=0; j<accounts[i].length; j++) {
385         if (log4j.isDebugEnabled()) log4j.debug("k:" + k+" - accounts["+i+"].length:"+accounts[i].length);
386         r[k++] = accounts[i][j];
387       }
388     return r;
389 }*/

390
391   public void filter(boolean notEmptyLines, String JavaDoc strLevel, boolean isLevel) {
392     if (log4j.isDebugEnabled()) log4j.debug("filter");
393     if (resultantAccounts==null) log4j.warn("No resultant Acct");
394     resultantAccounts = filterStructure(elementValueParent, notEmptyLines, strLevel, isLevel);
395   }
396 }
397
Popular Tags