如何将过滤条件传递给gae项目中的API for android

时间:2014-12-20 18:35:25

标签: android google-app-engine

我是android和GAE的新手,并尝试创建一个管理车辆库存的小型Android应用程序。我陷入了一个需要为特定经销商获取总计库存数量的情况。 Google创建的默认API不支持任何此类功能,因此我创建了自己的一个正常工作。但问题是,我需要从我的Android应用程序传递过滤条件,我不知道如何实现这一点。

我的GAE代码

@ApiMethod(name = "listAggregatedStock", path = "listAggregatedStock")
public CollectionResponse<AggregatedStock> listAggregatedStock(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit,
        @Nullable @Named("columns") String[] columns,
        @Nullable @Named("values") String[] values) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<AggregatedStock> execute = null;

    StringBuffer queryString = new StringBuffer();

    queryString.append(" select date, vehicleCode, vehicleSubCode, colorCode, sum(count) from Stock as AggregatedStock ");

    if(columns != null && columns.length > 0) {
        queryString.append(" where ");
        int i = 0;
        for(String column : columns){
            if(i > 0){
                queryString.append(" and ");
            }
            queryString.append(column + " = :" + column);
            i++;
        }
    }

    queryString.append(" group by date, vehicleCode, vehicleSubCode, colorCode ");

    try {
        mgr = getEntityManager();
        Query query = mgr.createQuery(queryString.toString());
        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }

        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }

        if(columns != null && columns.length > 0) {
            for(int i = 0; i < columns.length; i++) {
                query.setParameter(columns[i], values[i]);
            }
        }

        List<Object[]> results = (List<Object[]>) query.getResultList();

        execute = new ArrayList<AggregatedStock>();

        for (Object[] result : results) {
            execute.add(new AggregatedStock((Date) result[0],
                    (String) result[1], (String) result[2],
                    (String) result[3], ((Long) result[4]).intValue()));
        }

        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and
        // accomodate
        // for lazy fetch.
        for (AggregatedStock obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<AggregatedStock> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}

这就是我从我的Android应用程序中调用它的方式

@Override
    protected CollectionResponseAggregatedStock doInBackground(
            String... params) {

        String dealer = params[0];
        String vehicle = params[1];

        Stockendpoint.Builder endpointBuilder = new Stockendpoint.Builder(
                AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
                null);

        endpointBuilder = CloudEndpointUtils.updateBuilder(endpointBuilder);

        CollectionResponseAggregatedStock result;

        Stockendpoint endpoint = endpointBuilder.build();

        try {
            result = endpoint.listAggregatedStock().execute();
        } catch (IOException e) {
            e.printStackTrace();
            result = null;
        }
        return result;
    }

当前问题我面临的是,我无法传递列和值字符串[]。当我像这样添加它们时

result = endpoint.listAggregatedStock(null, null, new String[]{"column"}, new String[]{"value"}).execute();

签名不匹配。我不确定这是不是正确的方法。我使用的是Google插件生成的云库

1 个答案:

答案 0 :(得分:0)

我终于能够以这种方式实现它

@SuppressWarnings({ "unchecked" })
@ApiMethod(name = "listAggregatedStock", path = "listAggregatedStock")
public List<AggregatedStock> listAggregatedStock(@Nonnull @Named("dealer") String dealer) {

    List<AggregatedStock> execute = null;

    PersistenceManager pm = getPersistenceManager();

    StringBuilder sb = new StringBuilder();
    sb.append(" select date, vehicleCode, vehicleSubCode, colorCode, ");
    sb.append(" SUM(count) from Stock as AggregatedStock ");

    Query query = pm
            .newQuery(sb.toString());

    query.setFilter(" updatedByDealer == " + dealer);
    query.setGrouping(" date, vehicleCode, vehicleSubCode, colorCode ");
    query.setOrdering(" vehicleCode desc ");

    query.declareImports("import com.sandeepapplabs.dms.Stock");

    try {
        List<Object[]> results = (List<Object[]>) query.execute();

        execute = new ArrayList<AggregatedStock>();

        for (Object[] result : results) {
            execute.add(new AggregatedStock((Date) result[0],
                    (String) result[1], (String) result[2],
                    (String) result[3], ((Long) result[4]).intValue()));
        }

    } finally {
        pm.close();
    }

    return execute;
}

我没有采用GAE默认方式,而是实施了JDO