001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     anguenot
016 *
017 * $Id: WSAuditBean.java 30185 2008-02-14 17:56:36Z tdelprat $
018 */
019
020package org.nuxeo.ecm.platform.audit.ws;
021
022import static org.nuxeo.ecm.core.api.event.DocumentEventCategories.EVENT_DOCUMENT_CATEGORY;
023import static org.nuxeo.ecm.core.api.event.DocumentEventCategories.EVENT_LIFE_CYCLE_CATEGORY;
024
025import java.util.ArrayList;
026import java.util.Date;
027import java.util.HashSet;
028import java.util.List;
029import java.util.Set;
030
031import javax.jws.WebMethod;
032import javax.jws.WebParam;
033import javax.jws.WebService;
034import javax.jws.soap.SOAPBinding;
035import javax.jws.soap.SOAPBinding.Style;
036
037import org.nuxeo.ecm.core.schema.utils.DateParser;
038import org.nuxeo.ecm.platform.audit.api.LogEntry;
039import org.nuxeo.ecm.platform.audit.api.Logs;
040import org.nuxeo.ecm.platform.audit.ws.api.WSAudit;
041import org.nuxeo.ecm.platform.ws.AbstractNuxeoWebService;
042import org.nuxeo.runtime.api.Framework;
043
044/**
045 * Audit Web Service bean.
046 *
047 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
048 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
049 */
050@WebService(name = "WSAuditInterface", serviceName = "WSAuditService")
051@SOAPBinding(style = Style.DOCUMENT)
052public class WSAuditBean extends AbstractNuxeoWebService implements WSAudit {
053
054    private static final long serialVersionUID = 1L;
055
056    protected final Logs getLogsBean() {
057        return Framework.getService(Logs.class);
058    }
059
060    @WebMethod
061    public ModifiedDocumentDescriptor[] listModifiedDocuments(@WebParam(name = "sessionId") String sessionId,
062            @WebParam(name = "dataRangeQuery") String dateRangeQuery) {
063        initSession(sessionId);
064
065        BatchInfo batchInfo = BatchHelper.getBatchInfo(sessionId, dateRangeQuery);
066
067        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(null, batchInfo.getPageDateRange(),
068                EVENT_DOCUMENT_CATEGORY, null, batchInfo.getNextPage(), batchInfo.getPageSize());
069        if (logEntries.size() < batchInfo.getPageSize()) {
070            // we are at the end of the batch
071            // ==> reset the batch
072            BatchHelper.resetBatchInfo(sessionId);
073        } else {
074            // set the batchInfo ready for next call
075            batchInfo.prepareNextCall();
076        }
077
078        List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
079        Set<String> uuids = new HashSet<String>();
080        for (LogEntry logEntry : logEntries) {
081            if (!uuids.contains(logEntry.getDocUUID())) {
082                uuids.add(logEntry.getDocUUID());
083                ldocs.add(new ModifiedDocumentDescriptor(logEntry.getEventDate(), logEntry.getDocType(),
084                        logEntry.getDocUUID()));
085            }
086        }
087
088        ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
089        ldocs.toArray(docs);
090
091        return docs;
092    }
093
094    @WebMethod
095    public ModifiedDocumentDescriptorPage listModifiedDocumentsByPage(@WebParam(name = "sessionId") String sessionId,
096            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "docPath") String path,
097            @WebParam(name = "pageIndex") int page, @WebParam(name = "pageSize") int pageSize) {
098        initSession(sessionId);
099
100        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(null, dateRangeQuery, EVENT_DOCUMENT_CATEGORY, path,
101                page, pageSize);
102
103        boolean hasMorePage = logEntries.size() >= pageSize;
104
105        List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
106        Set<String> uuids = new HashSet<String>();
107        for (LogEntry logEntry : logEntries) {
108            if (!uuids.contains(logEntry.getDocUUID())) {
109                uuids.add(logEntry.getDocUUID());
110                ldocs.add(new ModifiedDocumentDescriptor(logEntry.getEventDate(), logEntry.getDocType(),
111                        logEntry.getDocUUID()));
112            }
113        }
114
115        ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
116        ldocs.toArray(docs);
117
118        return new ModifiedDocumentDescriptorPage(docs, page, hasMorePage);
119    }
120
121    @WebMethod
122    public ModifiedDocumentDescriptorPage listDeletedDocumentsByPage(@WebParam(name = "sessionId") String sessionId,
123            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "docPath") String path,
124            @WebParam(name = "pageIndex") int page, @WebParam(name = "pageSize") int pageSize) {
125        initSession(sessionId);
126
127        String[] eventIds = { "documentRemoved" };
128
129        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(eventIds, dateRangeQuery, "eventDocumentCategory",
130                path, page, pageSize);
131
132        boolean hasMorePage = logEntries.size() >= pageSize;
133
134        List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
135        Set<String> uuids = new HashSet<String>();
136        for (LogEntry logEntry : logEntries) {
137            if (!uuids.contains(logEntry.getDocUUID())) {
138                uuids.add(logEntry.getDocUUID());
139                ldocs.add(new ModifiedDocumentDescriptor(logEntry.getEventDate(), logEntry.getDocType(),
140                        logEntry.getDocUUID()));
141            }
142        }
143
144        ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
145        ldocs.toArray(docs);
146
147        return new ModifiedDocumentDescriptorPage(docs, page, hasMorePage);
148    }
149
150    @WebMethod
151    public EventDescriptorPage listEventsByPage(@WebParam(name = "sessionId") String sessionId,
152            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "pageIndex") int page,
153            @WebParam(name = "pageSize") int pageSize) {
154        initSession(sessionId);
155
156        String[] categories = new String[0];
157        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(null, dateRangeQuery, categories, null, page,
158                pageSize);
159        boolean hasMorePage = logEntries.size() >= pageSize;
160
161        List<EventDescriptor> events = new ArrayList<EventDescriptor>();
162
163        for (LogEntry logEntry : logEntries) {
164            events.add(new EventDescriptor(logEntry));
165        }
166
167        EventDescriptor[] evts = new EventDescriptor[events.size()];
168        events.toArray(evts);
169
170        return new EventDescriptorPage(evts, page, hasMorePage);
171    }
172
173    @WebMethod
174    public EventDescriptorPage listDocumentEventsByPage(@WebParam(name = "sessionId") String sessionId,
175            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "startDate") String startDate,
176            @WebParam(name = "path") String path, @WebParam(name = "pageIndex") int page,
177            @WebParam(name = "pageSize") int pageSize) {
178        initSession(sessionId);
179
180        String[] docCategories = { EVENT_DOCUMENT_CATEGORY, EVENT_LIFE_CYCLE_CATEGORY };
181
182        List<LogEntry> logEntries;
183        if (dateRangeQuery != null && dateRangeQuery.length() > 0) {
184            logEntries = getLogsBean().queryLogsByPage(null, dateRangeQuery, docCategories, path, page, pageSize);
185        } else {
186            Date limit = DateParser.parseW3CDateTime(startDate);
187            logEntries = getLogsBean().queryLogsByPage(null, limit, docCategories, path, page, pageSize);
188        }
189        boolean hasMorePage = logEntries.size() >= pageSize;
190
191        List<EventDescriptor> events = new ArrayList<EventDescriptor>();
192
193        for (LogEntry logEntry : logEntries) {
194            events.add(new EventDescriptor(logEntry));
195        }
196
197        EventDescriptor[] evts = new EventDescriptor[events.size()];
198        events.toArray(evts);
199
200        return new EventDescriptorPage(evts, page, hasMorePage);
201    }
202
203    @WebMethod
204    public EventDescriptorPage queryEventsByPage(@WebParam(name = "sessionId") String sessionId,
205            @WebParam(name = "whereClause") String whereClause, @WebParam(name = "pageIndex") int page,
206            @WebParam(name = "pageSize") int pageSize) {
207        initSession(sessionId);
208
209        List<LogEntry> logEntries = getLogsBean().nativeQueryLogs(whereClause, page, pageSize);
210        boolean hasMorePage = logEntries.size() >= pageSize;
211
212        List<EventDescriptor> events = new ArrayList<EventDescriptor>();
213
214        for (LogEntry logEntry : logEntries) {
215            events.add(new EventDescriptor(logEntry));
216        }
217
218        EventDescriptor[] evts = new EventDescriptor[events.size()];
219        events.toArray(evts);
220
221        return new EventDescriptorPage(evts, page, hasMorePage);
222    }
223
224}