<commit_before>
package org.mskcc.cbio.portal.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.mskcc.cbio.portal.model.ClinicalEvent;

/**
 *
 * @author gaoj
 */
public final class DaoClinicalEvent {
    private DaoClinicalEvent() {}
    
    public static int addClinicalEvent(ClinicalEvent clinicalEvent) {
        if (!MySQLbulkLoader.isBulkLoad()) {
            throw new IllegalStateException("Only buld load mode is allowed for importing clinical events");
        }
        
        MySQLbulkLoader.getMySQLbulkLoader("clinical_event").insertRecord(
                Long.toString(clinicalEvent.getClinicalEventId()),
                Integer.toString(clinicalEvent.getPatientId()),
                clinicalEvent.getStartDate().toString(),
                clinicalEvent.getStopDate()==null?null:clinicalEvent.getStopDate().toString(),
                clinicalEvent.getEventType()
                );
        return 1+addClinicalEventData(clinicalEvent);
    }
    
    private static int addClinicalEventData(ClinicalEvent clinicalEvent) {
        long eventId = clinicalEvent.getClinicalEventId();
        for (Map.Entry<String,String> entry : clinicalEvent.getEventData().entrySet()) {
            MySQLbulkLoader.getMySQLbulkLoader("clinical_event_data").insertRecord(
                    Long.toString(eventId),
                    entry.getKey(),
                    entry.getValue()
                    );
        }
        return 1;
        
    }
    
    public static List<ClinicalEvent> getClinicalEvent(int patientId) throws DaoException {
        return getClinicalEvent(patientId, null);
    }
    
    public static List<ClinicalEvent> getClinicalEvent(int patientId, String eventType) throws DaoException {

        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = JdbcUtil.getDbConnection(DaoClinicalEvent.class);

            // get events first
            if (eventType==null) {
                pstmt = con.prepareStatement("SELECT * FROM clinical_event WHERE PATIENT_ID=?");
            } else {
                pstmt = con.prepareStatement("SELECT * FROM clinical_event WHERE PATIENT_ID=? AND EVENT_TYPE=?");
            }
            pstmt.setInt(2, patientId);
            if (eventType!=null) {
                pstmt.setString(3, eventType);
            }

            rs = pstmt.executeQuery();
            Map<Long, ClinicalEvent> clinicalEvents = new HashMap<Long, ClinicalEvent>();
            while (rs.next()) {
               ClinicalEvent clinicalEvent = extractClinicalEvent(rs);
               clinicalEvents.put(clinicalEvent.getClinicalEventId(), clinicalEvent);
            }

            rs.close();
           
           // get data then
           if (!clinicalEvents.isEmpty()) {
                pstmt = con.prepareStatement("SELECT * FROM clinical_event_data WHERE CLINICAL_EVENT_ID IN ("
                        + StringUtils.join(clinicalEvents.keySet(), ",") + ")");

                rs = pstmt.executeQuery();
                while (rs.next()) {
                   long eventId = rs.getLong("CLINICAL_EVENT_ID");
                   clinicalEvents.get(eventId).addEventDatum(rs.getString("KEY"), rs.getString("VALUE"));
                }
            }

            return new ArrayList<ClinicalEvent>(clinicalEvents.values());
        } catch (SQLException e) {
           throw new DaoException(e);
        } finally {<commit_msg>Bug fix when retrieving clinical event data<commit_after>
package org.mskcc.cbio.portal.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.mskcc.cbio.portal.model.ClinicalEvent;

/**
 *
 * @author gaoj
 */
public final class DaoClinicalEvent {
    private DaoClinicalEvent() {}
    
    public static int addClinicalEvent(ClinicalEvent clinicalEvent) {
        if (!MySQLbulkLoader.isBulkLoad()) {
            throw new IllegalStateException("Only buld load mode is allowed for importing clinical events");
        }
        
        MySQLbulkLoader.getMySQLbulkLoader("clinical_event").insertRecord(
                Long.toString(clinicalEvent.getClinicalEventId()),
                Integer.toString(clinicalEvent.getPatientId()),
                clinicalEvent.getStartDate().toString(),
                clinicalEvent.getStopDate()==null?null:clinicalEvent.getStopDate().toString(),
                clinicalEvent.getEventType()
                );
        return 1+addClinicalEventData(clinicalEvent);
    }
    
    private static int addClinicalEventData(ClinicalEvent clinicalEvent) {
        long eventId = clinicalEvent.getClinicalEventId();
        for (Map.Entry<String,String> entry : clinicalEvent.getEventData().entrySet()) {
            MySQLbulkLoader.getMySQLbulkLoader("clinical_event_data").insertRecord(
                    Long.toString(eventId),
                    entry.getKey(),
                    entry.getValue()
                    );
        }
        return 1;
        
    }
    
    public static List<ClinicalEvent> getClinicalEvent(int patientId) throws DaoException {
        return getClinicalEvent(patientId, null);
    }
    
    public static List<ClinicalEvent> getClinicalEvent(int patientId, String eventType) throws DaoException {

        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = JdbcUtil.getDbConnection(DaoClinicalEvent.class);

            // get events first
            if (eventType==null) {
                pstmt = con.prepareStatement("SELECT * FROM clinical_event WHERE PATIENT_ID=?");
            } else {
                pstmt = con.prepareStatement("SELECT * FROM clinical_event WHERE PATIENT_ID=? AND EVENT_TYPE=?");
            }
            pstmt.setInt(1, patientId);
            if (eventType!=null) {
                pstmt.setString(2, eventType);
            }

            rs = pstmt.executeQuery();
            Map<Long, ClinicalEvent> clinicalEvents = new HashMap<Long, ClinicalEvent>();
            while (rs.next()) {
               ClinicalEvent clinicalEvent = extractClinicalEvent(rs);
               clinicalEvents.put(clinicalEvent.getClinicalEventId(), clinicalEvent);
            }

            rs.close();
           
           // get data then
           if (!clinicalEvents.isEmpty()) {
                pstmt = con.prepareStatement("SELECT * FROM clinical_event_data WHERE CLINICAL_EVENT_ID IN ("
                        + StringUtils.join(clinicalEvents.keySet(), ",") + ")");

                rs = pstmt.executeQuery();
                while (rs.next()) {
                   long eventId = rs.getLong("CLINICAL_EVENT_ID");
                   clinicalEvents.get(eventId).addEventDatum(rs.getString("KEY"), rs.getString("VALUE"));
                }
            }

            return new ArrayList<ClinicalEvent>(clinicalEvents.values());
        } catch (SQLException e) {
           throw new DaoException(e);
        } finally {
{'fraction_non_alphanumeric': 0.10045908722657305, 'fraction_numerical': 0.001080205238995409, 'mean_word_length': 2.210663198959688, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6960976', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1872, 'n_words': 368}