View Javadoc

1   package org.sourceforge.vlibrary.user.forms;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.StringTokenizer;
6   
7   import javax.servlet.http.HttpServletRequest;
8   
9   import org.apache.struts.action.ActionErrors;
10  import org.apache.struts.action.ActionForm;
11  import org.apache.struts.action.ActionMapping;
12  import org.apache.struts.action.ActionMessage;
13  import org.sourceforge.vlibrary.user.domain.Location;
14  import org.sourceforge.vlibrary.user.domain.Subject;
15  
16  /**
17   *
18   * Form bean for the Book Search page.  This form has the following fields:
19   * <ul>
20   * <li><b>selector</b> - type of search ("None","Title","Author" or "Subject")
21   * <li><b>title</b> - [REQUIRED if selector="Title"]
22   * <li><b>authorLastName</b> - [REQUIRED if selector="Author"]
23   * <li><b>subjects</b> - Subject(s) [COMPUTED if selector="Subject"]
24   * <li><b>subjectString</b> - Subject(s) \n-separated
25   *  [REQUIRED if selector="Subject"]
26   * <li><b>queryType</b> - "And" or "Or" [REQUIRED if selector="Subject"]
27   * <li><b>books<b> - List of books resulting from search [COMPUTED]
28   * </ul>
29   *
30   * @version $Revision$ $Date$
31   */
32  
33  public class BookSearchForm extends ActionForm {
34      private static final long serialVersionUID = 0;
35  
36      private String selector = "None";
37      private String title = null;
38      private String authorLastName = null;
39      private ArrayList<Subject> subjects = null;
40      private ArrayList<Location> locations = null;
41      private String queryType = "Or";
42      private ArrayList<Subject> subjectList = null;
43  
44      public BookSearchForm() {
45      }
46  
47      public String getAuthorLastName() {
48          return authorLastName;
49      }
50  
51      public void setAuthorLastName(String authorLastName) {
52          this.authorLastName = authorLastName;
53      }
54  
55      public String getQueryType() {
56          return queryType;
57      }
58  
59      public void setQueryType(String queryType) {
60          this.queryType = queryType;
61      }
62  
63      public String getSelector() {
64          return selector;
65      }
66  
67      public void setSelector(String selector) {
68          this.selector = selector;
69      }
70  
71      public ArrayList<Subject> getSubjects() {
72          return subjects;
73      }
74  
75      public void setSubjects(ArrayList<Subject> subjects) {
76          this.subjects = subjects;
77      }
78  
79      public String getSubjectString() {
80          if (subjects == null) return null;
81  
82          StringBuffer buf = new StringBuffer();
83          Subject su = null;
84  
85          for (int i=0;i<subjects.size();i++) {
86              su = subjects.get(i);
87              buf.append(su.getDescription());
88              buf.append("\n");
89          }
90  
91          return buf.toString();
92      }
93  
94      public String getSubjectListString() {
95          if (subjectList == null) return null;
96          StringBuffer buf = new StringBuffer();
97          Subject su = null;
98  
99          for (int i=0;i<subjectList.size();i++) {
100             su = (Subject)subjectList.get(i);
101             buf.append(su.getDescription());
102             buf.append("\n");
103         }
104 
105         return buf.toString();
106     }
107 
108     /**
109      * Takes String from Subjects text area and fills subjects ArrayList
110      * with subject objects constructed from the descriptions in the
111      * \n-delimited input string. <br>
112      * <strong>nb: ID fields in subject objects will not be set </strong>.
113      * @param inString String -- \n-delimited list of author full names
114      */
115     /** todo -- replace with generic function */
116     public void setSubjectString(String inString) {
117         StringTokenizer st = new StringTokenizer(inString,"\n");
118         if (!st.hasMoreElements()) return;
119         subjects = null;
120         subjects = new ArrayList<Subject>();
121         while (st.hasMoreElements()) {
122             Subject su = new Subject(st.nextToken());
123             subjects.add(su);
124         }
125     }
126 
127     public String getTitle() {
128         return title;
129     }
130 
131     public void setTitle(String title) {
132         this.title = title;
133     }
134 
135     /**
136      * Validate the properties that have been set from this HTTP request,
137      * and return an <code>ActionErrors</code> object that encapsulates any
138      * validation errors that have been found.  If no errors are found, return
139      * <code>null</code> or an <code>ActionErrors</code> object with no
140      * recorded error messages.<br>
141      *
142      * Uses <code>LibraryMgr.subjectExists()</code> to validate subjects
143      *
144      * @param mapping The mapping used to select this instance
145      * @param request The servlet request we are processing
146      */
147     public ActionErrors validate(ActionMapping mapping,
148      HttpServletRequest request) {
149 
150         ActionErrors errors = new ActionErrors();
151 
152         if ( selector.equals("Title") &&
153          ( title == null || title.length() < 1)) {
154             errors.add("title", new ActionMessage("error.book.title.required"));
155 
156             return errors;
157         }
158 
159         if ( selector.equals("Author") &&
160          (authorLastName == null || authorLastName.length() < 1)) {
161             errors.add("author", new ActionMessage("error.book.author.required"));
162 
163             return errors;
164         }
165 
166         // If this is a subject search, make sure we have at least one subject
167         if ( selector.equals("Subject") && subjects == null) {
168             errors.add("subjects",
169              new ActionMessage("error.book.search.subject.required"));
170 
171             return errors;
172         }
173 
174         return errors;
175     }
176 
177     /** Getter for property subjectList.
178      * @return Value of property subjectList.
179      */
180     public ArrayList<Subject> getSubjectList() {
181         return subjectList;
182     }
183 
184     /** Setter for property subjectList.
185      * @param subjectList New value of property subjectList.
186      */
187     public void setSubjectList(ArrayList<Subject> subjectList) {
188         this.subjectList = subjectList;
189     }
190 
191     public void setLocations(ArrayList<Location> locations) {
192         this.locations = locations;
193     }
194 
195     public ArrayList<Location> getLocations() {
196         return locations;
197     }
198 }