001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    import java.util.ArrayList;
007    import java.util.Date;
008    import java.util.List;
009    import java.util.StringTokenizer;
010    
011    import org.w3c.dom.Element;
012    import org.w3c.dom.NodeList;
013    
014    /**
015     * @author lakshman
016     * 
017     */
018    public class IndexRecord {
019            private Date time;
020    
021            private String[] selections;
022    
023            private String[][] paramsArray;
024    
025            private String sourceName = null;
026    
027            public void setSourceName(String s) {
028                    sourceName = s;
029            }
030    
031            /** returns a source name if one was set. */
032            public String getSourceName() {
033                    return sourceName;
034            }
035    
036            public IndexRecord(Date time, String[] selections, String[][] params) {
037                    this.time = time;
038                    if (selections.length != 3) {
039                            throw new IllegalArgumentException("Require 3 selections");
040                    }
041                    this.selections = selections;
042    
043                    paramsArray = params;
044            }
045    
046            public IndexRecord(Date time, String[] selections, String[] params) {
047                    this(time, selections, new String[][] { params });
048            }
049    
050            public String[] getParams(int index) {
051                    return paramsArray[index];
052            }
053    
054            public String[] getParams() {
055                    return getParams(0);
056            }
057    
058            public String[] getSelections() {
059                    return selections;
060            }
061    
062            public Date getTime() {
063                    return time;
064            }
065    
066            public int getNumParams() {
067                    return (paramsArray.length);
068            }
069    
070            static private Date getDateFromString(String timeString, String frac) {
071                    long tm_long = 1000 * Long.parseLong(timeString.trim());
072                    if (frac != null) {
073                            double ftm = Double.parseDouble(frac);
074                            tm_long += (int) Math.round(1000 * ftm);
075                    }
076                    Date time = new Date(tm_long);
077                    return time;
078            }
079    
080            public static IndexRecord createIndexRecord(String timeString, String timeFrac, String[] paramsList, String[] paramsChanges, String selectionsString, String indexLocation)
081            {
082                    // time
083                    Date time = getDateFromString(timeString,timeFrac);             
084                    
085                    // selections
086                    List<String> selectionList = StringUtil.split(selectionsString.trim());
087                    if (selectionList.size() == 2) {
088                            selectionList.add("");
089                    }
090                    String[] selections = selectionList.toArray(new String[0]);
091                    
092                    // params
093                    List<List<String>> paramsArray = new ArrayList<List<String>>();
094                    List<String> p1 = null, p2 = null;
095                    for (int i = 0; i < paramsList.length; ++i) {
096                            List<String> p = StringUtil.split(paramsList[i].trim());
097                            if (i == 0) {
098                                    p1 = new ArrayList<String>(p);
099                                    p2 = p;
100                                    paramsArray.add(p);
101                                    continue;
102                            }
103                            String changes = paramsChanges[i];
104                            if (changes == null || changes == "") {
105                                    paramsArray.add(p);
106                                    continue;
107                            }
108                            StringTokenizer st = new StringTokenizer(changes);
109                            int seq = 0;
110                            p2 = new ArrayList<String>(p1);
111                            while (st.hasMoreTokens()) {
112                                    int pos = (new Integer(st.nextToken())).intValue();
113                                    p2.set(pos, p.get(seq++));
114                            }
115                            paramsArray.add(p2);
116                    }
117    
118                    String[][] aparams = new String[paramsArray.size()][];
119                    for (int j = 0; j < paramsArray.size(); ++j) {
120                            List<String> params = paramsArray.get(j);
121                            for (int i = 0; i < params.size(); ++i) {
122                                    if (params.get(i).equals("{indexlocation}")) {
123                                            params.set(i, indexLocation);
124                                    }
125                            }
126                            aparams[j] = params.toArray(new String[0]);
127                    }
128    
129                    return new IndexRecord(time, selections, aparams);
130            }
131            
132            public static IndexRecord createIndexRecord(Element e, String indexLocation) {
133                    // time
134                    Element timeElement = (Element) e.getElementsByTagName("time").item(0);
135                    String timeString = timeElement.getFirstChild().getNodeValue();
136                    String timeFrac   = timeElement.getAttribute("fractional");
137    
138                    // selections
139                    Element selElement = (Element) e.getElementsByTagName("selections")
140                                    .item(0);
141                    String selections = selElement.getFirstChild().getNodeValue();
142    
143                    // params
144                    NodeList paramsList = e.getElementsByTagName("params");
145                    String[] params = new String[paramsList.getLength()];
146                    String[] changes = new String[paramsList.getLength()];
147                    for (int i=0; i < params.length; ++i){
148                            Element pe = ((Element) paramsList.item(i));
149                            params[i] = pe.getTextContent();
150                            changes[i]= pe.getAttribute("changes");
151                    }
152    
153                    return createIndexRecord(timeString,timeFrac,params,changes,selections,indexLocation);
154            }
155    
156            public String toString() {
157                    StringBuffer b = new StringBuffer();
158                    b.append("IndexRecord: ");
159                    for (int i = 0; i < selections.length; ++i)
160                            b.append(selections[i]);
161                    b.append(" at " + time);
162                    return b.toString();
163            }
164    
165            public String toXMLString() {
166                    StringBuffer b = new StringBuffer();
167                    b.append("<item>\n");
168                    double fraction = time.getTime() % 1000 / (double) 1000;
169                    b.append("<time fractional=\"" + String.valueOf(fraction) + "\">"
170                                    + String.valueOf(time.getTime() / 1000) + "</time>\n");
171                    for (int i = 0; i < paramsArray.length; i++) {
172                            b.append("<params>");
173                            String[] al = paramsArray[i];
174                            for (int j = 0; j < al.length; j++)
175                                    b.append(al[j]).append(" ");
176                            b.append("</params>\n");
177                    }
178                    b.append("<selections>");
179                    for (int j = 0; j < selections.length; j++)
180                            b.append(selections[j]).append(" ");
181                    b.append("</selections>\n");
182                    b.append("</item>\n");
183                    return (b.toString());
184            }
185    
186            public String getDataType() {
187                    return selections[1];
188            }
189    
190            /** An empty string if this record has no subtype */
191            public String getSubType() {
192                    return selections[2];
193            }
194    
195            public String getTimeStamp() {
196                    return selections[0];
197            }
198    }