How to update a database with resteasy @PUT. Please help
olasamuel Mar 14, 2011 4:54 AMHi All,
Please, I need a help regarding this and I will appreciate it if someone can put me through. OK this is the problem (and please, note, I am new to RestEasy and I am trying with it and always ready to learn).
I am creating a resteasy application where a user's account will be deducted based on. So when the user initiate a transaction, the system will reserve some units from his/her account and then after the transaction, the total money will be deducted and then the system will then update the user's account table using the RestEasy @PUT request method. I have tried my best but all I can get is to GET inside the user's account and fetch the units out but my system is not deducting nor updating table using the @PUT request method. I have also tried to go through the example but the fact is that most of the examples that I have seen so far have always gone in one direction which only to explain and empahsize on the the @GET Request Method.
Can someone please help put me through? I will greatly appreciate this. Below is my code. Thank you.
public interface SubscriberTopUpResource {
@GET
@PUT
@Path("/reserveUnits")
@Produces(MediaType.TEXT_PLAIN)
public String reserveUnits(@PathParam("msisdn") String msisdn, @PathParam("requestedUnits") long requestedUnits);
}
public String reserveUnits(@PathParam("msisdn") String msisdn, @PathParam("requestedUnits") long requestedUnits) {
String balInquiry = "select balance from SubscriberTopUp where msisdn= :msisdn";
System.out.println("I was able to get here");
Query balInquiryQuery = entityManager.createQuery(balInquiry);
System.out.println("Something happened here");
long allocatedUnits = 0;
System.out.println("Another thing happened here");
@SuppressWarnings("unchecked")
List<Long> bals = (List<Long>)balInquiryQuery.setParameter("msisdn", Long.valueOf(msisdn)).getResultList();
System.out.println("OK Lets see if another thing happened here again");
//BigInteger newbal = new BigInteger(String.valueOf(requestedUnits));
if (bals.size() > 0)
{
allocatedUnits = Math.min(requestedUnits, bals.get(0));
if (bals.get(0) > requestedUnits)
FacesMessages.createFacesMessage(FacesMessage.SEVERITY_WARN, "Insufficient Fund.", "The System could only allocated " + allocatedUnits + "units");
System.out.println("You Reuested " + requestedUnits + " The current units in your account is " + bals + "units");
//update the balance
long oldBalance = bals.get(0);
long newBalance = oldBalance - allocatedUnits;
//subscriberTopUp = new SubscriberTopUp();
String balQuery = "Update SubscriberTopUp SET balance = :newBalance WHERE msisdn = :msisdn";
System.out.println("The Update statement is :"+balQuery);
Query query = entityManager.createQuery(balQuery);
System.out.println("I can also see entity manager getting through " + query);
query.setParameter("newBalance", newBalance);
System.out.println("I finally got here " + query);
query.setParameter("msisdn", Long.valueOf(msisdn));
System.out.println("And Lastly " + query);
entityManager.flush();
//int rowsAffected = (Integer) query.getSingleResult();
System.out.println("Also the commit was done");
entityManager.getTransaction().commit();
//System.out.println("I just got here : updated : "+String.valueOf(rowsAffected));
} else {
System.out.println("Null Record Returned" + bals.toString());
}
allocatedUnits = bals.get(0);
System.out.println("Returning : "+allocatedUnits);
return String.valueOf(bals);
}
}
Please, I need a help regarding this and I will appreciate it if someone can put me through. OK this is the problem (and please, note, I am new to RestEasy and I am trying with it and always ready to learn).
I am creating a resteasy application where a user's account will be deducted based on. So when the user initiate a transaction, the system will reserve some units from his/her account and then after the transaction, the total money will be deducted and then the system will then update the user's account table using the RestEasy @PUT request method. I have tried my best but all I can get is to GET inside the user's account and fetch the units out but my system is not deducting nor updating table using the @PUT request method. I have also tried to go through the example but the fact is that most of the examples that I have seen so far have always gone in one direction which only to explain and empahsize on the the @GET Request Method.
Can someone please help put me through? I will greatly appreciate this. Below is my code. Thank you.
public interface SubscriberTopUpResource {
@GET
@PUT
@Path("/reserveUnits")
@Produces(MediaType.TEXT_PLAIN)
public String reserveUnits(@PathParam("msisdn") String msisdn, @PathParam("requestedUnits") long requestedUnits);
}
public String reserveUnits(@PathParam("msisdn") String msisdn, @PathParam("requestedUnits") long requestedUnits) {
String balInquiry = "select balance from SubscriberTopUp where msisdn= :msisdn";
System.out.println("I was able to get here");
Query balInquiryQuery = entityManager.createQuery(balInquiry);
System.out.println("Something happened here");
long allocatedUnits = 0;
System.out.println("Another thing happened here");
@SuppressWarnings("unchecked")
List<Long> bals = (List<Long>)balInquiryQuery.setParameter("msisdn", Long.valueOf(msisdn)).getResultList();
System.out.println("OK Lets see if another thing happened here again");
//BigInteger newbal = new BigInteger(String.valueOf(requestedUnits));
if (bals.size() > 0)
{
allocatedUnits = Math.min(requestedUnits, bals.get(0));
if (bals.get(0) > requestedUnits)
FacesMessages.createFacesMessage(FacesMessage.SEVERITY_WARN, "Insufficient Fund.", "The System could only allocated " + allocatedUnits + "units");
System.out.println("You Reuested " + requestedUnits + " The current units in your account is " + bals + "units");
//update the balance
long oldBalance = bals.get(0);
long newBalance = oldBalance - allocatedUnits;
//subscriberTopUp = new SubscriberTopUp();
String balQuery = "Update SubscriberTopUp SET balance = :newBalance WHERE msisdn = :msisdn";
System.out.println("The Update statement is :"+balQuery);
Query query = entityManager.createQuery(balQuery);
System.out.println("I can also see entity manager getting through " + query);
query.setParameter("newBalance", newBalance);
System.out.println("I finally got here " + query);
query.setParameter("msisdn", Long.valueOf(msisdn));
System.out.println("And Lastly " + query);
entityManager.flush();
//int rowsAffected = (Integer) query.getSingleResult();
System.out.println("Also the commit was done");
entityManager.getTransaction().commit();
//System.out.println("I just got here : updated : "+String.valueOf(rowsAffected));
} else {
System.out.println("Null Record Returned" + bals.toString());
}
allocatedUnits = bals.get(0);
System.out.println("Returning : "+allocatedUnits);
return String.valueOf(bals);
}
}