001/*
002 * (C) Copyright 2017 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 *     Kevin Leturc
018 */
019package org.nuxeo.mongodb.audit;
020
021import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_CATEGORY;
022import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_COMMENT;
023import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_LIFE_CYCLE;
024import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_PATH;
025import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_TYPE;
026import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_UUID;
027import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_DATE;
028import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_ID;
029import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EXTENDED;
030import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_LOG_DATE;
031import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_PRINCIPAL_NAME;
032import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_REPOSITORY_ID;
033
034import java.util.Arrays;
035import java.util.Map;
036import java.util.Map.Entry;
037
038import org.bson.Document;
039import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
040import org.nuxeo.ecm.platform.audit.api.LogEntry;
041import org.nuxeo.runtime.mongodb.MongoDBSerializationHelper;
042
043/**
044 * Writer for MongoDB Audit.
045 *
046 * @since 9.1
047 */
048public class MongoDBAuditEntryWriter {
049
050    public static Document asDocument(LogEntry logEntry) {
051        Document document = new Document(MongoDBSerializationHelper.MONGODB_ID, Long.valueOf(logEntry.getId()));
052        document.put(LOG_CATEGORY, logEntry.getCategory());
053        document.put(LOG_PRINCIPAL_NAME, logEntry.getPrincipalName());
054        document.put(LOG_COMMENT, logEntry.getComment());
055        document.put(LOG_DOC_LIFE_CYCLE, logEntry.getDocLifeCycle());
056        document.put(LOG_DOC_PATH, logEntry.getDocPath());
057        document.put(LOG_DOC_TYPE, logEntry.getDocType());
058        document.put(LOG_DOC_UUID, logEntry.getDocUUID());
059        document.put(LOG_EVENT_ID, logEntry.getEventId());
060        document.put(LOG_REPOSITORY_ID, logEntry.getRepositoryId());
061        document.put(LOG_EVENT_DATE, logEntry.getEventDate());
062        document.put(LOG_LOG_DATE, logEntry.getLogDate());
063
064        Map<String, ExtendedInfo> extendedInfo = logEntry.getExtendedInfos();
065        Document extended = new Document();
066        for (Entry<String, ExtendedInfo> entry : extendedInfo.entrySet()) {
067            String key = entry.getKey();
068            ExtendedInfo ei = entry.getValue();
069            if (ei != null && ei.getSerializableValue() != null) {
070                Object value = ei.getSerializableValue();
071                if (value instanceof Object[]) {
072                    value = Arrays.asList((Object[]) value);
073                }
074                extended.put(key, value);
075            } else {
076                extended.put(key, null);
077            }
078        }
079        document.put(LOG_EXTENDED, extended);
080        return document;
081    }
082
083}