Commit 48ce6ff0 authored by Junita Sinambela's avatar Junita Sinambela
Browse files

ubah dikit 1

parents a90ff3fc 69e5d815
......@@ -94,7 +94,8 @@ public class Database {
//
// System.out.println(saveOfferToTradeBox("553b6adc3c892b2edffa877c", 4, 1, 5, 3));
getTradebox("553b64d23c892b2d5b772c74");
// getTradebox("553b64d23c892b2d5b772c74");
changeLocalTradeboxToFalse("553c36143c892b1bbf7723d6");
}
/**
......@@ -319,7 +320,7 @@ public class Database {
.append("n1", nOffered)
.append("demanded_item", indexDemandedItem)
.append("n2", nDemanded)
.append("availability", false);
.append("availability", true);
collection.insertOne(offerDoc);
//Update inventory
......@@ -390,7 +391,7 @@ public class Database {
{
String oneOfferString = cursor.next().toJson();
JSONObject jsonObj = new JSONObject(oneOfferString);
if(jsonObj.getString("usertoken").equals(usertoken)
if(!jsonObj.getString("usertoken").equals(usertoken)
&& (jsonObj.getInt("offered_item") == itemIdx))
{
oneoffer = new ArrayList<>();
......@@ -415,6 +416,40 @@ public class Database {
return allOffer;
}
public static ArrayList<ArrayList<Object>> getTradebox()
{
MongoCollection<Document> collection = database.getCollection("tradebox");
MongoCursor<Document> cursor = collection.find().iterator();
ArrayList<ArrayList<Object>> allOffer;
ArrayList<Object> oneoffer;
try{
allOffer = new ArrayList<>();
while(cursor.hasNext())
{
String oneOfferString = cursor.next().toJson();
JSONObject jsonObj = new JSONObject(oneOfferString);
oneoffer = new ArrayList<>();
oneoffer.add(jsonObj.getInt("offered_item"));
oneoffer.add(jsonObj.getInt("n1"));
oneoffer.add(jsonObj.getInt("demanded_item"));
oneoffer.add(jsonObj.getInt("n2"));
oneoffer.add(jsonObj.getBoolean("availability"));
oneoffer.add(getJsonObjId(jsonObj.toString()));
allOffer.add(oneoffer);
}
if(allOffer.isEmpty())
return null;
} catch (JSONException | NullPointerException ex) {
return null;
}
finally
{
cursor.close();
}
return allOffer;
}
public static ArrayList<ArrayList<Object>> getTradebox(int itemIdx)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
......@@ -452,6 +487,71 @@ public class Database {
return allOffer;
}
/**
* Check availability the offer to be canceled
* @param offer_token
* @return
*/
public static boolean isAvailable(String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
Document offerDoc = collection.find(eq("_id", new ObjectId(offer_token))).first();
try {
JSONObject jObj = new JSONObject(offerDoc.toJson());
return jObj.getBoolean("availability");
} catch (JSONException ex) {
return false;
}
}
/**
* Dipanggil oleh acceptAcceptFromAnotherServer
* @param offer_token
* @return
*/
public static boolean changeLocalTradeboxToFalse(String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
try{
collection.updateOne(eq("_id", new ObjectId(offer_token)), new Document("$set", new Document("availability", false)));
return true;
}catch (MongoException ex)
{
return false;
}
}
/**
* Jika berhasil, return 1, jika already taken, return 0, jika tidak ada di database, return -1
* @param token
* @param offer_token
* @return
*/
public static int cancelOffer(String token, String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
try {
JSONObject data = new JSONObject(collection.find(eq("_id", new ObjectId(offer_token))));
if(isAvailable(offer_token)) //If isavailable untuk dicancel
{
int itemId = data.getInt("offered_item");
int nItem = data.getInt("n1");
ArrayList<Integer> inv = getInventory(token);
inv.set(itemId, inv.get(itemId) + nItem);
updateInventory(token, inv);
collection.deleteOne(eq("_id", new ObjectId(offer_token)));
return 1;
}
else //already taken
{
return 0;
}
} catch (JSONException | NullPointerException ex) {
System.out.println("[EXEPTION]: cancelOffer database exeption ");
return -1;
}
}
/**
* Mengembalikan token, username, dan password dari username yang dimasukkan
......
......@@ -182,6 +182,16 @@ public class Message {
response = sendFind(token, item);
break;
}
case "findoffer":
{
int item = jsonObj.getInt("item");
response = findOffer(item);
break;
}
case "sendaccept":
{
break;
}
case "serverStatus":
response = serverStatus(jsonString);
break;
......@@ -571,7 +581,7 @@ public class Message {
String response;
JSONObject jsonAllOffers;
try{
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token);
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox();
jsonAllOffers = new JSONObject();
if(allOffers != null)
{
......@@ -595,7 +605,7 @@ public class Message {
}
/**
* Find offer sesuai dengan item id yang diinginkan. Dikirim oleh client.
* Mencari offer sesuai dengan item id yang diinginkan. Dikirim oleh client.
* @param token
* @param itemId
* @return
......@@ -668,28 +678,59 @@ public class Message {
}
return response;
}
/**
* Fungsi untuk catch method find offer dari server lain
* @param offer_token
* Fungsi saat server lain kirim Accept ke server ini
* @param offerToken
* @return
*/
public static String accept(String offer_token) //12
public static String accept(String offerToken) //14
{
String response;
JSONObject jsonObj = new JSONObject();
try
JSONObject jsonObj;
//Ubah status tradebox (dengan id = offerToken) jadi false
if(Database.isAvailable(offerToken)) //Jika tradebox dengan id=offerToken
{
jsonObj.put("status", "ok");
Database.changeLocalTradeboxToFalse(offer_token);
response = jsonObj.toString();
}catch(JSONException ex)
jsonObj = new JSONObject();
if(Database.changeLocalTradeboxToFalse(offerToken))
{
response = statusOKOnly();
}
else
{
response = fail("offer is already fetched!");
}
}
else
{
System.out.println("getFindOffer() Error: " + jsonObj.toString());
response = error();
response = fail("offer is not available");
}
return response;
}
public static String fetchItem(String token, String offer_token) //15
{
return "";
}
public static String cancelOffer(String token, String offer_token)
{
int result = Database.cancelOffer(token, offer_token);
if(result == 1)
{
return statusOKOnly();
}
else if(result == 0)
{
return fail("Alrady Finished");
}
else if(result == -1)
{
return fail("Offer token not exist");
}
else
return error();
}
/**
* Fungsi yang mengembalikan status OK tanpa tambahan status lainnya
* @return String JSON status ok
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package server.helper;
import org.json.JSONException;
import org.json.JSONObject;
import static server.helper.Message.error;
/**
*
* @author akhfa
*/
public class Sender {
private static boolean log = false;
/**
* Mengirim findoffer to another server. Untuk menemukan semua offer yang tersedia dari semua server
* @param item
* @return
*/
public static String sendFindOfferToAnotherServer(int item) //12
{
JSONObject jsonObj = new JSONObject();
String response;
try
{
jsonObj.put("method", "findoffer");
jsonObj.put("item", item);
response = jsonObj.toString();
}
catch(JSONException ex)
{
System.out.println("sendFindOfferToAnotherServer Exeption");
response = error();
}
if(log)
{
System.out.println(response);
}
return response;
}
/**
* Fungsi untuk mengirim sinyal Accept ke server dimana offer_token berasal
* @param offerToken
* @return
*/
public static String sendAcceptToAnotherServer(String offerToken)
{
String result;
try{
JSONObject jObj = new JSONObject();
jObj.put("method", "accept");
jObj.put("offer_token", offerToken);
result = jObj.toString();
}
catch(JSONException ex)
{
System.out.println("sendAcceptToAnotherServer Exeption");
result = error();
}
return result;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment