001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.javascript.search;
034:
035: import com.flexive.faces.model.FxResultSetDataModel;
036: import com.flexive.shared.search.FxResultSet;
037: import com.flexive.shared.search.ResultViewType;
038: import com.flexive.war.JsonWriter;
039:
040: import java.io.IOException;
041: import java.io.Writer;
042: import java.util.List;
043:
044: /**
045: * Renders columns and rows in JSON notation to be used
046: * by Dojo's table widget.
047: *
048: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: * @version $Rev: 1 $
050: */
051: abstract class TableDataWriter {
052: protected static final String COL_ID = "__id";
053: protected static final String COL_VERSION = "__version";
054:
055: public static class Column {
056: private final String label;
057: private final String dataType;
058: private final int index;
059:
060: public Column(String label, String dataType, int index) {
061: this .label = label;
062: this .dataType = dataType;
063: this .index = index;
064: }
065:
066: public String getLabel() {
067: return label;
068: }
069:
070: public String getDataType() {
071: return dataType;
072: }
073:
074: public int getIndex() {
075: return index;
076: }
077: }
078:
079: protected JsonWriter out;
080: protected int rowCounter = 0;
081:
082: protected TableDataWriter(JsonWriter out) {
083: this .out = out;
084: }
085:
086: public static TableDataWriter getInstance(JsonWriter out,
087: ResultViewType viewType) {
088: switch (viewType) {
089: case LIST:
090: return new ListDataWriter(out);
091: case THUMBNAILS:
092: return new ThumbnailDataWriter(out);
093: }
094: throw new IllegalArgumentException("Unknown viewType: "
095: + viewType);
096: }
097:
098: public static TableDataWriter getInstance(Writer out,
099: ResultViewType viewType) {
100: return getInstance(new JsonWriter(out), viewType);
101: }
102:
103: public void writeColumns(List<Column> columns) throws IOException {
104: out.startArray();
105: for (Column column : columns) {
106: out.startMap();
107: out.writeAttribute("field",
108: getColumnName(column.getIndex()));
109: if (column.getDataType() != null) {
110: out.writeAttribute("dataType", column.getDataType());
111: }
112: out.writeAttribute("label", column.getLabel());
113: out.closeMap();
114: }
115: out.closeArray();
116: }
117:
118: /**
119: * Render the column headers for the given search result set.
120: *
121: * @param rs the search result set
122: * @throws IOException if the output could not be written
123: */
124: public void writeColumns(FxResultSet rs) throws IOException {
125: writeColumns(getColumns(rs));
126: }
127:
128: /**
129: * Render the column headers for the given search result set model.
130: *
131: * @param dataModel the search result set model
132: * @throws IOException if the output could not be written
133: */
134: public void writeColumns(FxResultSetDataModel dataModel)
135: throws IOException {
136: // force loading
137: dataModel.setRowIndex(dataModel.getStartRow());
138: dataModel.fetchResult(dataModel.getRowIndex());
139: // get columns
140: writeColumns(getColumns(dataModel.getResult()));
141: }
142:
143: /**
144: * Generate columns for the given result set.
145: *
146: * @param rs the result set
147: * @return generated columns
148: */
149: public abstract List<Column> getColumns(FxResultSet rs);
150:
151: /**
152: * Renders all rows contained in the result set.
153: *
154: * @param dataModel the result set data model containing the search result
155: * @throws IOException if the output could not be written
156: */
157: public abstract void writeRows(FxResultSetDataModel dataModel)
158: throws IOException;
159:
160: protected String getColumnName(int index) {
161: return "c" + (index + 1);
162: }
163:
164: }
|