Android Open Source - meets-android Mage Meets Collection






From Project

Back to project page meets-android.

License

The source code is released under:

MIT License

If you think the Android project meets-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 com.theagilemonkeys.meets.magento.models.base;
//from w  ww  . ja v  a  2  s  .  com
import com.theagilemonkeys.meets.ApiMethod;
import com.theagilemonkeys.meets.ApiMethodModelHelper;
import com.theagilemonkeys.meets.models.base.MeetsCollection;
import com.theagilemonkeys.meets.models.base.MeetsListener;
import com.theagilemonkeys.meets.models.base.MeetsModel;

import org.jdeferred.AlwaysCallback;
import org.jdeferred.Deferred;
import org.jdeferred.DonePipe;
import org.jdeferred.FailPipe;
import org.jdeferred.Promise;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Android Meets SDK
 * Original work Copyright (c) 2014 [TheAgileMonkeys]
 *
 * @author ??lvaro Lpez Espinosa
 */
public class MageMeetsCollection<MODEL extends MeetsModel> extends ArrayList<MODEL> implements MeetsCollection<MODEL> {
    private transient ApiMethodModelHelper<MeetsCollection<MODEL>> apiMethodCtrl = new ApiMethodModelHelper<MeetsCollection<MODEL>>(this);
    private Class<? extends ApiMethod> fetchApiMethodClass;
    protected int page = 1;
    protected int pageSize = 20;
    protected boolean resetOnFetch = false;
    protected Map<String, Object> filters = new HashMap<String, Object>();

    public MageMeetsCollection(Class<? extends ApiMethod> fetchApiMethodClass) {
        this.fetchApiMethodClass = fetchApiMethodClass;
    }

    @Override
    public MeetsCollection<MODEL> fetch() {
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                filters.put("page", page);
                filters.put("limit", pageSize);
                return filters;
            }
        };

        try {
            ApiMethod apiMethod = fetchApiMethodClass.getConstructor(boolean.class)
                                                     .newInstance(true);

            pushMethod(apiMethod, params).always(updateAndTrigger);
            return this;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public MeetsCollection<MODEL> insert(MODEL model) {
        int oldIndexModel = getIndex(model.getId());

        if ( oldIndexModel < 0 ){
            add(model);
            // Trigger MeetsCollection specific listeners
            Map<MeetsListener<MeetsCollection<MODEL>>, Boolean> listeners = apiMethodCtrl.getListenersWithKeepInfo();
            for ( MeetsListener<MeetsCollection<MODEL>> listener : listeners.keySet()) {
                if (listener instanceof MeetsListener.Collection)
                    ((MeetsListener.Collection<MeetsCollection<MODEL>, MODEL>) listener).onPush(this, model);
            }
        }
        return this;
    }

    @Override
    public MODEL extract(int modelId) {
        MODEL model = null;
        int oldIndexModel = getIndex(modelId);
        if ( oldIndexModel >= 0 ) {
            model = remove(oldIndexModel);
            // Trigger MeetsCollection specific listeners
            Map<MeetsListener<MeetsCollection<MODEL>>, Boolean> listeners = apiMethodCtrl.getListenersWithKeepInfo();
            for ( MeetsListener<MeetsCollection<MODEL>> listener : listeners.keySet()) {
                if (listener instanceof MeetsListener.Collection)
                    ((MeetsListener.Collection<MeetsCollection<MODEL>, MODEL>) listener).onExtract(this, model);
            }
        }
        return model;
    }

    @Override
    public MeetsCollection<MODEL> reset() {
        // We have to extract models manually to trigger events
        for (MeetsModel meetsModel : this){
            extract(meetsModel.getId());
        }
        // Trigger MeetsCollection specific listeners
        Map<MeetsListener<MeetsCollection<MODEL>>, Boolean> listeners = apiMethodCtrl.getListenersWithKeepInfo();
        for ( MeetsListener<MeetsCollection<MODEL>> listener : listeners.keySet()) {
            if (listener instanceof MeetsListener.Collection)
                ((MeetsListener.Collection<MeetsCollection<MODEL>, MODEL>) listener).onReset(this);
        }
        return this;
    }

    @Override
    public int getSize() {
        return size();
    }

    @Override
    public MODEL get(int index) {
        return super.get(index);
    }

    public int getIndex(int modelId){
        int n = size();
        for( int i = 0; i < n; ++i)  {
            MODEL model = get(i);
            if ( model.getId() == modelId )
                return i;
        }
        return -1;
    }

    @Override
    public MeetsCollection<MODEL> nextPage() {
        ++page;
        return this;
    }

    @Override
    public MeetsCollection<MODEL> setPage(int page) {
        this.page = page;
        return this;
    }

    @Override
    public MeetsCollection<MODEL> setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    @Override
    public MeetsCollection<MODEL> setResetOnFetch(boolean reset) {
        resetOnFetch = reset;
        return this;
    }

    @Override
    public MeetsCollection<MODEL> setFilters(Map<String, Object> filters) {
        this.filters = filters;
        return this;
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public MeetsCollection<MODEL> include(String... weakAttributes) {
        throw new UnsupportedOperationException("Still not implemented");
    }

    protected transient AlwaysCallback updateAndTrigger = new AlwaysCallback() {
        @Override
        public void onAlways(Promise.State state, Object resolved, Object rejected) {
            if (state == Promise.State.RESOLVED) updateFromFetchedResult(resolved);
            triggerListeners((Exception) rejected);
        }
    };

    public void updateFromFetchedResult(Object result) {
        Collection<MODEL> models;
        if (result instanceof Map){
            // Rest results comes in Map format
            models = ( (Map<String, MODEL>) result).values();
        }
        else{
            //Soap result comes in collection format
            models = (Collection<MODEL>) result;
        }

        if (resetOnFetch){
            reset();
        }

        for (MODEL model : models){
            insert(model);
        }
    }

    /////////////////////////////////////////////////////////////////////////
    //////////////////// ApiMethodModelHelper delegation /////////////////////
    /////////////////////////////////////////////////////////////////////////


    public Promise pushMethod(ApiMethod method) {
        return apiMethodCtrl.pushMethod(method);
    }

    public MeetsCollection<MODEL> forceNextCacheToBe(boolean enable) {
        return apiMethodCtrl.forceNextCacheToBe(enable);
    }

    public Promise pushPipe(DonePipe donePipe, FailPipe failPipe) {
        return apiMethodCtrl.pushPipe(donePipe, failPipe);
    }

    public Map<MeetsListener<MeetsCollection<MODEL>>,Boolean> getListenersWithKeepInfo() {
        return apiMethodCtrl.getListenersWithKeepInfo();
    }

    public void triggerListeners(Exception e) {
        apiMethodCtrl.triggerListeners(e);
    }

    public Promise pushMethod(ApiMethod method, ApiMethodModelHelper.DelayedParams params) {
        return apiMethodCtrl.pushMethod(method, params);
    }

    public Promise pushDeferred(Deferred deferred) {
        return apiMethodCtrl.pushDeferred(deferred);
    }

    public MeetsCollection<MODEL> setModelCache(boolean enable) {
        return apiMethodCtrl.setModelCache(enable);
    }

    public void triggerListeners() {
        apiMethodCtrl.triggerListeners();
    }

    @Override
    public MeetsCollection<MODEL> await(MeetsListener<MeetsCollection<MODEL>> listener) {
        return apiMethodCtrl.await(listener);
    }

    @Override
    public MeetsCollection<MODEL> await(MeetsListener<MeetsCollection<MODEL>> listener, boolean keepListening) {
        return apiMethodCtrl.await(listener, keepListening);
    }

    @Override
    public MeetsCollection<MODEL> noLongerWait(MeetsListener<MeetsCollection<MODEL>> listener) {
        return apiMethodCtrl.noLongerWait(listener);
    }

    @Override
    public MeetsCollection<MODEL> allNoLongerWait() {
        return apiMethodCtrl.allNoLongerWait();
    }

    @Override
    public MeetsCollection<MODEL> nextWaitForPrevious() {
        return apiMethodCtrl.nextWaitForPrevious();
    }
}




Java Source Code List

com.theagilemonkeys.meets.ApiMethodModelHelperInterface.java
com.theagilemonkeys.meets.ApiMethodModelHelper.java
com.theagilemonkeys.meets.ApiMethod.java
com.theagilemonkeys.meets.MeetsSpiceService.java
com.theagilemonkeys.meets.Meets.java
com.theagilemonkeys.meets.magento.RestApiMethod.java
com.theagilemonkeys.meets.magento.SoapApiMethod.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryInfo.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryLevel.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryTree.java
com.theagilemonkeys.meets.magento.methods.CatalogInventoryStockItemList.java
com.theagilemonkeys.meets.magento.methods.CatalogProductAttributeOptions.java
com.theagilemonkeys.meets.magento.methods.CatalogProductInfo.java
com.theagilemonkeys.meets.magento.methods.CatalogProductList.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressCreate.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressDelete.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressInfo.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressList.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressUpdate.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerCreate.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerInfo.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerList.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerUpdate.java
com.theagilemonkeys.meets.magento.methods.Products.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCreate.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCustomerAddresses.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCustomerSet.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartInfo.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartOrder.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartPaymentList.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartPaymentMethod.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartProductAdd.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartProductRemove.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartShippingList.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartShippingMethod.java
com.theagilemonkeys.meets.magento.models.MageMeetsAddress.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartItem.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartPayment.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartShipping.java
com.theagilemonkeys.meets.magento.models.MageMeetsCart.java
com.theagilemonkeys.meets.magento.models.MageMeetsCategory.java
com.theagilemonkeys.meets.magento.models.MageMeetsCustomer.java
com.theagilemonkeys.meets.magento.models.MageMeetsProduct.java
com.theagilemonkeys.meets.magento.models.MageStockInfoList.java
com.theagilemonkeys.meets.magento.models.MageStockItem.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsCollectionPojos.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsCollection.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsFactory.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsModel.java
com.theagilemonkeys.meets.models.MeetsAddress.java
com.theagilemonkeys.meets.models.MeetsCart.java
com.theagilemonkeys.meets.models.MeetsCategory.java
com.theagilemonkeys.meets.models.MeetsCustomer.java
com.theagilemonkeys.meets.models.MeetsProduct.java
com.theagilemonkeys.meets.models.MeetsStock.java
com.theagilemonkeys.meets.models.base.MeetsCollectionPojos.java
com.theagilemonkeys.meets.models.base.MeetsCollection.java
com.theagilemonkeys.meets.models.base.MeetsFactory.java
com.theagilemonkeys.meets.models.base.MeetsListener.java
com.theagilemonkeys.meets.models.base.MeetsModel.java
com.theagilemonkeys.meets.utils.Copier.java
com.theagilemonkeys.meets.utils.StringUtils.java
com.theagilemonkeys.meets.utils.soap.Serializable.java
com.theagilemonkeys.meets.utils.soap.SoapParser.java