drugLabels = Arrays.asList(parameters.get(FIELD_DRUG_LABELS).split(","));
}
// fill drop down with drug names from database
List<Option> options = new ArrayList<Option>();
options.add(new Option("", "", false));
// drugNames is comma separated list which can contain ID, UUID or drugname
StringTokenizer tokenizer = new StringTokenizer(drugNames, ",");
int drugListIndexPos = 0;
String displayText = "";
DrugOrderField dof = new DrugOrderField();
while (tokenizer.hasMoreElements()) {
String drugName = (String) tokenizer.nextElement();
Drug drug = null;
// see if this is a uuid
if (HtmlFormEntryUtil.isValidUuidFormat(drugName.trim())) {
drug = conceptService.getDrugByUuid(drugName.trim());
}
// if we didn't find by id, find by uuid or name
if (drug == null){
drug = conceptService.getDrugByNameOrId(drugName.trim());
}
if (drug != null) {
displayText = drug.getName();
if (drugLabels != null) {
displayText = drugLabels.get(drugListIndexPos);
}
options.add(new Option(displayText, drug.getDrugId().toString(), false));
if (drugsUsedAsKey == null) {
drugsUsedAsKey = new ArrayList<Drug>();
}
drugsUsedAsKey.add(drug);
DrugOrderAnswer doa = new DrugOrderAnswer(drug, displayText);
dof.addDrugOrderAnswer(doa);
drugListIndexPos ++;
} else if (drugName.length() > 0 && drugName.charAt(0) == '/' && drugName.charAt(drugName.length()-1) == '/'){
options.add(new Option("[ " + drugName.substring(1,drugName.length()-1) + " ]", "~", false));
} else {
throw new IllegalArgumentException("No Drug found for drug name/id/uuid " + drugName);
}
}
if (drugsUsedAsKey == null)
throw new IllegalArgumentException("You must provide a valid drug name, or a valid ID or a valid UUID in " + parameters);
// there need to be the same number of drugs as drug labels
if (drugLabels != null && drugsUsedAsKey.size() != drugLabels.size())
throw new IllegalArgumentException("There are a different number of drugLabels (" + drugLabels.size() + ") than drugs (" + drugsUsedAsKey.size() + ").");
// Register Drug Widget
if (checkbox && drugsUsedAsKey.size() == 1){
CheckboxWidget cb = new CheckboxWidget();
cb.setLabel(displayText);
cb.setValue(drugsUsedAsKey.get(0).getDrugId().toString());
drugWidget = cb;
} else {
DropdownWidget dw = new DropdownWidget();
dw.setOptions(options);
drugWidget = dw;
}
context.registerWidget(drugWidget);
drugErrorWidget = new ErrorWidget();
context.registerErrorWidget(drugWidget, drugErrorWidget);
//start date
startDateWidget = new DateWidget();
startDateErrorWidget = new ErrorWidget();
context.registerWidget(startDateWidget);
context.registerErrorWidget(startDateWidget, startDateErrorWidget);
if (!hideDoseAndFrequency){
// dose validation by drug is done in validateSubmission()
doseWidget = new NumberFieldWidget(0d, 9999999d, true);
//set default value (maybe temporarily)
String defaultDoseStr = parameters.get(CONFIG_DEFAULT_DOSE);
if (!StringUtils.isEmpty(defaultDoseStr)){
try {
defaultDose = Double.valueOf(defaultDoseStr);
doseWidget.setInitialValue(defaultDose);
} catch (Exception ex){
throw new RuntimeException("optional attribute 'defaultDose' must be numeric or empty.");
}
}
doseErrorWidget = new ErrorWidget();
context.registerWidget(doseWidget);
context.registerErrorWidget(doseWidget, doseErrorWidget);
createFrequencyWidget(context, mss);
createFrequencyWeekWidget(context, mss);
}
if (!usingDurationField){
discontinuedDateWidget = new DateWidget();
discontinuedDateErrorWidget = new ErrorWidget();
context.registerWidget(discontinuedDateWidget);
context.registerErrorWidget(discontinuedDateWidget, discontinuedDateErrorWidget);
}
if (parameters.get(FIELD_DISCONTINUED_REASON) != null){
String discReasonConceptStr = (String) parameters.get(FIELD_DISCONTINUED_REASON);
Concept discontineReasonConcept = HtmlFormEntryUtil.getConcept(discReasonConceptStr);
if (discontineReasonConcept == null)
throw new IllegalArgumentException("discontinuedReasonConceptId is not set to a valid conceptId or concept UUID");
dof.setDiscontinuedReasonQuestion(discontineReasonConcept);
discontinuedReasonWidget = new DropdownWidget();
discontinuedReasonErrorWidget = new ErrorWidget();
List<Option> discOptions = new ArrayList<Option>();
discOptions.add(new Option("", "", false));
if (parameters.get(FIELD_DISCONTINUED_REASON_ANSWERS) != null){
//setup a list of the reason concepts
List<Concept> discReasons = new ArrayList<Concept>();
String discAnswersString = (String) parameters.get(FIELD_DISCONTINUED_REASON_ANSWERS);
String[] strDiscAnswers = discAnswersString.split(",");
for (int i = 0; i < strDiscAnswers.length; i++){
String thisAnswer = strDiscAnswers[i];
Concept answer = HtmlFormEntryUtil.getConcept(thisAnswer, "discontinueReasonAnswers includes a value that is not a valid conceptId or concept UUID");
discReasons.add(answer);
}
if (parameters.get(FIELD_DISCONTINUED_REASON_ANSWER_LABELS) != null){
// use the listed discontinueReasons, and use labels:
String discLabelsString = parameters.get(FIELD_DISCONTINUED_REASON_ANSWER_LABELS);
String[] strDiscAnswerLabels = discLabelsString.split(",");
//a little validation:
if (strDiscAnswerLabels.length != discReasons.size())
throw new RuntimeException("discontinueReasonAnswers and discontinueReasonAnswerLabels must contain the same number of members.");
for (int i = 0; i < strDiscAnswerLabels.length; i ++ ){
discOptions.add(new Option( strDiscAnswerLabels[i], discReasons.get(i).getConceptId().toString(),false));
dof.addDiscontinuedReasonAnswer(new ObsFieldAnswer(strDiscAnswerLabels[i].trim(), discReasons.get(i)));
}
} else {
// use the listed discontinueReasons, and use their ConceptNames.
for (Concept c: discReasons){
discOptions.add(new Option( c.getBestName(Context.getLocale()).getName(), c.getConceptId().toString(),false));
dof.addDiscontinuedReasonAnswer(new ObsFieldAnswer(c.getBestName(Context.getLocale()).getName(), c));
}
}
} else {
//just use the conceptAnswers
for (ConceptAnswer ca : discontineReasonConcept.getAnswers()){
discOptions.add(new Option( ca.getAnswerConcept().getBestName(Context.getLocale()).getName(), ca.getAnswerConcept().getConceptId().toString(),false));
dof.addDiscontinuedReasonAnswer(new ObsFieldAnswer(ca.getAnswerConcept().getBestName(Context.getLocale()).getName(), ca.getAnswerConcept()));
}
}
if (discOptions.size() == 1)
throw new IllegalArgumentException("discontinue reason Concept doesn't have any ConceptAnswers");