simplified table serializing

This commit is contained in:
Peter Schojer
2008-05-27 13:59:06 +00:00
parent e07a2126e9
commit d0a169567b
10 changed files with 74 additions and 193 deletions

View File

@@ -41,31 +41,30 @@
#include "Poco/WebWidgets/ExtJS/ExtJS.h"
#include "Poco/WebWidgets/TableModelSerializer.h"
#include "Poco/Any.h"
namespace Poco {
namespace WebWidgets {
class Table;
namespace ExtJS {
class ExtJS_API ArrayTableSerializer: public Poco::WebWidgets::TableModelSerializer
class ExtJS_API ArrayTableSerializer
/// ArrayTableSerializer serializes a Table in JSON format
{
public:
typedef Poco::AutoPtr<ArrayTableSerializer> Ptr;
static void serialize(std::ostream& ostr, const Poco::WebWidgets::Table* pTable, std::size_t rowBegin = 0, std::size_t rowCnt = 0);
/// Serializes the table starting with row 0. A rowCnt of 0 means serialize all rows
static const std::string& contentType();
private:
ArrayTableSerializer();
/// Creates the ArrayTableSerializer with the given number of columns.
virtual ~ArrayTableSerializer();
~ArrayTableSerializer();
/// Destroys the ArrayTableSerializer.
void serialize(std::ostream& ostr, const Poco::WebWidgets::Table* pTable, std::size_t rowBegin = 0, std::size_t rowCnt = 0);
/// Serializes the table starting with row 0. A rowCnt of 0 means serialize all rows
const std::string& contentType() const;
};

View File

@@ -44,6 +44,7 @@
#include "Poco/WebWidgets/Type.h"
#include "Poco/WebWidgets/Renderer.h"
#include "Poco/WebWidgets/Cell.h"
#include "Poco/WebWidgets/Table.h"
#include <map>
@@ -51,7 +52,6 @@
namespace Poco {
namespace WebWidgets {
class Table;
class TableColumn;
class TableModel;
@@ -98,6 +98,8 @@ protected:
static void renderStore(const Table* pTable, std::ostream& ostr);
/// Renders the data store of the table
static void onBeforeLoad(void* pSender, Table::LoadData& ld);
};

View File

@@ -44,16 +44,6 @@ namespace WebWidgets {
namespace ExtJS {
ArrayTableSerializer::ArrayTableSerializer()
{
}
ArrayTableSerializer::~ArrayTableSerializer()
{
}
void ArrayTableSerializer::serialize(std::ostream& ostr, const Table* pTable, std::size_t rowBegin, std::size_t rowCntUser)
{
//[
@@ -119,7 +109,7 @@ void ArrayTableSerializer::serialize(std::ostream& ostr, const Table* pTable, st
}
const std::string& ArrayTableSerializer::contentType() const
const std::string& ArrayTableSerializer::contentType()
{
static const std::string ct("text/javascript");
return ct;

View File

@@ -38,10 +38,12 @@
#include "Poco/WebWidgets/ExtJS/FormRenderer.h"
#include "Poco/WebWidgets/ExtJS/Utility.h"
#include "Poco/WebWidgets/ExtJS/TableCellHandlerFactory.h"
#include "Poco/WebWidgets/ExtJS/ArrayTableSerializer.h"
#include "Poco/WebWidgets/Table.h"
#include "Poco/WebWidgets/WebApplication.h"
#include "Poco/WebWidgets/RequestHandler.h"
#include "Poco/WebWidgets/DateFormatter.h"
#include "Poco/Delegate.h"
#include <sstream>
@@ -146,7 +148,9 @@ void TableRenderer::renderProperties(const Table* pTable, const RenderContext& c
ostr << ",height:" << pTable->getHeight();
ostr << ",store:";
renderStore(pTable, ostr);
WebApplication::instance().registerAjaxProcessor(Poco::NumberFormatter::format(id), const_cast<Table*>(pTable));
Table* pT = const_cast<Table*>(pTable);
pT->beforeLoad += Poco::delegate(&TableRenderer::onBeforeLoad);
WebApplication::instance().registerAjaxProcessor(Poco::NumberFormatter::format(id), pT);
}
@@ -258,5 +262,13 @@ void TableRenderer::renderStore(const Table* pTable, std::ostream& ostr)
}
void TableRenderer::onBeforeLoad(void* pSender, Table::LoadData& ld)
{
ld.pResponse->setChunkedTransferEncoding(true);
ld.pResponse->setContentType(ArrayTableSerializer::contentType());
std::ostream& out = ld.pResponse->send();
ArrayTableSerializer::serialize(out, ld.pTable, ld.firstRow, ld.rowCnt);
}
} } } // namespace Poco::WebWidgets::ExtJS

View File

@@ -35,7 +35,6 @@
#include "CppUnit/TestSuite.h"
#include "Poco/WebWidgets/ExtJS/Utility.h"
#include "Poco/WebWidgets/ExtJS/TableCellHandler.h"
#include "Poco/WebWidgets/ExtJS/ArrayTableSerializer.h"
#include "Poco/WebWidgets/Page.h"
#include "Poco/WebWidgets/Renderer.h"
#include "Poco/WebWidgets/RenderContext.h"
@@ -1171,7 +1170,7 @@ void ExtJSTest::testTable()
Table::TableColumns tc;
tc.push_back(new TableColumn(0, "StaticText"));
tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton"));
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer());
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2));
pTable->setValue(std::string("one"), 0,0);
pTable->setValue(std::string("two"), 1,0);
pTable->setValue(std::string("three"), 2,0);
@@ -1202,7 +1201,7 @@ void ExtJSTest::testTableEdit()
Table::TableColumns tc;
tc.push_back(new TableColumn(new TextFieldCell(0), "DynText"));
tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton"));
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer());
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2));
pTable->setValue(std::string("one"), 0,0);
pTable->setValue(std::string("two"), 1,0);
pTable->setValue(std::string("three"), 2,0);
@@ -1237,7 +1236,7 @@ void ExtJSTest::testTableComboBox()
pCom->insert(std::string("3"));
tc.push_back(new TableColumn(pCom, "DynText"));
tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton"));
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer());
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2));
pTable->setValue(std::string("1"), 0,0);
pTable->setValue(std::string("2"), 1,0);
pTable->setValue(std::string("3"), 2,0);
@@ -1268,7 +1267,7 @@ void ExtJSTest::testTableButton()
Table::TableColumns tc;
tc.push_back(new TableColumn(new ButtonCell(0), "ButtonText"));
tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton"));
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer());
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2));
pTable->setValue(std::string("one"), 0,0);
pTable->setValue(std::string("two"), 1,0);
pTable->setValue(std::string("three"), 2,0);
@@ -1299,7 +1298,7 @@ void ExtJSTest::testTableImageButton()
Table::TableColumns tc;
tc.push_back(new TableColumn(new ImageButtonCell(0,new Image(Poco::URI("dummy.jpg")))));
tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton"));
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer());
Table::Ptr pTable = new Table(tc, new SimpleTableModel(2));
pTable->setValue(std::string("Sunset.jpg"), 0,0);
pTable->setValue(std::string("Sunset.jpg"), 1,0);
pTable->setValue(std::string("Sunset.jpg"), 2,0);

View File

@@ -649,10 +649,6 @@
RelativePath=".\include\Poco\WebWidgets\TableModel.h"
>
</File>
<File
RelativePath=".\include\Poco\WebWidgets\TableModelSerializer.h"
>
</File>
<File
RelativePath=".\include\Poco\WebWidgets\TabView.h"
>
@@ -717,10 +713,6 @@
RelativePath=".\src\TableModel.cpp"
>
</File>
<File
RelativePath=".\src\TableModelSerializer.cpp"
>
</File>
<File
RelativePath=".\src\TabView.cpp"
>

View File

@@ -46,7 +46,8 @@
#include "Poco/WebWidgets/TableColumn.h"
#include "Poco/WebWidgets/RequestProcessor.h"
#include "Poco/WebWidgets/JavaScriptEvent.h"
#include "Poco/WebWidgets/TableModelSerializer.h"
#include "Poco/FIFOEvent.h"
#include "Poco/Net/HTTPServerResponse.h"
#include <vector>
@@ -86,15 +87,26 @@ public:
CellValueChange(std::size_t row, std::size_t col, const Poco::Any& oldValue, const Poco::Any& newValue);
};
struct LoadData
{
Poco::Net::HTTPServerResponse* pResponse;
Table* pTable;
int firstRow;
int rowCnt;
LoadData(Poco::Net::HTTPServerResponse* pResponse, Table* pTable, int firstRow, int rowCnt);
};
JavaScriptEvent<Table::CellClick> cellClicked;
JavaScriptEvent<Table::CellValueChange> cellValueChanged;
FIFOEvent<LoadData> beforeLoad; /// thrown whenever a load is requested, internal event to which the TableRenderer must register
Table(const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer);
Table(const TableColumns& tc, TableModel::Ptr pModel);
/// Creates an anonymous Table.
Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer);
Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel);
/// Creates a Table with the given name.
std::size_t getColumnCount() const;
@@ -140,10 +152,10 @@ private:
void handleVal(const std::string& val);
void handleCnt(const std::string& val);
protected:
Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer);
Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel);
/// Creates a Table and assigns it the given name.
Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer);
Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel);
/// Creates a Table.
~Table();
@@ -160,7 +172,6 @@ private:
int _cnt;
std::string _val;
std::string _ev;
TableModelSerializer::Ptr _pSer;
};

View File

@@ -1,78 +0,0 @@
//
// TableModelSerializer.h
//
// $Id: //poco/Main/WebWidgets/include/Poco/WebWidgets/TableModelSerializer.h#4 $
//
// Library: WebWidgets
// Package: Views
// Module: TableModelSerializer
//
// Definition of the TableModelSerializer class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef WebWidgets_TableModelSerializer_INCLUDED
#define WebWidgets_TableModelSerializer_INCLUDED
#include "Poco/WebWidgets/WebWidgets.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
namespace Poco {
namespace WebWidgets {
class Table;
class WebWidgets_API TableModelSerializer: public Poco::RefCountedObject
/// TableModelSerializer defines the interface for Table serialization
{
public:
typedef Poco::AutoPtr<TableModelSerializer> Ptr;
TableModelSerializer();
/// Creates the TableModelSerializer with the given number of columns.
virtual ~TableModelSerializer();
/// Destroys the TableModelSerializer.
virtual void serialize(std::ostream& ostr, const Table* pTable, std::size_t rowBegin = 0, std::size_t rowCnt = 0) = 0;
/// Serializes the table starting with row 0. A rowCnt <= 0 means serialize all rows
virtual const std::string& contentType() const = 0;
/// Returns the content type that the answer will be encoded in
};
} } // namespace Poco::WebWidgets
#endif // WebWidgets_TableModelSerializer_INCLUDED

View File

@@ -53,7 +53,7 @@ const std::string Table::EV_CELLVALUECHANGED("edit");
const std::string Table::EV_LOADDATA("load");
Table::Table(const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer):
Table::Table(const TableColumns& tc, TableModel::Ptr pModel):
View(typeid(Table)),
_pModel(pModel),
_columns(tc),
@@ -61,14 +61,13 @@ Table::Table(const TableColumns& tc, TableModel::Ptr pModel, TableModelSerialize
_row(-1),
_cnt(-1),
_val(),
_ev(),
_pSer(pSer)
_ev()
{
checkValidConfig();
}
Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer):
Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel):
View(name, typeid(Table)),
_pModel(pModel),
_columns(tc),
@@ -76,14 +75,13 @@ Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pM
_row(-1),
_cnt(-1),
_val(),
_ev(),
_pSer(pSer)
_ev()
{
checkValidConfig();
}
Table::Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer):
Table::Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel):
View(name, type),
_pModel(pModel),
_columns(tc),
@@ -91,14 +89,13 @@ Table::Table(const std::string& name, const std::type_info& type, const TableCol
_row(-1),
_cnt(-1),
_val(),
_ev(),
_pSer(pSer)
_ev()
{
checkValidConfig();
}
Table::Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer):
Table::Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel):
View(type),
_pModel(pModel),
_columns(tc),
@@ -106,8 +103,7 @@ Table::Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr
_row(-1),
_cnt(-1),
_val(),
_ev(),
_pSer(pSer)
_ev()
{
checkValidConfig();
}
@@ -133,7 +129,6 @@ void Table::checkValidConfig()
if ((*it))
adoptChild((*it));;
}
poco_check_ptr (_pSer);
}
@@ -173,14 +168,18 @@ void Table::handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request
{
/// serialize the Table back
/// check for cnt and start if only a segment was requested
response.setChunkedTransferEncoding(true);
response.setContentType(_pSer->contentType());
std::ostream& out = response.send();
if (_row < 0)
_row = 0;
if (_cnt < 0)
_cnt = 0;
_pSer->serialize(out, this, _row, _cnt);
LoadData ld(&response, this, _row, _cnt);
_col = -1;
_row = -1;
_cnt = -1;
_val.clear();
_ev.clear();
beforeLoad.notify(this, ld);
}
else
{
@@ -272,4 +271,13 @@ Table::CellValueChange::CellValueChange(std::size_t r, std::size_t c, const Poco
}
Table::LoadData::LoadData(Poco::Net::HTTPServerResponse* pR, Table* pT, int row, int cnt):
pResponse(pR),
pTable(pT),
firstRow(row),
rowCnt(cnt)
{
}
} } // namespace Poco::WebWidgets

View File

@@ -1,54 +0,0 @@
//
// TableModelSerializer.cpp
//
// $Id: //poco/Main/WebWidgets/src/TableModelSerializer.cpp#3 $
//
// Library: WebWidgets
// Package: Views
// Module: TableModelSerializer
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/WebWidgets/TableModelSerializer.h"
namespace Poco {
namespace WebWidgets {
TableModelSerializer::TableModelSerializer()
{
}
TableModelSerializer::~TableModelSerializer()
{
}
} } // namespace Poco::WebWidgets