com.formkiq.core.dao.ObjectEventDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.core.dao.ObjectEventDaoImpl.java

Source

/*
 * 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.core.dao;

import java.util.List;
import java.util.UUID;

import javax.persistence.Query;

import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.type.PostgresUUIDType;
import org.hibernate.type.StringType;
import org.hibernate.type.TimestampType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.formkiq.core.domain.ObjectEvent;
import com.formkiq.core.domain.User;
import com.formkiq.core.domain.type.EventType;
import com.formkiq.core.domain.type.ObjectEventDTO;
import com.formkiq.core.domain.type.ObjectEventListDTO;
import com.formkiq.core.util.DateService;

/**
 * EventDao Implementation.
 *
 */
@Repository
public class ObjectEventDaoImpl extends AbstractDaoImpl implements ObjectEventDao {

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

    @SuppressWarnings("resource")
    @Override
    public boolean deleteEvent(final User user, final String object, final EventType type) {

        String sql = "delete from object_events where " + " user_id=:user and object_id=:object and type=:type";

        Session session = getEntityManager().unwrap(Session.class);

        int result = session.createSQLQuery(sql).setParameter("user", user.getUserid(), PostgresUUIDType.INSTANCE)
                .setParameter("object", UUID.fromString(object), PostgresUUIDType.INSTANCE)
                .setParameter("type", type.name()).executeUpdate();

        return result > 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ObjectEvent> findEvents(final String uuid, final EventType type) {

        Query query = getEntityManager()
                .createQuery("SELECT o FROM ObjectEvent o " + " WHERE o.objectid = :obj and o.type=:type");

        query.setParameter("obj", UUID.fromString(uuid));
        query.setParameter("type", type);

        return query.getResultList();
    }

    @SuppressWarnings({ "unchecked", "resource" })
    @Override
    public ObjectEventListDTO list(final User user) {

        StringBuilder sql = new StringBuilder("select oe.object_event_id as eventid, oe.type, "
                + "f.name as foldername, oe.notification, "
                + " oe.object_id as object, oe.inserted_date as insertedDate " + "from object_events oe "
                + "join folders f on f.folder_id=oe.object_id " + "where oe.user_id=:user " + "order by f.name");

        Session session = getEntityManager().unwrap(Session.class);

        List<ObjectEventDTO> list = session.createSQLQuery(sql.toString()).addScalar("eventid", StringType.INSTANCE)
                .addScalar("type", StringType.INSTANCE).addScalar("foldername", StringType.INSTANCE)
                .addScalar("notification", StringType.INSTANCE).addScalar("object", StringType.INSTANCE)
                .addScalar("insertedDate", TimestampType.INSTANCE)
                .setParameter("user", user.getUserid(), PostgresUUIDType.INSTANCE)
                .setResultTransformer(new AliasToBeanResultTransformer(ObjectEventDTO.class)).list();

        ObjectEventListDTO dto = new ObjectEventListDTO();
        dto.setEvents(list);

        return dto;
    }

    @Override
    public ObjectEvent saveObjectEvent(final ObjectEvent oe) {

        if (StringUtils.isEmpty(oe.getObjecteventid())) {

            oe.setObjecteventid(UUID.randomUUID());
            oe.setInsertedDate(this.dateservice.now());
            getEntityManager().persist(oe);

            return oe;
        }

        return getEntityManager().merge(oe);
    }

    @Override
    public ObjectEvent findEvent(final User user, final String object, final EventType type) {

        Query query = getEntityManager().createQuery("SELECT o FROM ObjectEvent o " + " WHERE o.objectid = :obj "
                + "and o.type=:type and o.userid=:user");

        query.setParameter("user", user.getUserid());
        query.setParameter("obj", UUID.fromString(object));
        query.setParameter("type", type);

        return (ObjectEvent) getSingleResult(query);
    }

    @SuppressWarnings("resource")
    @Override
    public ObjectEventDTO findEvent(final UUID eventid) {
        StringBuilder sql = new StringBuilder("select oe.object_event_id as eventid, " + "u.email as email, "
                + "oe.type, " + "f.name as foldername, " + "oe.notification, " + "oe.object_id as object, "
                + "oe.inserted_date as insertedDate " + "from object_events oe "
                + "join folders f on f.folder_id=oe.object_id " + "join users u on u.user_id=oe.user_id "
                + "where oe.object_event_id=:id");

        Session session = getEntityManager().unwrap(Session.class);

        ObjectEventDTO dto = (ObjectEventDTO) session.createSQLQuery(sql.toString())
                .addScalar("eventid", StringType.INSTANCE).addScalar("type", StringType.INSTANCE)
                .addScalar("email", StringType.INSTANCE).addScalar("foldername", StringType.INSTANCE)
                .addScalar("notification", StringType.INSTANCE).addScalar("object", StringType.INSTANCE)
                .addScalar("insertedDate", TimestampType.INSTANCE)
                .setParameter("id", eventid, PostgresUUIDType.INSTANCE)
                .setResultTransformer(new AliasToBeanResultTransformer(ObjectEventDTO.class)).uniqueResult();

        return dto;
    }
}