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 javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
022import static org.nuxeo.ecm.restapi.server.jaxrs.directory.DirectorySessionRunner.withDirectorySession;
023
024import java.util.List;
025
026import javax.ws.rs.DELETE;
027import javax.ws.rs.GET;
028import javax.ws.rs.PUT;
029import javax.ws.rs.core.Response;
030import javax.ws.rs.core.Response.Status;
031
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.directory.Directory;
035import org.nuxeo.ecm.directory.DirectoryException;
036import org.nuxeo.ecm.directory.Session;
037import org.nuxeo.ecm.directory.api.DirectoryDeleteConstraint;
038import org.nuxeo.ecm.directory.api.DirectoryEntry;
039import org.nuxeo.ecm.directory.api.DirectoryService;
040import org.nuxeo.ecm.webengine.model.WebObject;
041import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
042import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
043import org.nuxeo.runtime.api.Framework;
044
045/**
046 * @since 5.7.3
047 */
048@WebObject(type = "directoryEntry")
049public class DirectoryEntryObject extends DefaultObject {
050
051    protected DirectoryEntry entry;
052
053    protected Directory directory;
054
055    protected String entryId;
056
057    @Override
058    protected void initialize(Object... args) {
059        if (args.length < 1) {
060            throw new IllegalArgumentException("Directory Entry object takes one parameter");
061        }
062
063        entry = (DirectoryEntry) args[0];
064        directory = getDirectoryFromEntry(entry);
065        entryId = (String) args[1];
066    }
067
068    @GET
069    public DirectoryEntry doGet() {
070        return entry;
071    }
072
073    @PUT
074    public DirectoryEntry doUpdateEntry(final DirectoryEntry entry) {
075        checkEditGuards();
076        return withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() {
077
078            @Override
079            DirectoryEntry run(Session session) {
080                DocumentModel docEntry = entry.getDocumentModel();
081                session.updateEntry(docEntry);
082                return new DirectoryEntry(directory.getName(), session.getEntry(docEntry.getId()));
083
084            }
085        });
086    }
087
088    private void checkEditGuards() {
089        ((DirectoryObject) prev).checkEditGuards();
090    }
091
092    /**
093     * @since 8.4
094     */
095    private void checkDeleteGuards() {
096        List<DirectoryDeleteConstraint> deleteConstraints = directory.getDirectoryDeleteConstraints();
097        DirectoryService directoryService = Framework.getService(DirectoryService.class);
098        if (deleteConstraints != null && !deleteConstraints.isEmpty()) {
099            for (DirectoryDeleteConstraint deleteConstraint : deleteConstraints) {
100                if (!deleteConstraint.canDelete(directoryService, entryId)) {
101                    throw new NuxeoException("This entry is referenced in another vocabulary.", SC_BAD_REQUEST);
102                }
103            }
104        }
105    }
106
107    @DELETE
108    public Response doDeleteEntry() {
109        checkEditGuards();
110        checkDeleteGuards();
111        withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() {
112
113            @Override
114            DirectoryEntry run(Session session) {
115                session.deleteEntry(entry.getDocumentModel());
116                return null;
117            }
118        });
119
120        return Response.ok().status(Status.NO_CONTENT).build();
121
122    }
123
124    private Directory getDirectoryFromEntry(final DirectoryEntry entry) {
125        DirectoryService ds = Framework.getService(DirectoryService.class);
126        Directory directory;
127        try {
128            directory = ds.getDirectory(entry.getDirectoryName());
129        } catch (DirectoryException e) {
130            throw new WebResourceNotFoundException("directory not found");
131        }
132        return directory;
133    }
134
135}