mercredi 14 décembre 2016

Revamping my servlet to follow JSON API standard using Katarsis library. Where should I keep my global variables?

I have to rework on my servlet (which reponses in JSON format) to provide response in JSON API standard. So, I’ve started rewriting my servlet with model and repository. I’m not much familiar with JSON API standard... As a self learner i face the following difficulties in implementing it with JSON API standard.

Here is a sample snippet describing my servlet

public class TokenSystem extends HttpServlet {

public static final int QUEUE_LIMIT_EXCEEDED;
public static final int NOTIFY_CUSTOMER;

private static String statusCode;

//Some enum values
//Some Status code in case of error

//A inner class (a helper class) which helps in computing response values

class BillGenerationHelper {

long number_of_users = -1;
String owner = null, bill_type = null;
BillDetails obj = null;
int billStatus = -1;
boolean isBillPaid = false;
int billno = -1;

protected void setBillNumber() throws Exception {
this.billno = 123; //Some number after some computation
}

protected setBillDetails {
this.billStatus = 123; //Some status value - After some computation
this.isBillPaid = false; //DB look up and update the value
}

protected long getPurchasedItemSize() throws Exception {

JSONObject itemProperties = getProperties(); //util method to obtain property value
//Based on item - returns the size of the item

return item_size;
}

protected String replaceItemInrack() throws Exception {
//Based on the purchased status - the item has to be placed 
//Item has to be placed in the bill tray - if success 
//Item has to be placed in the store tray - if failure

}

protected JSONObject constructResponse() {
//This constructs the response in JSON format 
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //Based on purchase status 
    //Either have to place the item in store tray or bill tray and return some response values to the client

    return constructResponse();
}
}
}

How can I organise this with JSON API standard. I have done the following things - please correct me if i'm wrong

  • I have implemented my resource - where only getter and setter are allowed i think. i.e.; I can place the values over here which are either obtained as request params or obtained in response
  • I have my repository - where it would be nice to have findOneTarget or findManyTargets like a servlet containing doGet and doPost
  • I have written my Util class which helps in constructing my response
  • And I am invoking my setMethods from repository

Where can i keep my global variables that is used across servlet which is used for some computations? ie; To be more clear i have few values which are used for constructing my response attribute values (these values would be neither in request params nor in response params) so, i couldn’t include them in my resource class. So where shall i keep them? Is util class is a right way of doing?

I'm not satisfied in doing it with util class since i have to pass too many arguments into the methods to compute a value :-(

Is it good to do all the computational work in repository set methods?

Please help me with this.

PS : Happy to edit and add required info if needed.

Aucun commentaire:

Enregistrer un commentaire