KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > communication > forum > component > UIAdvancedSearch


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.communication.forum.component;
6
7 import java.util.*;
8 import org.exoplatform.faces.core.component.UIDateInput;
9 import org.exoplatform.faces.core.component.UISelectBox;
10 import org.exoplatform.faces.core.component.UISimpleForm;
11 import org.exoplatform.faces.core.component.UIStringInput;
12 import org.exoplatform.faces.core.component.model.*;
13 import org.exoplatform.faces.core.event.ExoActionEvent;
14 import org.exoplatform.faces.core.event.ExoActionListener;
15 import org.exoplatform.services.communication.forum.Category;
16 import org.exoplatform.services.communication.forum.Forum;
17 import org.exoplatform.services.communication.forum.ForumIndexerPlugin;
18 import org.exoplatform.services.communication.forum.ForumService;
19 import org.exoplatform.services.indexing.RangeFieldSearchInput;
20 import org.exoplatform.services.indexing.SingleFieldSearchInput;
21 /**
22  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
23  * @since Aug 27, 2004
24  * @version $Id: UISearch.java,v 1.4 2004/10/20 18:46:31 benjmestrallet Exp $
25  */

26 public class UIAdvancedSearch extends UISimpleForm {
27   static final public String JavaDoc SEARCH_ACTION = "search" ;
28   
29   private UISelectBox uiTargetInput_ ;
30   private UIStringInput subjectInput_ ;
31   private UIStringInput bodyInput_ ;
32   private UIStringInput authorInput_ ;
33   private UIDateInput uiFromDateImput_ ;
34   private UIDateInput uiToDateImput_ ;
35   
36   public UIAdvancedSearch(ForumService fservice) throws Exception JavaDoc {
37     super("searchForm", "post", null) ;
38     setId("UIAdvancedSearch") ;
39     setClazz("UIAdvancedSearch");
40     
41     List categories = fservice.getCategories();
42     List options = new ArrayList() ;
43     options.add(new SelectItem("All Categories", ""));
44     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
45     for(int i = 0; i < categories.size() ; i++) {
46       b.setLength(0) ;
47       Category category = (Category) categories.get(i) ;
48       b.append("category:").append(category.getId()) ;
49       options.add(new SelectItem("<<<< " + category.getCategoryName() + " >>>>", b.toString()));
50       List forums = fservice.getForums(category.getId()) ;
51       for(int j = 0 ; j < forums.size(); j++) {
52         b.setLength(0) ;
53         Forum forum = (Forum) forums.get(j) ;
54         b.append("forum:").append(forum.getId()) ;
55         options.add(new SelectItem(forum.getForumName(), b.toString()));
56       }
57     }
58     uiTargetInput_ = new UISelectBox("target", "", options);
59     subjectInput_ = new UIStringInput("subject", "") ;
60     bodyInput_ = new UIStringInput("body", "") ;
61     authorInput_ = new UIStringInput("author", "") ;
62     uiFromDateImput_ = new UIDateInput("fromDate", new GregorianCalendar(2004, 0, 1).getTime() ) ;
63     uiToDateImput_ = new UIDateInput("toDate", new Date()) ;
64     
65     addActionListener(SearchActionListener.class, SEARCH_ACTION) ;
66     
67     add(new HeaderRow().
68         add(new Cell("#{UISearch.header.search}").addColspan("2")));
69     add(new Row().
70         add(new LabelCell("#{UISearch.label.search-in}")).
71         add(new ComponentCell(this, uiTargetInput_).addClazz("search-in")));
72     add(new Row().
73         add(new LabelCell("#{UISearch.label.search-subject}")).
74         add(new ComponentCell(this, subjectInput_)));
75     add(new Row().
76         add(new LabelCell("#{UISearch.label.search-body}")).
77         add(new ComponentCell(this, bodyInput_)));
78     add(new Row().
79         add(new LabelCell("#{UISearch.label.search-author}")).
80         add(new ComponentCell(this, authorInput_)));
81     add(new Row().
82         add(new LabelCell("#{UISearch.label.search-date}")).
83         add(new ListComponentCell().
84             add(this, uiFromDateImput_).
85             add("#{UISearch.label.to}").
86             add(this, uiToDateImput_))) ;
87     
88     add(new Row().
89         add(new ListComponentCell().
90             add(new FormButton("#{UISearch.button.search}", SEARCH_ACTION)).
91             addColspan("2").addAlign("center"))) ;
92   }
93   
94   static public class SearchActionListener extends ExoActionListener {
95     public void execute(ExoActionEvent event) throws Exception JavaDoc {
96       UIAdvancedSearch uiForm = (UIAdvancedSearch) event.getSource();
97       List inputs = new ArrayList() ;
98       String JavaDoc value = uiForm.uiTargetInput_.getValue() ;
99       if(value.length() > 0) {
100         if(value.startsWith("category:")) {
101           value = value.substring("category:".length() , value.length()) ;
102           inputs.add(new SingleFieldSearchInput(ForumIndexerPlugin.CATEGORY_ID_FIELD, value)) ;
103         } else if(value.startsWith("forum:")) {
104           value = value.substring("forum:".length() , value.length()) ;
105           inputs.add(new SingleFieldSearchInput(ForumIndexerPlugin.FORUM_ID_FIELD, value)) ;
106         }
107       }
108       value = uiForm.subjectInput_.getValue() ;
109       if(value.length() > 0) {
110         inputs.add(new SingleFieldSearchInput(ForumIndexerPlugin.SUBJECT_FIELD, value)) ;
111       }
112       
113       value = uiForm.bodyInput_.getValue() ;
114       if(value.length() > 0) {
115         inputs.add(new SingleFieldSearchInput(ForumIndexerPlugin.BODY_FIELD, value)) ;
116       }
117       value = uiForm.authorInput_.getValue() ;
118       if(value.length() > 0) {
119         inputs.add(new SingleFieldSearchInput(ForumIndexerPlugin.AUTHOR_FIELD, value)) ;
120       }
121       
122       Date from = uiForm.uiFromDateImput_.getValue() ;
123       Date to = uiForm.uiToDateImput_.getValue() ;
124       inputs.add(new RangeFieldSearchInput(ForumIndexerPlugin.DATE_FIELD, from, to)) ;
125       
126       UIForumSearcher uiSearcher = (UIForumSearcher) uiForm.getAncestorOfType(UIForumSearcher.class) ;
127       uiSearcher.advancedSearch(inputs, null) ;
128     }
129   }
130 }
Popular Tags