Android Open Source - Android-App-Project___Stay-Anonymous Cloud Query






From Project

Back to project page Android-App-Project___Stay-Anonymous.

License

The source code is released under:

Apache License

If you think the Android project Android-App-Project___Stay-Anonymous 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

/*
 * Copyright (c) 2013 Google Inc./*from   w ww  .j  a  v a  2s. co m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.backend.core;

import com.google.cloud.backend.android.mobilebackend.model.QueryDto;
import com.google.gson.Gson;

import android.util.Base64;

/**
 * A query object to execute it with {@link CloudBackend}. See
 * {@link CloudBackendTest#testList()} for detailed usage.
 */
public class CloudQuery {

    private static final Gson gson = new Gson();

    /**
     * An enum for specifying sort order of the query.
     */
    public enum Order {
        ASC, DESC
    };

    /**
     * An enum to specify scope of query.
     */
    public enum Scope {
        /**
         * PAST: query will be executed only for the entities that have been
         * stored in past, and will not be executed when the entities will be
         * updated in future.
         */
        PAST,

        /**
         * FUTURE: query will be executed only when entities are updated in
         * future, and will not be executed against the current entities.
         */
        FUTURE,

        /**
         * FUTURE_AND_PAST: query will be executed for the current entities and
         * future updates.
         */
        FUTURE_AND_PAST
    }

    private Filter filter;

    private final QueryDto queryDto;

    /**
     * Creates an instance of {@link CloudQuery}.
     * 
     * @param kindName Name of the kind that this query will execute with.
     */
    public CloudQuery(String kindName) {
        this.queryDto = new QueryDto();
        this.queryDto.setKindName(kindName);
        this.queryDto.setScope(Scope.PAST.name());
    }

    /**
     * Creates an instance of {@link CloudQuery} from the values of the
     * specified CloudQuery. queryDto field will be deep copied, and filter
     * field will be shallow copied.
     * 
     * @param cq {@link CloudQuery} that will be used for instantiation.
     */
    public CloudQuery(CloudQuery cq) {
        this.queryDto = copyQueryDto(cq.queryDto);
        this.filter = cq.filter;
    }

    private QueryDto copyQueryDto(QueryDto cq) {
        QueryDto ncq = new QueryDto();
        ncq.setFilterDto(cq.getFilterDto());
        ncq.setKindName(cq.getKindName());
        ncq.setLimit(cq.getLimit());
        ncq.setQueryId(cq.getQueryId());
        ncq.setRegId(cq.getRegId());
        ncq.setScope(cq.getScope());
        ncq.setSortAscending(cq.getSortAscending());
        ncq.setSortedPropertyName(cq.getSortedPropertyName());
        ncq.setSubscriptionDurationSec(cq.getSubscriptionDurationSec());
        return ncq;
    }

    /**
     * Sets registration id.
     * 
     * @param regId
     */
    protected void setRegId(String regId) {
        this.queryDto.setRegId(regId);
    }

    /**
     * Returns a kind name of this query.
     * 
     * @return kind name
     */
    public String getKindName() {
        return this.queryDto.getKindName();
    }

    /**
     * Set Sort property name and sort order to this query.
     * 
     * @param propertyName Name of the target name.
     * @param sortOrder {@link Order} of this sort.
     * @return {@link CloudQuery}
     */
    public CloudQuery setSort(String propertyName, Order sortOrder) {
        this.queryDto.setSortedPropertyName(propertyName);
        this.queryDto.setSortAscending(sortOrder == Order.ASC);
        return this;
    }

    /**
     * Set Limit number on the result set to this query.
     * 
     * @param limit Max number of lines for the query result set.
     * @return {@link CloudQuery}
     */
    public CloudQuery setLimit(int limit) {
        this.queryDto.setLimit(limit);
        return this;
    }

    /**
     * Creates a {@link QueryDto} for this CloudQuery.
     * 
     * @return {@link QueryDto} for this CloudQuery
     */
    protected QueryDto convertToQueryDto() {

        // generate FilterDto
        if (filter != null) {
            this.queryDto.setFilterDto(filter.getFilterDto());
        }
        return this.queryDto;
    }

    /**
     * Returns {@link Filter} (filter) for this query.
     * 
     * @return {@link Filter}
     */
    public Filter getFilter() {
        return filter;
    }

    /**
     * Sets a filter {@link Filter} for this query.
     * 
     * @param filter
     * @return {@link CloudQuery}
     */
    public CloudQuery setFilter(Filter filter) {
        this.filter = filter;
        return this;
    }

    /**
     * Sets a {@link Scope} of this query.
     * 
     * @param scope {@link Scope} of this query.
     * @return {@link CloudQuery}
     */
    public CloudQuery setScope(Scope scope) {
        this.queryDto.setScope(scope.name());
        return this;
    }

    /**
     * Returns {@link Scope} of this query.
     * 
     * @return
     */
    public Scope getScope() {
        return Scope.valueOf(this.queryDto.getScope());
    }

    /**
     * Returns true if this query is a continuous query.
     * 
     * @return
     */
    public boolean isContinuous() {
        return (getScope() == Scope.FUTURE || getScope() == Scope.FUTURE_AND_PAST);
    }

    /**
     * Sets subscription duration (in sec) for this query.
     * 
     * @param duration
     */
    public void setSubscriptionDurationSec(int duration) {
        this.queryDto.setSubscriptionDurationSec(duration);
    }

    @Override
    public String toString() {
        return "CloudQuery (" + this.queryDto.getKindName() + "/" + getScope() + "): filter: "
                + filter;
    }

    /**
     * Sets the specified queryId to this query. This method will be used when
     * developer want to control identity of each query explicitly, such as
     * grouping multiple {@link CloudQuery} as one query and map it to one
     * handler.
     * 
     * @param queryId {@link String} that identifies each query
     */
    public void setQueryId(String queryId) {
        this.queryDto.setQueryId(queryId);
    }

    /**
     * Returns queryId of this query. Unless developer sets it by
     * {@link #setQueryId(String)} explicitly, this method will return an ID
     * based on the query condition. It means the same ID will be returned for
     * multiple {@link CloudQuery}s if they have the same query condition.
     * 
     * @return
     */
    public String getQueryId() {
        setDefaultQueryIdIfNeeded();
        return this.queryDto.getQueryId();
    }

    private void setDefaultQueryIdIfNeeded() {
        if (this.queryDto.getQueryId() == null) {
            int queryHash = (gson.toJson(queryDto) + gson.toJson(filter)).hashCode();
            String queryId = Base64.encodeToString(String.valueOf(queryHash).getBytes(),
                    Base64.NO_PADDING | Base64.NO_WRAP);
            this.queryDto.setQueryId(queryId);
        }
    }
}




Java Source Code List

com.google.cloud.backend.GCMBroadcastReceiver.java
com.google.cloud.backend.GCMIntentService.java
com.google.cloud.backend.android.mobilebackend.MobilebackendRequestInitializer.java
com.google.cloud.backend.android.mobilebackend.MobilebackendRequest.java
com.google.cloud.backend.android.mobilebackend.Mobilebackend.java
com.google.cloud.backend.android.mobilebackend.model.BlobAccess.java
com.google.cloud.backend.android.mobilebackend.model.EntityDto.java
com.google.cloud.backend.android.mobilebackend.model.EntityListDto.java
com.google.cloud.backend.android.mobilebackend.model.FilterDto.java
com.google.cloud.backend.android.mobilebackend.model.Filter.java
com.google.cloud.backend.android.mobilebackend.model.QueryDto.java
com.google.cloud.backend.core.CloudBackendAsync.java
com.google.cloud.backend.core.CloudBackendFragment.java
com.google.cloud.backend.core.CloudBackendMessaging.java
com.google.cloud.backend.core.CloudBackend.java
com.google.cloud.backend.core.CloudCallbackHandler.java
com.google.cloud.backend.core.CloudEndpointUtils.java
com.google.cloud.backend.core.CloudEntity.java
com.google.cloud.backend.core.CloudQuery.java
com.google.cloud.backend.core.Consts.java
com.google.cloud.backend.core.Filter.java
mobile_final.ChattingRoomActivity.java
mobile_final.EventListAdaptor.java
mobile_final.PostAdapter.java
mobile_final.SecondActivity.java
mobile_final.SplashFragment.java
mobile_final.StartingActivity.java
mobile_final.TestActivity.java
mobile_final.VoteHandler.java