001: package simpleorm.simplewebapp.eg.simple;
002:
003: import simpleorm.simplewebapp.core.WException;
004:
005: import java.util.*;
006:
007: /**
008: * A crude mock up of a database to demonstrate SimpleWebApp.
009: * (The database classes use a real database via SimpleOrm.)
010: */
011: public abstract class WTestDatabase {
012:
013: /** db can be overriden in hibernate test. */
014: public static WTestDatabase db = new WMockDatabase().addTestRows();
015:
016: public void beginTransaction() {
017: }
018:
019: public void endTransaction() {
020: }
021:
022: void addRow(String id, String name, String color, String alignment,
023: String happy, Integer count, Date datefield) {
024: WTestRecord row = addRow(id);
025: row.name = name;
026: row.color = color;
027: row.alignment = alignment;
028: row.happy = happy;
029: row.count = count;
030: row.datefield = datefield;
031: }
032:
033: public abstract WTestRecord addRow(String id);
034:
035: public abstract WTestRecord findRow(String id);
036:
037: public abstract void removeRow(String id);
038:
039: public abstract String getDatabaseName();
040:
041: /** Return a sorted iterator over all rows whose name contains nameWord */
042: public abstract Iterator<WTestRecord> getIterator(String nameWord,
043: String sorter);
044:
045: public WTestDatabase addTestRows() {
046: addRow("one", "The First one", "Green", "Middle", null, 100,
047: null);
048: addRow("two", "This is the second row in the table", "Red",
049: "Middle", null, 200, new Date("1/2/34"));
050: addRow("three", "Third one row", "Green", "Middle", null, 300,
051: new Date("2/3/45"));
052: addRow("four", "A forth row", "Red", "Left", null, 401,
053: new Date("3/4/56"));
054: addRow("five", "Fifth one", "Blue", "Right", null, 501,
055: new Date("4/5/67"));
056: //Map fourth = new HashMap();
057: //fourth.put("four", "Fourth as map");
058: //fourth, // Alternate form todo But does not work -- more bugs in EL, see manuallisttest.jsp for experiments
059: return this ;
060: }
061:
062: public static class WMockDatabase extends WTestDatabase {
063:
064: Map<String, WTestRecord> dbrows = new LinkedHashMap(10);
065:
066: public @Override
067: String getDatabaseName() {
068: return "Mock Database";
069: }
070:
071: public WTestRecord addRow(String id) {
072: WTestRecord row = new WTestRecord(dbrows.size(), id);
073: dbrows.put(id, row);
074: return row;
075: }
076:
077: public WTestRecord findRow(String id) {
078: return dbrows.get(id);
079: }
080:
081: public void removeRow(String id) {
082: dbrows.remove(id);
083: }
084:
085: /** Return a sorted iterator over all rows whose name contains nameWord */
086: public Iterator<WTestRecord> getIterator(String nameWord,
087: String sorter) {
088:
089: TreeMap<Comparable, WTestRecord> selected = new TreeMap<Comparable, WTestRecord>();
090: for (WTestRecord row : dbrows.values()) {
091: if (nameWord == null || "".equals(nameWord)
092: || row.name.contains(nameWord)) {
093: Comparable key;
094: if (sorter == null || "".equals(sorter))
095: key = 1000000 + row.index;
096: else if ("id".equals(sorter))
097: key = row.idx;
098: else if ("name".equals(sorter))
099: key = row.name;
100: else if ("color".equals(sorter))
101: key = row.color;
102: else if ("alignment".equals(sorter))
103: key = row.alignment;
104: else if ("count".equals(sorter))
105: key = row.count;
106: else
107: throw new WException("Bad sorter " + sorter);
108: selected
109: .put(key + "_" + (1000000 + row.index), row);
110: }
111: }
112: return selected.values().iterator();
113: }
114:
115: }
116: }
|