View Javadoc

1   package org.musicontroller.gui;
2   
3   import java.io.IOException;
4   import java.util.List;
5   
6   import org.apache.lucene.queryParser.ParseException;
7   import org.apache.tapestry.IExternalPage;
8   import org.apache.tapestry.IRequestCycle;
9   import org.apache.tapestry.html.BasePage;
10  import org.musicontroller.core.Artist;
11  import org.musicontroller.core.Band;
12  import org.musicontroller.core.Keyword;
13  import org.musicontroller.core.Playlist;
14  import org.musicontroller.core.Song;
15  import org.musicontroller.core.searching.ISearcher;
16  import org.musicontroller.core.searching.Item;
17  import org.musicontroller.core.searching.MCSearcher;
18  import org.musicontroller.dao.Dao;
19  import org.musicontroller.security.User;
20  
21  /**
22   * Search-page.
23   * @author Arjan Verstoep
24   * @version $Id: Search.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
25   */
26  public abstract class Search extends BasePage implements IExternalPage {
27  	public abstract User getUser();
28  	public abstract Dao getDao();
29  	
30  	public abstract String getFeedback();
31  	public abstract void setFeedback(String feedback);
32  	
33  	public abstract List<Item> getItems();
34  	public abstract void setItems(List<Item> items);
35  	
36  	public abstract Band getTheband();
37  	public abstract void setTheband(Band band);
38  
39  	public abstract Song getSong();
40  	public abstract void setSong(Song song);
41  	
42  	public abstract Playlist getPlaylist();
43  	public abstract void setPlaylist(Playlist playlist);
44  	
45  	public abstract Keyword getKeyword();
46  	public abstract void setKeyword(Keyword keyword);
47  
48  	public abstract Artist getArtist();
49  	public abstract void setArtist(Artist artist);
50  
51  	private Item _item;
52  	private String _query;
53  	
54  	public void activateExternalPage(Object[] args, IRequestCycle cycle) {
55  		String query = (String) args[0];
56  		setSearchQuery(query);
57  	
58  		doSearch();
59  	}
60  	
61  	public String getSearchQuery() {
62  		return _query;
63  	}
64  	public void setSearchQuery(String searchquery) {
65  		_query = searchquery;
66  		doSearch();
67  	}
68  	
69  	public void doSearch() {
70  		ISearcher searcher = new MCSearcher();
71  		try {
72  			String qry = getSearchQuery();
73  			if (qry==null || "".equals(qry)) {
74  				setFeedback("Enter a query!");
75  			} else {
76  				String loginname = getUser()==null ? null : getUser().getLoginname(); 
77  				setItems(searcher.searchGeneral(loginname, qry));
78  				if (getItems().size()==0) {
79  					setFeedback("Nothing found...");
80  				} else {
81  					setFeedback(null);
82  				}
83  			}
84  		} catch (IOException e) {
85  			setFeedback("The searchindex could not be accessed: "+e);
86  		} catch (ParseException e) {
87  			setFeedback("Could not parse searchquery: "+e);
88  		} catch (Exception e) {
89  			setFeedback("Error: "+e);
90  		}
91  	}
92  
93  	public Item getItem() {
94  		return _item;
95  	}
96  	
97  	/**
98  	 * Sets the loop-iterator.
99  	 * @param item The loop-variable. If this variable points to an item that does
100 	 * not exist anymore in the database, the loop-variable is modified. The 
101 	 * setNullItem()-method is called. This has as an effect that the GUI will not
102 	 * try to render a null keyword,band,song,... which would result in a
103 	 * nullpointerexception and a Tapestry-stacktrace.
104 	 */
105 	public void setItem(Item item) {
106 		_item = item;
107 		if (_item.getItemKind().equals("song")) {
108 			Song song = getDao().getSongById(_item.getItemId());
109 			if (song==null) {
110 				_item.setNullItem();
111 			} else {
112 				setSong(song);
113 			}
114 		} else if (_item.getItemKind().equals("band")) {
115 			Band band = getDao().getBandById(_item.getItemId());
116 			if (band==null) {
117 				_item.setNullItem();
118 			} else {
119 				setTheband(band);
120 			}
121 		} else if (_item.getItemKind().equals("playlist")) {
122 			Playlist playlist = getDao().getPlaylistById(_item.getItemId(),null);
123 			if (playlist==null) {
124 				_item.setNullItem();
125 			} else {
126 				setPlaylist(playlist);
127 			}
128 		} else if (_item.getItemKind().equals("keyword")) {
129 			Keyword keyword = getDao().getKeywordById(_item.getItemId());
130 			if (keyword==null) {
131 				_item.setNullItem();
132 			} else {
133 				setKeyword(keyword);
134 			}
135 		} else if (_item.getItemKind().equals("artist")) {
136 			Artist artist = getDao().getArtistById(_item.getItemId());
137 			if (artist==null) {
138 				_item.setNullItem();
139 			} else {
140 				setArtist(artist);
141 			}
142 		}
143 	}
144 	
145 }