All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smartparam.manager.audit.jdbc.JdbcEventLogRepository Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Adam Dubiel, Przemek Hertel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.smartparam.manager.audit.jdbc;

import java.util.Arrays;
import java.util.List;
import org.polyjdbc.core.query.QueryRunner;
import org.polyjdbc.core.query.TransactionRunner;
import org.polyjdbc.core.query.TransactionWrapper;
import org.polyjdbc.core.query.VoidTransactionWrapper;
import org.smartparam.engine.config.initialization.InitializableComponent;
import org.smartparam.manager.audit.EventLogFilters;
import org.smartparam.manager.audit.EventLogRepository;
import org.smartparam.manager.audit.EventLogEntry;

/**
 *
 * @author Adam Dubiel
 */
public class JdbcEventLogRepository implements EventLogRepository, InitializableComponent {

    private final TransactionRunner transactionRunner;

    private final JdbcEventLogEntryDAO jdbcEventLogEntryDAO;

    private final SchemaCreator schemaCreator;

    public JdbcEventLogRepository(TransactionRunner transactionRunner,
            JdbcEventLogEntryDAO jdbcEventLogEntryDAO, SchemaCreator schemaCreator) {
        this.transactionRunner = transactionRunner;
        this.jdbcEventLogEntryDAO = jdbcEventLogEntryDAO;
        this.schemaCreator = schemaCreator;
    }

    @Override
    public boolean supports(Class entryClass) {
        return true;
    }

    @Override
    public void initialize() {
        schemaCreator.createSchema();
    }

    @Override
    public List list(final EventLogFilters filters) {
        return transactionRunner.run(new TransactionWrapper>() {
            @Override
            public List perform(QueryRunner queryRunner) {
                return jdbcEventLogEntryDAO.list(queryRunner, filters);
            }
        });
    }

    @Override
    public void save(EventLogEntry eventLogEntry) {
        save(Arrays.asList(eventLogEntry));
    }

    @Override
    public void save(final List eventLogEntries) {
        transactionRunner.run(new VoidTransactionWrapper() {
            @Override
            public void performVoid(QueryRunner queryRunner) {
                for (EventLogEntry entry : eventLogEntries) {
                    jdbcEventLogEntryDAO.save(queryRunner, entry);
                }
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy