Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.formkiq.server.dao.FormDaoImpl Maven / Gradle / Ivy
package com.formkiq.server.dao;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.type.IntegerType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.formkiq.server.config.DateService;
import com.formkiq.server.domain.Asset;
import com.formkiq.server.domain.ClientForm;
import com.formkiq.server.domain.ClientFormLog;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.type.ClientFormType;
import com.formkiq.server.domain.type.FormDTO;
import com.formkiq.server.domain.type.FormListDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.domain.type.SyncType;
import com.formkiq.server.util.Strings;
/**
* FormDao Hibernate Implementation.
*
*/
@Repository
public class FormDaoImpl extends AbstractDaoImpl implements FormDao {
/** Max number of sync records. */
private static final int MAX_SYNC_RECORDS = 10;
/** DateService. */
@Autowired
private DateService dateservice;
@Override
public void deleteForm(final ClientFormType type, final String client,
final String uuid) {
ClientForm form = this.findForm(client, uuid);
if (form != null) {
if (!StringUtils.isEmpty(form.getAssetid())) {
String sql0 = "delete from assets where asset_id=:asset";
getEntityManager().createNativeQuery(sql0)
.setParameter("asset", form.getAssetid())
.executeUpdate();
}
String sql1 = "delete from client_forms "
+ "where client_id=:client and uuid=:uuid and type=:type";
getEntityManager().createNativeQuery(sql1)
.setParameter("client", client)
.setParameter("uuid", uuid)
.setParameter("type", type.name())
.executeUpdate();
Date now = this.dateservice.now();
saveLog(form, SyncType.DELETE, now);
}
}
@Override
public Asset findAsset(final String assetId) {
return getEntityManager().find(Asset.class, assetId);
}
@Override
public byte[] findAssetData(final String assetId) {
byte[] data = null;
Asset asset = getEntityManager().find(Asset.class, assetId);
if (asset != null) {
data = this.toBytesArray(asset.getData());
}
return data;
}
@Override
public ClientForm findForm(final String clientId, final String formId) {
String jql = "select c from ClientForm c where c.clientid=:client"
+ " and c.uuid=:form";
Query query = getEntityManager().createQuery(jql)
.setParameter("client", clientId)
.setParameter("form", formId);
return (ClientForm) getSingleResult(query);
}
@Override
public ClientForm findForm(final User user, final String client,
final String form) {
String sql = "select * "
+ " from client_forms fl "
+ " where fl.uuid=:form and fl.client_id=:client "
+ " and exists ("
+ " select * from user_clients uc "
+ " where uc.user_id=:user and uc.client_id=:client "
+ ")";
Session session = getEntityManager().unwrap(Session.class);
SQLQuery query = session.createSQLQuery(sql)
.addEntity(ClientForm.class);
query.setParameter("user", user.getUserid());
query.setParameter("form", form);
query.setParameter("client", client);
ClientForm log = (ClientForm) query.uniqueResult();
return log;
}
@SuppressWarnings("unchecked")
@Override
public FormListDTO findForms(final ClientFormType type, final String client,
final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);
StringBuilder sql = new StringBuilder(
"select data #>> '{name}' as name, "
+ "data #>> '{uuid}' as uuid, "
+ "data #>> '{updated_date}' as updatedDate, "
+ "sha1_hash as sha1hash "
+ "from client_forms where client_id=:client "
+ "and parent_uuid is null "
+ "and type=:type "
+ "order by name");
sql.append(" OFFSET " + offset + " FETCH FIRST " + (max + 1)
+ " ROWS ONLY");
Session session = getEntityManager().unwrap(Session.class);
List list = session.createSQLQuery(sql.toString())
.setParameter("client", client)
.setParameter("type", type.name())
.setResultTransformer(
new AliasToBeanResultTransformer(FormDTO.class))
.list();
FormListDTO dto = new FormListDTO();
List truncated = updatePagination(dto, offset, max, list);
dto.setForms(truncated);
return dto;
}
@SuppressWarnings("unchecked")
@Override
public FormListDTO findForms(final ClientFormType type, final String client,
final String form, final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);
StringBuilder sql = new StringBuilder(
"select data #>> '{name}' as name, "
+ "data #>> '{uuid}' as uuid, "
+ "data #>> '{label1}' as label1, "
+ "data #>> '{label2}' as label2, "
+ "data #>> '{label3}' as label3, "
+ "data #>> '{updated_date}' as updatedDate, "
+ "sha1_hash as sha1hash "
+ "from client_forms where client_id=:client "
+ "and parent_uuid=:parent "
+ "and type=:type "
+ "order by name");
sql.append(" OFFSET " + offset + " FETCH FIRST " + (max + 1)
+ " ROWS ONLY");
Session session = getEntityManager().unwrap(Session.class);
List list = session.createSQLQuery(sql.toString())
.setParameter("client", client)
.setParameter("parent", form)
.setParameter("type", type.name())
.setResultTransformer(
new AliasToBeanResultTransformer(FormDTO.class))
.list();
for (FormDTO dto : list) {
dto.setLabel1(Strings.extractLabelAndValue(dto.getLabel1())[0]);
dto.setLabel2(Strings.extractLabelAndValue(dto.getLabel2())[0]);
dto.setLabel3(Strings.extractLabelAndValue(dto.getLabel3())[0]);
}
FormListDTO dto = new FormListDTO();
List truncated = updatePagination(dto, offset, max, list);
dto.setForms(truncated);
return dto;
}
@Override
public SyncListDTO findFormSyncList(final ClientFormType type,
final String client, final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, MAX_SYNC_RECORDS);
String sql = "select fl.log_id as form_log, "
+ " fl.uuid as id, cf.sha1_hash as sha1hash, "
+ " fl.synctype as synctype "
+ " from client_form_logs fl left join client_forms cf "
+ " on fl.client_id=cf.client_id and fl.uuid=cf.uuid "
+ " where fl.client_id=:client and fl.log_id > :token "
+ " and fl.type=:type "
+ " order by fl.log_id asc";
Session session = getEntityManager().unwrap(Session.class);
org.hibernate.Query query = session
.createSQLQuery(sql)
.setParameter("client", client)
.setParameter("token", Integer.valueOf(offset))
.setParameter("type", type.name())
.setMaxResults(MAX_SYNC_RECORDS);
SyncListDTO dto = convertToSyncList(query, offset, max);
if (!StringUtils.isEmpty(token)) {
dto.setPrevtoken(token);
}
return dto;
}
@Override
public Asset saveAsset(final Asset asset) {
if (StringUtils.isEmpty(asset.getAssetId())) {
getEntityManager().persist(asset);
} else {
getEntityManager().merge(asset);
}
return asset;
}
@Override
public ClientForm saveForm(final ClientForm form) {
Date now = this.dateservice.now();
if (form.getUpdatedDate() == null) {
form.setUpdatedDate(now);
}
if (form.getInsertedDate() == null) {
form.setInsertedDate(now);
}
if (StringUtils.isEmpty(form.getClientFormId())) {
getEntityManager().persist(form);
} else {
getEntityManager().merge(form);
}
saveLog(form, SyncType.UPDATE, now);
return form;
}
/**
* Saves Log.
* @param form {@link ClientForm}
* @param sync {@link SyncType}
* @param now {@link Date}
* @return {@link ClientFormLog}
*/
private ClientFormLog saveLog(final ClientForm form, final SyncType sync,
final Date now) {
ClientFormLog log = new ClientFormLog();
log.setClientid(form.getClientid());
log.setSynctype(sync);
log.setType(form.getType());
log.setUUID(form.getUUID());
log.setInsertedDate(now);
getEntityManager().persist(log);
return log;
}
@Override
public boolean hasFormChildren(final String client, final String uuid) {
String sql = "select count(*) as count from client_forms "
+ " where client_id=:client and parent_uuid=:uuid ";
Session session = getEntityManager().unwrap(Session.class);
Integer count = (Integer) session.createSQLQuery(sql)
.addScalar("count", IntegerType.INSTANCE)
.setParameter("client", client)
.setParameter("uuid", uuid)
.setMaxResults(1)
.uniqueResult();
return count.intValue() > 0;
}
}