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