/*
 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     anguenot
 *
 * $Id$
 */

package org.nuxeo.ecm.platform.audit.search.resources.indexing;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.security.ACP;
import org.nuxeo.ecm.core.search.api.client.IndexingException;
import org.nuxeo.ecm.core.search.api.client.indexing.resources.AbstractIndexableResource;
import org.nuxeo.ecm.core.search.api.client.indexing.security.IndexingSecurityConstants;
import org.nuxeo.ecm.platform.audit.api.AuditException;
import org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData;
import org.nuxeo.ecm.platform.audit.api.LogEntry;
import org.nuxeo.ecm.platform.audit.api.Logs;
import org.nuxeo.ecm.platform.audit.api.delegate.AuditLogsServiceDelegate;
import org.nuxeo.ecm.platform.audit.search.resources.indexing.api.AuditIndexableResource;

/**
 * Audit indexable resource implementation.
 *
 * <p>
 * Takes care of fetching log entries, generating log entry data maps and
 * returns log entry data given a key.
 * </p>
 *
 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
 *
 */
public class AuditIndexableResourceImpl extends AbstractIndexableResource
        implements AuditIndexableResource {

    private static final long serialVersionUID = 1L;

    @SuppressWarnings("unused")
    private static final Log log = LogFactory.getLog(AuditIndexableResourceImpl.class);

    protected long logUUID;

    protected Logs auditLogsService;

    public AuditIndexableResourceImpl() {
    }

    public AuditIndexableResourceImpl(long logUUID) {
        this.logUUID = logUUID;
    }

    public Serializable getValueFor(String indexableDataName)
            throws IndexingException {
        login();
        Serializable value = getLogEntryDataMapFor(logUUID).get(
                indexableDataName);
        logout();
        return value;
    }

    public Map<String, Serializable> getLogEntryDataMapFor(long id)
            throws IndexingException {

        Logs logsRemote = getAuditLogsService();

        if (logsRemote != null) {
            throw new IndexingException("LogsBean remote stub is null...");
        }

        LogEntry entry;
        try {
            entry = logsRemote.getLogEntryByID(logUUID);
        } catch (AuditException e) {
            throw new IndexingException(e.getMessage());
        }

        if (entry == null) {
            throw new IndexingException(
                    "Impossible to get the corresponding log entry...");
        }
        return getLogEntryDataMap(entry);
    }

    protected Logs getAuditLogsService() {
        if (auditLogsService == null) {
            auditLogsService = AuditLogsServiceDelegate.getRemoteAuditLogsService();
        }
        return auditLogsService;
    }

    protected static Map<String, Serializable> getLogEntryDataMap(LogEntry entry) {
        Map<String, Serializable> dataMap = new HashMap<String, Serializable>();
        dataMap.put(BuiltinLogEntryData.LOG_ID, entry.getId());
        dataMap.put(BuiltinLogEntryData.LOG_EVENT_ID, entry.getEventId());
        dataMap.put(BuiltinLogEntryData.LOG_EVENT_DATE, entry.getEventDate());
        dataMap.put(BuiltinLogEntryData.LOG_DOC_UUID, entry.getDocUUID());
        dataMap.put(BuiltinLogEntryData.LOG_DOC_PATH, entry.getDocPath());
        dataMap.put(BuiltinLogEntryData.LOG_DOC_TYPE, entry.getDocType());
        dataMap.put(BuiltinLogEntryData.LOG_PRINCIPAL_NAME,
                entry.getPrincipalName());
        dataMap.put(BuiltinLogEntryData.LOG_COMMENT, entry.getComment());
        dataMap.put(BuiltinLogEntryData.LOG_CATEGORY, entry.getCategory());
        dataMap.put(BuiltinLogEntryData.LOG_DOC_LIFE_CYCLE,
                entry.getDocLifeCycle());
        return dataMap;
    }

    public String computeId() {
        return ((Long) logUUID).toString();
    }

    @Override
    // TODO for JA: validate this (no specific security filtering
    // on audit index)
    public ACP computeAcp() {
        return IndexingSecurityConstants.getOpenAcp();
    }

}

@@ -795,93 +795,8 @@
 p;%0A%0A
-import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 impo
@@ -835,16 +835,16 @@
 ty.ACP;%0A
+
 import o
@@ -1504,19 +1504,16 @@
 tation.%0A
- *%0A
  * %3Cp%3E%0A 
@@ -1628,16 +1628,8 @@
 ey.%0A
- * %3C/p%3E%0A
  *%0A 
@@ -1688,19 +1688,16 @@
 not%3C/a%3E%0A
- *%0A
  */%0Apubl
@@ -1869,129 +1869,8 @@
 L;%0A%0A
-    @SuppressWarnings(%22unused%22)%0A    private static final Log log = LogFactory.getLog(AuditIndexableResourceImpl.class);%0A%0A
     
@@ -2525,9 +2525,9 @@
 ote 
-!
+=
 = nu

Fix seemingly serious ("==" replaced by "!=") bug, strangely undetected so far.