View Javadoc

1   /*
2    * Created on Jun 16, 2006
3    *
4    */
5   package org.musicontroller.gui.dynamic;
6   
7   import java.util.HashMap;
8   import java.util.LinkedList;
9   import java.util.List;
10  import java.util.Map;
11  
12  import org.apache.tapestry.event.PageBeginRenderListener;
13  import org.apache.tapestry.event.PageEndRenderListener;
14  import org.apache.tapestry.event.PageEvent;
15  import org.apache.tapestry.html.BasePage;
16  import org.musicontroller.core.Song;
17  import org.musicontroller.dao.Dao;
18  
19  public abstract class Suggestions extends BasePage {
20  	
21  	private Song _suggestion;
22  	private static final int MAX_SUGGESTIONS = 10;
23  	
24  	public abstract String getQuery();
25  	public abstract void setQuery(String query);
26  	public abstract Dao getDao();
27  	
28  	private List<Song> _suggestions;
29  	
30  	public Suggestions() {
31  		super();
32  		addPageBeginRenderListener(
33  			new PageBeginRenderListener() {
34  		        public void pageBeginRender(PageEvent e) {
35  		        	_suggestions = null;
36  		        }
37  			}		
38  		);
39  		
40  		this.addPageEndRenderListener(
41  			new PageEndRenderListener() {
42  				public void pageEndRender(PageEvent e) {
43  					_suggestions = null;
44  				}
45  			}
46  		);
47  	}
48  	
49  	@SuppressWarnings("unchecked")
50  	private List<Song> getSuggestionsListInternal(String query) {
51  		if (_suggestions == null) {
52  			if ("".equals(getQuery())) {
53  				_suggestions = new LinkedList<Song>();
54  			} else {
55  				Map<String,Object> params = new HashMap<String,Object>();
56  				params.put("name",getQuery().toUpperCase()+"%");
57  				_suggestions = getDao().search("from Song s where upper(s.name) like :name",params,MAX_SUGGESTIONS);
58  			}
59  		}
60  		return _suggestions;
61  	}
62  	
63  	public List<Song> getSuggestionsList() {
64  		return getSuggestionsListInternal(getQuery());
65  	}
66  	
67  	public Song getSuggestion() {
68  		return _suggestion;
69  	}
70  	public void setSuggestion(Song song) {
71  		_suggestion = song;
72  	}
73  	
74  }