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

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

There is a newer version: 0.6.1
Show newest version
package com.formkiq.server.dao;

import java.sql.Blob;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.springframework.util.StringUtils;

import com.formkiq.server.domain.type.SyncDTO;
import com.formkiq.server.domain.type.SyncListDTO;

/**
 * AbstractDao.
 *
 */
public abstract class AbstractDaoImpl implements AbstractDao {

    /** EntityManager. */
	@PersistenceContext
	private EntityManager entityManager;

    @Override
    public Blob convertToBlob(final byte[] data) {
        Session session = getEntityManager().unwrap(Session.class);
        Blob blob = Hibernate.getLobCreator(session).createBlob(data);
        return blob;
    }

	/**
     * Converts Query to Sync List DTO.
     * @param query {@link org.hibernate.Query}
     * @param lastNextToken {@link String}
     * @param maxRecords int
     * @return {@link SyncListDTO}
     */
    @SuppressWarnings("unchecked")
    protected SyncListDTO convertToSyncList(final org.hibernate.Query query,
            final int lastNextToken,
            final int maxRecords) {

        List list = query.list();

        Collection dtos = new HashSet(list.size());

        SyncListDTO dto = new SyncListDTO();

        for (Object[] obj : list) {
            Integer logId = (Integer) obj[0];
            String uuid = (String) obj[1];
            String hash = (String) obj[2];

            dtos.add(new SyncDTO(uuid, hash));
            dto.setNexttoken("" + logId);
        }

        dto.setData(dtos);
        dto.setTruncated(dtos.size() == maxRecords);

        if (StringUtils.isEmpty(dto.getNexttoken()) && lastNextToken > 0) {
            dto.setNexttoken("" + lastNextToken);
        }

        return dto;
    }

    /**
	 * @return EntityManager
	 */
    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    /**
	 * Return a single result, catch NoResultException and return NULL.
	 * @param query Query
	 * @return Object
	 */
	protected Object getSingleResult(final Query query) {

	    Object t;

		try {

			t = query.getSingleResult();

		} catch (NoResultException e) {
			t = null;
		}

		return t;
	}

    @Override
    public byte[] toBytesArray(final Blob blob) {

        try {
            int blobLength = (int) blob.length();
            byte[] blobAsBytes = blob.getBytes(1, blobLength);
            return blobAsBytes;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy