details: /erp/devel/pi/rev/3bf58b3f0bce
changeset: 9121:3bf58b3f0bce
user: Martin Taal <martin.taal <at> openbravo.com>
date: Tue Dec 14 13:55:03 2010 +0100
summary: Improved count computation, solved typo in startrows
diffstat:
modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java
| 207 +++++----
modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonConstants.java
| 8 +-
modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonUtils.java
| 2 +-
3 files changed, 115 insertions(+), 102 deletions(-)
diffs (276 lines):
diff -r fe902e8d37f1 -r 3bf58b3f0bce
modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java
---
a/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java
Tue Dec 14 13:25:28 2010 +0100
+++
b/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java
Tue Dec 14 13:55:03 2010 +0100
@@ -18,6 +18,7 @@
*/
package org.openbravo.service.json;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -70,98 +71,123 @@
Check.isNotNull(entityName, "The name of the service/entityname should
not be null");
Check.isNotNull(parameters, "The parameters should not be null");
- final String startRowStr =
parameters.get(JsonConstants.STARTROW_PARAMETER);
- final String endRowStr = parameters.get(JsonConstants.ENDROW_PARAMETER);
+ final JSONObject jsonResult = new JSONObject();
+ final JSONObject jsonResponse = new JSONObject();
+ List<BaseOBObject> bobs;
+ final String id = parameters.get(JsonConstants.ID);
+ // if the id is set that's a special case of one object being requested
+ if (id != null) {
+ bobs = new ArrayList<BaseOBObject>();
+ final BaseOBObject bob = OBDal.getInstance().get(entityName, id);
+ if (bob != null) {
+ bobs.add(bob);
+ }
+ } else {
+ final String startRowStr =
parameters.get(JsonConstants.STARTROW_PARAMETER);
+ final String endRowStr =
parameters.get(JsonConstants.ENDROW_PARAMETER);
- final DataEntityQueryService queryService = OBProvider.getInstance().get(
- DataEntityQueryService.class);
- queryService.setEntityName(entityName);
+ final DataEntityQueryService queryService =
OBProvider.getInstance().get(
+ DataEntityQueryService.class);
+ queryService.setEntityName(entityName);
- if (parameters.containsKey(JsonConstants.USE_ALIAS)) {
- queryService.setUseAlias();
- }
- // set the filter parameters
- for (String key : parameters.keySet()) {
- if (!key.startsWith("_")) {
- queryService.addFilterParameter(key, parameters.get(key));
- } else if (key.equals(JsonConstants.WHERE_PARAMETER)
- || key.equals(JsonConstants.IDENTIFIER) ||
key.equals(JsonConstants.ORG_PARAMETER)) {
- // the _where is used in a special way
- queryService.addFilterParameter(key, parameters.get(key));
+ if (parameters.containsKey(JsonConstants.USE_ALIAS)) {
+ queryService.setUseAlias();
}
- }
+ // set the filter parameters
+ for (String key : parameters.keySet()) {
+ if (!key.startsWith("_")) {
+ queryService.addFilterParameter(key, parameters.get(key));
+ } else if (key.equals(JsonConstants.WHERE_PARAMETER)
+ || key.equals(JsonConstants.IDENTIFIER) ||
key.equals(JsonConstants.ORG_PARAMETER)) {
+ // the _where is used in a special way
+ queryService.addFilterParameter(key, parameters.get(key));
+ }
+ }
- if (parameters.get(JsonConstants.OR_EXPRESSION_PARAMETER) != null) {
- queryService.setDoOrExpression();
- }
+ if (parameters.get(JsonConstants.OR_EXPRESSION_PARAMETER) != null) {
+ queryService.setDoOrExpression();
+ }
- if (parameters.containsKey(JsonConstants.TEXTMATCH_PARAMETER_OVERRIDE)) {
-
queryService.setTextMatching(parameters.get(JsonConstants.TEXTMATCH_PARAMETER_OVERRIDE));
- } else {
-
queryService.setTextMatching(parameters.get(JsonConstants.TEXTMATCH_PARAMETER));
- }
+ if
(parameters.containsKey(JsonConstants.TEXTMATCH_PARAMETER_OVERRIDE)) {
+
queryService.setTextMatching(parameters.get(JsonConstants.TEXTMATCH_PARAMETER_OVERRIDE));
+ } else {
+
queryService.setTextMatching(parameters.get(JsonConstants.TEXTMATCH_PARAMETER));
+ }
- boolean preventCountOperation =
!parameters.containsKey(JsonConstants.NOCOUNT_PARAMETER)
- || "true".equals(parameters.get(JsonConstants.NOCOUNT_PARAMETER));
+ boolean preventCountOperation =
!parameters.containsKey(JsonConstants.NOCOUNT_PARAMETER)
+ || "true".equals(parameters.get(JsonConstants.NOCOUNT_PARAMETER));
- // only do the count if a paging request is done
- // note preventCountOperation variable is considered further below
- boolean doCount = false;
- int count = -1;
- int startRow = 0;
- int computedMaxResults = Integer.MAX_VALUE;
- if (startRowStr != null) {
- startRow = Integer.parseInt(startRowStr);
- queryService.setFirstResult(startRow);
- doCount = true;
- }
+ // only do the count if a paging request is done
+ // note preventCountOperation variable is considered further below
+ boolean doCount = false;
+ int count = -1;
+ int startRow = 0;
+ int computedMaxResults = Integer.MAX_VALUE;
+ if (startRowStr != null) {
+ startRow = Integer.parseInt(startRowStr);
+ queryService.setFirstResult(startRow);
+ doCount = true;
+ }
- if (endRowStr != null && endRowStr != null) {
- int endRow = Integer.parseInt(endRowStr);
- computedMaxResults = endRow - startRow + 1;
- // note computedmaxresults must be set before
- // endRow is increased by 1
- // increase by 1 to see if there are more results.
+ if (endRowStr != null && endRowStr != null) {
+ int endRow = Integer.parseInt(endRowStr);
+ computedMaxResults = endRow - startRow + 1;
+ // note computedmaxresults must be set before
+ // endRow is increased by 1
+ // increase by 1 to see if there are more results.
+ if (preventCountOperation) {
+ endRow++;
+ // set count here, is corrected in specific cases later
+ count = endRow;
+ }
+ queryService.setMaxResults(computedMaxResults);
+ doCount = true;
+ } else {
+ // can't do this if there is no endrow...
+ preventCountOperation = false;
+ }
+
+ final String sortBy = parameters.get(JsonConstants.SORTBY_PARAMETER);
+ if (sortBy != null) {
+ queryService.setOrderBy(sortBy);
+ } else if (parameters.get(JsonConstants.ORDERBY_PARAMETER) != null) {
+
queryService.setOrderBy(parameters.get(JsonConstants.ORDERBY_PARAMETER));
+ }
+
+ // compute a new startrow if the targetrecordid was passed in
+ int targetRowNumber = -1;
+ if (parameters.containsKey(JsonConstants.TARGETRECORDID_PARAMETER)) {
+ final String targetRecordId =
parameters.get(JsonConstants.TARGETRECORDID_PARAMETER);
+ targetRowNumber = queryService.getRowNumber(targetRecordId);
+ if (targetRowNumber != -1) {
+ queryService.setFirstResult(targetRowNumber);
+ startRow = targetRowNumber;
+ }
+ }
+
+ bobs = queryService.list();
+
if (preventCountOperation) {
- endRow++;
- // set count here, is corrected in specific cases later
- count = endRow;
+ count = bobs.size() + startRow;
+ // computedMaxResults is one too much, if we got one to much then
correct
+ // the result and up the count so that the grid knows that there are
more
+ if (bobs.size() == computedMaxResults) {
+ bobs = bobs.subList(0, bobs.size() - 1);
+ count++;
+ }
}
- queryService.setMaxResults(computedMaxResults);
- doCount = true;
- } else {
- // can't do this if there is no endrow...
- preventCountOperation = false;
- }
- final String sortBy = parameters.get(JsonConstants.SORTBY_PARAMETER);
- if (sortBy != null) {
- queryService.setOrderBy(sortBy);
- } else if (parameters.get(JsonConstants.ORDERBY_PARAMETER) != null) {
-
queryService.setOrderBy(parameters.get(JsonConstants.ORDERBY_PARAMETER));
- }
-
- // compute a new startrow if the targetrecordid was passed in
- int targetRowNumber = -1;
- if (parameters.containsKey(JsonConstants.TARGETRECORDID_PARAMETER)) {
- final String targetRecordId =
parameters.get(JsonConstants.TARGETRECORDID_PARAMETER);
- targetRowNumber = queryService.getRowNumber(targetRecordId);
- if (targetRowNumber != -1) {
- queryService.setFirstResult(targetRowNumber);
- startRow = targetRowNumber;
+ if (doCount && !preventCountOperation) {
+ count = queryService.count();
}
- }
-
- List<BaseOBObject> bobs = queryService.list();
-
- if (preventCountOperation) {
- // computedMaxResults is one too much, if we got one to much then
correct
- // the result, the count is already correct
- if (bobs.size() == computedMaxResults) {
- bobs = bobs.subList(0, bobs.size() - 1);
- } else {
- // got less so correct the count
- count = bobs.size() + startRow;
+ jsonResponse.put(JsonConstants.RESPONSE_STARTROW, startRow);
+ jsonResponse.put(JsonConstants.RESPONSE_ENDROW, (bobs.size() > 0 ?
bobs.size() + startRow
+ - 1 : 0));
+ // bobs can be empty and count > 0 if the order by forces a join
without results
+ if (bobs.isEmpty()) {
+ jsonResponse.put(JsonConstants.RESPONSE_TOTALROWS, 0);
+ } else if (doCount) {
+ jsonResponse.put(JsonConstants.RESPONSE_TOTALROWS, count);
}
}
@@ -169,27 +195,8 @@
DataToJsonConverter.class);
toJsonConverter.setAdditionalProperties(JsonUtils.getAdditionalProperties(parameters));
final List<JSONObject> jsonObjects = toJsonConverter.toJsonObjects(bobs);
-
- if (doCount && !preventCountOperation) {
- count = queryService.count();
- }
-
- final JSONObject jsonResult = new JSONObject();
- final JSONObject jsonResponse = new JSONObject();
- if (targetRowNumber != -1) {
- jsonResponse.put(JsonConstants.RESPONSE_SCROLLTO, targetRowNumber);
- }
- jsonResponse.put(JsonConstants.RESPONSE_STARTROWS, startRow);
+ jsonResponse.put(JsonConstants.RESPONSE_DATA, new
JSONArray(jsonObjects));
jsonResponse.put(JsonConstants.RESPONSE_STATUS,
JsonConstants.RPCREQUEST_STATUS_SUCCESS);
- jsonResponse.put(JsonConstants.RESPONSE_ENDROW, (bobs.size() > 0 ?
bobs.size() + startRow - 1
- : 0));
- // bobs can be empty and count > 0 if the order by forces a join without
results
- if (bobs.isEmpty()) {
- jsonResponse.put(JsonConstants.RESPONSE_TOTALROWS, 0);
- } else if (doCount) {
- jsonResponse.put(JsonConstants.RESPONSE_TOTALROWS, count);
- }
- jsonResponse.put(JsonConstants.RESPONSE_DATA, new
JSONArray(jsonObjects));
jsonResult.put(JsonConstants.RESPONSE_RESPONSE, jsonResponse);
// if (jsonObjects.size() > 0) {
diff -r fe902e8d37f1 -r 3bf58b3f0bce
modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonConstants.java
---
a/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonConstants.java
Tue Dec 14 13:25:28 2010 +0100
+++
b/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonConstants.java
Tue Dec 14 13:55:03 2010 +0100
@@ -64,9 +64,15 @@
public static final String TEXTMATCH_SUBSTRING = "substring";
// these constants are used in response
- public static final String RESPONSE_SCROLLTO = "scrollTo";
public static final String RESPONSE_STATUS = "status";
+
+ /**
+ * @deprecated has been replaced by {...@link #RESPONSE_STARTROW}
+ */
+ @Deprecated
public static final String RESPONSE_STARTROWS = "startRows";
+
+ public static final String RESPONSE_STARTROW = "startRow";
public static final String RESPONSE_ENDROW = "endRow";
public static final String RESPONSE_TOTALROWS = "totalRows";
public static final String RESPONSE_ERRORS = "errors";
diff -r fe902e8d37f1 -r 3bf58b3f0bce
modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonUtils.java
---
a/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonUtils.java
Tue Dec 14 13:25:28 2010 +0100
+++
b/modules/org.openbravo.service.json/src/org/openbravo/service/json/JsonUtils.java
Tue Dec 14 13:55:03 2010 +0100
@@ -124,7 +124,7 @@
try {
jsonResponse.put(JsonConstants.RESPONSE_STATUS,
JsonConstants.RPCREQUEST_STATUS_SUCCESS);
- jsonResponse.put(JsonConstants.RESPONSE_STARTROWS, "0");
+ jsonResponse.put(JsonConstants.RESPONSE_STARTROW, "0");
jsonResponse.put(JsonConstants.RESPONSE_ENDROW, "0");
jsonResponse.put(JsonConstants.RESPONSE_TOTALROWS, "0");
jsonResponse.put(JsonConstants.RESPONSE_DATA, new JSONArray());
------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits