<commit_before>package org.javarosa.patient.util;

import java.util.Date;
import java.util.Vector;

import org.javarosa.core.model.IDataReference;
import org.javarosa.core.model.IFormDataModel;
import org.javarosa.core.model.data.DateData;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.IntegerData;
import org.javarosa.core.model.utils.DateUtils;
import org.javarosa.core.model.utils.IPreloadHandler;
import org.javarosa.patient.model.Patient;
import org.javarosa.patient.model.data.ImmunizationAnswerData;
import org.javarosa.patient.model.data.ImmunizationData;
import org.javarosa.patient.model.data.NumericListData;

public class PatientPreloadHandler implements IPreloadHandler {
	
	/** The patient for this handler */
	private Patient patient;
	
	/**
	 * Creates a preload handler that can pull values from
	 * the patient object provided.
	 * 
	 * @param thePatient the patient whose data is to be 
	 * retrevied from this preload handler
	 */
	public PatientPreloadHandler(Patient thePatient) {
		patient = thePatient;
	}
	
	/**
	 * 
	 */
	public IAnswerData handlePreload(String preloadParams) {
		IAnswerData returnVal = null;
		if("vaccination_table".equals(preloadParams)) {
			ImmunizationData table = patient.getVaccinations();
			returnVal = new ImmunizationAnswerData(table);
		} else if(preloadParams.equals("monthsOnTreatment")) {
			DateData dateData = (DateData)patient.getRecord("treatmentStart");
			if(dateData != null) {
				int months = DateUtils.getMonthsDifference((Date)dateData.getValue(), new Date()); 
				returnVal = new IntegerData(months);
			}
		}
		else {
			int selectorStart = preloadParams.indexOf("[");
			if(selectorStart == -1) {
				returnVal = (IAnswerData)patient.getRecord(preloadParams);
			} else {
				String type = preloadParams.substring(0, selectorStart);
				String selector = preloadParams.substring(selectorStart, preloadParams.length());
				Vector data = patient.getRecordSet(type, selector);
				returnVal = new NumericListData();
				returnVal.setValue(data);
			}<commit_msg>[r941] [branches/dev] Immunization data is now wired up for disabled doses, and disabling based on age<commit_after>package org.javarosa.patient.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import org.javarosa.core.model.IDataReference;
import org.javarosa.core.model.IFormDataModel;
import org.javarosa.core.model.data.DateData;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.IntegerData;
import org.javarosa.core.model.utils.DateUtils;
import org.javarosa.core.model.utils.IPreloadHandler;
import org.javarosa.patient.model.Patient;
import org.javarosa.patient.model.data.ImmunizationAnswerData;
import org.javarosa.patient.model.data.ImmunizationData;
import org.javarosa.patient.model.data.NumericListData;

public class PatientPreloadHandler implements IPreloadHandler {
	
	/** The patient for this handler */
	private Patient patient;
	
	/**
	 * Creates a preload handler that can pull values from
	 * the patient object provided.
	 * 
	 * @param thePatient the patient whose data is to be 
	 * retrevied from this preload handler
	 */
	public PatientPreloadHandler(Patient thePatient) {
		patient = thePatient;
	}
	
	/**
	 * 
	 */
	public IAnswerData handlePreload(String preloadParams) {
		IAnswerData returnVal = null;
		if("vaccination_table".equals(preloadParams)) {
			ImmunizationData table = patient.getVaccinations();
			int largestAge = 0;
			Date birthDate = patient.getBirthDate();
			int months = DateUtils.getMonthsDifference(birthDate, new Date());
			if(months >= 9) {
				largestAge = 4;
			} else if(months >= 3) {
				largestAge = 3;
			} else if(months >= 2) {
				largestAge = 2;
			} else if(months >= 1){
				largestAge = 1;
			}
			table.setLargestAgeColumn(largestAge);
			returnVal = new ImmunizationAnswerData(table);
		} else if(preloadParams.equals("monthsOnTreatment")) {
			DateData dateData = (DateData)patient.getRecord("treatmentStart");
			if(dateData != null) {
				int months = DateUtils.getMonthsDifference((Date)dateData.getValue(), new Date()); 
				returnVal = new IntegerData(months);
			}
		}
		else {
			int selectorStart = preloadParams.indexOf("[");
			if(selectorStart == -1) {
				returnVal = (IAnswerData)patient.getRecord(preloadParams);
			} else {
				String type = preloadParams.substring(0, selectorStart);
				String selector = preloadParams.substring(selectorStart, preloadParams.length());
				Vector data = patient.getRecordSet(type, selector);
				returnVal = new NumericListData();
				returnVal.setValue(data);
			}
{'fraction_non_alphanumeric': 0.11010362694300518, 'fraction_numerical': 0.0034542314335060447, 'mean_word_length': 4.608958837772397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4232999', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1397, 'n_words': 302}