Android Open Source - RealtimeStorage-Android Process Rest Response






From Project

Back to project page RealtimeStorage-Android.

License

The source code is released under:

MIT License

If you think the Android project RealtimeStorage-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package co.realtime.storage;
//from  w w w  .  j a  va  2s.co  m
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import co.realtime.storage.StorageRef.StorageDataType;
import co.realtime.storage.StorageRef.StorageOrder;
import co.realtime.storage.StorageRef.StorageProvisionLoad;
import co.realtime.storage.StorageRef.StorageProvisionType;
import co.realtime.storage.entities.TableMetadata;
import co.realtime.storage.ext.OnBooleanResponse;
import co.realtime.storage.ext.OnItemSnapshot;
import co.realtime.storage.ext.OnTableCreation;
import co.realtime.storage.ext.OnTableMetadata;
import co.realtime.storage.ext.OnTableUpdate;

//import co.realtime.storage.ext.OnItemSnapshot;
//import co.realtime.storage.ext.OnTableCreation;
//import co.realtime.storage.ext.OnTableMetadata;
import co.realtime.storage.ext.OnTableSnapshot;


class ProcessRestResponse {
  public static void processIsAuthenticated(Map<String, Object> response, OnBooleanResponse onBooleanResponse){
    if(onBooleanResponse == null) return;
    onBooleanResponse.run((Boolean)response.get("data"));
  }
  
  @SuppressWarnings("unchecked")
  public static void processListTables(Map<String, Object> response, StorageContext context,  OnTableSnapshot onTableSnapshot){    
    if(onTableSnapshot == null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    ArrayList<String> tables = (ArrayList<String>) linkedHashMap.get("tables");
    for(String tabName : tables){
      onTableSnapshot.run(new TableSnapshot(context, tabName));
    }
    onTableSnapshot.run(null);
  }
  
  @SuppressWarnings("unchecked")
  public static void processCreateTable(Map<String, Object> response, OnTableCreation onTableCreation){
    if(onTableCreation == null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    String tableName = (String) linkedHashMap.get("table");
    Double creationDate = (Double) linkedHashMap.get("creationDate");
    String status = (String) linkedHashMap.get("status");
    onTableCreation.run(tableName, creationDate, status);
  }
  
  @SuppressWarnings("unchecked")
  public static void processDeleteTable(Map<String, Object> response, OnBooleanResponse onBooleanResponse){
    if(onBooleanResponse == null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    String status = (String)linkedHashMap.get("status");
    if(status.equals("deleting")){
      onBooleanResponse.run(true);
    } else {
      onBooleanResponse.run(false);
    }
  }
  
  @SuppressWarnings("unchecked")
  public static void processDescribeTable(Map<String, Object> response, StorageContext context, OnTableMetadata onTableMetadata){    
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    TableMetadata tm = new TableMetadata();
    tm.setApplicationKey((String) linkedHashMap.get("applicationKey"));
    tm.setName((String) linkedHashMap.get("name"));
    LinkedHashMap<String, Object> provisionType = (LinkedHashMap<String, Object>)linkedHashMap.get("provisionType");
    tm.setProvisionType( StorageProvisionType.values()[(Integer) provisionType.get("id") - 1]);
    LinkedHashMap<String, Object> provisionLoad = (LinkedHashMap<String, Object>)linkedHashMap.get("provisionLoad");
    tm.setProvisionLoad(StorageProvisionLoad.values()[(Integer) provisionLoad.get("id") - 1]);
    LinkedHashMap<String, Object> throughput = (LinkedHashMap<String, Object>)linkedHashMap.get("throughput");
    tm.setThroughputRead((Integer) throughput.get("read"));
    tm.setThroughputWrite((Integer) throughput.get("write"));
    tm.setCreationDate((Long) linkedHashMap.get("creationDate"));
    tm.setUpdateDate((Long) linkedHashMap.get("updateDate"));
    tm.setIsActive((Boolean) linkedHashMap.get("isActive"));
    LinkedHashMap<String, Object> key = (LinkedHashMap<String, Object>)linkedHashMap.get("key");
    LinkedHashMap<String, Object> primary = (LinkedHashMap<String, Object>)key.get("primary");
    tm.setPrimaryKeyName((String) primary.get("name"));
    tm.setPrimaryKeyType(StorageDataType.fromString((String)primary.get("dataType")));        
    LinkedHashMap<String, Object> secondary = (LinkedHashMap<String, Object>)key.get("secondary");
    if(secondary!=null){
      tm.setSecondaryKeyName((String) secondary.get("name"));
      tm.setSecondaryKeyType(StorageDataType.fromString((String) secondary.get("dataType")));
    }
    tm.setStatus((String) linkedHashMap.get("status"));
    tm.setSize(Long.parseLong(String.valueOf(linkedHashMap.get("size"))));
    tm.setItemCount(Long.parseLong(String.valueOf(linkedHashMap.get("itemCount"))));
    
    context.addTableMeta(tm);
    
    if(onTableMetadata != null)
      onTableMetadata.run(tm);
    
  }
  
  private static void _fireItemSnapshotCallback(OnItemSnapshot onItemSnapshot, TableRef tableRef, LinkedHashMap<String, ItemAttribute> item){
    TableMetadata tm = tableRef.context.getTableMeta(tableRef.name);
    String primary = tm.getPrimaryKeyName();
    String secondary = tm.getSecondaryKeyName();
    ItemAttribute primaryValue = item.get(primary);
    ItemAttribute secondaryValue = null;
    if(secondary!=null)
      secondaryValue = item.get(secondary);
    onItemSnapshot.run(new ItemSnapshot(tableRef, item, primaryValue, secondaryValue));
  }
  
  private static void fireItemSnapshotCallback(final OnItemSnapshot onItemSnapshot, final TableRef tableRef, final LinkedHashMap<String, ItemAttribute> item){
    TableMetadata tm = tableRef.context.getTableMeta(tableRef.name);
    if(tm==null){
      tableRef.meta(new OnTableMetadata(){
        @Override
        public void run(TableMetadata tableMetadata) {
          _fireItemSnapshotCallback(onItemSnapshot, tableRef, item);
        }}, null);
    } else {
      _fireItemSnapshotCallback(onItemSnapshot, tableRef, item);
    }
  }
  
  @SuppressWarnings("unchecked")
  public static void processPutItem(Map<String, Object> response, TableRef tableRef, OnItemSnapshot onItemSnapshot){
    if(onItemSnapshot==null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    LinkedHashMap<String, ItemAttribute> item = convertItemMap(linkedHashMap);
    fireItemSnapshotCallback(onItemSnapshot, tableRef, item);
    //ItemSnapshot item = new ItemSnapshot(tableRef, itemMap);    
    //onItemSnapshot.run(item);
  }  
  
  @SuppressWarnings("unchecked")
  public static void processUpdateTable(Map<String, Object> response, OnTableUpdate onTableUpdate){
    if(onTableUpdate==null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    String tableName = (String) linkedHashMap.get("table");
    String status = (String) linkedHashMap.get("status");
    onTableUpdate.run(tableName, status);
  }

  public static void processListItems(ArrayList<LinkedHashMap<String, Object>> allItems, TableRef tableRef, OnItemSnapshot onItemSnapshot, StorageOrder order, String sortKey, Long limit){
    if(onItemSnapshot==null) return;
    ArrayList<LinkedHashMap<String, ItemAttribute>> items = new ArrayList<LinkedHashMap<String, ItemAttribute>>();
    for(LinkedHashMap<String, Object> item : allItems){
      items.add(convertItemMap(item));
      //onItemSnapshot.run(new ItemSnapshot(tableRef, convertItemMap(item)));
    }
    if(order == StorageOrder.ASC){
      Collections.sort(items, new LHMItemsComparator(sortKey));
    }
    if(order == StorageOrder.DESC){
      Collections.sort(items, Collections.reverseOrder(new LHMItemsComparator(sortKey)));
    }
    /*
    for(LinkedHashMap<String, StorageItem> item : items){      
      onItemSnapshot.run(new ItemSnapshot(tableRef, item));
    }*/
    Long itemsToDeliver = (limit == null) ? items.size() : (limit > items.size() ? items.size() : limit );
    for(int l = 0; l < itemsToDeliver; l++){
      //onItemSnapshot.run(new ItemSnapshot(tableRef, items.get(l)));
      fireItemSnapshotCallback(onItemSnapshot, tableRef, items.get(l));
    }
    onItemSnapshot.run(null);
  }
  
  public static void processQueryItems(ArrayList<LinkedHashMap<String, Object>> allItems, TableRef tableRef, OnItemSnapshot onItemSnapshot){
    if(onItemSnapshot==null) return;
    for(LinkedHashMap<String, Object> item : allItems){    
      //onItemSnapshot.run(new ItemSnapshot(tableRef, convertItemMap(item)));
      LinkedHashMap<String, ItemAttribute> itemMap = convertItemMap(item);
      fireItemSnapshotCallback(onItemSnapshot, tableRef, itemMap);
    }
    onItemSnapshot.run(null);
  }  
  
  @SuppressWarnings("unchecked")
  public static void processDelItem(Map<String, Object> response, TableRef tableRef, OnItemSnapshot onItemSnapshot){
    if(onItemSnapshot==null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    if(linkedHashMap == null || linkedHashMap.size()==0){
      onItemSnapshot.run(null);
      return;
    }
    //ItemSnapshot item = new ItemSnapshot(tableRef, convertItemMap(linkedHashMap));    
    //onItemSnapshot.run(item);
    LinkedHashMap<String, ItemAttribute> item = convertItemMap(linkedHashMap);
    fireItemSnapshotCallback(onItemSnapshot, tableRef, item);
  }  
  
  @SuppressWarnings("unchecked")
  public static void processGetItem(Map<String, Object> response, TableRef tableRef, OnItemSnapshot onItemSnapshot, boolean endWithNull){
    if(onItemSnapshot==null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    if(linkedHashMap == null || linkedHashMap.size()==0){
      onItemSnapshot.run(null);
      return;
    }
    //ItemSnapshot item = new ItemSnapshot(tableRef, convertItemMap(linkedHashMap));    
    //onItemSnapshot.run(item);
    LinkedHashMap<String, ItemAttribute> item = convertItemMap(linkedHashMap);
    fireItemSnapshotCallback(onItemSnapshot, tableRef, item);
    if(endWithNull){
      onItemSnapshot.run(null);
    }
  }    
  
  @SuppressWarnings("unchecked")
  public static void processUpdateItem(Map<String, Object> response, TableRef tableRef, OnItemSnapshot onItemSnapshot){
    if(onItemSnapshot==null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    if(linkedHashMap == null || linkedHashMap.size()==0){
      onItemSnapshot.run(null);
      return;
    }
    //ItemSnapshot item = new ItemSnapshot(tableRef, convertItemMap(linkedHashMap));    
    //onItemSnapshot.run(item);
    LinkedHashMap<String, ItemAttribute> item = convertItemMap(linkedHashMap);
    fireItemSnapshotCallback(onItemSnapshot, tableRef, item);
  }    
  
  public static LinkedHashMap<String, ItemAttribute> convertItemMap(LinkedHashMap<String, Object> map){
    LinkedHashMap<String, ItemAttribute> ret = new LinkedHashMap<String, ItemAttribute>();
    for (Entry<String, Object> entry : map.entrySet()){
      Object value = entry.getValue();
      if(value instanceof String)
        ret.put(entry.getKey(), new ItemAttribute((String)value));
      if(value instanceof Number)
        ret.put(entry.getKey(), new ItemAttribute((Number)value));
    }
    return ret;
  }
  
  @SuppressWarnings("unchecked")
  public static void processInDeCrResponse(Map<String, Object> response, TableRef tableRef, OnItemSnapshot onItemSnapshot){
    if(onItemSnapshot==null) return;
    LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>)response.get("data");
    LinkedHashMap<String, ItemAttribute> item = convertItemMap(linkedHashMap);
    fireItemSnapshotCallback(onItemSnapshot, tableRef, item);
  }
}




Java Source Code List

adapters.TodoCustomAdapter.java
co.realtime.sample.ApplicationTest.java
co.realtime.sample.MainActivity.java
co.realtime.storage.ApplicationTest.java
co.realtime.storage.EventCollection.java
co.realtime.storage.Event.java
co.realtime.storage.Filter.java
co.realtime.storage.ItemAttribute.java
co.realtime.storage.ItemRef.java
co.realtime.storage.ItemSnapshot.java
co.realtime.storage.LHMItemsComparator.java
co.realtime.storage.OnRestCompleted.java
co.realtime.storage.PostBodyBuilder.java
co.realtime.storage.ProcessRestResponse.java
co.realtime.storage.RestWebservice.java
co.realtime.storage.Rest.java
co.realtime.storage.StorageContext.java
co.realtime.storage.StorageRef.java
co.realtime.storage.TableRef.java
co.realtime.storage.TableSnapshot.java
co.realtime.storage.entities.Heartbeat.java
co.realtime.storage.entities.IORMapping.java
co.realtime.storage.entities.KeySchema.java
co.realtime.storage.entities.Key.java
co.realtime.storage.entities.TableMetadata.java
co.realtime.storage.entities.Throughput.java
co.realtime.storage.ext.OnBooleanResponse.java
co.realtime.storage.ext.OnError.java
co.realtime.storage.ext.OnHeartbeat.java
co.realtime.storage.ext.OnItemSnapshot.java
co.realtime.storage.ext.OnPresence.java
co.realtime.storage.ext.OnReconnected.java
co.realtime.storage.ext.OnReconnecting.java
co.realtime.storage.ext.OnTableCreation.java
co.realtime.storage.ext.OnTableMetadata.java
co.realtime.storage.ext.OnTableSnapshot.java
co.realtime.storage.ext.OnTableUpdate.java
co.realtime.storage.ext.StorageException.java
config.Config.java
handlers.StorageHandler.java
helpers.ListNameHelper.java
listeners.ClickListener.java
listeners.EditorListener.java
ui.MyViewPager.java