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        try (Session session = ds.open(getDirectoryName())) {
082            session.createEntry(creationEntry);
083            creationEntry = null;
084            showAddForm = false;
085            entries = null;
086        }
087    }
088
089    public void resetCreateEntry() {
090        creationEntry = null;
091        showAddForm = false;
092    }
093
094    public void resetEditEntry() {
095        editableEntry = null;
096        showAddForm = false;
097    }
098
099    public DocumentModel getEditableEntry() {
100        return editableEntry;
101    }
102
103    protected Map<String, Serializable> getQueryFilter() {
104        return Collections.emptyMap();
105    }
106
107    protected Set<String> getOrderSet() {
108        return Collections.emptySet();
109    }
110
111    public DocumentModelList getEntries() throws DirectoryException {
112        if (entries == null) {
113            DirectoryService ds = Framework.getService(DirectoryService.class);
114            try (Session session = ds.open(getDirectoryName())) {
115                Map<String, Serializable> emptyMap = getQueryFilter();
116                Set<String> emptySet = getOrderSet();
117
118                entries = session.query(emptyMap, emptySet, null, true);
119            }
120        }
121        return entries;
122    }
123
124    public void editEntry(String entryId) throws DirectoryException {
125        DirectoryService ds = Framework.getService(DirectoryService.class);
126        try (Session session = ds.open(getDirectoryName())) {
127            editableEntry = session.getEntry(entryId);
128        }
129    }
130
131    public void saveEntry() throws DirectoryException {
132        DirectoryService ds = Framework.getService(DirectoryService.class);
133        try (Session directorySession = ds.open(getDirectoryName())) {
134            UnrestrictedSessionRunner sessionRunner = new UnrestrictedSessionRunner(documentManager) {
135                @Override
136                public void run() {
137                    directorySession.updateEntry(editableEntry);
138                }
139            };
140            sessionRunner.runUnrestricted();
141            editableEntry = null;
142            entries = null;
143        }
144    }
145
146    public void deleteEntry(String entryId) throws DirectoryException {
147        DirectoryService ds = Framework.getService(DirectoryService.class);
148        try (Session directorySession = ds.open(getDirectoryName())) {
149            UnrestrictedSessionRunner sessionRunner = new UnrestrictedSessionRunner(documentManager) {
150                @Override
151                public void run() {
152                    directorySession.deleteEntry(entryId);
153                }
154            };
155            sessionRunner.runUnrestricted();
156            if (editableEntry != null && editableEntry.getId().equals(entryId)) {
157                editableEntry = null;
158            }
159            entries = null;
160        }
161    }
162}