
com.formkiq.server.dao.AbstractDaoImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
/*
* Copyright (C) 2016 FormKiQ Inc.
*
* 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.formkiq.server.dao;
import java.sql.Blob;
import java.sql.SQLException;
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 com.formkiq.server.domain.type.PaginationDTO;
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;
}
/**
* @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 - 2025 Weber Informatics LLC | Privacy Policy