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

Java tutorial

Introduction

Here is the source code for com.formkiq.core.dao.SystemDaoImpl.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.Map;
import java.util.UUID;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.hibernate.type.PostgresUUIDType;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.formkiq.core.domain.SystemProperty;
import com.formkiq.core.domain.User;
import com.formkiq.core.domain.type.SystemPropertyDTO;

/**
 * SystemDao implementation.
 *
 */
@Repository
public class SystemDaoImpl extends AbstractDaoImpl implements SystemDao {

    /** User Priority. */
    private static final int USER_PRIORITY = 100;

    @SuppressWarnings("resource")
    @Override
    public int delete(final User user, final String key) {

        String sql = "delete from system_properties where key=:key ";

        if (user != null) {
            sql += " and user_id=:user";
        }

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

        Query q = session.createSQLQuery(sql).setParameter("key", key);

        if (user != null) {
            q.setParameter("user", user.getUserid(), PostgresUUIDType.INSTANCE);
        }

        int result = q.executeUpdate();
        return result;
    }

    /**
     * Find SystemProperty.
     * @param userid {@link UUID}
     * @param key {@link String}
     * @return {@link SystemProperty}
     */
    @SuppressWarnings("unchecked")
    private SystemProperty find(final UUID userid, final String key) {

        String jql = "select p from SystemProperty p " + "where p.key=:key";

        if (userid != null) {
            jql += " and (p.userid=:user or p.userid is null) " + "order by p.priority desc";
        } else {
            jql += " and p.userid is null";
        }

        javax.persistence.Query query = getEntityManager().createQuery(jql).setParameter("key", key);

        if (userid != null) {
            query.setParameter("user", userid);
        }

        List<SystemProperty> list = query.getResultList();
        return !list.isEmpty() ? list.get(0) : null;
    }

    @SuppressWarnings({ "unchecked", "resource" })
    @Override
    public List<SystemPropertyDTO> getProperties(final User user, final String key) {

        String sql = "select p.key as key, p.value as value, " + "p.user_id as \"user.uuid\","
                + "u.email as \"user.email\"" + " from system_properties p "
                + " left join users u on u.user_id=p.user_id";

        if (user != null || StringUtils.hasText(key)) {
            sql += " where ";
        }

        if (user != null) {
            sql += " p.user_id=:user";
        }

        if (StringUtils.hasText(key)) {
            sql += user != null ? " and " : " ";
            sql += "key=:key";
        }

        sql += " order by p.key ";

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

        SQLQuery q = session.createSQLQuery(sql).addScalar("key", StringType.INSTANCE)
                .addScalar("value", StringType.INSTANCE).addScalar("user.email", StringType.INSTANCE)
                .addScalar("user.uuid", PostgresUUIDType.INSTANCE);

        if (user != null) {
            q.setParameter("user", user.getUserid(), PostgresUUIDType.INSTANCE);
        }

        if (StringUtils.hasText(key)) {
            q.setParameter("key", key);
        }

        List<Map<String, Object>> maplist = q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
                .list();

        List<SystemPropertyDTO> list = transformMapListToObject(maplist, SystemPropertyDTO.class);

        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public SystemProperty getProperty(final User user, final String key) {

        String jql = "select p from SystemProperty p " + "where p.key=:key";

        if (user != null) {
            jql += " and (p.userid=:user) ";
        } else {
            jql += " and p.userid is null";
        }

        javax.persistence.Query query = getEntityManager().createQuery(jql).setParameter("key", key);

        if (user != null) {
            query.setParameter("user", user.getUserid());
        }

        List<SystemProperty> list = query.getResultList();
        return !list.isEmpty() ? list.get(0) : null;
    }

    @Override
    public String getValue(final User user, final String key) {
        UUID userid = user != null ? user.getUserid() : null;
        SystemProperty sp = find(userid, key);
        return sp != null ? sp.getValue() : null;
    }

    @Override
    public SystemProperty save(final SystemProperty property) {

        if (property.getSystempropertyid() != null) {
            return getEntityManager().merge(property);
        }

        property.setSystempropertyid(UUID.randomUUID());

        if (property.getUserid() != null) {
            property.setPriority(USER_PRIORITY);
        } else {
            property.setPriority(0);
        }

        getEntityManager().persist(property);
        return property;
    }
}