edu.uiowa.icts.bluebutton.dao.LabTestHome.java Source code

Java tutorial

Introduction

Here is the source code for edu.uiowa.icts.bluebutton.dao.LabTestHome.java

Source

package edu.uiowa.icts.bluebutton.dao;

/*
 * #%L
 * blue-button Spring MVC Web App
 * %%
 * Copyright (C) 2014 - 2015 University of Iowa Institute for Clinical and Translational Science (ICTS)
 * %%
 * 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.
 * #L%
 */

import edu.uiowa.icts.spring.*;
import edu.uiowa.icts.bluebutton.domain.*;
import edu.uiowa.icts.bluebutton.json.LoincCode;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Order;

import edu.uiowa.icts.util.SortColumn;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Generated by Protogen 
 * @since 10/29/2014 09:18:02 CDT
 */
@Repository("edu_uiowa_icts_bluebutton_dao_LabTestHome")
@Transactional
public class LabTestHome extends GenericDao<LabTest> implements LabTestService {

    private static final Log log = LogFactory.getLog(LabTestHome.class);

    public LabTestHome() {
        setDomainName("edu.uiowa.icts.bluebutton.domain.LabTest");
    }

    public LabTest findById(Integer id) {
        return (LabTest) this.sessionFactory.getCurrentSession().get(LabTest.class, id);
    }

    @Override
    public void importCSV(InputStream fileInputStream) throws IOException {
        Reader in = new BufferedReader(new InputStreamReader(fileInputStream));
        Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader("LAB_TEST_ID", "LAB_TEST_NAME", "TEST_DESCRIPTION",
                "UNITS", "LOINC_NUM", "DATETIME_CREATED", "DATETIME_LASTMODIFIED").withSkipHeaderRecord(true)
                .parse(in);
        for (CSVRecord record : records) {
            LabTest labTest = new LabTest();
            labTest.setLabTestId(new Integer(record.get("LAB_TEST_ID")));
            labTest.setName(record.get("LAB_TEST_NAME"));
            labTest.setDescription(record.get("TEST_DESCRIPTION"));
            labTest.setUnits(record.get("UNITS"));
            labTest.setLoincCode(record.get("LOINC_NUM"));
            labTest.setDateCreated(record.get("DATETIME_CREATED"));
            labTest.setDateUpdated(record.get("DATETIME_LASTMODIFIED"));

            this.sessionFactory.getCurrentSession().save(labTest);
        }

    }

    @Override
    public List<LoincCode> getRangesByGenderAndAge(String sex, Double age) {
        return labTestQuery(sex, age, null);
    }

    @Override
    public List<LoincCode> getRangesByGenderAgeAndLoincCodes(String sex, Double age, String loincCodeCsvList) {
        return labTestQuery(sex, age, loincCodeCsvList);
    }

    private List<LoincCode> labTestQuery(String sex, Double age, String loincCodeCsvList) {
        List<LoincCode> list = new ArrayList<LoincCode>();
        if (sex != null && sex.length() > 0 && age != null) {
            sex = sex.substring(0, 1).toUpperCase();

            String sql = "select loinc_code as \"loinc_code\", min_normal as \"min_normal\", max_normal as \"max_normal\""
                    + "   from bluebutton.lab_test lt, bluebutton.lab_test_range ltr "
                    + "   where lt.lab_test_id = ltr.lab_test_id " + "   and ltr.sex like :sex "
                    + "   and :age >= ltr.min_age_years  " + "   and :age < ltr.max_age_years "
                    + "   and loinc_code IS NOT NULL ";
            if (loincCodeCsvList != null) {
                sql += "and loinc_code in (:loincCodeCsvList) ";
            }
            SQLQuery query = this.sessionFactory.getCurrentSession().createSQLQuery(sql);
            query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
            query.setString("sex", "%" + sex + "%");
            query.setDouble("age", age);
            if (loincCodeCsvList != null) {
                query.setParameterList("loincCodeCsvList", loincCodeCsvList.split(","));
            }
            List data = query.list();

            for (Object object : data) {
                Map row = (Map) object;
                LoincCode lc = new LoincCode(row.get("loinc_code").toString(),
                        new Double((double) row.get("min_normal")), new Double((double) row.get("max_normal")));
                list.add(lc);
            }
        }
        return list;
    }

    /*
     * 
    select loinc_code, min_normal, max_normal
    from bluebutton.lab_test lt, bluebutton.lab_test_range ltr
    where lt.lab_test_id = ltr.lab_test_id
    and ltr.sex like '%F%'
    and 27 > ltr.min_age_years 
    and 27 <= ltr.max_age_years;
        
    select loinc_code, count(*)
    from bluebutton.lab_test lt, bluebutton.lab_test_range ltr
    where lt.lab_test_id = ltr.lab_test_id
    and ltr.sex like '%F%'
    and 27 >= ltr.min_age_years 
    and 27 < ltr.max_age_years
    and loinc_code IS NOT NULL
    group by loinc_code;
     */

}