Skip to content

Commit

Permalink
implementation in SmartGwt over Request Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Sopov committed May 7, 2012
1 parent 86a903d commit 3868305
Show file tree
Hide file tree
Showing 25 changed files with 712 additions and 173 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SmartGwt sandbox
================

smarttest - A try to use GenericGwtRpcDataSource (http://forums.smartclient.com/showthread.php?t=10850)
SandBox for various technologies from GWT ecosystem.
2 changes: 2 additions & 0 deletions smarttest/.settings/com.google.gdt.eclipse.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
launchConfigExternalUrlPrefix=
27 changes: 27 additions & 0 deletions smarttest/src/com/sopovs/moradanen/SmartRfTest.gwt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='smartrftest'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>

<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->

<!-- Other module inherits -->
<inherits name="com.smartgwt.SmartGwt" />
<inherits name='com.google.web.bindery.requestfactory.RequestFactory' />
<!-- Specify the app entry point class. -->
<entry-point class='com.sopovs.moradanen.rf.smartgwt.client.SmartRfTest'/>

<!-- Specify the paths for translatable code -->
<source path='rf/smartgwt/client'/>
<source path='rf/shared'/>
<source path='rf/smartgwt/shared'/>
<source path='smartgwt/client'/>
<source path='smartgwt/shared/lib'/>

</module>
7 changes: 5 additions & 2 deletions smarttest/src/com/sopovs/moradanen/rf/client/RfTest.ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
<thead>
<tr>
<th>
<a href="RfTest.html">RequestFactory</a>
<a href="RfTest.html">PureGWT(RequestFactory)</a>
</th>
<th>
<a href="SmartTest.html">SmartGwt</a>
<a href="SmartRfTest.html">SmartGwt(RequestFactory)</a>
</th>
<th>
<a href="SmartTest.html">SmartGwt(GWT-RPC)</a>
</th>
<th>
<a href="VAADIN/">Vaadin</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.sopovs.moradanen.rf.smartgwt.client;

import java.util.ArrayList;
import java.util.List;

import com.google.web.bindery.requestfactory.shared.Request;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.sopovs.moradanen.rf.shared.CompanyProxy;
import com.sopovs.moradanen.rf.smartgwt.shared.SmartRequestFactory;

public class CompanyDataSource extends GenericDataSource<CompanyProxy> {

public static final String ID = "id";
private static final String NAME = "name";
public static final String SECTOR_ID = "sectorId";

public CompanyDataSource(SmartRequestFactory rf) {
super(rf);
}

@Override
protected List<DataSourceField> getDataSourceFields() {
List<DataSourceField> result = new ArrayList<DataSourceField>();
DataSourceTextField id = new DataSourceTextField(ID, "Id");
id.setPrimaryKey(true);
result.add(id);
result.add(new DataSourceTextField(NAME, "Name"));
return result;
}

@Override
protected void copyValues(CompanyProxy from, Record to) {
to.setAttribute(ID, from.getId());
to.setAttribute(NAME, from.getName());
}

@Override
protected Request<List<CompanyProxy>> fetch(Integer startRow, Integer endRow, String sortBy,
List<String> filterCriteria) {
return getRf().companyRequest().fetchCompanies(startRow, endRow, sortBy, filterCriteria);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.sopovs.moradanen.rf.smartgwt.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.sopovs.moradanen.rf.smartgwt.shared.SmartRequestFactory;
import com.sopovs.moradanen.smartgwt.client.lib.GwtRpcDataSource;
import com.sopovs.moradanen.smartgwt.shared.lib.GenericGwtRpcList;

public abstract class GenericDataSource<R> extends GwtRpcDataSource {

private final SmartRequestFactory rf;

public GenericDataSource(SmartRequestFactory rf) {
for (DataSourceField field : getDataSourceFields()) {
addField(field);
}
this.rf = rf;
}

protected abstract List<DataSourceField> getDataSourceFields();

protected abstract Request<List<R>> fetch(Integer startRow, Integer endRow, String sortBy,
List<String> filterCriteria);

protected abstract void copyValues(R from, Record to);

private static String safeToString(Object obj) {
return obj == null ? null : obj.toString();
}

@Override
protected final void executeFetch(final String requestId, DSRequest request, final DSResponse response) {
final Integer startRow = request.getStartRow();
final Integer endRow = request.getEndRow();
Criteria criteria = request.getCriteria();
List<String> criterias = new ArrayList<String>();
if (criteria != null) {
for (Object entry : criteria.getValues().entrySet()) {
@SuppressWarnings("unchecked")
Entry<String, Object> rEntry = (Entry<String, Object>) entry;
criterias.add(rEntry.getKey());
criterias.add(safeToString(rEntry.getValue()));
}
}

fetch(startRow,
endRow,
// we can't use request.getSortBy() here because it throws a ClassCastException (known bug).
//TODO: replace with request.getSortBy() as soon as the bug is fixed.
request.getAttribute("sortBy"),
criterias).fire(new Receiver<List<R>>() {

@Override
public void onSuccess(List<R> result) {
List<Record> records = new ArrayList<Record>();
for (R data : result) {
Record newRec = new Record();
copyValues(data, newRec);
records.add(newRec);
}
// if those are set, the client wants paging. you have to use GenericGwtRpcList
if (startRow != null && endRow != null && result instanceof GenericGwtRpcList<?>) {
Integer totalRows = ((GenericGwtRpcList<R>) result).getTotalRows();
response.setStartRow(startRow);
if (totalRows == null) {
throw new NullPointerException("totalRows cannot be null when using GenericGwtRpcList");
}
// endRow can't be higher than totalRows
response.setEndRow(endRow.intValue() < totalRows.intValue() ? endRow : totalRows);
response.setTotalRows(totalRows);
}
response.setData(records.toArray(new Record[records.size()]));
processResponse(requestId, response);
}
});
}

@Override
protected final void executeAdd(String requestId, DSRequest request, DSResponse response) {
throw new IllegalStateException();

}

@Override
protected final void executeUpdate(String requestId, DSRequest request, DSResponse response) {
throw new IllegalStateException();

}

@Override
protected final void executeRemove(String requestId, DSRequest request, DSResponse response) {
throw new IllegalStateException();
}

protected SmartRequestFactory getRf() {
return rf;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.sopovs.moradanen.rf.smartgwt.client;

import java.util.ArrayList;
import java.util.List;

import com.google.web.bindery.requestfactory.shared.Request;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.sopovs.moradanen.rf.shared.PersonProxy;
import com.sopovs.moradanen.rf.smartgwt.shared.SmartRequestFactory;

public class PersonDataSource extends GenericDataSource<PersonProxy> {

private static final String DESCRIPTION = "description";
private static final String SECOND_NAME = "secondName";
private static final String FIRST_NAME = "firstName";
private static final String ID = "id";
public static final String COMPANY_ID = "companyId";

public PersonDataSource(SmartRequestFactory rf) {
super(rf);
}

@Override
protected List<DataSourceField> getDataSourceFields() {
List<DataSourceField> result = new ArrayList<DataSourceField>();
DataSourceTextField id = new DataSourceTextField(ID, "Id");
id.setPrimaryKey(true);
result.add(id);
result.add(new DataSourceTextField(FIRST_NAME, "First Name"));
result.add(new DataSourceTextField(SECOND_NAME, "Second Name"));
result.add(new DataSourceTextField(DESCRIPTION, "Description Name"));
return result;
}

@Override
protected void copyValues(PersonProxy from, Record to) {
to.setAttribute(ID, from.getId());
to.setAttribute(FIRST_NAME, from.getFirstName());
to.setAttribute(SECOND_NAME, from.getSecondName());
to.setAttribute(DESCRIPTION, from.getDescription());
}

@Override
protected Request<List<PersonProxy>> fetch(Integer startRow, Integer endRow, String sortBy,
List<String> filterCriteria) {
return getRf().personRequest().fetchPersons(startRow, endRow, sortBy, filterCriteria);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.sopovs.moradanen.rf.smartgwt.client;

import java.util.ArrayList;
import java.util.List;

import com.google.web.bindery.requestfactory.shared.Request;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.sopovs.moradanen.rf.shared.SectorProxy;
import com.sopovs.moradanen.rf.smartgwt.shared.SmartRequestFactory;

public class SectorDataSource extends GenericDataSource<SectorProxy> {

private static final String DATASOURCE_ID = "SectorDS";
public static final String ID = "id";
private static final String PARENT_ID = "parentId";
private static final String NAME = "name";

public SectorDataSource(SmartRequestFactory rf) {
super(rf);
}

@Override
protected Request<List<SectorProxy>> fetch(Integer startRow, Integer endRow, String sortBy,
List<String> filterCriteria) {
return getRf().sectorRequest().fetchSectors(startRow, endRow, sortBy, filterCriteria);
}

@Override
protected List<DataSourceField> getDataSourceFields() {
List<DataSourceField> result = new ArrayList<DataSourceField>();

DataSourceTextField id = new DataSourceTextField(ID, "Id");
id.setPrimaryKey(true);
id.setRequired(true);
result.add(id);

DataSourceTextField parentId = new DataSourceTextField(PARENT_ID, "Parent Id");
parentId.setForeignKey(DATASOURCE_ID + ".id");
parentId.setRootValue("0");
result.add(parentId);
result.add(new DataSourceTextField(NAME, "Name"));
return result;
}

@Override
public void copyValues(SectorProxy from, Record to) {
to.setAttribute(ID, from.getId());
to.setAttribute(PARENT_ID, from.getParentId());
to.setAttribute(NAME, from.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.sopovs.moradanen.rf.smartgwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.smartgwt.client.data.DataSource;
import com.sopovs.moradanen.rf.smartgwt.shared.SmartRequestFactory;
import com.sopovs.moradanen.smartgwt.client.AbstractSmartTest;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class SmartRfTest extends AbstractSmartTest {

private final SmartRequestFactory rf = GWT.create(SmartRequestFactory.class);

public SmartRfTest() {
rf.initialize(new SimpleEventBus());
}

@Override
protected DataSource createSectorDataSource() {
return new SectorDataSource(rf);
}

@Override
protected DataSource createCompanyDataSource() {
return new CompanyDataSource(rf);
}

@Override
protected DataSource createPersonDataSource() {
return new PersonDataSource(rf);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sopovs.moradanen.rf.smartgwt.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.sopovs.moradanen.rf.smartgwt.client.CompanyDataSource;
import com.sopovs.moradanen.server.DummyDao;
import com.sopovs.moradanen.server.domain.Company;

public class CompanyService {
public static List<Company> fetchCompanies(Integer startRow, Integer endRow,
@SuppressWarnings("unused") String sortBy,
List<String> filterCriteria) {
Map<String, String> realCriteria = new HashMap<String, String>();
for (Iterator<String> it = filterCriteria.iterator(); it.hasNext();) {
realCriteria.put(it.next(), it.next());
}

String sectorId = realCriteria.get(CompanyDataSource.SECTOR_ID);
List<Company> result = new ArrayList<Company>();
for (Company com : DummyDao.COMPANIES) {
if (com.getFocusedSectors().contains(DummyDao.SECTORS.get(Integer.valueOf(sectorId)))) {
result.add(com);
}
}

//TODO if the requested range is bigger than result - return whole result without sublisting and creating new ArrayList
return new ArrayList<Company>(result.subList(startRow, Math.min(result.size(), endRow)));
}
}
Loading

0 comments on commit 3868305

Please sign in to comment.