001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.web.forms;
007
008import javax.servlet.*;
009import javax.servlet.http.*;
010import java.io.*;
011import java.util.*;
012import java.sql.*;
013
014import fc.jdbc.*;
015import fc.io.*;
016import fc.util.*;
017
018/**
019A test dependency implementation.
020*/
021public class TestDependency implements Dependency
022{ 
023static List usa_states =  Arrays.asList( new Select.Option[] { 
024      new Select.Option("pa"), 
025      new Select.Option("ny", true)
026      });
027
028static List int_states =  Arrays.asList( new Select.Option[] { 
029      new Select.Option("int#1"), 
030      new Select.Option("int#2"), 
031      new Select.Option("int#3")
032      });
033
034static  String  selectCountryFirst = "--select a country first--";
035
036static List select_country_msg =  Arrays.asList( new Select.Option[] { 
037      new Select.Option(selectCountryFirst)
038      });
039
040Select        country;
041DependentSelect   states;
042Log         log;
043String        last_name;
044
045public TestDependency(Select country, DependentSelect state)
046  {
047  this.country = country;
048  this.states = state;
049  states.addDependency(this);
050  log = states.log;
051  last_name = country.name + "_last_value_id";
052  }
053
054public Object getInitialValues(Field f) 
055  {
056  DependentSelect.Data data = new DependentSelect.Data();  //initially empty
057  data.addOption(new Select.Option(selectCountryFirst));
058  return data;
059  }
060  
061public void updateValues(FormData fd, HttpServletRequest req) 
062throws DependencyException
063  {
064  String country_val = country.getStringValue(fd);
065  
066  log.bug("country_val:", country_val);
067
068  if (needsUpdate(country_val, fd)) {
069    log.bug("needsupdate=true");  
070    fd.setDependencyUpdated();
071    }
072
073  Hidden prev_country =  fd.getDynamicField(last_name);
074  String prev_val   =  (prev_country != null) ? 
075                prev_country.getValue(fd).intern() : "";
076    
077  DependentSelect.Data data = (DependentSelect.Data) 
078                        fd.getData(states.getName());
079  
080  if (country_val == null || country_val.equalsIgnoreCase("--- select ---")){
081    states.setValue(data, select_country_msg);
082    }
083  else if (country_val.equalsIgnoreCase("usa")) { 
084    states.setValue(data, usa_states);
085    if (prev_val != "usa") 
086      data.clearSubmittedData();
087    }
088  else if (country_val.equalsIgnoreCase("uk")) {
089    states.setValue(data, int_states);
090    if (prev_val != "uk") 
091      data.clearSubmittedData();
092    }
093  else
094    throw new DependencyException("Bad country value (" + country_val + "), cannot calculate state dependency from this country");  
095    
096  }
097
098boolean needsUpdate(String country_val, FormData fd)
099  { 
100  if (country_val == null)
101    return false;
102  
103  Hidden prev_country =  fd.getDynamicField(last_name);
104  log.bug("prev_country: ", prev_country);
105    
106  if (prev_country == null)
107    return true;
108  
109  String prev_val = prev_country.getValue(fd); //can be null
110  
111  log.bug("country_val: ", country_val, "; prev_value:", prev_val);
112
113  return ! country_val.equals(prev_val);
114  }
115
116public void setDependencyDataFromSubmit(FormData fd, HttpServletRequest req)
117  {
118  String[] hidden_values = req.getParameterValues(last_name);
119  
120  log.bug("hidden_values=", Arrays.toString(hidden_values));
121  
122  if (hidden_values != null) {
123    Hidden h = new Hidden(last_name, hidden_values);
124    fd.addDynamicField(h);
125    }
126  }
127    
128public void renderDependencyData(FormData fd, Writer out) 
129throws SQLException, IOException
130  {
131  log.bug("start..");
132  if (fd != null) {
133    String country_val = country.getStringValue(fd);
134    if (country_val != null) {
135      Hidden h = new Hidden(last_name, country_val);
136      h.render(out); 
137      }
138    }
139  }
140    
141public boolean isSubmitDataValid(FormData fd)
142  {
143  return true; 
144  }
145
146public String toString() {
147  return "country-->state dependency";
148  }
149
150}          //~class DependencyOnSelect