View Javadoc

1   package org.musicontroller.gui.edit;
2   
3   import java.util.Date;
4   import java.util.List;
5   
6   import org.apache.tapestry.IExternalPage;
7   import org.apache.tapestry.IRequestCycle;
8   import org.apache.tapestry.annotations.InjectObject;
9   import org.apache.tapestry.engine.ExternalServiceParameter;
10  import org.apache.tapestry.engine.IEngineService;
11  import org.apache.tapestry.engine.ILink;
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.Playlist;
17  import org.musicontroller.dao.BagAndKeywordUtils;
18  import org.musicontroller.security.User;
19  import org.musicontroller.service.McService;
20  
21  /**
22   * <p>Implements the MusiController screen Edit Playlist. The functions of this screen:
23   * <ul>
24   *   <li> Change the name of the playlist.
25   *   <li> Change the release date of the playlist.
26   *   <li> Change the names of the songs on the playlist.
27   *   <li> Change the lengths of the songs on the playlist.
28   *   <li> Change the keywords of the songs on the playlist.
29   *   <li> Add or Remove keywords to all songs at once.
30   *   <li> Add or Remove an artist as a performer to some or all of the songs.
31   *   <li> Change the cover art image of the playlist.
32   * </ul>
33   * <p>In additon, this screen provides:
34   * <ul>
35   *   <li> A link to the playlist info screen of the playlist.
36   *   <li> A button to request all songs in the playlist.
37   *   <li> A button to download a ZIP file with all songs in the playlist.
38   *   <li> A picture of the cover art.
39   * </ul>
40   * 
41   * @author Hans Drexler
42   * @version $Id: PlaylistEdit.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
43   */
44  public abstract class PlaylistEdit extends BasePage implements PageBeginRenderListener, PageEndRenderListener, IExternalPage {
45  
46  	public abstract long getPlaylistId();
47  	public abstract void setPlaylistId(long id);
48  	public abstract void setPlaylistName(String playlistname);
49  	public abstract void setPlaylistKeywords(String keywords);
50  	public abstract void setPlaylistReleasedate(Date releasedate);
51  	public abstract void setSongBeanList(List<SongBean> list);
52  	
53  	public abstract String getPlaylistName();
54  	public abstract String getPlaylistKeywords();
55  	public abstract Date getPlaylistReleasedate();
56  	public abstract List<SongBean> getSongBeanList();
57  	
58  	public abstract List<AiRelationBean> getAiRelationList();
59  	public abstract void setAiRelationList(List<AiRelationBean> relations);
60  
61  	public abstract String getArtistfirstname();
62  	public abstract String getArtistlastname();
63  	public abstract String getInstruments();
64  	public abstract String getTracks();
65  	public abstract void setArtistfirstname(String firstname);
66  	public abstract void setArtistlastname(String lastname);
67  	public abstract void setInstruments(String instruments);
68  	public abstract void setTracks(String tracks);
69  	
70  	public abstract List<PlaylistMergeBean> getMergePlaylistBeanList();
71  	public abstract void setMergePlaylistBeanList(List<PlaylistMergeBean> list);
72  		
73  	public abstract McService getMcService();
74  
75  	@InjectObject("engine-service:external")
76  	public abstract IEngineService getExternalService();
77  
78  	public abstract User getUser();
79  	
80  	private Playlist _playlist;
81  	
82  	/**
83  	 * The first parameter is always the playlist id. The second argument is optional.
84  	 * When supplied, it contains a <code>CoverArtSearchResult</code> that has been
85  	 * selected as the new cover art for this playlist. This argument can be ignored by
86  	 * this page.
87  	 */
88  	public void activateExternalPage(Object[] args, IRequestCycle cycle) {
89  		Long playlistid = (Long) args[0];
90  		setPlaylistId(playlistid);
91  	}
92  
93  	/**
94  	 * Prepare the page for rendering. 
95  	 * <ul>
96  	 * <li>Fills the page properties for the playlist name, band name, release date
97  	 * and keywords. 
98  	 * <li>Builds a list of SongBean objects filled with the id, songname, bandname, 
99  	 *     length and keyword list of each song. The list is saved as a page property. 
100 	 *     The user will be able to see and modify the values stored in the SongBean objects.
101 	 * <li>Builds a list of AiRelationBean objects for the artist-instrument relations that
102 	 *     are part of every song in the playlist.
103 	 * <li>Builds a list of PlaylistMergeBean objects, one for each playlist that has exactly
104 	 *     the same name as the playlist being edited. Each bean has a boolean property with 
105 	 *     which the user can indicate if the playslist should be merged with the playlist
106 	 *     being edited.   
107 	 * <li>Clear input fields that should always be reset before rendering.
108 	 * @param event The page event.
109 	 */
110 	public void pageBeginRender(PageEvent event) {
111 		Playlist playlist = getPlaylist();
112 		setPlaylistName(playlist.getName());
113 		setPlaylistReleasedate(playlist.getReleasedate());
114 		setPlaylistKeywords("");
115 		
116 		setSongBeanList(getMcService().constructSongBeanList(playlist));
117 
118 		// Create a list of song beans from the playlist and initialize the songBeanList.
119 		// Build the list of all artist-instrument relations of all songs in the playlist and
120 		// construct a list of AiRelationBean objects from that list.
121 		List<AiRelationBean> beanlist = BagAndKeywordUtils.buildAiRelationBeanList(playlist);
122 		setAiRelationList(beanlist);
123 		
124 		// Determine a list of playlists having the same name as this playlist. Remove the playlist
125 		// being edited from that list. Then, build a list of PlaylistMergeBeans for remaining playlists.
126 		setMergePlaylistBeanList(getMcService().constructMergeBeanList(playlist));
127 
128 		// Clear input fields that should always be empty.
129 		setArtistfirstname("");
130 		setArtistlastname("");
131 		setInstruments("");
132 		setTracks("");
133 	}
134 
135 	/**
136 	 * Clear the page variables just before the page returns to the page pool.
137 	 * @param event The page event.
138 	 */
139 	public void pageEndRender(PageEvent event) {
140 		_playlist = null;
141 	}
142 	
143 	/**
144 	 * Lazily loads the playlist using the playlist id.
145 	 * @return The playlist to edit.
146 	 */
147 	public Playlist getPlaylist() {
148 		long id = getPlaylistId();
149 		if(_playlist==null) {
150 			_playlist = getMcService().getPlaylistById(id);
151 		}
152 		return _playlist;
153 	}
154 	
155 	/**
156 	 * Save the changed playlist properties releaseDate, playlist name. Merges
157 	 * the playlists with another playlist of the same name if the user authorized it.
158 	 * @param cycle The Tapestry MVC cycle.
159 	 */
160 	public void save(IRequestCycle cycle) {
161 		getMcService().setPlaylistProperties(getPlaylist(),getPlaylistName(),getPlaylistReleasedate());
162 		getMcService().mergePlaylist(getPlaylist(), getUser(), getMergePlaylistBeanList());
163 	}
164 		
165 	/**
166 	 * Add the keywords entered by the user to all songs in the playlist.
167 	 * @param cycle The Tapestry MVC cycle.
168 	 */
169 	public void addKeywords(IRequestCycle cycle) {
170 		getMcService().addKeywordsToPlaylist(getPlaylist(), getPlaylistKeywords(),null);	}
171 
172 	/**
173 	 * Remove the keywords entered by the user from all songs in the playlist.
174 	 * @param cycle The Tapestry MVC cycle.
175 	 */
176 	public void removeKeywords(IRequestCycle cycle) {
177 		getMcService().removeKeywordsFromPlaylist(getPlaylist(), getPlaylistKeywords(),null);
178 	}
179 
180 	/**
181 	 * Cycle through the songs in the list of song beans and apply
182 	 * changes to the song name, song length and song keywords to each.
183 	 * 
184 	 * @param cycle The Tapestry MVC cycle.
185 	 */
186 	public void editSongs(IRequestCycle cycle) {
187 		List<SongBean> songbeans = getSongBeanList();
188 		getMcService().editSongsOfPlaylist(getPlaylist(),songbeans);
189 	}
190 		
191 	public void addMusician(IRequestCycle cycle) {
192 		getMcService().addMusician(getPlaylist(), getArtistfirstname(), getArtistlastname(), getInstruments(), getTracks());
193 	}
194 
195 	public void deleteMusician(IRequestCycle cycle) {
196 		getMcService().deleteMusician(getPlaylist(), getArtistfirstname(), getArtistlastname(), getInstruments(), getTracks());
197 	}
198 			
199 	/**
200 	 * Goto the EditCoverArt page. Pass the page 2 parameters. The first is the playlist id of the playlist being
201 	 * edited. The second is the name of the page the CoverArtEdit page should return to ("PlaylistEdit").
202 	 */
203 	public ILink editCoverArt(IRequestCycle cycle) {
204 		IEngineService service = getExternalService();
205 		ExternalServiceParameter parameter = new ExternalServiceParameter("CoverArtEdit", new Object[]{getPlaylist().getId(),"PlaylistEdit"});
206 		ILink link = service.getLink(false, parameter);
207 		return link;
208 	}
209 }