001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Funsho David
018 */
019
020package org.nuxeo.ecm.restapi.server.jaxrs.annotation;
021
022import java.util.List;
023
024import javax.ws.rs.DELETE;
025import javax.ws.rs.GET;
026import javax.ws.rs.POST;
027import javax.ws.rs.PUT;
028import javax.ws.rs.Path;
029import javax.ws.rs.PathParam;
030import javax.ws.rs.Produces;
031import javax.ws.rs.QueryParam;
032import javax.ws.rs.core.MediaType;
033import javax.ws.rs.core.Response;
034
035import org.nuxeo.ecm.annotation.Annotation;
036import org.nuxeo.ecm.annotation.AnnotationService;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.webengine.model.WebAdapter;
039import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * @since 10.1
044 */
045@WebAdapter(name = AnnotationAdapter.NAME, type = "annotationAdapter")
046@Produces(MediaType.APPLICATION_JSON)
047public class AnnotationAdapter extends DefaultAdapter {
048
049    public static final String NAME = "annotation";
050
051    @POST
052    public Response createAnnotation(Annotation annotation) {
053        AnnotationService annotationService = Framework.getService(AnnotationService.class);
054        Annotation result = annotationService.createAnnotation(getContext().getCoreSession(), annotation);
055        return Response.status(Response.Status.CREATED).entity(result).build();
056    }
057
058    @GET
059    public List<Annotation> getAnnotations(@QueryParam("xpath") String xpath) {
060        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
061        AnnotationService annotationService = Framework.getService(AnnotationService.class);
062        return annotationService.getAnnotations(getContext().getCoreSession(), doc.getId(), xpath);
063    }
064
065    @GET
066    @Path("{annotationId}")
067    public Annotation getAnnotation(@PathParam("annotationId") String annotationId) {
068        AnnotationService annotationService = Framework.getService(AnnotationService.class);
069        return annotationService.getAnnotation(getContext().getCoreSession(), annotationId);
070    }
071
072    @PUT
073    public Annotation updateAnnotation(Annotation annotation) {
074        AnnotationService annotationService = Framework.getService(AnnotationService.class);
075        annotationService.updateAnnotation(getContext().getCoreSession(), annotation);
076        return annotation;
077    }
078
079    @DELETE
080    @Path("{annotationId}")
081    public Response deleteAnnotation(@PathParam("annotationId") String annotationId) {
082        AnnotationService annotationService = Framework.getService(AnnotationService.class);
083        annotationService.deleteAnnotation(getContext().getCoreSession(), annotationId);
084        return Response.noContent().build();
085    }
086
087}