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

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

There is a newer version: 0.6.1
Show newest version
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.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.FormLog;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.type.FormDTO;
import com.formkiq.server.domain.type.FormListDTO;
import com.formkiq.server.domain.type.SyncListDTO;

/**
 * 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 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.formid=: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.form_id=: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 String client) {

        String sql = "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 "
                + "order by name";

        Session session = getEntityManager().unwrap(Session.class);
        List list = session.createSQLQuery(sql)
                .setParameter("client", client)
                .setResultTransformer(
                        new AliasToBeanResultTransformer(FormDTO.class))
                .list();

        FormListDTO dto = new FormListDTO();
        dto.setForms(list);
        return dto;
    }

    @SuppressWarnings("unchecked")
    @Override
    public FormListDTO findForms(final String client, final String form) {

        String sql = "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=:parent "
                + "order by name";

        Session session = getEntityManager().unwrap(Session.class);
        List list = session.createSQLQuery(sql)
                .setParameter("client", client)
                .setParameter("parent", form)
                .setResultTransformer(
                        new AliasToBeanResultTransformer(FormDTO.class))
                .list();

        FormListDTO dto = new FormListDTO();
        dto.setForms(list);
        return dto;
    }

    @Override
    public SyncListDTO findFormSyncList(final String client,
            final int nextToken) {

        String sql = "select fl.form_log_id as form_log, "
                + " cf.form_id as id, cf.sha1_hash as sha1hash "
                + " from form_logs fl join client_forms cf "
                + " on fl.client_id=cf.client_id and fl.form_id=cf.form_id "
                + " where fl.client_id=:client and fl.form_log_id > :token"
                + " order by fl.form_log_id asc";

        Session session = getEntityManager().unwrap(Session.class);
        org.hibernate.Query query = session
                .createSQLQuery(sql)
                .setParameter("client", client)
                .setParameter("token", Integer.valueOf(nextToken))
                .setMaxResults(MAX_SYNC_RECORDS);

        SyncListDTO dto = convertToSyncList(query, nextToken, MAX_SYNC_RECORDS);
        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);
        }

        return form;
    }

    @Override
    public FormLog saveFormLog(final FormLog form) {

        Date now = this.dateservice.now();
        form.setInsertedDate(now);
        getEntityManager().persist(form);

        return form;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy