Checkbox values to varchar via Spring
- by iowatiger08
I am trying to get a varchar message from a database to display the selected values of a checkbox field in a jsp for patient's medication's dosage frequency. The possible values will be saved in comma-delimited string in the varchar. For most form fields there is simply a one form value to one database field ratio, but in this case, I am needing to merge the values that would come as a string[] into the comma-delimited string and then when retrieving that record for that medication of that patient, display the selected values from the comma-delimited string as selected from the selectableDosageFrequencyList.
You assistance in this is greatly appreciated as I am not sure what I am missing here.
In the application context, I created the list of possible values as part of the ServiceBean.
<property name="selectableDosageFrequencyList">
<set>
<value>On an empty stomach</value>
<value>Every other day</value>
<value>4 times daily</value>
<value>3 times daily</value>
<value>Twice daily</value>
<value>At bedtime</value>
<value>With meal</value>
<value>As needed</value>
<value>Once daily</value>
</set>
</property>
This is set up in the flow as requestscope.
<view-state id="addEditMedication" model="medication">
<on-render>
<set name="requestScope.selectableDosageFrequencyList" value="memberService.buildSelectableDosageFrequencyList(patient)" />
</on-render>
...
<transition on="next" to="assessment" >
<evaluate expression="memberService.updateMedication(patient, medication)" />
</transition>
</view-state>
I have helper methods in the memberService that need to be executed when the form is init and then when the form is completed.
//get the form fields selected and build the new string for db
public String setSelectedDosageFrequency(String [] dosageFrequencies){
String frequencies = null;
if (dosageFrequencies != null){
for (String s : dosageFrequencies){
frequencies = frequencies + "," + s;
}
}
return frequencies;
}
//get value from database and build selected Set
public LinkedHashSet<String> getSelectedDosageFrequencyList(String dosageFrequency){
String copyOfDosages =dosageFrequency;//may not need to do this
LinkedHashSet<String> setofSelectedDosageFrequency = new LinkedHashSet<String> ();
while (copyOfDosages!= null && copyOfDosages.length()>0){
for (String aFrequency: selectableDosageFrequencyList){
if (copyOfDosages.contains(aFrequency)){
setofSelectedDosageFrequency.add(aFrequency);
if (!copyOfDosages.equals(aFrequency) && copyOfDosages.endsWith(aFrequency)){
copyOfDosages.replaceAll(","+aFrequency, "");
}else if (!copyOfDosages.equals(aFrequency) && copyOfDosages.contains(aFrequency=",")){
copyOfDosages.replaceAll(aFrequency+",", "");
}else
copyOfDosages.replaceAll(aFrequency, "");
copyOfDosages.trim();
}
}
}
return setofSelectedDosageFrequency;
}
The Medication class that backs the form will have a variable for dosage-frequency as a string.
private String dosageFrequency;
The jsp I currently am doing this.
<div class="formField">
<form:label path="dosageFrequency">Dosage Frequency</form:label>
<ul class="multi-column double" style="width: 550px;">
<form:checkboxes path="dosageFrequency" items="${selectableDosageFrequencyList}" itemLabel="${selectableDosageFrequencyList}" element="li" />
</ul>
</div>