Firestore checking if a document exist in collection - android

Im trying to make a code for uploading item/stock to firestore. what i want is if the Item is already registered in firestore, then recalculate the quantity. But if the item is not registered yet in firestore, system add new document to firestore.
I already make a code like below, if i try to add item that is already registered it succeed on recalculating the quantity but the problem is when i want to add new item ( that is not registered in database) it doesnt work. Can somebody fix my code.
final FirebaseFirestore db = FirebaseFirestore.getInstance();
merk = etMerk.getText().toString().trim();
type = etType.getText().toString().trim();
typemerk = type + " - " + merk;
qty = etQty.getText().toString().trim();
price = etPrice.getText().toString().trim();
date = datetime.getText().toString();
final Map<String, Object> stock = new HashMap<>();
stock.put("date", date);
stock.put("type", typemerk);
stock.put("qty", qty);
stock.put("price", price);
stock.put("merk", merk);
final FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
final CollectionReference documentReference = rootRef.collection("watchlist");
Query query = documentReference.whereEqualTo("type", typemerk);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());
String id = document.getString("id");
String oldqty = document.getString("qty");
Integer i = Integer.parseInt(oldqty) + Integer.parseInt(qty);
String newQty = String.valueOf(i);
Map<Object, String> map = new HashMap<>();
map.put("qty", newQty);
rootRef.collection("watchlist").document(document.getId()).set(map, SetOptions.merge());
Toast.makeText(AddItemActivity.this, "Berhasil menambah stok", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
} else {
Log.d(Tag.ITEM, "not register in DB", task.getException());
Toast.makeText(AddItemActivity.this, "new item to database", Toast.LENGTH_SHORT).show();
documentReference
.add(stock)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(AddItemActivity.this, "Berhasil Memasukkan Barang ke Stok", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
etMerk.setText("");
etType.setText("");
etQty.setText("");
etPrice.setText("");
etMerk.setFocusable(true);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(AddItemActivity.this, "Gagal Memasukkan stok, silahkan coba lagi.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
});
}

You should check to make sure that your query returned successfully and that it's not empty. It can return an empty result if the query was processed but there was no matching result.
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("myCollection")
.whereEqualTo("myQuery", myQueryValue)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
boolean documentExists;
if(task.isSuccessful()){
Log.d("QueryResult", "Is query result empty: " + task.getResult().isEmpty());
documentExists = !task.getResult().isEmpty();
}else{
Log.e("QueryResult", "Error getting documents.", task.getException());
documentExists = false;
}
if(documentExists){
Log.d("QueryResult", "The document exists");
// Do whatever you need to do with the document
}else{
Log.d("QueryResult", "The document doesn't exist or there was an error retrieving it");
// Create the document or whatever else you need to do
}
}
});

Related

How to retrieve the data that inserted in the map firebase fire store

I'm creating an app in the android studio IDE and I want to display the inserted data in a text box
but the problem is I don't know how to retrieve the data that I inserted in the driver field. The driver field is a map, Is there a way to retrieve the data insert in the driver field? This is my firebase fire store look like, I want to get the tricycle number data.
can anyone give me example on how to retrieve a data in a map collection field?
Following the documentation on getting data from Firestore, you can get a DataSnapshot object of the data at that location. Then using DocumentSnapshot#get() to get the value of an individual field.
This can be done using:
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference driverDocRef = db.collection("Driver Locations")
.document(driverId);
driverDocRef.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + document.get("driver.tricyclenumber", String.class));
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
To make this into a function that you can reuse elsewhere, you can make use of Task#onSuccessTask() to chain tasks together.
One such implementation of this would be:
public Task<String> getDriverTricycleNumber(String driverId) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference driverDocRef = db.collection("Driver Locations")
.document(driverId);
return driverDocRef.get()
.onSuccessTask(new SuccessContinuation<DocumentSnapshot, String>() {
#NonNull
#Override
public Task<String> then(DocumentSnapshot document) {
if (!document.exists()) {
throw new DriverNotFoundException(); // <-- a custom exception of your choosing
}
return document.get("driver.tricyclenumber", String.class);
}
});
}
// to use:
getDriverTricycleNumber("someDriverId")
.addOnCompleteListener(new OnCompleteListener<Number>() {
#Override
public void onComplete(#NonNull Task<Number> task) {
if (task.isSuccessful()) {
String tricycleNumber = task.getResult();
Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + tricycleNumber);
} else {
Log.d(TAG, "Couldn't get tricycle number", task.getException());
}
}
});
Note: Optionally, you can simplify the above code using modern arrow notation and chaining:
public Task<String> getDriverTricycleNumber(String driverId) {
return FirebaseFirestore.getInstance()
.collection("Driver Locations")
.document(driverId)
.get()
.onSuccessTask(document -> {
if (!document.exists()) {
throw new DriverNotFoundException(); // <-- a custom exception of your choosing
}
return document.get("driver.tricyclenumber", String.class);
});
}
getDriverTricycleNumber("someDriverId")
.addOnSuccessListener(tricycleNumber -> {
Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + tricycleNumber);
})
.addOnFailureListener(exception -> {
Log.d(TAG, "Couldn't get tricycle number", exception);
});
If it's a map, then just use completely-standard dot notation:
const myFirstname = data.driver.firstname;
const myLastname = data.driver.lastname
etc etc etc

Android firestore Retrieve Data map

How to retrieve the data in nested object? Im able to get all the data in document but fail to retrieve the nested object, Just want to retrieve all the data under usr_card-card1,card2 into array or map
userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
fStore = FirebaseFirestore.getInstance();
DocumentReference documentReference = fStore.collection("users").document(userid);
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String usr_name = "usr_name",usr_email="usr_email";
String name = document.getString(usr_name);
String email = document.getString(usr_email);
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
Map type fields in Firestore documents show up as Map type objects in Java.
Map<String, Object> usr_card = (Map<String, Object) document.get("usr_card");
You can work with the returned Map exactly as you would any other Map.

Android Sequence (Skipping method)

i have some problem understanding whats wrong with android code about calling function
as show below, I have 2 private void
cekSaved(place.getName());
addUserInfo(place.getName(),"");
i expect the android run ceksaved method first then addUserInfo but android running adduser first then run ceksaved function
i need help understanding this
code is :
private void cekSaved(String param1){
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference newUserInfo = db.collection("trip").document();
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
db.collection("trip")
.whereEqualTo("user_id", userID )
.whereEqualTo("city", param1)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful())
{
for (QueryDocumentSnapshot document : task.getResult()) {
status = "ada";
Log.i(TAG, "onComplete: "+status);
}
}
else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
private void addUserInfo(String city, String tittle){
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference newUserInfo = db.collection("trip").document();
place_id = newUserInfo.getId();
Log.i(TAG, "addUserInfo: "+status);
if(!status.equals("ada")) {
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
userTrip usertrip = new userTrip();
usertrip.setCity(city);
usertrip.setTittle("My Trip To " + city);
usertrip.setTrip_id(newUserInfo.getId());
usertrip.setUser_id(userID);
Log.i(TAG, "addUserInfo: trip baru telah di buat");
newUserInfo.set(usertrip).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
ToastMessage("OK");
} else {
ToastMessage("fail to register");
}
}
});
}else{ToastMessage("sudah ada");}
}
You're getting values from Firebase, it's an async call, so you don't really know which one will be completed first. You can see that either calls have an onCompleteListener, if you want to run addUserInfo after cekSaved you have to call addUserInfo inside onCompleteListener of cekSaved method.

FirebaseFirestore not executing the given query

I am try to import a single field value from a document.The problem here is code is not printing giving the error.I have check the code from firestore documentation too.
I tried using OnSuccessListener too,yet no result.
private FirebaseFirestore db,db1;
Map<String,Object > number=new HashMap<>();
db = FirebaseFirestore.getInstance();
DocumentReference docRef = db.collection("goals").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
String TAG="lol";
System.out.println("IT CAME Here") ;
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
number=document.getData();
name = document.getString("number");
} else {
Log.d(TAG, "No such document");
}
} else {
System.out.println("get failed with ");
Log.d(TAG, "get failed with ", task.getException());
}
}
});
Not even the print statements in the else loops are being printed.It seems like its skipping that block of code

Android Firestore update field value if item exist in database

If the value in the database already exist (for example I want to add Cheetos to firestore and in the database cheetos already exist) then I want to update the quantity field of that item. I make the code like this but the app still add new item when the value is the same. I think the system doesn't detect my 'if(task.getResult().getDocuments().size()>0'.
this is the firestore data
this is the method code
private void uploadItem() {
merk = etMerk.getText().toString().trim();
type = etType.getText().toString().trim();
typemerk = merk + " - " + type;
qty = etQty.getText().toString().trim();
price = etPrice.getText().toString().trim();
date = datetime.getText().toString();
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("watchlist").whereEqualTo("merk",typemerk)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().getDocuments().size()>0){
Toast.makeText(AddItemActivity.this, "Barang Sama", Toast.LENGTH_SHORT).show();
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());
String itemid = document.getString("id");
String date = document.getString("date");
String type = document.getString("type");
String Oldqty = document.getString("qty");
String price = document.getString("price");
int sum= Integer.parseInt(Oldqty) + Integer.parseInt(qty);
String newQty = String.valueOf(sum);
Map<String, Object> newstock = new HashMap<>();
newstock.put("qty",newQty);
FirebaseFirestore database = FirebaseFirestore.getInstance();
database.collection("watchlist")
.document(itemid).update(newstock).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(AddItemActivity.this, "Berhasil Menambahkan jumlah barang", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
etMerk.setText("");
etType.setText("");
etQty.setText("");
etPrice.setText("");
etMerk.setFocusable(true);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(AddItemActivity.this, "Gagal Memasukkan stok, silahkan coba lagi.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
else {
upload();
}
} else {
Log.w(Tag.ITEM, "error getting documents", task.getException());
}
}
});
}
This code works
private void cutStock() {
merk = etMerk.getText().toString().trim();
type = etType.getText().toString().trim();
typemerk = type + " - " + merk;
qty = etQty.getText().toString().trim();
price = etPrice.getText().toString().trim();
date = datetime.getText().toString();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference documentReference = db.collection("watchlist");
final CollectionReference documentSales = db.collection("sales");
documentReference.whereEqualTo("type",typemerk)
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());
String id = document.getString("id");
String oldqty = document.getString("qty");
Integer i = Integer.parseInt(oldqty) - Integer.parseInt(qty);
String newQty = String.valueOf(i);
Map<Object, String> map = new HashMap<>();
map.put("qty",newQty);
db.collection("watchlist").document(document.getId()).set(map, SetOptions.merge());
Map<String, Object> sales = new HashMap<>();
sales.put("date", date);
sales.put("type", typemerk);
sales.put("qty", qty);
sales.put("price", price);
documentSales.add(sales).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(AddItemActivity.this, "Berhasil mencetak transaksi", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
etMerk.setText("");
etType.setText("");
etQty.setText("");
etPrice.setText("");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(AddItemActivity.this, "Gagal mencetak", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
progressBar.setVisibility(View.GONE);
}
}
else {
progressBar.setVisibility(View.GONE);
Toast.makeText(AddItemActivity.this, "Barang tidak terdaftar", Toast.LENGTH_SHORT).show();
Log.w(Tag.ITEM, "error getting documents", task.getException());
}
}
});
}

Categories

Resources