View Javadoc

1   package org.musicontroller.gui;
2   
3   import java.text.Format;
4   import java.text.SimpleDateFormat;
5   
6   import org.apache.tapestry.IExternalPage;
7   import org.apache.tapestry.IRequestCycle;
8   import org.apache.tapestry.event.PageBeginRenderListener;
9   import org.apache.tapestry.event.PageEndRenderListener;
10  import org.apache.tapestry.event.PageEvent;
11  import org.apache.tapestry.html.BasePage;
12  import org.musicontroller.core.Playlist;
13  import org.musicontroller.dao.Dao;
14  import org.musicontroller.dao.PlaylistKey;
15  import org.musicontroller.security.User;
16  
17  public abstract class PlaylistInfo extends BasePage implements IExternalPage {
18  	public abstract Long getPlaylistId();
19  	public abstract void setPlaylistId(Long id);
20  	public abstract Long getKeywordId();
21  	public abstract void setKeywordId(Long id);
22  	
23  	public abstract Dao getDao();
24  	public abstract User getUser();
25  	
26  	private Playlist _playlist;
27  	
28  	public PlaylistInfo() {
29  		super();
30  		addPageBeginRenderListener(
31  			new PageBeginRenderListener() {
32  		        public void pageBeginRender(PageEvent e) {
33  		        	_playlist = null;
34  		        }
35  			}		
36  		);
37  		
38  		this.addPageEndRenderListener(
39  			new PageEndRenderListener() {
40  				public void pageEndRender(PageEvent e) {
41  					_playlist = null;
42  				}
43  			}
44  		);
45  	}
46  	
47  	/**
48  	 * Used when rendering the page
49  	 * @return The Playlist
50  	 */
51  	public Playlist getPlaylist() {
52  		if (_playlist==null) {
53  			if (getPlaylistId()!=null) {
54  				_playlist = getDao().getPlaylistById(getPlaylistId(),getUser());
55  			}
56  			
57  			if (getKeywordId()!=null) {
58  				_playlist = getDao().songsByKeyword(getKeywordId());
59  			}
60  		}
61  		return _playlist;
62  	}
63  
64  	private Format _dateFormat;
65  	
66  	public Format getDateformat() {
67  		if(_dateFormat==null) {
68  			_dateFormat =  new SimpleDateFormat("dd-MM-yyyy");
69  		}	
70  		return _dateFormat;
71  	}
72  	
73  	/**
74  	 * Returns the name of the playlist, or "No playlist" if no playlist is set.
75  	 * @return The name of the playlist, or "No playlist".
76  	 */
77  	public String getName() {
78  		Playlist list = getPlaylist();
79  		return list==null ? "No playlist" : list.getName();
80  	}
81  
82  	/**
83  	 * Returns the release date of the playlist, or "No playlist" if no playlist is set.
84  	 * @return The release date of the playlist, or "No playlist".
85  	 */
86  	public String getReleasedate() {
87  		Playlist list = getPlaylist();
88  		return list==null ? "No playlist" : list.getFormattedReleasedate();
89  	}
90  
91  	/**
92  	 * Takes a Long or an Integer and returns the Long assiciated with it, or null if the parameter was null.
93  	 * @param o The Object
94  	 * @return The Longvalue
95  	 */
96  	private Long extractLongFromObject(Object o) {
97  		if (o==null) return null;
98  		if (o instanceof Integer) {
99  			Integer i = (Integer) o;
100 			return Long.valueOf(i.longValue());
101 		}
102 		if (o instanceof Long) {
103 			return (Long) o;
104 		}
105 		return null;
106 	}
107 	
108 	public void activateExternalPage(Object[] args, IRequestCycle cycle) { 
109 		Long playlistid = extractLongFromObject(args[0]);
110 		Long keywordid = extractLongFromObject(args[1]);
111 		setPlaylistId(playlistid);
112 		setKeywordId(keywordid);
113 	}
114 	
115 	//TODO UI-placement of the nextmonth and previousmonth-links should be better
116 	
117 	/**
118 	 * Return id that is the previous month of the current id
119 	 */
120 	public long getPreviousMonth() {
121 		PlaylistKey key = new PlaylistKey(getPlaylistId());
122 		key.substractMonth();
123 		return key.getId();
124 	}
125 
126 	/**
127 	 * Return id that is the next month of the current id
128 	 */
129 	public long getNextMonth() {
130 		PlaylistKey key = new PlaylistKey(getPlaylistId());
131 		key.addMonth();
132 		return key.getId();
133 	}
134 
135 	public boolean isFiltered() {
136 		if (getPlaylistId()==null) {
137 			return false;
138 		}
139 		return new PlaylistKey(getPlaylistId()).getFilter()!=null;
140 	}
141 	
142 	public boolean isSpecial() {
143 		if (getPlaylistId()==null) {
144 			return true;
145 		}
146 		return new PlaylistKey(getPlaylistId()).isSpecial();
147 	}
148 	
149 }