0
package symca.gescoe.com.sampleparseapp.payment_task;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;

import symca.gescoe.com.sampleparseapp.R;


import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseACL;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import java.util.List;


public class PayRent extends Activity {

    TextView ownerName, ownerEmail, ownerPropType, ownerLocation, ownerAccountNo, renterAccountNo;
    EditText propId;
    int confirmFlag = 0;
    int rentFinal = 0, mainFinal = 0, totalFinal = 0;

    int rent;
    int maintenance;
    String ownerNameStr;
    String ownerEmailStr;
    String ownerPropTypeStr;
    String ownerLocationStr;
    String ownerAccountNoStr;
    String renterAccountNoStr;
    String propIdStr;
    Button btnpayRent;
    ImageView btnConfirm;
    int payInterestFlag = 1;
    ProgressDialog progressDialog;

    String proType, location, email, fname, lname, ownerAccount, renterAccount;
    String ownerObjId,ownerAvlBal;
    int bhk;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_rent);

//        ParseACL defaultACL = new ParseACL();
//       defaultACL.setPublicWriteAccess(true);//------temp


        btnConfirm = (ImageView) findViewById(R.id.btnConfirmIDPay);
        btnpayRent = (Button) findViewById(R.id.btnPayRent);
        propId = (EditText) findViewById(R.id.tvPropIDPay);
        ownerName = (TextView) findViewById(R.id.tvNamePay);
        ownerEmail = (TextView) findViewById(R.id.tvEmailPay);
        ownerPropType = (TextView) findViewById(R.id.tvPropTypePay);
        ownerLocation = (TextView) findViewById(R.id.tvLocPay);
        ownerAccountNo = (TextView) findViewById(R.id.tvLandAccountNoPay);
        renterAccountNo = (TextView) findViewById(R.id.tvRenterAccountNoPay);


        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLauPropertyPay);


        btnConfirm.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                confirmFlag = 1;
                progressDialog = ProgressDialog.show(PayRent.this, "Please Wait...", "Property Details Loading..");

                String propIdStr = propId.getText().toString();

                if (!propIdStr.equals(""))
                {


                    final ParseQuery<ParseObject> query = ParseQuery.getQuery("PostAdd");
                    query.whereEqualTo("objectId", propIdStr);

                    query.getFirstInBackground(new GetCallback<ParseObject>()
                    {
                        @Override
                        public void done(ParseObject object, ParseException e)
                        {

                            if (e == null)//-----from Post Add Table--------------
                            {

                                email = object.getString("PostEmail");
                                bhk = object.getInt("BHK");
                                proType = object.getString("PropType");
                                location = object.getString("Location");

                                rent = object.getInt("ERent");
                                maintenance = object.getInt("Emaintainance");



                                //-------------from User Table-------
                                ParseQuery query = ParseUser.getQuery();
                                query.whereEqualTo("email", email);

                                query.getFirstInBackground(new GetCallback()
                                {
                                    @Override
                                    public void done(ParseObject object, ParseException e)
                                    {
                                        String interest = object.getString("PaymentInterest");

                                        if (interest.equals("Interested"))
                                        {
                                            payInterestFlag = 0;
                                            fname = object.getString("FirstName");
                                            lname = object.getString("LastName");
                                            ownerAccount = object.getString("AccountNo");
                                            renterAccount = ParseUser.getCurrentUser().getString("AccountNo");


                                            ownerName.setText(fname + " " + lname);
                                            ownerAccountNo.setText(ownerAccount);
                                            renterAccountNo.setText(renterAccount);
                                            ownerEmail.setText(email);
                                            ownerLocation.setText(location);
                                            ownerPropType.setText(bhk + " " + "BHK " + proType);

                                            progressDialog.dismiss();
                                        } else
                                        {
                                            progressDialog.dismiss();
                                            payInterestFlag = 1;
                                            AlertDialog.Builder alertPayInterest = new AlertDialog.Builder(PayRent.this);
                                            alertPayInterest.setTitle("Not Interested!");
                                            alertPayInterest.setIcon(R.drawable.notonline);
                                            alertPayInterest.setMessage("Ohhh!!!\nSelected Add Owner Not Interested In Online Payment");
                                            alertPayInterest.show();


                                        }

                                    }
                                });


                            } else
                            {
                                progressDialog.dismiss();

                                propId.setError("Property Id is Not Valid!!!");

                            }

                        }
                    });

                }//if
                else {
                    progressDialog.dismiss();
                    propId.setError("Property Id Blank");
                }

            }
        });

//----------------------------------------------------------------------

        btnpayRent.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                Toast.makeText(getApplicationContext(), "Click Rent", Toast.LENGTH_SHORT).show();


                if ((!propId.getText().toString().equals("")) && (confirmFlag == 1))
                {


                    LayoutInflater factory = LayoutInflater.from(PayRent.this);
                    final View textEntryView = factory.inflate(R.layout.custom_dialogue_payrent, null);

                    final EditText inputRent = (EditText) textEntryView.findViewById(R.id.edtRentCustom);
                    inputRent.setEnabled(false);
                    final EditText inputMain = (EditText) textEntryView.findViewById(R.id.edtMainCustom);
                    inputMain.setEnabled(false);


                    ImageView imgRent = (ImageView) textEntryView.findViewById(R.id.edtImgRentPay);
                    ImageView imgMain = (ImageView) textEntryView.findViewById(R.id.edtImgMainPay);


                    // Toast.makeText(getApplicationContext(),"rent is "+rent,Toast.LENGTH_SHORT).show();
                    inputRent.setText("" + rent);
                    inputMain.setText("" + maintenance);

                    imgRent.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            inputRent.setEnabled(true);
                            //inputRent.requestFocus();

                        }
                    });

                    imgMain.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            inputMain.setEnabled(true);
                        }
                    });


                    final AlertDialog.Builder alert = new AlertDialog.Builder(PayRent.this);
                    alert.setIcon(R.drawable.donation);
                    alert.setTitle("Payment Details");
                    alert.setView(textEntryView);
                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int whichButton)
                        {
                            //-------------Positive Button

                            rentFinal = Integer.parseInt(inputRent.getText().toString());
                            mainFinal = Integer.parseInt(inputMain.getText().toString());
                            totalFinal = rentFinal + mainFinal;

                            //--transaction table-------
                            ParseObject objTrans = new ParseObject("Transactions");
                            objTrans.put("Debtor", renterAccount);//renter
                            objTrans.put("Creditor", ownerAccount);//owner
                            objTrans.put("TotalAmt", totalFinal);
                            objTrans.saveInBackground();
                            //--------------------------------


                            final ParseQuery<ParseObject> querypradip = ParseQuery.getQuery("Pay");
                            querypradip.whereEqualTo("AccountNo", ownerAccount);
                            querypradip.getFirstInBackground(new GetCallback<ParseObject>() {
                                public void done(ParseObject object, ParseException e) {
                                    if (object == null) {
                                        Log.d("score", "The getFirst request failed."+e.getMessage());
                                    } else {
                                        Log.d("score", "Retrieved the object.");

                                        ownerObjId=object.getObjectId();
                                        ownerAvlBal=object.getString("AvailableBal");

                                        Log.d("Oid", ownerObjId);
                                        Log.d("OBal", ownerAvlBal);
                                        Toast.makeText(getApplicationContext(), "ownerId"+ownerObjId+"\nAval Bal"+ownerAvlBal, Toast.LENGTH_LONG).show();



                                        //--------------------UPDATE--------------





                                      ParseQuery query = new ParseQuery("Pay");
                                        query.getInBackground(ownerObjId, new GetCallback()
                                        {
                                            public void done(final ParseObject object, ParseException e)
                                            {
                                                if (e == null)
                                                {

                                                    object.put("Bal", 5000);

                                                    object.saveInBackground();
                                                    Toast.makeText(getApplicationContext(), "After Save In Back", Toast.LENGTH_LONG).show();

                                                    object.saveInBackground(new SaveCallback()
                                                    {
                                                        public void done(ParseException e)
                                                        {

                                                            object.put("AvailableBal", 7000);
                                                            object.saveInBackground();

                                                        }
                                                    });

                                                }

                                                else
                                                {
                                                    e.printStackTrace();
                                                    Log.e("In Update",e.getMessage());
                                                }

                                            }
                                        });

                                    }
                                }
                            });



                            //---reter table---------------
                            ParseQuery<ParseObject> queryPay = new ParseQuery<ParseObject>("Pay");
                            queryPay.whereEqualTo("AccountNo", renterAccount);
                            queryPay.getFirstInBackground(new GetCallback<ParseObject>() {
                                @Override
                                public void done(ParseObject object, ParseException e) {
                                    String AvailBal = object.getString("AvailableBal");
                                    String renterID = object.getObjectId();

                                    final int remainsBal = Integer.parseInt(AvailBal) - totalFinal;

                                    Log.e("Renter", "************" + remainsBal);
                                    Log.e("RenterID", "************" + renterID);

                                    //----------------Update------------------
                                    ParseQuery queryUpdatePay = new ParseQuery("Pay");
                                    queryUpdatePay.getInBackground(renterID, new GetCallback() {
                                        @Override
                                        public void done(ParseObject Obj, ParseException e) {
                                            if (e == null) {

                                                Obj.put("AvailableBal", "" + remainsBal);
                                                Log.e("Update", "************" + remainsBal);
                                                Obj.saveInBackground();


                                            } else {
                                                // do something with the error...
                                            }
                                        }

                                    });
                                }//done


                            });





                        }
                     });//Ok






                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {



                                }
                            }

                    );
                    alert.show();

                } else
                if (propId.getText().toString().equals(""))
                {
                    propId.setError("Property Id Blank!");
                }

                if (confirmFlag == 0)
                {
                    Toast.makeText(getApplicationContext(), "Please Confirm Property Id before Pay", Toast.LENGTH_SHORT).show();
                }


            }
        });


    }//OnCreate


}

I am able to store(Simply put using object) data in Pay table as well as also get from table but when I am trying to update it doesn't show any change in table.......... same I am applying for Current User(renter data) it done all get data,update but except current user others users data not update

rici
  • 234,347
  • 28
  • 237
  • 341

1 Answers1

0

I do not want to try and understand such a large block of code. In the future please try and boil down the relevant pieces. However:

  1. Looks like you do a query right after saving. Note that saveInBackground() is async, thus it starts (as the name suggests) to contact parse.com and save in a background thread. If the query depend on the object being saved, then you need to wait for it to complete by adding the SaveCallback parameter saveInBackground(new SaveCallback)
  2. If it is because the update fails, then again implement the SaveCallback to see what Exception is thrown saveInBackground(new SaveCallback)
  3. You should never be able to update any other User objects than your own. That would be a huge security issue. A work around could be to create a cloud function using useMasterKey() and call that function from your app.
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33