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

Java tutorial

Introduction

Here is the source code for edu.uiowa.icts.bluebutton.dao.LoincCodeCategoryHome.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 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.HashMap;
import java.util.List;
import java.util.Map;

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

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.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Generated by Protogen 
 * @since 01/27/2015 10:53:12 CST
 */
@Repository("edu_uiowa_icts_bluebutton_dao_LoincCodeCategoryHome")
@Transactional
public class LoincCodeCategoryHome extends GenericDao<LoincCodeCategory> implements LoincCodeCategoryService {

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

    public LoincCodeCategoryHome() {
        setDomainName("edu.uiowa.icts.bluebutton.domain.LoincCodeCategory");
    }

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

    @Override
    public void importCSV(InputStream fileInputStream) throws IOException {
        Reader in = new BufferedReader(new InputStreamReader(fileInputStream));
        Iterable<CSVRecord> records = CSVFormat.EXCEL
                .withHeader("PATH_TO_ROOT", "SEQUENCE", "IMMEDIATE_PARENT", "CODE", "CODE_TEXT")
                .withSkipHeaderRecord(true).parse(in);
        Map<String, String> codeTextMap = new HashMap<String, String>();
        for (CSVRecord record : records) {
            LoincCodeCategory lcc = new LoincCodeCategory();
            lcc.setLoincCode(record.get("CODE"));
            lcc.setName(record.get("CODE_TEXT"));
            codeTextMap.put(record.get("CODE"), record.get("CODE_TEXT"));
            if (!record.get("PATH_TO_ROOT").equals("")) {
                String[] rootName = record.get("PATH_TO_ROOT").split("\\.");
                lcc.setRootCategoryName(codeTextMap.get(rootName[0]));
                if (rootName.length > 1) {
                    lcc.setSubrootCategoryName(codeTextMap.get(rootName[1]));
                }
            }
            this.sessionFactory.getCurrentSession().save(lcc);
        }

    }

    @Override
    public List<LoincCode> findByLoincCodes(String loincCodeCsvList) {
        List<LoincCode> list = new ArrayList<LoincCode>();
        String[] arrayOfLoincCodes = loincCodeCsvList.split(",");
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(LoincCodeCategory.class);
        criteria.add(Restrictions.in("loincCode", arrayOfLoincCodes));

        List<LoincCodeCategory> loincList = (List<LoincCodeCategory>) criteria.list();
        for (LoincCodeCategory lcc : loincList) {
            LoincCode lc = new LoincCode();
            lc.setCode(lcc.getLoincCode());
            lc.setSubRootName(lcc.getSubrootCategoryName());
            list.add(lc);
        }
        return list;
    }

}