Android Open Source - meets-android Api Method Model Helper






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;
/* w  w  w.j a v a 2  s. c  om*/
import com.octo.android.robospice.persistence.DurationInMillis;
import com.theagilemonkeys.meets.models.base.MeetsListener;

import org.jdeferred.Deferred;
import org.jdeferred.DonePipe;
import org.jdeferred.FailPipe;
import org.jdeferred.Promise;
import org.jdeferred.impl.DeferredObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Android Meets SDK
 * Original work Copyright (c) 2014 [TheAgileMonkeys]
 *
 * @author ??lvaro Lpez Espinosa
 */
public class ApiMethodModelHelper<MODEL> implements ApiMethodModelHelperInterface<MODEL> {

    protected transient Map<MeetsListener<MODEL>, Boolean> listenersWithKeepInfo = new HashMap<MeetsListener<MODEL>, Boolean>();
    protected transient Deferred masterPromise = new DeferredObject().resolve(this);
    private boolean serially = false;
    private Boolean forceNextCacheEnable = null;
    private boolean modelCacheEnable = true;
    private MODEL model;

    public ApiMethodModelHelper(MODEL model){
        this.model = model;
    }

    public Promise pushMethod(final ApiMethod method){
        return pushMethod(method, new DelayedParams() {
            @Override public Map<String, Object> buildParams() { return null; }
            @Override public List<String> buildUrlExtraSegments() { return null; }
        });
    }

    public Promise pushMethod(final ApiMethod method, final DelayedParams params) {
        // With this clear all references to previous onDone, onFail and onAlways callbacks, avoiding possible
        // memory leaks
        if (! masterPromise.isPending())
            masterPromise = new DeferredObject().resolve(model);

        if ( ! modelCacheEnable ) {
            method.setCacheDuration(DurationInMillis.ALWAYS_EXPIRED);
        }

        if ( forceNextCacheEnable != null ) {
            method.setCacheDuration(forceNextCacheEnable ? ApiMethod.globalCacheDuration.get() : DurationInMillis.ALWAYS_EXPIRED);
            forceNextCacheEnable = null;
        }

        if ( serially ){
            serially = false;
            return pushPipe(new DonePipe() {
                                @Override
                                public Deferred pipeDone(Object result) {
                                    return method.run(params.buildParams(), params.buildUrlExtraSegments());
                                }
                            }, new FailPipe() {
                                @Override
                                public Deferred pipeFail(Object o) {
                                    return new DeferredObject().reject(o);
                                }
                            });
        }
        else{
            return pushDeferred(method.run(params.buildParams(), params.buildUrlExtraSegments()));
        }
    }

    public Promise pushDeferred(final Deferred deferred){
        masterPromise = (Deferred) masterPromise.then(new DonePipe() {
            @Override
            public Deferred pipeDone(Object result) {
                return deferred;
            }
        }, new FailPipe() {
            @Override
            public Deferred pipeFail(Object result) {
                return new DeferredObject().reject(result);
            }
        });

        return masterPromise;
    }

    public Promise pushPipe(DonePipe donePipe, FailPipe failPipe){
        masterPromise = (Deferred) masterPromise.then(donePipe,failPipe);
        return masterPromise;
    }

    @Override
    public MODEL await(MeetsListener<MODEL> listener) {
        await(listener, false);
        return model;
    }

    @Override
    public MODEL await(MeetsListener<MODEL> listener, boolean keepListening) {
        if ( ! masterPromise.isPending() ){
            Exception e = new Exception("Last operation failed");
            if ( masterPromise.isResolved() ) listener.onDone(model);
            else if (masterPromise.isRejected()) listener.onFail(model, e);
            listener.onAlways(model, e);
            // Avoid register the listener if user don't want to keep it
            if (! keepListening ) return model;
        }

        listenersWithKeepInfo.put(listener, keepListening);
        return model;
    }

    @Override
    public MODEL noLongerWait(MeetsListener<MODEL> listener) {
        listenersWithKeepInfo.remove(listener);
        return model;
    }

    @Override
    public MODEL allNoLongerWait() {
        listenersWithKeepInfo.clear();
        return model;
    }

    //////// Methods called after fetched ///////

    public void triggerListeners() {
        triggerListeners(null);
    }
    public void triggerListeners(Exception e) {
        if (masterPromise.isPending()) return;
        Iterator<Map.Entry<MeetsListener<MODEL>, Boolean>> entries = listenersWithKeepInfo.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<MeetsListener<MODEL>, Boolean> entry = entries.next();
            MeetsListener<MODEL> listener = entry.getKey();
            Boolean keepListening = entry.getValue();

            if (! keepListening ) entries.remove(); // Remove the last value returned by next()

            if ( e == null ) listener.onDone(model);
            else listener.onFail(model, e);
            listener.onAlways(model, e);
        }
    }

    public Map<MeetsListener<MODEL>, Boolean> getListenersWithKeepInfo() {
        return listenersWithKeepInfo;
    }

    @Override
    public MODEL nextWaitForPrevious() {
        serially = true;
        return model;
    }

    /**
     * Force to enable or disable cache during the next operations, bypassing model cache. After that,
     * the cache will return to its previous state (see {@link #setModelCache(boolean)} for details}
     * @return This model
     */
    public MODEL forceNextCacheToBe(boolean enable) {
        forceNextCacheEnable = enable;
        return model;
    }

    /**
     * Disable or enable the cache for all operations in this model. Note that when model cache is enabled,
     * the configuration for it will be taken from ApiMethod, so if cache is disabled in ApiMethod, this
     * functions does nothing.
     * By default is enabled
     * @param enable Whether to enable or disable cache
     * @return This object
     */
    public MODEL setModelCache(boolean enable) {
        modelCacheEnable = enable;
        return model;
    }

    public static abstract class DelayedParams {
        public Map<String, Object> buildParams() {
            return null;
        }
        public List<String> buildUrlExtraSegments() {
            return null;
        }
    }
}




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