Android Open Source - meets-android Mage Meets Customer






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;
//www.  java 2s  . c  om
import com.google.api.client.util.Key;
import com.theagilemonkeys.meets.ApiMethodModelHelper;
import com.theagilemonkeys.meets.Meets;
import com.theagilemonkeys.meets.magento.methods.CustomerAddressCreate;
import com.theagilemonkeys.meets.magento.methods.CustomerAddressDelete;
import com.theagilemonkeys.meets.magento.methods.CustomerAddressList;
import com.theagilemonkeys.meets.magento.methods.CustomerCustomerCreate;
import com.theagilemonkeys.meets.magento.methods.CustomerCustomerInfo;
import com.theagilemonkeys.meets.magento.methods.CustomerCustomerList;
import com.theagilemonkeys.meets.magento.methods.CustomerCustomerUpdate;
import com.theagilemonkeys.meets.magento.models.base.MageMeetsCollectionPojos;
import com.theagilemonkeys.meets.magento.models.base.MageMeetsModel;
import com.theagilemonkeys.meets.models.MeetsAddress;
import com.theagilemonkeys.meets.models.MeetsCustomer;
import com.theagilemonkeys.meets.utils.StringUtils;
import com.theagilemonkeys.meets.utils.soap.Serializable;

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

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

import javax.annotation.Nullable;

/**
 * Android Meets SDK
 * Original work Copyright (c) 2014 [TheAgileMonkeys]
 *
 * @author ??lvaro Lpez Espinosa
 */
public class MageMeetsCustomer extends MageMeetsModel<MeetsCustomer> implements MeetsCustomer {
    public final static String MODE_GUEST = "guest";
    public final static String MODE_CUSTOMER = "customer";

    @Key private String mode = MODE_CUSTOMER;
    @Key private int customer_id;
    @Key private String email;
    @Key private String firstname;
    @Key private String lastname;
    @Key private String taxvat;
    @Key private int store_id;
    @Key private int website_id;
    @Key private String password;
    @Key private String password_hash;
    @Key private MageMeetsCollectionPojos.Addresses addresses;

    {
        store_id = Meets.storeId;
        website_id = Meets.websiteId;
    }

    //This is specific of magento
    public MeetsCustomer setMode(String mode){
        this.mode = mode;
        return this;
    }

    @Override
    public MeetsCustomer fetch() {
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("customerId", getId());
                return params;
            }
        };

        pushMethod(new CustomerCustomerInfo(), params).always(updateAndTrigger);
        return this;
    }

    @Override
    public MeetsCustomer setId(int id) {
        customer_id = id;
        return this;
    }

    @Override
    public int getId() {
        return customer_id;
    }

    @Override
    public String getFirstName() {
        return firstname;
    }

    @Override
    public String getLastName() {
        return lastname;
    }

    @Override
    public String getFullName() {
        return StringUtils.safeValueOf(firstname) + " " + StringUtils.safeValueOf(lastname);
    }

    @Override
    public String getEmail() {
        return email;
    }

    @Override
    public String getVatNumber() {
        return taxvat;
    }

    @Override
    public String getPasswordHash() {
        return password_hash;
    }

    @Override
    public MeetsAddress getDefaultBillingAddress() {
        if (addresses != null && addresses.size() > 0) {
            for (MeetsAddress address : addresses){
                if ( address.isDefaultBilling() ) return address;
            }
        }
        return null;
    }

    @Override
    public MeetsAddress getDefaultShippingAddress() {
        if (addresses != null && addresses.size() > 0) {
            for (MeetsAddress address : addresses){
                if ( address.isDefaultShipping() ) return address;
            }
        }
        return null;
    }

    @Override
    public List<MeetsAddress> getAddresses() {
        List<MeetsAddress> res = new ArrayList<MeetsAddress>();
        if (addresses != null) res.addAll(addresses);
        return res;
    }

    private MeetsCustomer create() {
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("customerData", MageMeetsCustomer.this);
                return params;
            }
        };

        forceNextCacheToBe(false);
        pushMethod(new CustomerCustomerCreate(), params).always(updateAndTrigger);
        return this;
    }

    @Override
    public MeetsCustomer save() {
        if (isNew()){
            return create();
        }
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("customerId", getId());
                params.put("customerData", MageMeetsCustomer.this);
                return params;
            }
        };

        forceNextCacheToBe(false);
        pushMethod(new CustomerCustomerUpdate(), params).always(onlyTrigger);
        return this;
    }

    @Override
    public MeetsCustomer fetchByEmail(final String email) {
        return internalFetchByEmail(email, null);
    }

    @Override
    public MeetsCustomer fetchByEmailAndCheckPassword(String email, String password) {
        return internalFetchByEmail(email, password);
    }

    private MeetsCustomer internalFetchByEmail(final String email, @Nullable final String password) {
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                List<Map<String, Object>> complexFilter = new Serializable.List<Map<String, Object>>();
                    //Email filter key
                    Map<String, Object> filter = new Serializable.Map<String, Object>();
                        //Email filter value
                        Map<String, Object> filterValue = new Serializable.Map<String, Object>();
                        filterValue.put("key", "eq");
                        filterValue.put("value", email);
                    filter.put("key", "email");
                    filter.put("value", filterValue);
                complexFilter.add(filter);

                Map<String, Object> filters = new Serializable.Map<String, Object>();
                filters.put("complex_filter", complexFilter);

                Map<String, Object> params = new HashMap<String, Object>();
                params.put("filters", filters);
                return params;
            }
        };

        forceNextCacheToBe(false);
        pushMethod(new CustomerCustomerList(), params);
        pushPipe(new DonePipe() {
            @Override
            public Deferred pipeDone(Object o) {
                MageMeetsCollectionPojos.Customers customers = (MageMeetsCollectionPojos.Customers) o;
                Exception e;
                if (customers.size() == 0) {
                    e = new Exception("Customer not found");
                }
                else{
                    MeetsCustomer customer = customers.get(0);
                    if (password == null || internalCheckPassword(password, customer.getPasswordHash())) {
                        updateAndTrigger.onAlways(Promise.State.RESOLVED, customer, null);
                        return new DeferredObject().resolve(MageMeetsCustomer.this);
                    }
                    e = new Exception("Incorrect password");
                }

                updateAndTrigger.onAlways(Promise.State.REJECTED, null, e);
                return new DeferredObject().reject(e);
            }
        }, null);
        return this;
    }

    @Override
    public boolean checkPassword(String password) {
        return checkPassword(password);
    }

    private boolean internalCheckPassword(String password, String passwordHash){
        String[] pass_salt = passwordHash.split(":");
        String pass = pass_salt[0];
        String salt = pass_salt[1];
        String typedPassword = StringUtils.MD5Hash(salt + password);
        return pass.equals(typedPassword);
    }

    @Override
    public MeetsCustomer fetchAddresses() {
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                Map<String,Object> params = new HashMap<String, Object>();
                params.put("customerId", getId());
                return params;
            }
        };

        pushMethod(new CustomerAddressList(), params)
                .done(new DoneCallback() {
                    @Override
                    public void onDone(Object o) {
                        addresses = (MageMeetsCollectionPojos.Addresses) o;
                    }
                })
                .always(onlyTrigger);
        return this;
    }

    @Override
    public MeetsCustomer addAddress(final MeetsAddress meetsAddress) {
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("customerId", getId());
                params.put("addressData", meetsAddress);
                return params;
            }
        };

        forceNextCacheToBe(false);
        pushMethod(new CustomerAddressCreate(), params)
                .done(new DoneCallback() {
                    @Override
                    public void onDone(Object o) {
                        MeetsAddress resultAddress = (MeetsAddress) o;
                        meetsAddress.setId(resultAddress.getId());
                        if (addresses == null) addresses = new MageMeetsCollectionPojos.Addresses();

                        // Update the default billing and shipping state in the other addresses
                        for (MeetsAddress address : addresses) {
                            if (meetsAddress.isDefaultBilling() && address.isDefaultBilling())
                                address.setDefaultBilling(false);
                            if (meetsAddress.isDefaultShipping() && address.isDefaultShipping())
                                address.setDefaultShipping(false);
                        }

                        addresses.add((MageMeetsAddress) meetsAddress);
                    }
                })
                .always(onlyTrigger);
        return this;
    }

    @Override
    public MeetsCustomer removeAddress(final int addressId) {
        //TODO: Remove the address locally before the request (as with cart items) to avoid remove addresses not present in the customer
        ApiMethodModelHelper.DelayedParams params = new ApiMethodModelHelper.DelayedParams() {
            @Override
            public Map<String, Object> buildParams() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("addressId", addressId);
                return params;
            }
        };

        forceNextCacheToBe(false);
        pushMethod(new CustomerAddressDelete(), params)
                .done(new DoneCallback() {
                    @Override
                    public void onDone(Object o) {
                        for(int i = 0; i < addresses.size(); ++i)
                        {
                            MeetsAddress address = addresses.get(i);
                            if (address.getId() == addressId){
                                addresses.remove(i);
                                return;
                            }
                        }
                    }
                })
                .always(onlyTrigger);
        return this;
    }

    @Override
    public MeetsCustomer setFirstName(String firstName) {
        this.firstname = firstName;
        return this;
    }

    @Override
    public MeetsCustomer setLastName(String lastname) {
        this.lastname = lastname;
        return this;
    }

    @Override
    public MeetsCustomer setEmail(String email) {
        this.email = email;
        return this;
    }

    @Override
    public MeetsCustomer setVatNumber(String taxvat) {
        this.taxvat = taxvat;
        return this;
    }

    @Override
    public MeetsCustomer setPassword(String password) {
        this.password = password;
        return this;
    }
}




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