001/*
002 * (C) Copyright 2011 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.admin.oauth;
020
021import java.io.Serializable;
022import java.util.Collections;
023import java.util.Map;
024import java.util.Set;
025
026import org.jboss.seam.annotations.In;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.core.api.PropertyException;
031import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
032import org.nuxeo.ecm.directory.BaseSession;
033import org.nuxeo.ecm.directory.DirectoryException;
034import org.nuxeo.ecm.directory.Session;
035import org.nuxeo.ecm.directory.api.DirectoryService;
036import org.nuxeo.runtime.api.Framework;
037
038public abstract class DirectoryBasedEditor implements Serializable {
039
040    /**
041     *
042     */
043    private static final long serialVersionUID = 1L;
044
045    protected DocumentModelList entries;
046
047    protected DocumentModel editableEntry;
048
049    protected DocumentModel creationEntry;
050
051    protected abstract String getDirectoryName();
052
053    protected abstract String getSchemaName();
054
055    protected boolean showAddForm = false;
056
057    @In(create = true)
058    protected transient CoreSession documentManager;
059
060    public boolean getShowAddForm() {
061        return showAddForm;
062    }
063
064    public void toggleShowAddForm() {
065        showAddForm = !showAddForm;
066    }
067
068    public DocumentModel getCreationEntry() throws PropertyException {
069        if (creationEntry == null) {
070            creationEntry = BaseSession.createEntryModel(null, getSchemaName(), null, null);
071        }
072        return creationEntry;
073    }
074
075    public void refresh() {
076        entries = null;
077    }
078
079    public void createEntry() throws DirectoryException {
080        DirectoryService ds = Framework.getService(DirectoryService.class);
081        Framework.doPrivileged(() -> {
082            try (Session session = ds.open(getDirectoryName())) {
083                session.createEntry(creationEntry);
084                creationEntry = null;
085                showAddForm = false;
086                entries = null;
087            }
088        });
089    }
090
091    public void resetCreateEntry() {
092        creationEntry = null;
093        showAddForm = false;
094    }
095
096    public void resetEditEntry() {
097        editableEntry = null;
098        showAddForm = false;
099    }
100
101    public DocumentModel getEditableEntry() {
102        return editableEntry;
103    }
104
105    protected Map<String, Serializable> getQueryFilter() {
106        return Collections.emptyMap();
107    }
108
109    protected Set<String> getOrderSet() {
110        return Collections.emptySet();
111    }
112
113    public DocumentModelList getEntries() throws DirectoryException {
114        if (entries == null) {
115            DirectoryService ds = Framework.getService(DirectoryService.class);
116            Framework.doPrivileged(() -> {
117                try (Session session = ds.open(getDirectoryName())) {
118                    Map<String, Serializable> emptyMap = getQueryFilter();
119                    Set<String> emptySet = getOrderSet();
120                    entries = session.query(emptyMap, emptySet, null, true);
121                }
122            });
123        }
124        return entries;
125    }
126
127    public void editEntry(String entryId) throws DirectoryException {
128        DirectoryService ds = Framework.getService(DirectoryService.class);
129        Framework.doPrivileged(() -> {
130            try (Session session = ds.open(getDirectoryName())) {
131                editableEntry = session.getEntry(entryId);
132            }
133        });
134    }
135
136    public void saveEntry() throws DirectoryException {
137        DirectoryService ds = Framework.getService(DirectoryService.class);
138        Framework.doPrivileged(() -> {
139            try (Session directorySession = ds.open(getDirectoryName())) {
140                UnrestrictedSessionRunner sessionRunner = new UnrestrictedSessionRunner(documentManager) {
141                    @Override
142                    public void run() {
143                        directorySession.updateEntry(editableEntry);
144                    }
145                };
146                sessionRunner.runUnrestricted();
147                editableEntry = null;
148                entries = null;
149            }
150        });
151    }
152
153    public void deleteEntry(String entryId) throws DirectoryException {
154        DirectoryService ds = Framework.getService(DirectoryService.class);
155        Framework.doPrivileged(() -> {
156            try (Session directorySession = ds.open(getDirectoryName())) {
157                UnrestrictedSessionRunner sessionRunner = new UnrestrictedSessionRunner(documentManager) {
158                    @Override
159                    public void run() {
160                        directorySession.deleteEntry(entryId);
161                    }
162                };
163                sessionRunner.runUnrestricted();
164                if (editableEntry != null && editableEntry.getId().equals(entryId)) {
165                    editableEntry = null;
166                }
167                entries = null;
168            }
169        });
170    }
171}