KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > PrintData


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 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-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.io.*;
17 import java.util.*;
18
19 import org.w3c.dom.*;
20 import javax.xml.parsers.*;
21 import javax.xml.transform.*;
22 import javax.xml.transform.stream.*;
23 import javax.xml.transform.dom.*;
24 import org.xml.sax.*;
25 import org.xml.sax.helpers.*;
26
27 import org.compiere.*;
28 import org.compiere.util.*;
29
30 /**
31  * Print Data Structure.
32  * Created by DataEngine
33  * A Structure has rows, wich contain elements.
34  * Elements can be end nodes (PrintDataElements) or data structures (PrintData).
35  * The row data is sparse - i.e. null if not existing.
36  * A Structure has optional meta info about content (PrintDataColumn).
37  *
38  * @author Jorg Janke
39  * @version $Id: PrintData.java,v 1.13 2003/04/24 06:15:05 jjanke Exp $
40  */

41 public class PrintData implements Serializable
42 {
43     /**
44      * Data Parent Constructor
45      * @param name data element name
46      */

47     public PrintData (Properties ctx, String JavaDoc name)
48     {
49         if (name == null)
50             throw new IllegalArgumentException JavaDoc("PrintData - Name cannot be null");
51         m_ctx = ctx;
52         m_name = name;
53     } // PrintData
54

55     /**
56      * Data Parent Constructor
57      * @param name data element name
58      * @param nodes ArrayList with nodes (content not checked)
59      */

60     public PrintData (Properties ctx, String JavaDoc name, ArrayList nodes)
61     {
62         if (name == null)
63             throw new IllegalArgumentException JavaDoc("PrintData - Name cannot be null");
64         m_ctx = ctx;
65         m_name = name;
66         if (nodes != null)
67             m_nodes = nodes;
68     } // PrintData
69

70     /** Context */
71     private Properties m_ctx;
72     /** Data Structure Name */
73     private String JavaDoc m_name;
74     /** Data Structure rows */
75     private ArrayList m_rows = new ArrayList();
76     /** Current Row Data Structure elements */
77     private ArrayList m_nodes = null;
78     /** Current Row */
79     private int m_row = -1;
80     /** List of Function Rows */
81     private ArrayList m_functionRows = new ArrayList();
82
83     /** Table has LevelNo */
84     private boolean m_hasLevelNo = false;
85     /** Level Number Indicator */
86     private static final String JavaDoc LEVEL_NO = "LEVELNO";
87
88     /** Optional Column Meta Data */
89     private PrintDataColumn[] m_columnInfo = null;
90     /** Optional sql */
91     private String JavaDoc m_sql = null;
92     /** Optional TableName */
93     private String JavaDoc m_TableName = null;
94
95     /** XML Element Name */
96     public static final String JavaDoc XML_TAG = "compiereData";
97     /** XML Row Name */
98     public static final String JavaDoc XML_ROW_TAG = "row";
99     /** XML Attribute Name */
100     public static final String JavaDoc XML_ATTRIBUTE_NAME = "name";
101     /** XML Attribute Count */
102     public static final String JavaDoc XML_ATTRIBUTE_COUNT = "count";
103     /** XML Attribute Number */
104     public static final String JavaDoc XML_ATTRIBUTE_NO = "no";
105     /** XML Attribute Function Row */
106     public static final String JavaDoc XML_ATTRIBUTE_FUNCTION_ROW = "function_row";
107
108     /**
109      * Get Context
110      * @return context
111      */

112     public Properties getCtx()
113     {
114         return m_ctx;
115     } // getName
116

117     /**
118      * Get Name
119      * @return name
120      */

121     public String JavaDoc getName()
122     {
123         return m_name;
124     } // getName
125

126     /*************************************************************************/
127
128     /**
129      * Set optional Column Info
130      * @param newInfo Column Info
131      */

132     public void setColumnInfo (PrintDataColumn[] newInfo)
133     {
134         m_columnInfo = newInfo;
135     } // setColumnInfo
136

137     /**
138      * Get optional Column Info
139      * @return Column Info
140      */

141     public PrintDataColumn[] getColumnInfo()
142     {
143         return m_columnInfo;
144     } // getColumnInfo
145

146     /**
147      * Set SQL (optional)
148      * @param sql SQL
149      */

150     public void setSQL (String JavaDoc sql)
151     {
152         m_sql = sql;
153     } // setSQL
154

155     /**
156      * Get optional SQL
157      * @return SQL
158      */

159     public String JavaDoc getSQL()
160     {
161         return m_sql;
162     } // getSQL
163

164     /**
165      * Set TableName (optional)
166      * @param TableName TableName
167      */

168     public void setTableName (String JavaDoc TableName)
169     {
170         m_TableName = TableName;
171     } // setTableName
172

173     /**
174      * Get optional TableName
175      * @return TableName
176      */

177     public String JavaDoc getTableName()
178     {
179         return m_TableName;
180     } // getTableName
181

182     /**
183      * String representation
184      * @return info
185      */

186     public String JavaDoc toString()
187     {
188         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("PrintData[");
189         sb.append(m_name).append(",Rows=").append(m_rows.size());
190         if (m_TableName != null)
191             sb.append(",TableName=").append(m_TableName);
192         sb.append("]");
193         return sb.toString();
194     } // toString
195

196
197     /*************************************************************************/
198
199     /**
200      * Returns true if no Nodes in row
201      * @return true if no Nodes in row
202      */

203     public boolean isEmpty()
204     {
205         if (m_nodes == null)
206             return true;
207         return m_nodes.size() == 0;
208     } // isEmpty
209

210     /**
211      * Return Number of nodes in row
212      * @return number of nodes in row
213      */

214     public int getNodeCount()
215     {
216         if (m_nodes == null)
217             return 0;
218         return m_nodes.size();
219     } // getNodeCount
220

221     /*************************************************************************/
222
223     /**
224      * Add Row
225      * @param functionRow true if function row
226      * @param levelNo Line detail Level Number 0=Normal
227      */

228     public void addRow (boolean functionRow, int levelNo)
229     {
230         m_nodes = new ArrayList();
231         m_row = m_rows.size();
232         m_rows.add (m_nodes);
233         if (functionRow)
234             m_functionRows.add(new Integer JavaDoc(m_row));
235         if (m_hasLevelNo && levelNo != 0)
236             addNode(new PrintDataElement(LEVEL_NO, new Integer JavaDoc(levelNo), DisplayType.Integer));
237     } // addRow
238

239     /**
240      * Set Row Index
241      * @param row row index
242      * @return true if success
243      */

244     public boolean setRowIndex (int row)
245     {
246         if (row < 0 || row >= m_rows.size())
247             return false;
248         m_row = row;
249         m_nodes = (ArrayList)m_rows.get(m_row);
250         return true;
251     }
252
253     /**
254      * Set Row Index to next
255      * @return true if success
256      */

257     public boolean setRowNext()
258     {
259         return setRowIndex(m_row+1);
260     } // setRowNext
261

262     /**
263      * Get Row Count
264      * @return row count
265      */

266     public int getRowCount()
267     {
268         return m_rows.size();
269     } // getRowCount
270

271     /**
272      * Get Current Row Index
273      * @return row index
274      */

275     public int getRowIndex()
276     {
277         return m_row;
278     } // getRowIndex
279

280     /**
281      * Is the Row a Function Row
282      * @param row row no
283      * @return true if function row
284      */

285     public boolean isFunctionRow (int row)
286     {
287         return m_functionRows.contains(new Integer JavaDoc(row));
288     } // isFunctionRow
289

290     /**
291      * Is the current Row a Function Row
292      * @return true if function row
293      */

294     public boolean isFunctionRow ()
295     {
296         return m_functionRows.contains(new Integer JavaDoc(m_row));
297     } // isFunctionRow
298

299     /**
300      * Is the current Row a Function Row
301      * @return true if function row
302      */

303     public boolean isPageBreak ()
304     {
305         // page break requires function and meta data
306
if (isFunctionRow() && m_nodes != null)
307         {
308             for (int i = 0; i < m_nodes.size(); i++)
309             {
310                 Object JavaDoc o = m_nodes.get(i);
311                 if (o instanceof PrintDataElement)
312                 {
313                     PrintDataElement pde = (PrintDataElement)o;
314                     if (pde.isPageBreak())
315                         return true;
316                 }
317             }
318         }
319         return false;
320     } // isPageBreak
321

322     /**
323      * PrintData has Level No
324      * @param hasLevelNo true if sql contains LevelNo
325      */

326     public void setHasLevelNo (boolean hasLevelNo)
327     {
328         m_hasLevelNo = hasLevelNo;
329     } // hasLevelNo
330

331     /**
332      * PrintData has Level No
333      * @return true if sql contains LevelNo
334      */

335     public boolean hasLevelNo()
336     {
337         return m_hasLevelNo;
338     } // hasLevelNo
339

340     /**
341      * Get Line Level Number for current row
342      * @return line level no 0 = default
343      */

344     public int getLineLevelNo ()
345     {
346         if (m_nodes == null || !m_hasLevelNo)
347             return 0;
348
349         for (int i = 0; i < m_nodes.size(); i++)
350         {
351             Object JavaDoc o = m_nodes.get (i);
352             if (o instanceof PrintDataElement)
353             {
354                 PrintDataElement pde = (PrintDataElement)o;
355                 if (LEVEL_NO.equals (pde.getColumnName()))
356                 {
357                     Integer JavaDoc ii = (Integer JavaDoc)pde.getValue();
358                     return ii.intValue();
359                 }
360             }
361         }
362         return 0;
363     } // getLineLevel
364

365     /*************************************************************************/
366
367     /**
368      * Add Parent node to Data Structure row
369      * @param parent parent
370      */

371     public void addNode (PrintData parent)
372     {
373         if (parent == null)
374             throw new IllegalArgumentException JavaDoc("PrintData.addNode - Parent cannot be null");
375         if (m_nodes == null)
376             addRow(false, 0);
377         m_nodes.add (parent);
378     } // addNode
379

380     /**
381      * Add node to Data Structure row
382      * @param node node
383      */

384     public void addNode (PrintDataElement node)
385     {
386         if (node == null)
387             throw new IllegalArgumentException JavaDoc("PrintData.addNode - Node cannot be null");
388         if (m_nodes == null)
389             addRow(false, 0);
390         m_nodes.add (node);
391     } // addNode
392

393     /**
394      * Get Node with index in row
395      * @param index index
396      * @return PrintData(Element) of index or null
397      */

398     public Object JavaDoc getNode (int index)
399     {
400         if (m_nodes == null || index < 0 || index >= m_nodes.size())
401             return null;
402         return m_nodes.get(index);
403     } // getNode
404

405     /**
406      * Get Node with Name in row
407      * @param name name
408      * @return PrintData(Element) with Name or null
409      */

410     public Object JavaDoc getNode (String JavaDoc name)
411     {
412         int index = getIndex (name);
413         if (index < 0)
414             return null;
415         return m_nodes.get(index);
416     } // getNode
417

418     /**
419      * Get Node with AD_Column_ID in row
420      * @param AD_Column_ID AD_Column_ID
421      * @return PrintData(Element) with AD_Column_ID or null
422      */

423     public Object JavaDoc getNode (Integer JavaDoc AD_Column_ID)
424     {
425         int index = getIndex (AD_Column_ID.intValue());
426         if (index < 0)
427             return null;
428         return m_nodes.get(index);
429     } // getNode
430

431     /**
432      * Get Primary Key in row
433      * @return PK or null
434      */

435     public PrintDataElement getPKey()
436     {
437         if (m_nodes == null)
438             return null;
439         for (int i = 0; i < m_nodes.size(); i++)
440         {
441             Object JavaDoc o = m_nodes.get(i);
442             if (o instanceof PrintDataElement)
443             {
444                 PrintDataElement pde = (PrintDataElement)o;
445                 if (pde.isPKey())
446                     return pde;
447             }
448         }
449         return null;
450     } // getPKey
451

452     /**
453      * Get Index of Node in Structure (not recursing) row
454      * @param columnName name
455      * @return index or -1
456      */

457     public int getIndex (String JavaDoc columnName)
458     {
459         if (m_nodes == null)
460             return -1;
461         for (int i = 0; i < m_nodes.size(); i++)
462         {
463             Object JavaDoc o = m_nodes.get(i);
464             if (o instanceof PrintDataElement)
465             {
466                 if (columnName.equals(((PrintDataElement)o).getColumnName()))
467                     return i;
468             }
469             else if (o instanceof PrintData)
470             {
471                 if (columnName.equals(((PrintData)o).getName()))
472                     return i;
473             }
474             else
475                 Log.error("PrintData.getIndex - Element not PrintData(Element) " + o.getClass().getName());
476         }
477         // As Data is stored sparse, there might be lots of NULL values
478
// Log.error("PrintData.getIndex - Element not found - " + name);
479
return -1;
480     } // getIndex
481

482     /**
483      * Get Index of Node in Structure (not recursing) row
484      * @param AD_Column_ID AD_Column_ID
485      * @return index or -1
486      */

487     public int getIndex (int AD_Column_ID)
488     {
489         if (m_columnInfo == null)
490             return -1;
491         for (int i = 0; i < m_columnInfo.length; i++)
492         {
493             if (m_columnInfo[i].getAD_Column_ID() == AD_Column_ID)
494                 return getIndex(m_columnInfo[i].getColumnName());
495         }
496         Log.error("PrintData.getIndex - Column not found - AD_Column_ID=" + AD_Column_ID);
497         return -1;
498     } // getIndex
499

500     /*************************************************************************/
501
502     /**
503      * Dump All Data
504      */

505     public void dump()
506     {
507         dump(this);
508     } // dump
509

510     /**
511      * Dump All Data
512      */

513     public void dumpCurrentRow()
514     {
515         dumpRow(this, m_row);
516     } // dump
517

518     /**
519      * Dump PrintData
520      * @param pd print data
521      */

522     private static void dump (PrintData pd)
523     {
524         System.out.println("");
525         System.out.println(pd);
526         if (pd.getColumnInfo() != null)
527         {
528             System.out.println("ColumnInfo:");
529             for (int i = 0; i < pd.getColumnInfo().length; i++)
530                 System.out.println(i + ": " + pd.getColumnInfo()[i]);
531         }
532         for (int i = 0; i < pd.getRowCount(); i++)
533             dumpRow(pd, i);
534     } // dump
535

536     /**
537      * Dump Row
538      * @param pd print data
539      * @param row row
540      */

541     private static void dumpRow (PrintData pd, int row)
542     {
543         System.out.println("Row #" + row);
544         if (row < 0 || row >= pd.getRowCount())
545         {
546             System.out.println("- invalid -");
547             return;
548         }
549         pd.setRowIndex(row);
550         if (pd.getNodeCount() == 0)
551         {
552             System.out.println("- n/a -");
553             return;
554         }
555         for (int i = 0; i < pd.getNodeCount(); i++)
556         {
557             Object JavaDoc obj = pd.getNode(i);
558             if (obj instanceof PrintData)
559             {
560                 System.out.println("");
561                 dump((PrintData)obj);
562             }
563             else if (obj instanceof PrintDataElement)
564             {
565                 if (i != 0)
566                     System.out.print(" - ");
567                 System.out.print(((PrintDataElement)obj).dump());
568             }
569             else
570             {
571                 System.out.println("");
572                 System.out.println("- INVALID: " + obj);
573             }
574         }
575         System.out.println("");
576     } // dumpRow
577

578     /*************************************************************************/
579
580     /**
581      * Get XML Document representation
582      * @return XML document
583      */

584     public Document getDocument()
585     {
586         Document document = null;
587         try
588         {
589             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
590             // System.out.println(factory.getClass().getName());
591
DocumentBuilder builder = factory.newDocumentBuilder();
592             document = builder.newDocument();
593             document.appendChild(document.createComment(Compiere.getSummaryAscii()));
594         }
595         catch (Exception JavaDoc e)
596         {
597             System.err.println(e);
598             e.printStackTrace();
599         }
600         // Root
601
Element root = document.createElement(PrintData.XML_TAG);
602         root.setAttribute(XML_ATTRIBUTE_NAME, getName());
603         root.setAttribute(XML_ATTRIBUTE_COUNT, String.valueOf(getRowCount()));
604         document.appendChild(root);
605         processXML (this, document, root);
606         return document;
607     } // getDocument
608

609     /**
610      * Process PrintData Tree
611      * @param pd Print Data
612      * @param document document
613      * @param root element to add to
614      */

615     private static void processXML (PrintData pd, Document document, Element root)
616     {
617         for (int r = 0; r < pd.getRowCount(); r++)
618         {
619             pd.setRowIndex(r);
620             Element row = document.createElement(PrintData.XML_ROW_TAG);
621             row.setAttribute(XML_ATTRIBUTE_NO, String.valueOf(r));
622             if (pd.isFunctionRow())
623                 row.setAttribute(XML_ATTRIBUTE_FUNCTION_ROW, "yes");
624             root.appendChild(row);
625             //
626
for (int i = 0; i < pd.getNodeCount(); i++)
627             {
628                 Object JavaDoc o = pd.getNode(i);
629                 if (o instanceof PrintData)
630                 {
631                     PrintData pd_x = (PrintData)o;
632                     Element element = document.createElement(PrintData.XML_TAG);
633                     element.setAttribute(XML_ATTRIBUTE_NAME, pd_x.getName());
634                     element.setAttribute(XML_ATTRIBUTE_COUNT, String.valueOf(pd_x.getRowCount()));
635                     row.appendChild(element);
636                     processXML (pd_x, document, element); // recursive call
637
}
638                 else if (o instanceof PrintDataElement)
639                 {
640                     PrintDataElement pde = (PrintDataElement)o;
641                     if (!pde.isNull())
642                     {
643                         Element element = document.createElement(PrintDataElement.XML_TAG);
644                         element.setAttribute(PrintDataElement.XML_ATTRIBUTE_NAME, pde.getColumnName());
645                         if (pde.hasKey())
646                             element.setAttribute(PrintDataElement.XML_ATTRIBUTE_KEY, pde.getValueKey());
647                         element.appendChild(document.createTextNode(pde.getValueDisplay(null))); // not formatted
648
row.appendChild(element);
649                     }
650                 }
651                 else
652                     Log.error("PrintData.processTree - Element not PrintData(Element) " + o.getClass().getName());
653             } // columns
654
} // rows
655
} // processTree
656

657
658     /**
659      * Create XML representation to StreamResult
660      * @param result StreamResult
661      * @return true if success
662      */

663     public boolean createXML (StreamResult result)
664     {
665         try
666         {
667             DOMSource source = new DOMSource(getDocument());
668             TransformerFactory tFactory = TransformerFactory.newInstance();
669             Transformer transformer = tFactory.newTransformer();
670             transformer.transform (source, result);
671         }
672         catch (Exception JavaDoc e)
673         {
674             Log.error("PrintData.createXML (StreamResult)", e);
675             return false;
676         }
677         return true;
678     } // createXML
679

680     /**
681      * Create XML representation to File
682      * @param fileName file name
683      * @return true if success
684      */

685     public boolean createXML (String JavaDoc fileName)
686     {
687         try
688         {
689             File file = new File(fileName);
690             file.createNewFile();
691             StreamResult result = new StreamResult(file);
692             createXML (result);
693         }
694         catch (Exception JavaDoc e)
695         {
696             Log.error("PrintData.createXML (file)", e);
697             return false;
698         }
699         return true;
700     } // createXMLFile
701

702     /*************************************************************************/
703
704     /**
705      * Create PrintData from XML
706      * @param input InputSource
707      * @return PrintData
708      */

709     public static PrintData parseXML (Properties ctx, File input)
710     {
711         Log.trace(Log.l3_Util, "PrintData.parseXML", input);
712         PrintData pd = null;
713         try
714         {
715             PrintDataHandler handler = new PrintDataHandler(ctx);
716             SAXParserFactory factory = SAXParserFactory.newInstance();
717             SAXParser parser = factory.newSAXParser();
718             parser.parse(input, handler);
719             pd = handler.getPrintData();
720         }
721         catch (Exception JavaDoc e)
722         {
723             Log.error("PrintData.parseXML", e);
724         }
725         return pd;
726     } // parseXML
727

728
729     /*************************************************************************/
730
731     /**
732      * Test
733      * @param args test
734      */

735     public static void main(String JavaDoc[] args)
736     {
737         System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
738             "org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"); // System Default
739
// "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
740
System.setProperty("javax.xml.parsers.SAXParserFactory",
741             "org.apache.crimson.jaxp.SAXParserFactoryImpl"); // System Default
742
// "org.apache.xerces.jaxp.SAXParserFactoryImpl");
743

744         PrintData pd = new PrintData(new Properties(), "test1");
745         pd.addNode(new PrintDataElement("test1element1","testvalue<1>",0));
746         pd.addNode(new PrintDataElement("test1element2","testvalue&2&",0));
747
748         PrintData pdx = new PrintData(new Properties(), "test2");
749         pdx.addNode(new PrintDataElement("test2element1-1","testvalue11",0));
750         pdx.addNode(new PrintDataElement("test2element1-2","testvalue12",0));
751         pdx.addRow(false, 0);
752         pdx.addNode(new PrintDataElement("test2element2-1","testvalue21",0));
753         pdx.addNode(new PrintDataElement("test2element2-2","testvalue22",0));
754
755         pd.addNode(pdx);
756         pd.addNode(new PrintDataElement("test1element3","testvalue/3/",0));
757
758         pd.createXML("C:\\Temp\\printData.xml");
759         pd.createXML(new StreamResult(System.out));
760         System.out.println("");
761         pd.dump();
762
763         // parse
764
System.out.println("");
765         PrintData pd1 = parseXML (new Properties(), new File("C:\\Temp\\printData.xml"));
766         pd1.createXML(new StreamResult(System.out));
767         System.out.println("");
768         pd1.dump();
769     } // main
770

771 } // PrintData
772
Popular Tags