001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.server.jaxrs.directory;
018
019import static org.nuxeo.ecm.restapi.server.jaxrs.directory.DirectorySessionRunner.withDirectorySession;
020
021import javax.ws.rs.DELETE;
022import javax.ws.rs.GET;
023import javax.ws.rs.PUT;
024import javax.ws.rs.core.Response;
025import javax.ws.rs.core.Response.Status;
026
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.directory.Directory;
029import org.nuxeo.ecm.directory.DirectoryException;
030import org.nuxeo.ecm.directory.Session;
031import org.nuxeo.ecm.directory.api.DirectoryEntry;
032import org.nuxeo.ecm.directory.api.DirectoryService;
033import org.nuxeo.ecm.webengine.model.WebObject;
034import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
035import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * @since 5.7.3
040 */
041@WebObject(type = "directoryEntry")
042public class DirectoryEntryObject extends DefaultObject {
043
044    protected DirectoryEntry entry;
045
046    protected Directory directory;
047
048    @Override
049    protected void initialize(Object... args) {
050        if (args.length < 1) {
051            throw new IllegalArgumentException("Directory Entry obhect  takes one parameter");
052        }
053
054        entry = (DirectoryEntry) args[0];
055        directory = getDirectoryFromEntry(entry);
056
057    }
058
059    @GET
060    public DirectoryEntry doGet() {
061        return entry;
062    }
063
064    @PUT
065    public DirectoryEntry doUpdateEntry(final DirectoryEntry entry) {
066        checkEditGuards();
067        return withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() {
068
069            @Override
070            DirectoryEntry run(Session session) {
071                DocumentModel docEntry = entry.getDocumentModel();
072                session.updateEntry(docEntry);
073
074                String id = (String) docEntry.getPropertyValue(directory.getSchema() + ":" + directory.getIdField());
075
076                return new DirectoryEntry(directory.getName(), session.getEntry(id));
077
078            }
079        });
080
081    }
082
083    private void checkEditGuards() {
084        ((DirectoryObject) prev).checkEditGuards();
085    }
086
087    @DELETE
088    public Response doDeleteEntry() {
089        checkEditGuards();
090        withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() {
091
092            @Override
093            DirectoryEntry run(Session session) {
094                session.deleteEntry(entry.getDocumentModel());
095                return null;
096            }
097        });
098
099        return Response.ok().status(Status.NO_CONTENT).build();
100
101    }
102
103    private Directory getDirectoryFromEntry(final DirectoryEntry entry) {
104        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
105        Directory directory;
106        try {
107            directory = ds.getDirectory(entry.getDirectoryName());
108        } catch (DirectoryException e) {
109            throw new WebResourceNotFoundException("directory not found");
110        }
111        return directory;
112    }
113
114}