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

com.formkiq.server.dao.WorkflowDaoImpl 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 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.ClientWorkflow;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.WorkflowLog;
import com.formkiq.server.domain.type.FormDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.domain.type.WorkflowListDTO;

/**
 * Implementation of the Workflow Dao.
 *
 */
@Repository
public class WorkflowDaoImpl extends AbstractDaoImpl implements WorkflowDao {

    /** DateService. */
    @Autowired
    private DateService dateservice;

    @Override
    public ClientWorkflow findWorkflow(final User user, final String client,
            final String uuid) {

        String sql = "select * "
                + " from client_workflows fl "
                + " where fl.uuid=:uuid 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(ClientWorkflow.class);

        query.setParameter("user", user.getUserid());
        query.setParameter("uuid", uuid);
        query.setParameter("client", client);

        ClientWorkflow log = (ClientWorkflow) query.uniqueResult();

        return log;
    }

    @SuppressWarnings("unchecked")
    @Override
    public WorkflowListDTO findWorkflows(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_workflows 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();

        WorkflowListDTO dto = new WorkflowListDTO();
        dto.setWorkflows(list);
        return dto;
    }

    @SuppressWarnings("unchecked")
    @Override
    public WorkflowListDTO findWorkflows(final String client,
            final String workflow) {
        String sql = "select data #>> '{name}' as name, "
                + "data #>> '{uuid}' as uuid, "
                + "data #>> '{updated_date}' as updatedDate, "
                + "sha1_hash as sha1hash "
                + "from client_workflows 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", workflow)
                .setResultTransformer(
                        new AliasToBeanResultTransformer(FormDTO.class))
                .list();

        WorkflowListDTO dto = new WorkflowListDTO();
        dto.setWorkflows(list);
        return dto;
    }

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

        String sql = "select fl.workflow_log_id as workflow_log, "
                + " cf.uuid as id, cf.sha1_hash as sha1hash "
                + " from workflow_logs fl join client_workflows cf "
                + " on fl.client_id=cf.client_id "
                + "       and fl.uuid=cf.uuid "
                + " where fl.client_id=:client and fl.workflow_log_id > :token"
                + " order by fl.workflow_log_id asc";

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

        SyncListDTO dto = convertToSyncList(query, nextToken, -1);

        return dto;
    }

    @Override
    public ClientWorkflow saveWorkflow(final ClientWorkflow workflow) {

        Date now = this.dateservice.now();

        if (workflow.getUpdatedDate() == null) {
            workflow.setUpdatedDate(now);
        }

        if (workflow.getInsertedDate() == null) {
            workflow.setInsertedDate(now);
        }

        if (StringUtils.isEmpty(workflow.getClientWorkflowid())) {

            getEntityManager().persist(workflow);

        } else {

            getEntityManager().merge(workflow);
        }

        return workflow;
    }

    @Override
    public WorkflowLog saveWorkflowLog(final WorkflowLog log) {
        Date now = this.dateservice.now();
        log.setInsertedDate(now);
        getEntityManager().persist(log);

        return log;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy