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

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

package com.formkiq.server.dao;

import java.sql.Blob;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashSet;
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.PaginationDTO;
import com.formkiq.server.domain.type.SyncDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.domain.type.SyncType;
import com.formkiq.server.util.Strings;

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

    /** Default MAX Results. */
    protected static final int DEFAULT_MAX_RESULTS = 10;

    /** 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 LinkedHashSet(list.size());

        SyncListDTO dto = new SyncListDTO();

        for (Object[] obj : list) {
            int i = -1;
            Integer logId = (Integer) obj[++i];
            String uuid = (String) obj[++i];
            String hash = (String) obj[++i];
            String synctype = (String) obj[++i];
            SyncType st = SyncType.valueOf(synctype);

            if (!(StringUtils.isEmpty(hash) && SyncType.UPDATE.equals(st))) {
                dtos.add(new SyncDTO(uuid, hash, synctype));
            }

            dto.setNexttoken(
                    Strings.generateOffsetToken(logId.intValue(), maxRecords));
        }

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

        if (StringUtils.isEmpty(dto.getNexttoken()) && lastNextToken > 0) {
            String token = Strings.generateOffsetToken(lastNextToken,
                    maxRecords);
            dto.setNexttoken(token);
        }

        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);
        }
    }

    /**
     * Update Pagination Information and return truncated list objects.
     * @param  T
     * @param dto {@link PaginationDTO}
     * @param offset int
     * @param max int
     * @param list List
     * @return List
     */
    protected  List updatePagination(final PaginationDTO dto,
            final int offset, final int max, final List list) {

        int listcount = list.size();

        if (offset > 0) {
            String prevToken = Strings.generateOffsetToken(offset - max, max);
            dto.setPrevtoken(prevToken);
        }

        if (listcount > max) {
            int offset1 = offset + max;
            String nextToken = Strings.generateOffsetToken(offset1, max);
            dto.setNexttoken(nextToken);
        }

        if (list.size() > max) {
            list.remove(max);
        }

        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy