Ignore:
Timestamp:
Sep 23, 2012, 2:28:05 PM (12 years ago)
Author:
casties
Branch:
default
Children:
25:2140ef107551, 28:f4ed2ed33e5b
Message:

more work on groups ui.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java

    r23 r24  
    55
    66import org.apache.log4j.Logger;
    7 import org.restlet.data.Form;
    87import org.restlet.data.MediaType;
    98import org.restlet.data.Reference;
     
    1211import org.restlet.representation.StringRepresentation;
    1312import org.restlet.resource.Get;
    14 import org.restlet.resource.Post;
     13import org.restlet.resource.ResourceException;
    1514import org.restlet.resource.ServerResource;
    1615
    17 import de.mpiwg.itgroup.annotations.Actor;
    1816import de.mpiwg.itgroup.annotations.Group;
    1917import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
     
    3028    public static Logger logger = Logger.getLogger(GroupResource.class);
    3129
    32     private AnnotationStore store;
     30    protected AnnotationStore store;
     31   
     32    protected String requestId;
     33   
     34    protected Group group;
     35   
     36    @Override
     37    protected void doInit() throws ResourceException {
     38        super.doInit();
     39        // id from URI /annotations/groups/{id}
     40        requestId = (String) getRequest().getAttributes().get("id");
     41        logger.debug("group-id=" + requestId);
     42        // get store instance
     43        if (store == null) {
     44            store = ((BaseRestlet) getApplication()).getAnnotationStore();
     45        }
     46        // get group from store
     47        if (requestId != null) {
     48            group = (Group) store.getActor(new Group(requestId));
     49        }
     50    }
    3351
    3452    /**
     
    4058    @Get("html")
    4159    public Representation doGetHTML(Representation entity) {
    42         // id from URI /annotations/groups/{id}
    43         String id = (String) getRequest().getAttributes().get("id");
    44         logger.debug("group-id=" + id);
    45         if (id == null || id.isEmpty()) {
     60        if (requestId == null || requestId.isEmpty()) {
    4661            // invalid id
    4762            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
     
    4964        }
    5065        String result = null;
    51         store = getAnnotationStore();
    5266        Reference groupsUrl = this.getReference().getParentRef();
    5367        result = "<html><body>\n<h1>Group</h1>\n";
    5468        result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
    55         Group group = new Group(id);
    56         group = (Group) store.getActor(group);
    5769        result += "<table>";
    5870        result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
     
    6880    }
    6981
    70     /**
    71      * POST creates a new Group.
    72      *
    73      * @return
    74      */
    75     @Post
    76     public Representation doPost(Representation entity) {
    77         logger.debug("AnnotationsUiGroup doPost!");
    78         // TODO: do authentication
    79         Form form = new Form(entity);
    80         String id = form.getFirstValue("id");
    81         String name = form.getFirstValue("name");
    82         if (id == null || id.isEmpty()) {
    83             // invalid id
    84             setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
    85             return null;
    86         }
    87         String gid = null;
    88         Group newGroup = new Group(gid, null, name);
    89         store = getAnnotationStore();
    90         Actor storedGroup = store.storeActor(newGroup);
    91         gid = storedGroup.getId();
    92         // return 303: see other
    93         setStatus(Status.REDIRECTION_SEE_OTHER);
    94         // go GET URL for this group
    95         Reference groupUrl = this.getReference();
    96         groupUrl.addSegment(gid);
    97         this.getResponse().setLocationRef(groupUrl);
    98         return null;
    99     }
    100 
    101     protected AnnotationStore getAnnotationStore() {
    102         if (store == null) {
    103             store = ((BaseRestlet) getApplication()).getAnnotationStore();
    104         }
    105         return store;
    106     }
    10782}
Note: See TracChangeset for help on using the changeset viewer.