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.FxSharedUtils;
037: import com.flexive.shared.content.FxPK;
038: import com.flexive.shared.search.FxResultSet;
039: import com.flexive.shared.search.query.SqlQueryBuilder;
040: import com.flexive.war.JsonWriter;
041:
042: import java.io.IOException;
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: /**
047: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
048: * @version $Rev: 1 $
049: */
050: class ListDataWriter extends TableDataWriter {
051: ListDataWriter(JsonWriter out) {
052: super (out);
053: }
054:
055: /**
056: * {@inheritDoc}
057: */
058: @Override
059: public List<Column> getColumns(FxResultSet rs) {
060: if (rs == null) {
061: return new ArrayList<Column>(0);
062: }
063: List<Column> result = new ArrayList<Column>(rs.getColumnCount());
064: for (int i = SqlQueryBuilder.COL_USERPROPS; i < rs
065: .getColumnCount(); i++) {
066: // TODO determine correct data type
067: result.add(new Column(rs.getColumnName(i + 1), null, i));
068: }
069: return result;
070: }
071:
072: private void writeRow(Object[] values) throws IOException {
073: assert values[0] instanceof FxPK;
074:
075: out.startMap();
076: out.writeAttribute("rowId", rowCounter); // TODO: do we really need this?
077: out.writeAttribute("id", "row" + rowCounter); // write variables required for selection.js
078: out.writeAttribute("rowNum", rowCounter);
079: out.writeAttribute("positionId", values[0].toString()); // write PK
080: out.writeAttribute("colorSet", rowCounter % 2);
081:
082: for (int i = SqlQueryBuilder.COL_USERPROPS; i < values.length; i++) {
083: out.writeAttribute(getColumnName(i), FxSharedUtils
084: .formatResultValue(values[i], null, null, null));
085: if (values[i] instanceof FxPK) {
086: // add oid/version columns
087: FxPK pk = (FxPK) values[i];
088: out.writeAttribute(COL_ID, pk.getId());
089: out.writeAttribute(COL_VERSION, pk.getVersion());
090: }
091: }
092: out.closeMap();
093: rowCounter++;
094: }
095:
096: /**
097: * {@inheritDoc}
098: */
099: @Override
100: public void writeRows(FxResultSetDataModel dataModel)
101: throws IOException {
102: out.startArray();
103: if (dataModel != null) {
104: rowCounter = 0;
105: for (int i = dataModel.getStartRow(); i < dataModel
106: .getStartRow()
107: + dataModel.getFetchRows(); i++) {
108: dataModel.setRowIndex(i);
109: if (!dataModel.isRowAvailable()) {
110: break;
111: }
112: writeRow((Object[]) dataModel.getRowData());
113: }
114: }
115: out.closeArray();
116: }
117:
118: }
|