sos.ftphistory.db.JadeFilesDBLayer.java Source code

Java tutorial

Introduction

Here is the source code for sos.ftphistory.db.JadeFilesDBLayer.java

Source

/**
 * Copyright (C) 2014 BigLoupe http://bigloupe.github.io/SoS-JobScheduler/
 *
 * 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
 */
/********************************************************* begin of preamble
**
** Copyright (C) 2003-2012 Software- und Organisations-Service GmbH. 
** All rights reserved.
**
** This file may be used under the terms of either the 
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file. 
**
** or the
**  
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package sos.ftphistory.db;

import java.io.File;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;

import sos.ftphistory.JadeFilesFilter;
import sos.util.SOSDate;

import com.sos.hibernate.layer.SOSHibernateIntervalDBLayer;

/**
 * 
 * \class JadeHistoryDBLayer \brief JadeHistoryDBLayer -
 * 
 * \details
 * 
 * \section JadeHistoryDBLayer.java_intro_sec Introduction
 * 
 * \section JadeHistoryDBLayer.java_samples Some Samples
 * 
 * \code .... code goes here ... \endcode
 * 
 * <p style="text-align:center">
 * <br />
 * --------------------------------------------------------------------------- <br />
 * APL/Software GmbH - Berlin <br />
 * ##### generated by ClaviusXPress (http://www.sos-berlin.com) ######### <br />
 * ---------------------------------------------------------------------------
 * </p>
 * \author Uwe Risse \version 27.09.2011 \see reference
 * 
 * Created on 12.12.2011 14:40:18
 */

public class JadeFilesDBLayer extends SOSHibernateIntervalDBLayer {

    @SuppressWarnings("unused")
    private final String conClassName = "JadeFilesDBLayer";
    protected JadeFilesFilter filter = null;

    public JadeFilesDBLayer(File configurationFile_) {
        super();
        this.setConfigurationFile(configurationFile_);
        this.resetFilter();

    }

    public JadeFilesDBItem get(Long id) {
        if (id == null) {
            return null;
        }
        if (session == null) {
            initSession();
        }
        try {
            return (JadeFilesDBItem) this.getSession().get(JadeFilesDBItem.class, id);
        } catch (ObjectNotFoundException e) {
            return null;
        }
    }

    protected String getWhere() {

        String where = "";
        String and = "";

        if (filter.getCreatedFrom() != null) {
            where += and + " created >= :createdFrom";
            and = " and ";
        }

        if (filter.getCreatedTo() != null) {
            where += and + " created <= :createdTo ";
            and = " and ";
        }

        if (filter.getSourceDir() != null && !filter.getSourceDir().equals("")) {
            where += and + " sourceDir=:sourceDir";
            and = " and ";
        }

        if (filter.getSourceFilename() != null && !filter.getSourceFilename().equals("")) {
            where += and + " sourceFilename=:sourceFilename";
            and = " and ";
        }

        if (filter.getSourceHost() != null && !filter.getSourceHost().equals("")) {
            where += and + " sourceHost=:sourceHost";
            and = " and ";
        }

        if (filter.getSourceHostIp() != null && !filter.getSourceHostIp().equals("")) {
            where += and + " sourceHostIp=:sourceHostIp";
            and = " and ";
        }

        if (filter.getSourceUser() != null && !filter.getSourceUser().equals("")) {
            where += and + " sourceUser=:sourceUser";
            and = " and ";
        }

        if (where.trim().equals("")) {

        } else {
            where = "where " + where;
        }
        return where;

    }

    private void setWhere(Query query) {

        if (filter.getCreatedFrom() != null && !filter.getCreatedFrom().equals("")) {
            query.setTimestamp("createdFrom", filter.getCreatedFrom());
        }

        if (filter.getCreatedTo() != null && !filter.getCreatedTo().equals("")) {
            query.setTimestamp("createdTo", filter.getCreatedTo());
        }

        if (filter.getSourceDir() != null && !filter.getSourceDir().equals("")) {
            query.setText("sourceDir", filter.getSourceDir());
        }

        if (filter.getSourceFilename() != null && !filter.getSourceFilename().equals("")) {
            query.setText("sourceFilename", filter.getSourceFilename());
        }

        if (filter.getSourceHost() != null && !filter.getSourceHost().equals("")) {
            query.setText("sourceHost", filter.getSourceHost());
        }

        if (filter.getSourceHostIp() != null && !filter.getSourceHostIp().equals("")) {
            query.setText("sourceHostIp", filter.getSourceHostIp());
        }

        if (filter.getSourceUser() != null && !filter.getSourceUser().equals("")) {
            query.setText("sourceUser", filter.getSourceUser());
        }

    }

    public int delete() {

        if (session == null) {
            beginTransaction();
        }

        String q = "delete from JadeFilesHistoryDBItem e where e.jadeFilesDBItem.id IN (select id from JadeFilesDBItem "
                + getWhere() + ")";
        Query query = session.createQuery(q);
        setWhere(query);

        int row = query.executeUpdate();

        String hql = "delete from JadeFilesDBItem " + getWhere();
        query = session.createQuery(hql);

        setWhere(query);

        row = query.executeUpdate();
        return row;
    }

    private List<JadeFilesDBItem> executeQuery(Query query, int limit) {

        setWhere(query);

        if (limit > 0) {
            query.setMaxResults(limit);
        }

        List<JadeFilesDBItem> jadeFilesList = query.list();
        return jadeFilesList;

    }

    @Override
    public JadeFilesFilter getFilter() {
        return filter;
    }

    public void resetFilter() {
        this.filter = new JadeFilesFilter();
        this.filter.setDateFormat("yyyy-MM-dd HH:mm:ss");
        this.filter.setOrderCriteria("startTime");
        this.filter.setSortMode("desc");
    }

    public void setFilter(JadeFilesFilter filter) {
        this.filter = filter;
    }

    @Override
    public int deleteInterval() {
        return this.delete();
    }

}