1 /**
2 *
3 */
4 package net.sf.flatpack.xml;
5
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Map;
11
12 import net.sf.flatpack.util.FPConstants;
13
14 /**
15 * @author Benoit Xhenseval
16 *
17 */
18 public class MetaData {
19 private List columnsNames;
20 private Map columnIndexMap;
21 private Map xmlRecordElements;
22
23 public MetaData(final Map fullMapFromPZParser) {
24 columnsNames = (List) fullMapFromPZParser.get(FPConstants.DETAIL_ID);
25 columnIndexMap = (Map) fullMapFromPZParser.get(FPConstants.COL_IDX);
26 xmlRecordElements = fullMapFromPZParser;
27 }
28
29 public MetaData(final List columnNames, final Map columnIndexMap) {
30 this.columnsNames = Collections.unmodifiableList(columnNames);
31 this.columnIndexMap = Collections.unmodifiableMap(columnIndexMap);
32 this.xmlRecordElements = new HashMap();
33 }
34
35 MetaData(final List columnNames, final Map columnIndexMap, final Map xmlRecordElements) {
36 this.columnsNames = Collections.unmodifiableList(columnNames);
37 this.columnIndexMap = columnIndexMap;
38 this.xmlRecordElements = xmlRecordElements;
39 }
40
41 public List getColumnsNames() {
42 return columnsNames;
43 }
44
45 public Map getColumnIndexMap() {
46 return columnIndexMap;
47 }
48
49 public void setColumnIndexMap(final Map columnIndexMap) {
50 this.columnIndexMap = columnIndexMap;
51 }
52
53 public void setColumnsNames(final List columnsNames) {
54 this.columnsNames = Collections.unmodifiableList(columnsNames);
55 }
56
57 public boolean isAnyRecordFormatSpecified() {
58 return xmlRecordElements != null && !xmlRecordElements.isEmpty();
59 }
60
61 public Iterator xmlRecordIterator() {
62 return xmlRecordElements.entrySet().iterator();
63 }
64
65 public List getListColumnsForRecord(final String key) {
66 return ((XMLRecordElement) xmlRecordElements.get(key)).getColumns();
67 }
68
69 public int getColumnIndex(final String key, final String columnName) {
70 int idx = -1;
71 if (key != null && !key.equals(FPConstants.DETAIL_ID) && !key.equals(FPConstants.COL_IDX)) {
72 idx = ((XMLRecordElement) xmlRecordElements.get(key)).getColumnIndex(columnName);
73 } else if (key == null || key.equals(FPConstants.DETAIL_ID)) {
74 final Integer i = (Integer) columnIndexMap.get(columnName);
75 if (i != null) {
76 idx = i.intValue();
77 }
78 }
79 return idx;
80 }
81
82 public String toString() {
83 final StringBuffer buf = new StringBuffer();
84 buf.append("Col Names:").append(columnsNames).append(System.getProperty("line.separator"));
85 buf.append("Col Index Map:").append(columnIndexMap).append(System.getProperty("line.separator"));
86 buf.append("XML Record Elements:").append(xmlRecordElements).append(System.getProperty("line.separator"));
87 return buf.toString();
88 }
89 }