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