root/head/ambra/webapp/src/main/java/org/topazproject/ambra/search2/SearchParameters.java @ 8264

Revision 8264, 12.0 KB (checked in by ssterling, 6 months ago)

On Advanced Search, now allow searches with Date Range as the only criteria.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id HeadURL Revision
Line 
1/*
2 * $HeadURL$
3 * $Id$
4 *
5 * Copyright (c) 2006-2010 by Topaz, Inc.
6 * http://topazproject.org
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21package org.topazproject.ambra.search2;
22
23import java.io.Serializable;
24import java.text.DateFormat;
25import java.text.ParseException;
26import java.text.SimpleDateFormat;
27import java.util.Arrays;
28import java.util.Date;
29import java.util.LinkedList;
30import java.util.List;
31
32/**
33 * Manage all of the parameters for a search.
34 * <p/>
35 * In every method that sets a String, a NULL is replaced with a String of zero length and all
36 * input in trimmed.
37 * <p/>
38 * In every method that sets a String Array, a NULL is replaced with an array with zero elements
39 * and every element is trimmed.  Elements that are trimmed to zero length are removed from the array.
40 * <p/>
41 * Dates are stored as Date objects, whether they are set as Strings or Dates.  Empty date Strings
42 * create NULL Date objects.
43 *
44 * TODO: The "query" parameter is used only by Simple Search, so replace all "query" references with "textSearchAll" in the UI, then remove "query" parameter from this class.
45 *
46 * @author Scott Sterling
47 */
48public class SearchParameters implements Serializable {
49
50  private static final long serialVersionUID = -7837640277704487921L;
51  private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
52
53  private String        query = "";
54  private String        textSearchAll = "";
55  private String        textSearchExactPhrase = "";
56  private String        textSearchAtLeastOne = "";
57  private String        textSearchWithout = "";
58  private String        textSearchOption = "";
59  private String[]      creator = {};
60  private String        authorNameOp = "";
61  private String        subjectCatOpt = "";
62  private String        dateTypeSelect = "";
63  private Date          startDate = null;
64  private Date          endDate = null;
65  private String[]      limitToCategory = {};
66  private String        journalOpt = ""//  Whether to limit a search to Journals in limitToJournal
67  // Only search these Journals. If no journals were selected, default to the current journal.
68  private String[]      limitToJournal = {};
69
70  //  Formatting elements
71  private int           startPage = 0;
72  private int           pageSize = 0;
73
74  // TODO: The "query" parameter is used only by Simple Search, so replace all "query" references with "textSearchAll" in the UI, then remove from this class.
75  public String getQuery() {
76    return query;
77  }
78  // TODO: The "query" parameter is used only by Simple Search, so replace all "query" references with "textSearchAll" in the UI, then remove from this class.
79  public void setQuery(String query) {
80    if (query == null || query.trim().length() < 1)
81      this.query = "";
82    else
83      this.query = query.trim();
84  }
85
86  public String[] getCreator() {
87    return creator;
88  }
89  public void setCreator(String[] creator) {
90    if (creator == null || creator.length < 1) {
91      this.creator = new String[]{};
92    } else {
93      List<String> creatorList = new LinkedList<String>();
94      for (String author : creator) {
95        if (author != null && author.trim().length() > 0)
96          creatorList.add(author.trim());
97      }
98      this.creator = new String[creatorList.size()];
99      int counter = 0;
100      for (String author : creatorList) {
101        this.creator[counter++] = author;
102      }
103    }
104  }
105
106  public String getAuthorNameOp() {
107    return authorNameOp;
108  }
109  public void setAuthorNameOp(String authorNameOp) {
110    if (authorNameOp == null || authorNameOp.trim().length() < 1)
111      this.authorNameOp = "";
112    else
113      this.authorNameOp = authorNameOp.trim();
114  }
115
116  public String getTextSearchAll() {
117    return textSearchAll;
118  }
119  public void setTextSearchAll(String textSearchAll) {
120    if (textSearchAll == null || textSearchAll.trim().length() < 1)
121      this.textSearchAll = "";
122    else
123      this.textSearchAll = textSearchAll.trim();
124  }
125
126  public String getTextSearchExactPhrase() {
127    return textSearchExactPhrase;
128  }
129  public void setTextSearchExactPhrase(String textSearchExactPhrase) {
130    if (textSearchExactPhrase == null || textSearchExactPhrase.trim().length() < 1)
131      this.textSearchExactPhrase = "";
132    else
133      this.textSearchExactPhrase = textSearchExactPhrase.trim();
134  }
135
136  public String getTextSearchAtLeastOne() {
137    return textSearchAtLeastOne;
138  }
139  public void setTextSearchAtLeastOne(String textSearchAtLeastOne) {
140    if (textSearchAtLeastOne == null || textSearchAtLeastOne.trim().length() < 1)
141      this.textSearchAtLeastOne = "";
142    else
143      this.textSearchAtLeastOne = textSearchAtLeastOne.trim();
144  }
145
146  public String getTextSearchWithout() {
147    return textSearchWithout;
148  }
149  public void setTextSearchWithout(String textSearchWithout) {
150    if (textSearchWithout == null || textSearchWithout.trim().length() < 1)
151      this.textSearchWithout = "";
152    else
153      this.textSearchWithout = textSearchWithout.trim();
154  }
155
156  public String getTextSearchOption() {
157    return textSearchOption;
158  }
159  public void setTextSearchOption(String textSearchOption) {
160    if (textSearchOption == null || textSearchOption.trim().length() < 1)
161      this.textSearchOption = "";
162    else
163      this.textSearchOption = textSearchOption.trim();
164  }
165
166  public String getSubjectCatOpt() {
167    return subjectCatOpt;
168  }
169  public void setSubjectCatOpt(String subjectCatOpt) {
170    if (subjectCatOpt == null || subjectCatOpt.trim().length() < 1)
171      this.subjectCatOpt = "";
172    else
173      this.subjectCatOpt = subjectCatOpt.trim();
174  }
175
176  public String getDateTypeSelect() {
177    return dateTypeSelect;
178  }
179  public void setDateTypeSelect(String dateTypeSelect) {
180    if (dateTypeSelect == null || dateTypeSelect.trim().length() < 1)
181      this.dateTypeSelect = "";
182    else
183      this.dateTypeSelect = dateTypeSelect.trim();
184  }
185
186  public Date getStartDate() {
187    return startDate;
188  }
189  public void setStartDate(Date startDate) {
190    this.startDate = startDate;
191  }
192  /**
193   * @return The startDate formatted as yyyy-MM-dd or a zero-length String if startDate is null
194   */
195  public String getStartDateAsString() {
196    if (startDate == null)
197      return "";
198    else
199      return dateFormat.format(startDate);
200  }
201  /**
202   * Set the startDate to the Date indicated or to null if startDateAsString is null or blank
203   *
204   * @param startDateAsString The startDate formatted as yyyy-MM-dd
205   * @throws ParseException Thrown if the startDateAsString is not formatted as yyyy-MM-dd
206   */
207  public void setStartDateAsString(String startDateAsString) throws ParseException {
208    if (startDateAsString == null || startDateAsString.trim().length() < 1)
209      this.startDate = null;
210    else
211      this.startDate = dateFormat.parse(startDateAsString.trim());
212  }
213
214  public Date getEndDate() {
215    return endDate;
216  }
217  public void setEndDate(Date endDate) {
218    this.endDate = endDate;
219  }
220  /**
221   * @return The endDate formatted as yyyy-MM-dd or a zero-length String if endDate is null
222   */
223  public String getEndDateAsString() {
224    if (endDate == null)
225      return "";
226    else
227      return dateFormat.format(endDate);
228  }
229  /**
230   * Set the endDate to the Date indicated or to null if endDateAsString is null or blank
231   *
232   * @param endDateAsString The endDate formatted as yyyy-MM-dd
233   * @throws ParseException Thrown if the endDateAsString is not formatted as yyyy-MM-dd
234   */
235  public void setEndDateAsString(String endDateAsString) throws ParseException {
236    if (endDateAsString == null || endDateAsString.trim().length() < 1)
237      this.endDate = null;
238    else
239      this.endDate = dateFormat.parse(endDateAsString.trim());
240  }
241
242  /**
243   * True if the startDate and endDate define a valid date range which can be used for searches
244   * @return true if the startDate and endDate define a valid date range which can be used for searches
245   */
246  public boolean isDateRangeValid() {
247    if (startDate == null || endDate == null || endDate.before(startDate))
248      return false;
249    else
250      return true;
251  }
252
253  public String[] getLimitToCategory() {
254    return limitToCategory;
255  }
256  public void setLimitToCategory(String[] limitToCategory) {
257    if (limitToCategory == null || limitToCategory.length < 1) {
258      this.limitToCategory = new String[]{};
259    } else {
260      List<String> limitToCategoryList = new LinkedList<String>();
261      for (String category : limitToCategory) {
262        if (category != null && category.trim().length() > 0)
263          limitToCategoryList.add(category.trim());
264      }
265      this.limitToCategory = new String[limitToCategoryList.size()];
266      int counter = 0;
267      for (String category : limitToCategoryList) {
268        this.limitToCategory[counter++] = category;
269      }
270    }
271  }
272
273  public String getJournalOpt() {
274    return journalOpt;
275  }
276  public void setJournalOpt(String journalOpt) {
277    if (journalOpt == null || journalOpt.trim().length() < 1)
278      this.journalOpt = "";
279    else
280      this.journalOpt = journalOpt.trim();
281  }
282
283  public String[] getLimitToJournal() {
284    return limitToJournal;
285  }
286  public void setLimitToJournal(String[] limitToJournal) {
287    if (limitToJournal == null || limitToJournal.length < 1) {
288      this.limitToJournal = new String[]{};
289    } else {
290      List<String> limitToJournalList = new LinkedList<String>();
291      for (String journal : limitToJournal) {
292        if (journal != null && journal.trim().length() > 0)
293          limitToJournalList.add(journal.trim());
294      }
295      this.limitToJournal = new String[limitToJournalList.size()];
296      int counter = 0;
297      for (String journal : limitToJournalList) {
298        this.limitToJournal[counter++] = journal;
299      }
300    }
301  }
302
303  public int getStartPage() {
304    return startPage;
305  }
306  public void setStartPage(int startPage) {
307    this.startPage = startPage;
308  }
309
310  public int getPageSize() {
311    return pageSize;
312  }
313  public void setPageSize(int pageSize) {
314    this.pageSize = pageSize;
315  }
316
317  /**
318   * Check if there are enough of the right kinds of terms to perform a search.
319   * Returns <code>true</code> if there are non-zero-length Strings in any of the creator,
320   * textSearchAll, textSearchAtLeastOne, textSearchExactPhrase, or textSearchWithout parameters.
321   *
322   * @return true if a search can be performed on the submitted terms
323   */
324  public boolean isContainsSearchTerms() {
325    if (getCreator().length > 0
326        || getTextSearchAll().length() > 0 || getTextSearchAtLeastOne().length() > 0
327        || getTextSearchExactPhrase().length() > 0 || getTextSearchWithout().length() > 0
328        || ( getStartDate() != null && getEndDate() != null && ! getStartDate().equals(getEndDate()) ) )
329      return true;
330    return false;
331  }
332
333  @Override
334  public String toString() {
335    return "SearchParameters{" +
336        "query='" + query + '\'' +
337        ", textSearchAll='" + textSearchAll + '\'' +
338        ", textSearchExactPhrase='" + textSearchExactPhrase + '\'' +
339        ", textSearchAtLeastOne='" + textSearchAtLeastOne + '\'' +
340        ", textSearchWithout='" + textSearchWithout + '\'' +
341        ", textSearchOption='" + textSearchOption + '\'' +
342        ", creator=" + (creator == null ? null : Arrays.asList(creator)) +
343        ", authorNameOp='" + authorNameOp + '\'' +
344        ", subjectCatOpt='" + subjectCatOpt + '\'' +
345        ", dateTypeSelect='" + dateTypeSelect + '\'' +
346        ", startDate=" + startDate +
347        ", endDate=" + endDate +
348        ", limitToCategory=" + (limitToCategory == null ? null : Arrays.asList(limitToCategory)) +
349        ", journalOpt='" + journalOpt + '\'' +
350        ", limitToJournal=" + (limitToJournal == null ? null : Arrays.asList(limitToJournal)) +
351        ", startPage=" + startPage +
352        ", pageSize=" + pageSize +
353        '}';
354  }
355}
Note: See TracBrowser for help on using the browser.