001/*
002 * (C) Copyright 2013 Nuxeo SA (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 *     dmetzler
018 */
019package org.nuxeo.ecm.restapi.server.jaxrs.directory;
020
021import static org.nuxeo.ecm.restapi.server.jaxrs.directory.DirectorySessionRunner.withDirectorySession;
022
023import javax.ws.rs.DELETE;
024import javax.ws.rs.GET;
025import javax.ws.rs.PUT;
026import javax.ws.rs.core.Response;
027import javax.ws.rs.core.Response.Status;
028
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.directory.Directory;
031import org.nuxeo.ecm.directory.DirectoryException;
032import org.nuxeo.ecm.directory.Session;
033import org.nuxeo.ecm.directory.api.DirectoryEntry;
034import org.nuxeo.ecm.directory.api.DirectoryService;
035import org.nuxeo.ecm.webengine.model.WebObject;
036import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
037import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @since 5.7.3
042 */
043@WebObject(type = "directoryEntry")
044public class DirectoryEntryObject extends DefaultObject {
045
046    protected DirectoryEntry entry;
047
048    protected Directory directory;
049
050    @Override
051    protected void initialize(Object... args) {
052        if (args.length < 1) {
053            throw new IllegalArgumentException("Directory Entry obhect  takes one parameter");
054        }
055
056        entry = (DirectoryEntry) args[0];
057        directory = getDirectoryFromEntry(entry);
058
059    }
060
061    @GET
062    public DirectoryEntry doGet() {
063        return entry;
064    }
065
066    @PUT
067    public DirectoryEntry doUpdateEntry(final DirectoryEntry entry) {
068        checkEditGuards();
069        return withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() {
070
071            @Override
072            DirectoryEntry run(Session session) {
073                DocumentModel docEntry = entry.getDocumentModel();
074                session.updateEntry(docEntry);
075
076                String id = (String) docEntry.getPropertyValue(directory.getSchema() + ":" + directory.getIdField());
077
078                return new DirectoryEntry(directory.getName(), session.getEntry(id));
079
080            }
081        });
082
083    }
084
085    private void checkEditGuards() {
086        ((DirectoryObject) prev).checkEditGuards();
087    }
088
089    @DELETE
090    public Response doDeleteEntry() {
091        checkEditGuards();
092        withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() {
093
094            @Override
095            DirectoryEntry run(Session session) {
096                session.deleteEntry(entry.getDocumentModel());
097                return null;
098            }
099        });
100
101        return Response.ok().status(Status.NO_CONTENT).build();
102
103    }
104
105    private Directory getDirectoryFromEntry(final DirectoryEntry entry) {
106        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
107        Directory directory;
108        try {
109            directory = ds.getDirectory(entry.getDirectoryName());
110        } catch (DirectoryException e) {
111            throw new WebResourceNotFoundException("directory not found");
112        }
113        return directory;
114    }
115
116}