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

com.formkiq.server.dao.FolderLogDaoImpl Maven / Gradle / Ivy

package com.formkiq.server.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang3.tuple.Pair;
import org.hibernate.Session;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;

import javassist.bytecode.stackmap.TypeData.ClassName;

/**
 * Implementation of FolderLogDao.
 *
 */
@Repository
public class FolderLogDaoImpl extends AbstractDaoImpl implements FolderLogDao {

    /** Logger. */
    private static final Logger LOG = Logger.getLogger(ClassName.class
            .getName());

    @Override
    public int compactLog(final Date date) {

        String sql = "delete from folder_logs where folder_log_id in ( "
                + " select folder_log_id from folder_logs fl "
                + " where fl.inserted_date < :date and "
                + " exists (select fl2.folder_log_id from folder_logs fl2 "
                + "     where fl.folder_id=fl2.folder_id and fl.uuid=fl2.uuid "
                + "     and fl2.folder_log_id > fl.folder_log_id) "
                + " order by inserted_date)";

        Session session = getEntityManager().unwrap(Session.class);
        int result = session.createSQLQuery(sql)
                .setParameter("date", date)
                .executeUpdate();

        LOG.log(Level.INFO,
                "Compacting database - " + result + " records compressed");

        return result;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List> findFormsMissingLogs() {

        String sql = "select fl.folder_id as folderid, "
                + " fl.uuid as uuid "
                + " from folder_forms fl "
                + " where not exists "
                + " (select * from folder_logs fl2 "
                + "     where fl.folder_id=fl2.folder_id and fl.uuid=fl2.uuid)";

        Session session = getEntityManager().unwrap(Session.class);
        List objs = session.createSQLQuery(sql)
                .addScalar("folderid", StringType.INSTANCE)
                .addScalar("uuid", StringType.INSTANCE)
                .list();

        List> list = new ArrayList<>();

        for (Object[] obj : objs) {

            String folderid = obj[0].toString();
            String uuid = obj[1].toString();
            list.add(Pair.of(folderid, uuid));
        }

        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy