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 *
018 * $Id$
019 */
020
021package org.nuxeo.ecm.platform.audit.io;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.io.Serializable;
027import java.util.ArrayList;
028import java.util.Collection;
029import java.util.Collections;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.nuxeo.ecm.core.api.CoreInstance;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.core.api.DocumentNotFoundException;
040import org.nuxeo.ecm.core.api.DocumentRef;
041import org.nuxeo.ecm.core.api.IdRef;
042import org.nuxeo.ecm.core.api.NuxeoException;
043import org.nuxeo.ecm.core.io.DocumentTranslationMap;
044import org.nuxeo.ecm.platform.audit.api.LogEntry;
045import org.nuxeo.ecm.platform.audit.api.Logs;
046import org.nuxeo.ecm.platform.io.api.AbstractIOResourceAdapter;
047import org.nuxeo.ecm.platform.io.api.IOResources;
048import org.nuxeo.runtime.api.Framework;
049
050/**
051 * Adapter for import/export of audit logs.
052 *
053 * @author <a href="mailto:dm@nuxeo.com">Dragos Mihalache</a>
054 */
055public class IOAuditAdapter extends AbstractIOResourceAdapter {
056
057    private static final Log log = LogFactory.getLog(IOAuditAdapter.class);
058
059    private static final long serialVersionUID = -3661302796286246086L;
060
061    /**
062     * Should be overridden if IOLogEntryBase is subclassed.
063     *
064     * @return IOLogEntryBase instance that will know how to write and read log entries
065     */
066    protected IOLogEntryBase getLogEntryHelper() {
067        return new IOLogEntryBase();
068    }
069
070    @Override
071    public void setProperties(Map<String, Serializable> properties) {
072    }
073
074    /**
075     * Extract logs involving given documents.
076     * <p>
077     * The adapter properties will filter which logs must be taken into account.
078     */
079    @Override
080    public IOResources extractResources(String repo, Collection<DocumentRef> sources) {
081        if (sources == null || sources.isEmpty()) {
082            return null;
083        }
084        try (CoreSession session = CoreInstance.openCoreSessionSystem(repo)) {
085            Map<DocumentRef, List<LogEntry>> docLogs = new HashMap<DocumentRef, List<LogEntry>>();
086
087            Logs logService = Framework.getService(Logs.class);
088
089            for (DocumentRef docRef : sources) {
090                try {
091                    final String uuid;
092                    if (docRef.type() == DocumentRef.ID) {
093                        uuid = docRef.toString();
094                    } else {
095                        DocumentModel doc = session.getDocument(docRef);
096                        uuid = doc.getId();
097                    }
098
099                    List<LogEntry> logEntries = logService.getLogEntriesFor(uuid);
100
101                    docLogs.put(docRef, logEntries);
102                } catch (DocumentNotFoundException e) {
103                    List<LogEntry> emptyList = Collections.emptyList();
104                    docLogs.put(docRef, emptyList);
105                    continue;
106                }
107            }
108            return new IOAuditResources(docLogs);
109        }
110    }
111
112    @Override
113    public void getResourcesAsXML(OutputStream out, IOResources resources) {
114        if (!(resources instanceof IOAuditResources)) {
115            return;
116        }
117        IOAuditResources auditResources = (IOAuditResources) resources;
118
119        List<LogEntry> logEntries = new ArrayList<LogEntry>();
120
121        Map<DocumentRef, List<LogEntry>> docLogs = auditResources.getLogsMap();
122
123        Collection<List<LogEntry>> all = docLogs.values();
124        for (List<LogEntry> list : all) {
125            logEntries.addAll(list);
126        }
127
128        try {
129            IOLogEntryBase.write(logEntries, out);
130        } catch (IOException e) {
131            throw new NuxeoException("Cannot write logs", e);
132        }
133    }
134
135    @Override
136    public IOResources loadResourcesFromXML(InputStream stream) {
137        List<LogEntry> allEntries;
138        try {
139            allEntries = IOLogEntryBase.read(stream);
140        } catch (IOException e) {
141            throw new NuxeoException("Cannot read entries from " + stream);
142        }
143
144        // will put each log entry to its correspondent document ref
145        Map<DocumentRef, List<LogEntry>> docLogs = new HashMap<DocumentRef, List<LogEntry>>();
146        for (LogEntry logEntry : allEntries) {
147            DocumentRef docRef = new IdRef(logEntry.getDocUUID());
148
149            List<LogEntry> logEntries = docLogs.get(docRef);
150            if (logEntries == null) {
151                logEntries = new ArrayList<LogEntry>();
152                docLogs.put(docRef, logEntries);
153            }
154            logEntries.add(logEntry);
155        }
156
157        return new IOAuditResources(docLogs);
158    }
159
160    @Override
161    public void storeResources(IOResources newResources) {
162        if (!(newResources instanceof IOAuditResources)) {
163            return;
164        }
165        Logs logService = Framework.getService(Logs.class);
166        IOAuditResources auditResources = (IOAuditResources) newResources;
167        Map<DocumentRef, List<LogEntry>> docLogs = auditResources.getLogsMap();
168        for (Map.Entry<DocumentRef, List<LogEntry>> mapEntry : docLogs.entrySet()) {
169            DocumentRef docRef = mapEntry.getKey();
170            List<LogEntry> logs = mapEntry.getValue();
171            // need to set the given docRef - so transfer with the help of
172            // IOLogEntryBase (subclass eventually)
173            List<LogEntry> newLogs = IOLogEntryBase.translate(logs, docRef);
174            logService.addLogEntries(newLogs);
175        }
176    }
177
178    @Override
179    public IOResources translateResources(String repo, IOResources resources, DocumentTranslationMap map) {
180        if (map == null) {
181            return null;
182        }
183        if (!(resources instanceof IOAuditResources)) {
184            return resources;
185        }
186
187        IOAuditResources auditResources = (IOAuditResources) resources;
188        Map<DocumentRef, List<LogEntry>> newResourcesMap = new HashMap<DocumentRef, List<LogEntry>>();
189
190        for (Map.Entry<DocumentRef, List<LogEntry>> entry : auditResources.getLogsMap().entrySet()) {
191            DocumentRef oldRef = entry.getKey();
192            DocumentRef newRef = map.getDocRefMap().get(oldRef);
193            if (newRef == null) {
194                if (log.isErrorEnabled()) {
195                    log.error("newRef does not exist in translation map for " + oldRef);
196                }
197                continue;
198            }
199            List<LogEntry> docLogs = auditResources.getDocumentLogs(oldRef);
200
201            // need to set the given docRef - so transfer with the help of
202            // IOLogEntryBase (subclass eventually)
203            List<LogEntry> newLogs = IOLogEntryBase.translate(docLogs, newRef);
204            newResourcesMap.put(newRef, newLogs);
205        }
206
207        return new IOAuditResources(newResourcesMap);
208    }
209
210}