1:import javax.portlet.*;
   2:import java.util.Set;
   3:import java.util.HashSet;
   4:import java.sql.SQLException;
   5:import org.apache.commons.logging.Log;
   6:import org.apache.commons.logging.LogFactory;
   7:
   8:/**QueryPreferencesValidator provides ensures that the Query the user supplies is a valid one.
   9: *
  10: * @author Michael Klaene
  11: * @see javax.portlet.PreferencesValidator
  12: **/
  13:
  14:public class QueryPreferencesValidator implements PreferencesValidator {
  15:    
  16:    /** Designated Logger for this class. */
  17:    private final Log log = LogFactory.getLog(getClass().getName());
  18:    
  19:    public void validate(PortletPreferences preferences)
  20:    throws ValidatorException {
  21:        log.info("QueryPreferencesValidator validate method entered.");
  22:        Set errorsSet = new HashSet();
  23:        errorsSet.clear();
  24:        
  25:        //Though we could validate title, we won't.  Just the SQL.
  26:        String sql = preferences.getValue("sql","BAD SQL");
  27:        
  28:        //We've ensured the data is there.  Now, just call our QueryBean's execute method.
  29:        //If the SQL parse fails, catch it and ask user to check their form data.
  30:        try {
  31:            QueryBean qb = new QueryBean();
  32:            qb.executeQuery(sql);
  33:        }
  34:        catch(Exception e) {
  35:            log.warn("Exception occured executing QueryBean executeQuery in  " +
  36:            "QueryPreferencesValidator. sql : " + sql);
  37:            errorsSet.add(sql);
  38:            throw new ValidatorException(e.toString(),errorsSet);
  39:        }
  40:    }
  41:}