com.recomdata.grails.rositaui.utils.ValidationErrorCache.java Source code

Java tutorial

Introduction

Here is the source code for com.recomdata.grails.rositaui.utils.ValidationErrorCache.java

Source

/*
*   Copyright 2012-2013 The Regents of the University of Colorado
*
*   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 com.recomdata.grails.rositaui.utils;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.recomdata.grails.rositaui.etl.ValidationError;
import com.recomdata.grails.rositaui.etl.dao.ValidationErrorBatchPreparedStatementSetter;

public class ValidationErrorCache {

    int OBJECT_THRESHOLD = 5000;

    final ValidationErrorBatchPreparedStatementSetter validationErrors;

    SqlTemplates sqlTemplates = new SqlTemplates();

    private DataSource dataSource;
    private JdbcTemplate jdbc;

    public ValidationErrorCache(DataSource ds, Integer threshold) {
        this.dataSource = ds;
        if (dataSource != null) {
            this.jdbc = new JdbcTemplate(dataSource);
        }
        if (threshold != null && threshold > 0) {
            OBJECT_THRESHOLD = threshold;
        }

        this.validationErrors = new ValidationErrorBatchPreparedStatementSetter(threshold);

    }

    public void add(ValidationError o) {
        if (dataSource != null) {
            if (validationErrors.add(o)) {
                jdbc.batchUpdate(sqlTemplates.get("ValidationError"), validationErrors);
                validationErrors.clear();
            }
        }
    }

    public void saveAll() {
        if (dataSource != null) {
            jdbc.batchUpdate(sqlTemplates.get("ValidationError"), validationErrors);
            System.out.println("Saved all remaining validation errors");
        }
    }
}