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");
discontinuedReasonWidget.setOptions(discOptions);
context.registerWidget(discontinuedReasonWidget);
context.registerErrorWidget(discontinuedReasonWidget, discontinuedReasonErrorWidget);
}
createAdditionalWidgets(context);
if (context.getMode() != Mode.ENTER && context.getExistingOrders() != null) {
// If VIEW, EDIT
populateDrugOrderValuesFromDB(context, usingDurationField);
}
instructionsLabel = parameters.get(FIELD_INSTRUCTIONS_LABEL);
if (instructionsLabel != null){
instructionsWidget = new TextFieldWidget();
if (existingOrder != null){
instructionsWidget.setInitialValue(existingOrder.getInstructions());
}
instructionsErrorWidget = new ErrorWidget();
context.registerWidget(instructionsWidget);
context.registerErrorWidget(instructionsWidget, instructionsErrorWidget);
}
if (usingDurationField){
orderDurationWidget = new TextFieldWidget(4);
if (existingOrder != null && existingOrder.getAutoExpireDate() != null){
//set duration from autoExpireDate in days
Long autoDateMilis = existingOrder.getAutoExpireDate().getTime();
Long startDateMilis = existingOrder.getStartDate().getTime();
Long diffInMSec = autoDateMilis - startDateMilis;
// Find date difference in days
// (24 hours 60 minutes 60 seconds 1000 millisecond)
Long diffOfDays = diffInMSec / (24 * 60 * 60 * 1000);
orderDurationWidget.setInitialValue(String.valueOf(diffOfDays.intValue()));
}
orderDurationErrorWidget = new ErrorWidget();
context.registerWidget(orderDurationWidget);
context.registerErrorWidget(orderDurationWidget, orderDurationErrorWidget);
}
context.getSchema().addField(dof);
}