Skip to main content

Multimap at its best

private void fetchTransactions() {
        transactionViewFlipper.setDisplayedChild(0);

        StringRequest stringRequest = new StringRequest(POST, GET_ALL_MERCHANT_TRANSACTIONS, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getString("status").equals("SUCCESS")) {
                        JSONArray jsonArray = new JSONArray(jsonObject.getString("result"));
                        final Multimap<String, ArrayList<MerchantTransactionDetails>> multimap = LinkedListMultimap.create();
                        final ArrayList<String> monthNames = new ArrayList<>();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            monthTransactions = new ArrayList<>();
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            MerchantTransactionDetails merchantTransactionDetails = new MerchantTransactionDetails();
                            merchantTransactionDetails.setTransactionId(jsonObject1.getString("trans_id"));

                            merchantTransactionDetails.setTransactionType(jsonObject1.getString("transaction_type"));
                            merchantTransactionDetails.setTransactionAmount(jsonObject1.getString("transaction_amount"));
                            merchantTransactionDetails.setCashBackId(jsonObject1.getString("trans_cashback_id"));
                            merchantTransactionDetails.setOfferId(jsonObject1.getString("trans_offer_redeem_id"));
                            merchantTransactionDetails.setTransactionDate(jsonObject1.getString("transaction_created_date"));
                            merchantTransactionDetails.setTransactionCustomerName(jsonObject1.getString("v_uname"));
                            merchantTransactionDetails.setTransactionText(jsonObject1.getString("v_deal_text"));
                            merchantTransactionDetails.setTransactionDealImageUrl(jsonObject1.getString("markedasfeatured"));
                            merchantTransactionDetails.setCustomerId(jsonObject1.getString("customer_id"));
                            merchantTransactionDetails.setCustomerImage(jsonObject1.getString("v_image"));
                            merchantTransactionDetails.setCustomerContactNo(jsonObject1.getString("v_contact_no"));
                            merchantTransactionDetails.setCustomerEmail(jsonObject1.getString("v_email"));
                            merchantTransactionDetails.setTransactionBillNo(jsonObject1.getString("transaction_bill_number"));
                            merchantTransactionDetails.setBillImageUrl(jsonObject1.getString("transaction_bill_image"));
                            String key = Constants.getProperFormatDate(jsonObject1.getString("transaction_created_date")).split(" ")[0];
                            if (!monthNames.contains(key))
                                monthNames.add(key);
                            monthTransactions.add(merchantTransactionDetails);
                            multimap.put(key, monthTransactions);
                        }

                        JSONArray jsonArray1 = new JSONArray(jsonObject.getString("total_redeemed_cases"));
                        for (int i = 0; i < jsonArray1.length(); i++) {
                            monthTransactions = new ArrayList<>();
                            JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
                            MerchantTransactionDetails merchantTransactionDetails = new MerchantTransactionDetails();
                            merchantTransactionDetails.setTransactionId(jsonObject1.getString("trans_id"));

                            merchantTransactionDetails.setTransactionType(jsonObject1.getString("transaction_type"));
                            merchantTransactionDetails.setTransactionAmount(jsonObject1.getString("transaction_amount"));
                            merchantTransactionDetails.setCashBackId(jsonObject1.getString("trans_cashback_id"));
                            merchantTransactionDetails.setOfferId(jsonObject1.getString("trans_offer_redeem_id"));
                            merchantTransactionDetails.setTransactionDate(jsonObject1.getString("transaction_created_date"));
                            merchantTransactionDetails.setTransactionCustomerName(jsonObject1.getString("v_uname"));
                            merchantTransactionDetails.setTransactionText("Redeemed");
                            merchantTransactionDetails.setTransactionDealImageUrl("");
                            merchantTransactionDetails.setCustomerId(jsonObject1.getString("customer_id"));
                            merchantTransactionDetails.setCustomerImage(jsonObject1.getString("v_image"));
                            merchantTransactionDetails.setCustomerContactNo(jsonObject1.getString("v_contact_no"));
                            merchantTransactionDetails.setCustomerEmail(jsonObject1.getString("v_email"));
                            merchantTransactionDetails.setTransactionBillNo(jsonObject1.getString("transaction_bill_number"));
                            merchantTransactionDetails.setAmountPaidThorughWishlo(jsonObject1.getString("paid_through_wishlo_amount"));
                            merchantTransactionDetails.setBillImageUrl(jsonObject1.getString("transaction_bill_image"));
                            String key = Constants.getProperFormatDate(jsonObject1.getString("transaction_created_date")).split(" ")[0];
                            if (!monthNames.contains(key))
                                monthNames.add(key);
                            monthTransactions.add(merchantTransactionDetails);
                            multimap.put(key, monthTransactions);
                        }
                        Collections.reverse(monthTransactions);
                        transactionViewFlipper.setDisplayedChild(1);
                        if(getContext()==null){
                            final Handler handler =new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if(getContext() == null){
                                        handler.postDelayed(this,400);
                                    }else{
                                        transactionRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                                        transactionRecyclerView.setAdapter(new MonthlyTransactionAdapter(getContext(), multimap, monthNames));
                                    }

                                }
                            },400);
                        }else{
                            transactionRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                            transactionRecyclerView.setAdapter(new MonthlyTransactionAdapter(getContext(), multimap, monthNames));
                        }

                        Log.d("TAG", multimap.toString());
                    } else {
                        transactionViewFlipper.setDisplayedChild(3);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), "Parse Exception", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                transactionViewFlipper.setDisplayedChild(2);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> stringStringHashMap = new HashMap<>();
                stringStringHashMap.put("Content-Type", "application/x-www-form-urlencoded");
                stringStringHashMap.put("X-API-KEY", "1234");
                stringStringHashMap.put("Accept", "application/json");
                return stringStringHashMap;
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("merchant_id", getArguments().getString(MERCHANT_ID));
                Log.d(Constants.TAG, hashMap.toString());
                return hashMap;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                10000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        AppController.getInstance().addToRequestQueue(stringRequest);
    }





@Override
    public void onBindViewHolder(MonthlyTransactionAdapterViewHolder holder, int position) {
        Collection<ArrayList<MerchantTransactionDetails>> albumDataArrayList1 = multimap.get(monthName.get(position));

        Iterator it = albumDataArrayList1.iterator();

        ArrayList<MerchantTransactionDetails> albumDataArrayList2 = new ArrayList<>();
        while (it.hasNext()) {
            albumDataArrayList2.addAll((Collection<? extends MerchantTransactionDetails>) it.next());
        }
        holder.monthsRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        IndividualMonthDetailAdapter individualMonthDetailAdapter = new IndividualMonthDetailAdapter(context, albumDataArrayList2, mFont);
        holder.monthsRecyclerView.setAdapter(individualMonthDetailAdapter);
        holder.monthName.setText(monthName.get(position));
        holder.monthName.setTypeface(mFont);
    }


Comments

Popular posts from this blog

Meteor JS REST API

WRITE A POST Shahid Shaikh FOLLOW Engineer, Blogger from Mumbai. RESTful CRUD Operations in Meteor.js Published Dec 31, 2015 Last updated Jan 19, 2017 Introduction Meteor is a popular framework for building real-time web applications. We have already covered a tutorial which explains  how to build chat system using Meteor.js . In this tutorial, we will learn how to develop a RESTful API using Meteor which perform CRUD operations. Creating new project Create a new meteor project using the following command: meteor create appName Choose an application name of your choice. Once Meteor is done creating the project, you will see the directories and files created by Meteor in the folder name. We will not need them as of now, so we will delete all of those files and create whatever we need in the coming section. Installing iron-router In order to perform routing in Meteor, we will use  this awesome...

Meteor Mongodb Mup Commands

docker exec -it mongodb mongo docker cp mongodb:dump/ce5-meteor . If deployed with mup, you are in luck. You can find the steps here:  https://github.com/xpressabhi/mup-data-backup Here are the steps again: MongoDB Data Backup deployed via mup These commands run well only if meteor deployed with mup tool. Mup creates docker for mongodb hence taking backup becomes easy with these commands. Backup Take backup of running app data from docker then copy to local folder out of docker. docker exec -it mongodb mongodump --archive= /root/m ongodump. gz --gzip docker cp mongodb : /root/m ongodump. gz mongodump_$ (date +%Y-%m-%d_%H-%M-%S). gz Copy backup to server Move data to another server/local machine or a backup location scp /path/to/dumpfile root@ serverip : /path/ to/backup Delete old data from meteor deployment Get into mongo console running in docker then drop current database before getting new data. docker exec -it mongodb mongo appName db. runCommand ( { dropDatabase : 1 } ) ...