java - REST - mark entries read/unread -
i did simple resp app entering blog entries html page, here sample code.. ideas why? googles looks should ok.. showing did.. every entry has id, saved in arraylist , has simple attribute text.. wanna add special commands later, can add event in command line well.. wanna add function can mark read/unread, default unread.. ideas how that?
thanks
@path("/") public class rootresource { private final entrylistresource entries = new entrylistresource(); @get @path("favicon.ico") public response getfavicon() { return response.nocontent().build(); } @get public response get(@context uriinfo uriinfo) { final uri location = uriinfo.getabsolutepathbuilder().path("/entries").build(); return response.seeother(location).build(); } @path("entries") public entrylistresource getentries() { return entries; } }
and
@xmlrootelement(name = "entryref") public class entryresourceref { private int id; private uri href; @xmlattribute public uri gethref() { return href; } @xmlattribute public int getid() { return id; } public void setid(int id) { this.id = id; } public void sethref(uri href) { this.href = href; } }
finally
@xmlrootelement(name = "entry") @xmlaccessortype(xmlaccesstype.none) public class entryresource { private int id; private string text; @get @produces(mediatype.application_xml) public response get() { return response.ok(this).build(); } @get @produces(mediatype.text_html) public response gethtml(@context uriinfo uriinfo) { final stringbuilder sb = new stringbuilder(); @put @consumes(mediatype.text_plain) public response updateplain(string text,string description, @context uriinfo uriinfo) { return update(text, description, uriinfo.getabsolutepath()); } @put @consumes(mediatype.application_form_urlencoded) public response updateasform(@formparam("text") string text, @formparam("description") string description,@context uriinfo uriinfo) { return update(description,text, uriinfo.getabsolutepath()); } private response update(string text,string description, uri self) { if (gettext().equals(text)) { throw new webapplicationexception( response .status(response.status.conflict) .entity("the blog entry text has not been modified") .type(mediatype.text_plain) .build() ); } settext(text); return response.seeother(self).build(); }
sources
@xmlrootelement(name = "entries") @xmlaccessortype(xmlaccesstype.none) public class entrylistresource { private final atomicinteger nextentryid = new atomicinteger(0); private uriinfo uriinfo = null; @get @produces(mediatype.application_xml) public response get() { return response.ok(this).cachecontrol(cc).build(); } @xmlelement(name = "blog") public collection<entryresourceref> getentries() { final collection<entryresourceref> refs = new arraylist<entryresourceref>(); (final entryresource entry : entries.values()) { final entryresourceref ref = new entryresourceref(); ref.setid(entry.getid()); ref.sethref(getentryuri(entry)); refs.add(ref); } return refs; }
you may use 0 , 1 sort results.
while fetching records may append "read" or "unread" in query itself. example given below
select news.*, (case news.status when 0 'unread' when 1 'read' end) status news news
this native sql or can in hql well.
other solution can may take enum in news entity. can string shown in ui , in db save ordinal value i.e. 0 or 1. example code given below.
@table(name = "new") public class new { private newsstatus newsstatus; } public static enum newsstatus { read{ public string tostring(){ return "read"; } }, unread{ public string tostring(){ return "unread"; } } public int getvalue(){ return this.ordinal() + 1; } public string getlabel(){ return this.tostring(); } public static newsstatus getbylabel(string label) { if(label == null) return null; for(newsstatus ns : newsstatus.values()) { if(label.equalsignorecase(ns.getlabel())) return ns; } return null; } public static newsstatus getbyvalue(int value){ return newsstatus.values()[value-1]; } }
do let me know if further details required?
krish