cognition.pipeline.data.DNCWorkUnitDao.java Source code

Java tutorial

Introduction

Here is the source code for cognition.pipeline.data.DNCWorkUnitDao.java

Source

/*
Designed and developed by Ismail E. Kartoglu
Copyright 2015 King's College London
    
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 cognition.pipeline.data;

import cognition.pipeline.data.helper.BlobHelper;
import cognition.pipeline.data.helper.ClobHelper;
import cognition.pipeline.exception.WorkCoordinateNotFound;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import cognition.common.data.BaseDao;
import cognition.common.data.SessionWrapper;
import cognition.common.model.DNCWorkCoordinate;

import java.util.ArrayList;
import java.util.List;

@Repository
public class DNCWorkUnitDao extends BaseDao {

    @Autowired
    private BlobHelper blobHelper;

    @Autowired
    private ClobHelper clobHelper;

    /**
     *
     * @param coordinate Coordinate of the text in the source database.
     * @return The text in the coordinate.
     */
    public String getTextFromCoordinate(DNCWorkCoordinate coordinate) {
        if (coordinate.isBinary()) {
            throw new IllegalArgumentException("Coordinate is not a text coordinate. It's a binary one.");
        }

        Object result = getObjectFromCoordinate(coordinate);

        return clobHelper.getStringFromExpectedClob(result);
    }

    /**
     *
     * @param coordinate Coordinate of the binary object in the source database.
     * @return A byte array of the binary object.
     */
    public byte[] getByteFromCoordinate(DNCWorkCoordinate coordinate) {
        if (!coordinate.isBinary()) {
            throw new IllegalArgumentException("Coordinate is a text coordinate but binary is expected");
        }

        Object blobProxy = getObjectFromCoordinate(coordinate);

        return blobHelper.getByteFromExpectedBlobObject(blobProxy);
    }

    private Object getObjectFromCoordinate(DNCWorkCoordinate coordinate) {
        SessionWrapper sessionWrapper = createSourceSession();
        try {
            Query coordinateQuery = sessionWrapper.getNamedQuery("getObjectFromCoordinate");
            String queryString = coordinateQuery.getQueryString();
            queryString = queryString.replace(":sourceTable", coordinate.getSourceTable())
                    .replace(":sourceColumn", coordinate.getSourceColumn())
                    .replace(":pkColumnName", coordinate.getPkColumnName())
                    .replace(":id", Long.toString(coordinate.getIdInSourceTable()));

            List result = getSQLResultFromSource(queryString);

            if (CollectionUtils.isEmpty(result)) {
                throw new WorkCoordinateNotFound("Coordinate is invalid. No data found at " + coordinate);
            }

            return result.get(0);
        } finally {
            sessionWrapper.closeSession();
        }
    }

    /**
     * This method saves the conversion result in the specified saveTextToCoordinate
     * named query.
     * @param coordinate The original coordinate of the text
     */
    public void saveConvertedText(DNCWorkCoordinate coordinate) {
        SessionWrapper sessionWrapper = createTargetSession();
        try {
            Query query = sessionWrapper.getNamedQuery("saveTextToCoordinate");
            String queryString = query.getQueryString();
            SQLQuery sqlQuery = sessionWrapper.createSQLQuery(queryString);
            sqlQuery.setParameter(0, coordinate.getPatientId());
            sqlQuery.setParameter(1, coordinate.getSourceTable());
            sqlQuery.setParameter(2, coordinate.getSourceColumn());
            sqlQuery.setParameter(3, coordinate.getIdInSourceTable());
            sqlQuery.setParameter(4, coordinate.getConversionResult());
            sqlQuery.executeUpdate();
        } finally {
            sessionWrapper.closeSession();
        }
    }

    public List<DNCWorkCoordinate> getResults() {
        SessionWrapper sessionWrapper = createTargetSession();
        try {
            List<DNCWorkCoordinate> coordinates = new ArrayList<>();
            Query query = sessionWrapper.getNamedQuery("getResults");
            List rows = query.list();
            for (Object obj : rows) {
                Object[] row = (Object[]) obj;
                int personId = (int) row[0];
                String sourceTable = (String) row[1];
                String sourceColumn = (String) row[2];
                int docId = (int) row[3];
                String text = clobHelper.getStringFromExpectedClob(row[4]);

                DNCWorkCoordinate coordinate = new DNCWorkCoordinate();
                coordinate.setSourceTable(sourceTable);
                coordinate.setSourceColumn(sourceColumn);
                coordinate.setIdInSourceTable(docId);
                coordinate.setConversionResult(text);
                coordinate.setPatientId(personId);
                coordinates.add(coordinate);
            }

            return coordinates;
        } finally {
            sessionWrapper.closeSession();
        }
    }

}