com.gst.portfolio.note.service.NoteReadPlatformServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.gst.portfolio.note.service.NoteReadPlatformServiceImpl.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.gst.portfolio.note.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import com.gst.infrastructure.core.data.EnumOptionData;
import com.gst.infrastructure.core.domain.JdbcSupport;
import com.gst.infrastructure.core.service.RoutingDataSource;
import com.gst.portfolio.note.data.NoteData;
import com.gst.portfolio.note.domain.NoteType;
import com.gst.portfolio.note.exception.NoteNotFoundException;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

@Service
public class NoteReadPlatformServiceImpl implements NoteReadPlatformService {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public NoteReadPlatformServiceImpl(final RoutingDataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    private static final class NoteMapper implements RowMapper<NoteData> {

        public String schema() {
            return " select n.id as id, n.client_id as clientId, n.group_id as groupId, n.loan_id as loanId, n.loan_transaction_id as transactionId, "
                    + " n.note_type_enum as noteTypeEnum, n.note as note, n.created_date as createdDate, n.createdby_id as createdById, "
                    + " cb.username as createdBy, n.lastmodified_date as lastModifiedDate, n.lastmodifiedby_id as lastModifiedById, mb.username as modifiedBy "
                    + " from m_note n left join m_appuser cb on cb.id=n.createdby_id left join m_appuser mb on mb.id=n.lastmodifiedby_id ";
        }

        @Override
        public NoteData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum)
                throws SQLException {

            final Long id = JdbcSupport.getLong(rs, "id");
            final Long clientId = JdbcSupport.getLong(rs, "clientId");
            final Long groupId = JdbcSupport.getLong(rs, "groupId");
            final Long loanId = JdbcSupport.getLong(rs, "loanId");
            final Long transactionId = JdbcSupport.getLong(rs, "transactionId");
            // final Long depositAccountId = JdbcSupport.getLong(rs,
            // "depositAccountId");
            // final Long savingAccountId = JdbcSupport.getLong(rs,
            // "savingAccountId");
            final Integer noteTypeId = JdbcSupport.getInteger(rs, "noteTypeEnum");
            final EnumOptionData noteType = NoteEnumerations.noteType(noteTypeId);
            final String note = rs.getString("note");
            final DateTime createdDate = JdbcSupport.getDateTime(rs, "createdDate");
            final Long createdById = JdbcSupport.getLong(rs, "createdById");
            final DateTime lastModifiedDate = JdbcSupport.getDateTime(rs, "lastModifiedDate");
            final Long lastModifiedById = JdbcSupport.getLong(rs, "lastModifiedById");
            final String createdByUsername = rs.getString("createdBy");
            final String updatedByUsername = rs.getString("modifiedBy");
            return new NoteData(id, clientId, groupId, loanId, transactionId, null, null, noteType, note,
                    createdDate, createdById, createdByUsername, lastModifiedDate, lastModifiedById,
                    updatedByUsername);
        }
    }

    @Override
    public NoteData retrieveNote(final Long noteId, final Long resourceId, final Integer noteTypeId) {
        final NoteType noteType = NoteType.fromInt(noteTypeId);
        try {
            final NoteMapper rm = new NoteMapper();
            String conditionSql = getResourceCondition(noteType);
            if (StringUtils.isNotBlank(conditionSql)) {
                conditionSql = " and " + conditionSql;
            }

            final String sql = rm.schema() + " where n.id = ? " + conditionSql + " order by n.created_date DESC";

            return this.jdbcTemplate.queryForObject(sql, rm, new Object[] { noteId, resourceId });
        } catch (final EmptyResultDataAccessException e) {
            throw new NoteNotFoundException(noteId, resourceId, noteType.name().toLowerCase());
        }
    }

    @Override
    public Collection<NoteData> retrieveNotesByResource(final Long resourceId, final Integer noteTypeId) {
        final NoteType noteType = NoteType.fromInt(noteTypeId);
        final NoteMapper rm = new NoteMapper();
        final String conditionSql = getResourceCondition(noteType);

        final String sql = rm.schema() + " where " + conditionSql + " order by n.created_date DESC";

        return this.jdbcTemplate.query(sql, rm, new Object[] { resourceId });
    }

    public static String getResourceCondition(final NoteType noteType) {
        String conditionSql = "";
        switch (noteType) {
        case CLIENT:
            conditionSql = " n.client_id = ? and note_type_enum = " + NoteType.CLIENT.getValue();
            break;
        case LOAN:
            conditionSql = " n.loan_id = ? and ( n.note_type_enum = " + NoteType.LOAN.getValue()
                    + " or n.note_type_enum = " + NoteType.LOAN_TRANSACTION.getValue() + " )";
            break;
        case LOAN_TRANSACTION:
            conditionSql = " n.loan_transaction_id = ? ";
            break;
        case SAVING_ACCOUNT:
            conditionSql = " n.saving_account_id = ? ";
            break;
        case GROUP:
            conditionSql = " n.group_id = ? ";
            break;
        default:
            break;
        }

        return conditionSql;
    }
}