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.FolderDaoImpl Maven / Gradle / Ivy
package com.formkiq.server.dao;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.persistence.Query;
import org.apache.commons.lang3.tuple.Pair;
import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.type.IntegerType;
import org.hibernate.type.PostgresUUIDType;
import org.hibernate.type.StringType;
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.Folder;
import com.formkiq.server.domain.FolderAccess;
import com.formkiq.server.domain.FolderForm;
import com.formkiq.server.domain.FolderLog;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.type.ClientFormType;
import com.formkiq.server.domain.type.FolderDTO;
import com.formkiq.server.domain.type.FolderFormsListDTO;
import com.formkiq.server.domain.type.FolderListDTO;
import com.formkiq.server.domain.type.FolderPermission;
import com.formkiq.server.domain.type.FormDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.domain.type.SyncType;
import com.formkiq.server.domain.type.UserRole;
import com.formkiq.server.util.Strings;
/**
* Implementation of Folder Dao.
*
*/
@Repository
public class FolderDaoImpl extends AbstractDaoImpl implements FolderDao {
/** Max number of sync records. */
private static final int MAX_SYNC_RECORDS = 10;
/** AssetDao. */
@Autowired
private AssetDao assetDao;
/** DateService. */
@Autowired
private DateService dateservice;
@Override
public void deleteFolder(final String folder) {
UUID folderId = UUID.fromString(folder);
String sql0 = "delete from folders_access where folder_id=:folder";
String sql1 = "delete from folders where folder_id=:folder";
Session session = getEntityManager().unwrap(Session.class);
session.createSQLQuery(sql0)
.setParameter("folder", folderId, PostgresUUIDType.INSTANCE)
.executeUpdate();
session.createSQLQuery(sql1)
.setParameter("folder", folderId, PostgresUUIDType.INSTANCE)
.executeUpdate();
}
@Override
public void deleteForm(final String folder, final String uuid) {
FolderForm form = this.findForm(folder, uuid);
if (form != null) {
Session session = getEntityManager().unwrap(Session.class);
if (!StringUtils.isEmpty(form.getAssetid())) {
String sql0 = "delete from assets where asset_id=:asset";
session.createSQLQuery(sql0)
.setParameter("asset", form.getAssetid(),
PostgresUUIDType.INSTANCE)
.executeUpdate();
}
String sql1 = "delete from folder_forms "
+ "where folder_id=:folder and uuid=:uuid";
session.createSQLQuery(sql1)
.setParameter("folder", UUID.fromString(folder),
PostgresUUIDType.INSTANCE)
.setParameter("uuid", UUID.fromString(uuid),
PostgresUUIDType.INSTANCE)
.executeUpdate();
Date now = this.dateservice.now();
saveLog(form, SyncType.DELETE, now);
}
}
@Override
public Folder findFolder(final String uuid) {
return getEntityManager().find(Folder.class, UUID.fromString(uuid));
}
@Override
public FolderAccess findFolderAccess(final User user, final String folder) {
UUID folderuuid = UUID.fromString(folder);
String jql = "select fa from FolderAccess fa "
+ " where fa.userid=:user and fa.folderid=:folder";
Query query = getEntityManager().createQuery(jql)
.setParameter("user", user.getUserid())
.setParameter("folder", folderuuid);
FolderAccess fa = (FolderAccess) getSingleResult(query);
if (fa == null && UserRole.ROLE_ADMIN.equals(user.getRole())) {
fa = new FolderAccess();
fa.setPermissions(Arrays.asList(FolderPermission.PERM_FORM_ADMIN));
}
return fa;
}
@SuppressWarnings("unchecked")
@Override
public List findFoldersDTO(final String email) {
String sql = "select f.folder_id as folder, f.name as foldername, "
+ " fa.permissions as permissionsasstring"
+ " from users u "
+ " join folders_access fa on u.user_id=fa.user_id "
+ " join folders f on f.folder_id=fa.folder_id "
+ " where u.email=:email"
+ " order by f.name ";
Session session = getEntityManager().unwrap(Session.class);
List folders = session.createSQLQuery(sql)
.addScalar("folder", StringType.INSTANCE)
.addScalar("foldername", StringType.INSTANCE)
.addScalar("permissionsasstring", StringType.INSTANCE)
.setParameter("email", email)
.setResultTransformer(
new AliasToBeanResultTransformer(FolderDTO.class))
.list();
return folders;
}
@Override
public FolderDTO findFolderDTO(final User user, final String folderId) {
String sql = "select f.folder_id as folder, f.name as foldername, "
+ " fa.permissions as permissionsasstring"
+ " from users u "
+ " join folders_access fa on u.user_id=fa.user_id "
+ " join folders f on f.folder_id=fa.folder_id "
+ " and f.folder_id=:folder"
+ " where u.user_id=:user"
+ " order by f.name ";
Session session = getEntityManager().unwrap(Session.class);
FolderDTO folder = (FolderDTO) session.createSQLQuery(sql)
.addScalar("folder", StringType.INSTANCE)
.addScalar("foldername", StringType.INSTANCE)
.addScalar("permissionsasstring", StringType.INSTANCE)
.setParameter("user", user.getUserid(),
PostgresUUIDType.INSTANCE)
.setParameter("folder", UUID.fromString(folderId),
PostgresUUIDType.INSTANCE)
.setResultTransformer(
new AliasToBeanResultTransformer(FolderDTO.class))
.uniqueResult();
return folder;
}
@SuppressWarnings("unchecked")
@Override
public FolderListDTO findFolderList(final User user, final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);
StringBuilder sql = new StringBuilder(
"select distinct f.folder_id as folder, f.name as foldername, "
+ " fa.permissions as permissionsasstring "
+ " from folders_access fa "
+ " join folders f on f.folder_id=fa.folder_id "
+ " where fa.user_id=:user "
+ " order by foldername");
sql.append(" OFFSET " + offset + " FETCH FIRST " + (max + 1)
+ " ROWS ONLY");
Session session = getEntityManager().unwrap(Session.class);
List list = session.createSQLQuery(sql.toString())
.addScalar("folder", StringType.INSTANCE)
.addScalar("foldername", StringType.INSTANCE)
.addScalar("permissionsasstring", StringType.INSTANCE)
.setParameter("user", user.getUserid(),
PostgresUUIDType.INSTANCE)
.setResultTransformer(
new AliasToBeanResultTransformer(FolderDTO.class))
.list();
FolderListDTO dto = new FolderListDTO();
List truncated = updatePagination(dto, offset, max, list);
dto.setFolders(truncated);
return dto;
}
@Override
public FolderForm findForm(final String folder, final String form) {
String jql = "select c from FolderForm c where c.folderid=:folder"
+ " and c.uuid=:form";
Query query = getEntityManager().createQuery(jql)
.setParameter("folder", UUID.fromString(folder))
.setParameter("form", UUID.fromString(form));
return (FolderForm) getSingleResult(query);
}
@Override
public Pair findForm(final User user,
final String folder, final String form) {
FolderForm folderForm = null;
FolderAccess access = findFolderAccess(user, folder);
if (access != null) {
folderForm = findForm(folder, form);
}
return Pair.of(folderForm, access);
}
@Override
public FormDTO findFormDTO(final String folder, final String uuid) {
FormDTO dto = null;
String sql =
"select asset_id as assetid, "
+ "type, "
+ "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 folder_forms where folder_id=:folder and uuid=:uuid";
Session session = getEntityManager().unwrap(Session.class);
Object[] objs = (Object[]) session.createSQLQuery(sql.toString())
.addScalar("assetid", StringType.INSTANCE)
.addScalar("type", StringType.INSTANCE)
.addScalar("name", StringType.INSTANCE)
.addScalar("uuid", StringType.INSTANCE)
.addScalar("label1", StringType.INSTANCE)
.addScalar("label2", StringType.INSTANCE)
.addScalar("label3", StringType.INSTANCE)
.addScalar("updatedDate", StringType.INSTANCE)
.addScalar("sha1hash", StringType.INSTANCE)
.setParameter("folder", UUID.fromString(folder),
PostgresUUIDType.INSTANCE)
.setParameter("uuid", UUID.fromString(uuid),
PostgresUUIDType.INSTANCE)
.uniqueResult();
int i = 0;
if (objs != null && objs.length > 0) {
String assetId = objs[i++].toString();
dto = new FormDTO();
dto.setType((String) objs[i++]);
dto.setName((String) objs[i++]);
dto.setUUID((String) objs[i++]);
dto.setLabel1(Strings.extractLabelAndValue((String) objs[i++])[0]);
dto.setLabel2(Strings.extractLabelAndValue((String) objs[i++])[0]);
dto.setLabel3(Strings.extractLabelAndValue((String) objs[i++])[0]);
dto.setUpdateddate((String) objs[i++]);
dto.setSha1hash((String) objs[i++]);
byte[] data = this.assetDao.findAssetData(UUID.fromString(assetId));
dto.setData(data);
}
return dto;
}
@SuppressWarnings("unchecked")
@Override
public FolderFormsListDTO findForms(final String folder,
final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);
StringBuilder sql = new StringBuilder(
"select type, data #>> '{name}' as name, "
+ "data #>> '{uuid}' as uuid, "
+ "data #>> '{updated_date}' as updatedDate, "
+ "sha1_hash as sha1hash "
+ "from folder_forms where folder_id=:folder "
+ "and parent_uuid is null "
+ "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("folder", UUID.fromString(folder),
PostgresUUIDType.INSTANCE)
.setResultTransformer(
new AliasToBeanResultTransformer(FormDTO.class))
.list();
FolderFormsListDTO dto = new FolderFormsListDTO();
List truncated = updatePagination(dto, offset, max, list);
dto.setForms(truncated);
return dto;
}
@SuppressWarnings("unchecked")
@Override
public FolderFormsListDTO findForms(final String folder,
final String form, final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);
StringBuilder sql = new StringBuilder(
"select type, 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 folder_forms where folder_id=:folder "
+ "and parent_uuid=:parent "
+ "order by updatedDate desc");
sql.append(" OFFSET " + offset + " FETCH FIRST " + (max + 1)
+ " ROWS ONLY");
Session session = getEntityManager().unwrap(Session.class);
List list = session.createSQLQuery(sql.toString())
.setParameter("folder", UUID.fromString(folder),
PostgresUUIDType.INSTANCE)
.setParameter("parent", UUID.fromString(form),
PostgresUUIDType.INSTANCE)
.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]);
}
FolderFormsListDTO dto = new FolderFormsListDTO();
List truncated = updatePagination(dto, offset, max, list);
dto.setForms(truncated);
return dto;
}
@Override
public SyncListDTO findFormSyncList(final ClientFormType type,
final String folder, final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, MAX_SYNC_RECORDS);
String sql = "select fl.folder_log_id as form_log, "
+ " fl.uuid as id, cf.sha1_hash as sha1hash, "
+ " fl.synctype as synctype "
+ " from folder_logs fl "
+ " left join folder_forms cf "
+ " on fl.folder_id=cf.folder_id and fl.uuid=cf.uuid "
+ " where fl.folder_id=:folder and fl.folder_log_id > :token "
+ " and fl.type=:type "
+ " order by fl.folder_log_id asc";
Session session = getEntityManager().unwrap(Session.class);
org.hibernate.Query query = session
.createSQLQuery(sql)
.addScalar("form_log", IntegerType.INSTANCE)
.addScalar("id", StringType.INSTANCE)
.addScalar("sha1hash", StringType.INSTANCE)
.addScalar("synctype", StringType.INSTANCE)
.setParameter("folder", UUID.fromString(folder),
PostgresUUIDType.INSTANCE)
.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 boolean hasFiles(final String folder) {
String sql = "select count(*) as count from folder_forms "
+ " where folder_id=:folder";
Session session = getEntityManager().unwrap(Session.class);
Integer count = (Integer) session.createSQLQuery(sql)
.addScalar("count", IntegerType.INSTANCE)
.setParameter("folder", UUID.fromString(folder),
PostgresUUIDType.INSTANCE)
.setMaxResults(1)
.uniqueResult();
return count.intValue() > 0;
}
@Override
public boolean hasFormChildren(final String folder, final String uuid) {
String sql = "select count(*) as count from folder_forms "
+ " where folder_id=:folder and parent_uuid=:uuid ";
Session session = getEntityManager().unwrap(Session.class);
Integer count = (Integer) session.createSQLQuery(sql)
.addScalar("count", IntegerType.INSTANCE)
.setParameter("folder", UUID.fromString(folder),
PostgresUUIDType.INSTANCE)
.setParameter("uuid", UUID.fromString(uuid),
PostgresUUIDType.INSTANCE)
.setMaxResults(1)
.uniqueResult();
return count.intValue() > 0;
}
@Override
public Folder saveFolder(final Folder folder) {
Date now = this.dateservice.now();
if (folder.getFolderid() == null) {
folder.setFolderid(UUID.randomUUID());
folder.setInsertedDate(now);
getEntityManager().persist(folder);
} else {
getEntityManager().merge(folder);
}
return folder;
}
@Override
public FolderAccess saveFolderAccess(final FolderAccess access) {
Date now = this.dateservice.now();
if (access.getFolderaccessid() == null) {
access.setFolderaccessid(UUID.randomUUID());
access.setInsertedDate(now);
getEntityManager().persist(access);
} else {
getEntityManager().merge(access);
}
return access;
}
@Override
public FolderForm saveForm(final FolderForm form) {
Date now = this.dateservice.now();
if (form.getUpdatedDate() == null) {
form.setUpdatedDate(now);
}
if (form.getInsertedDate() == null) {
form.setInsertedDate(now);
}
if (StringUtils.isEmpty(form.getFolderformid())) {
form.setFolderformid(UUID.randomUUID());
getEntityManager().persist(form);
} else {
getEntityManager().merge(form);
}
saveLog(form, SyncType.UPDATE, now);
return form;
}
/**
* Saves Folder Log.
* @param form {@link FolderForm}
* @param sync {@link SyncType}
* @param now {@link Date}
* @return {@link FolderLog}
*/
private FolderLog saveLog(final FolderForm form,
final SyncType sync,
final Date now) {
FolderLog log = new FolderLog();
log.setFolderid(form.getFolderid());
log.setSynctype(sync);
log.setType(form.getType());
log.setUUID(form.getUUID());
log.setInsertedDate(now);
getEntityManager().persist(log);
return log;
}
}