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

Java tutorial

Introduction

Here is the source code for com.formkiq.core.dao.MigrationDaoImpl.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 org.hibernate.Session;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;

import com.formkiq.core.domain.type.MapPaginationDTO;
import com.formkiq.core.util.Strings;

/**
 * Implementation of Migration Dao.
 *
 */
@Repository
public class MigrationDaoImpl extends AbstractDaoImpl implements MigrationDao {

    @SuppressWarnings("resource")
    @Override
    public int findAssetCount() {
        String sql = "select count(*) as count from assets ";
        Session session = getEntityManager().unwrap(Session.class);

        Integer count = (Integer) session.createSQLQuery(sql).addScalar("count", IntegerType.INSTANCE)
                .uniqueResult();
        return count.intValue();
    }

    @SuppressWarnings({ "unchecked", "resource" })
    @Override
    public MapPaginationDTO findFolderAssetList(final String token) {

        int offset = Strings.getOffset(token);
        int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);

        StringBuilder sql = new StringBuilder("select folder_id as folder, asset_id as asset "
                + "from folder_forms " + "where folder_id is not null and asset_id is not null ");

        sql.append(" OFFSET " + offset + " FETCH FIRST " + (max + 1) + " ROWS ONLY");

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

        List<Map<String, String>> list = session.createSQLQuery(sql.toString())
                .addScalar("folder", StringType.INSTANCE).addScalar("asset", StringType.INSTANCE)
                .setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();

        MapPaginationDTO dto = new MapPaginationDTO();

        List<Map<String, String>> truncate = updatePagination(dto, offset, max, list);

        dto.setData(truncate);

        return dto;
    }
}