You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2015/11/28 06:30:47 UTC

[39/47] olingo-odata4 git commit: [OLINGO-568] Minor TecSvc enhancements

[OLINGO-568] Minor TecSvc enhancements


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/326e1775
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/326e1775
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/326e1775

Branch: refs/heads/OLINGO-811_CountForExpand
Commit: 326e1775a7ac5a9bd70fb39a4ffff24b301f97a7
Parents: 386f5a4
Author: mibo <mi...@apache.org>
Authored: Tue Nov 17 22:07:56 2015 +0100
Committer: mibo <mi...@apache.org>
Committed: Tue Nov 17 22:07:56 2015 +0100

----------------------------------------------------------------------
 .../queryoptions/options/SearchHandler.java     | 37 ++++++++++++++------
 1 file changed, 26 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/326e1775/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/options/SearchHandler.java
----------------------------------------------------------------------
diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/options/SearchHandler.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/options/SearchHandler.java
index e56e083..ccc1658 100644
--- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/options/SearchHandler.java
+++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/options/SearchHandler.java
@@ -29,26 +29,29 @@ import org.apache.olingo.server.api.uri.queryoption.search.SearchBinaryOperatorK
 import org.apache.olingo.server.api.uri.queryoption.search.SearchExpression;
 import org.apache.olingo.server.api.uri.queryoption.search.SearchTerm;
 
+import javax.xml.bind.DatatypeConverter;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
 import java.util.Iterator;
-import java.util.List;
+import java.util.ListIterator;
 import java.util.Locale;
 
 public class SearchHandler {
+  private static SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+
   public static void applySearchSystemQueryOption(final SearchOption searchOption, final EntityCollection entitySet)
       throws ODataApplicationException {
 
     if (searchOption != null) {
+      SearchExpression se = searchOption.getSearchExpression();
       Iterator<Entity> it = entitySet.getEntities().iterator();
       while(it.hasNext()) {
         boolean keep = false;
-        Entity next = it.next();
-        List<Property> propertyList = next.getProperties();
-        for (Property property : propertyList) {
-          SearchExpression se = searchOption.getSearchExpression();
-          if(isTrue(se, property)) {
-            keep = true;
-            break;
-          }
+        Entity entity = it.next();
+        ListIterator<Property> properties = entity.getProperties().listIterator();
+        while(properties.hasNext() && !keep) {
+          keep = isTrue(se, properties.next());
         }
         if(!keep) {
           it.remove();
@@ -59,13 +62,25 @@ public class SearchHandler {
 
   private static boolean isTrue(SearchTerm term, Property property) {
     if(property.isPrimitive() && !property.isNull()) {
-      // TODO: mibo(151117): pass EDM information to do correct 'string' convertation
-      String propertyString = property.asPrimitive().toString();
+      String propertyString = asString(property);
       return propertyString != null && propertyString.contains(term.getSearchTerm());
     }
     return false;
   }
 
+  private static String asString(Property property) {
+    // TODO: mibo(151117): improve 'string' conversion
+    Object primitive = property.asPrimitive();
+    if(primitive instanceof Calendar) {
+      return SIMPLE_DATE_FORMAT.format(((Calendar) primitive).getTime());
+    } else if(primitive instanceof Date) {
+      return SIMPLE_DATE_FORMAT.format((Date) primitive);
+    } else if(primitive instanceof byte[]) {
+      return DatatypeConverter.printBase64Binary((byte[]) primitive);
+    }
+    return primitive.toString();
+  }
+
   private static boolean isTrue(SearchBinary binary, Property property) throws ODataApplicationException {
     SearchExpression left = binary.getLeftOperand();
     SearchExpression right = binary.getRightOperand();