title: Checkbox values to varchar via Spring
body: <p>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.  </p>
  
  <p>You assistance in this is greatly appreciated as I am not sure what I am missing here. </p>
  
  <p>In the application context, I created the list of possible values as part of the ServiceBean.</p>
  
  <pre><code>    &lt;property name="selectableDosageFrequencyList"&gt;
          &lt;set&gt;
              &lt;value&gt;On an empty stomach&lt;/value&gt;
              &lt;value&gt;Every other day&lt;/value&gt;
              &lt;value&gt;4 times daily&lt;/value&gt; 
              &lt;value&gt;3 times daily&lt;/value&gt;
              &lt;value&gt;Twice daily&lt;/value&gt;
              &lt;value&gt;At bedtime&lt;/value&gt; 
              &lt;value&gt;With meal&lt;/value&gt;
              &lt;value&gt;As needed&lt;/value&gt;
              &lt;value&gt;Once daily&lt;/value&gt;               
          &lt;/set&gt;
      &lt;/property&gt;
  </code></pre>
  
  <p>This is set up in the flow as requestscope. </p>
  
  <pre><code>&lt;view-state id="addEditMedication" model="medication"&gt;
  
      &lt;on-render&gt;
          &lt;set name="requestScope.selectableDosageFrequencyList" value="memberService.buildSelectableDosageFrequencyList(patient)" /&gt;     
      &lt;/on-render&gt;
  ...
      &lt;transition on="next" to="assessment" &gt;
          &lt;evaluate expression="memberService.updateMedication(patient, medication)" /&gt;
      &lt;/transition&gt;
  &lt;/view-state&gt;
  </code></pre>
  
  <p>I have helper methods in the memberService that need to be executed when the form is init and then when the form is completed.</p>
  
  <pre><code>    //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&lt;String&gt; getSelectedDosageFrequencyList(String dosageFrequency){
      String copyOfDosages =dosageFrequency;//may not need to do this
      LinkedHashSet&lt;String&gt; setofSelectedDosageFrequency = new LinkedHashSet&lt;String&gt; ();
      while (copyOfDosages!= null &amp;&amp; copyOfDosages.length()&gt;0){
          for (String aFrequency: selectableDosageFrequencyList){
          if (copyOfDosages.contains(aFrequency)){
              setofSelectedDosageFrequency.add(aFrequency);
              if (!copyOfDosages.equals(aFrequency) &amp;&amp; copyOfDosages.endsWith(aFrequency)){
                  copyOfDosages.replaceAll(","+aFrequency, "");       
              }else if (!copyOfDosages.equals(aFrequency) &amp;&amp; copyOfDosages.contains(aFrequency=",")){
                  copyOfDosages.replaceAll(aFrequency+",", "");
              }else
                  copyOfDosages.replaceAll(aFrequency, "");
              copyOfDosages.trim();
          }
          }
      }   
      return setofSelectedDosageFrequency;        
  }
  </code></pre>
  
  <p>The Medication class that backs the form will have a variable for dosage-frequency as a string.</p>
  
  <pre><code>private String dosageFrequency;
  </code></pre>
  
  <p>The jsp I currently am doing this. </p>
  
  <pre><code>        &lt;div class="formField"&gt;
              &lt;form:label path="dosageFrequency"&gt;Dosage Frequency&lt;/form:label&gt;
              &lt;ul class="multi-column double" style="width: 550px;"&gt;
                  &lt;form:checkboxes path="dosageFrequency" items="${selectableDosageFrequencyList}" itemLabel="${selectableDosageFrequencyList}" element="li" /&gt;
              &lt;/ul&gt;
          &lt;/div&gt;  
  </code></pre>