View Javadoc

1   package org.musicontroller.gui.edit;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.util.Collection;
9   
10  import org.apache.tapestry.IExternalPage;
11  import org.apache.tapestry.IRequestCycle;
12  import org.apache.tapestry.annotations.InjectObject;
13  import org.apache.tapestry.engine.ExternalServiceParameter;
14  import org.apache.tapestry.engine.IEngineService;
15  import org.apache.tapestry.engine.ILink;
16  import org.apache.tapestry.html.BasePage;
17  import org.apache.tapestry.request.IUploadFile;
18  import org.musicontroller.core.Playlist;
19  import org.musicontroller.service.McService;
20  import org.varienaja.util.coverart.CoverArtManager;
21  import org.varienaja.util.coverart.CoverArtSearchResult;
22  import org.varienaja.util.coverart.ImageScaler;
23  
24  /**
25   * <p>Implements the MusiController cover art edit screen. The functions of this screen:
26   * <ul>
27   *   <li> Change the cover art of a playlist, by selecting the new cover art image from a list.
28   *   <li> Upload a cover art image for a playlist. The uploaded image becomes the new cover art image.
29   * </ul>
30   * <p>In additon, this screen provides:
31   * <ul>
32   *   <li> A link to the playlist edit screen of the playlist.
33   *   <li> A picture of the current cover art.
34   * </ul>
35   * <p>This page receives parameters as External page link parameters. These are:
36   * <ol>
37   * <li>The playlistid, this can be null in case of a non-persistent playlist. If not null,
38   *     the search criteria for the cover art search are queried from this playlist.
39   * <li>The name of the return page. This page will render the named page when a cover is
40   *     selected.
41   * <li>A band name, and -
42   * <li>A playlist name. The band name and playlist name are used as search criteria if the playlist
43   *     id is null.
44   * </ol>
45   * 
46   * @author Hans Drexler
47   * @version $Id: CoverArtEdit.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
48   */
49  public abstract class CoverArtEdit extends BasePage implements IExternalPage {
50  	/**
51  	 * The maximum width of displayed images.
52  	 */
53  	private final static int MAXDISPLAYWIDTH=600;
54  
55  	public abstract long getPlaylistId();
56  	public abstract void setPlaylistId(long id);
57  	public abstract void setPlaylistName(String playlistname);
58  	public abstract String getPlaylistName();
59  	public abstract void setBandName(String bandname);
60  	public abstract String getBandName(); 
61  	public abstract Playlist getPlaylist();
62  	public abstract void setPlaylist(Playlist playlist);
63  	public abstract String getReturnPage();
64  	public abstract void setReturnPage(String returnPage);
65  	public abstract IUploadFile getUploadFile();
66  	public abstract McService getMcService();
67  	public abstract void setCoverArtCandidate(CoverArtSearchResult index);
68  	public abstract CoverArtSearchResult getCoverArtCandidate();
69  
70  	@InjectObject("engine-service:external")
71  	public abstract IEngineService getExternalService();
72  
73  	/**
74  	 * Interpret the parameters passed to this page. The page has 2 parameters:
75  	 * <ul>
76  	 * <li>playlistid - The Id of the playlist whose cover art is edited. This parameter
77  	 *                  can be null, in whcih case the playlist is not persistent (yet).
78  	 * <li>returnpage - This is the name of the tapestry page to use as return page.
79  	 * </ul>
80  	 */
81  	public void activateExternalPage(Object[] args, IRequestCycle cycle) {
82  		long playlistid = -1;
83  		if(args[0]==null) {
84  			// The playlist is not persistent (yet). Don't use the database!
85  			Playlist transientPlaylist = new Playlist();
86  			transientPlaylist.setId(playlistid);
87  			setPlaylist(transientPlaylist);
88  			// If the playlist id is null, then the third and fourth parameter can specify the search criteria.
89  			if(args.length>2 && args[2]!=null) {
90  				setBandName((String)args[2]);
91  			}
92  			if(args.length>3 && args[3]!=null) {
93  				setPlaylistName((String)args[3]);
94  				getPlaylist().setName(getPlaylistName());
95  			}
96  		} else {
97  			playlistid = (Long) args[0];			
98  			setPlaylist(getMcService().getPlaylistById(playlistid));
99  		}
100 		String returnPage = (String) args[1];
101 		setPlaylistId(playlistid);
102 		setReturnPage(returnPage);
103 	}
104 
105 	/**
106 	 * The user uploads a cover art image.
107 	 *
108 	 */
109 	public void uploadCoverArt() {
110 		if (getUploadFile() == null) {
111 			return;
112 		}
113 
114 		InputStream fis = getUploadFile().getStream();
115 		FileOutputStream fos = null;
116 
117 		try {
118 			fos = new FileOutputStream(new File(getUploadFile().getFileName()));
119 			byte[] buffer = new byte[1024];
120 			while (true) {
121 				int length = fis.read(buffer);
122 				if (length < 0) {
123 					break;
124 				}
125 				fos.write(buffer, 0, length);
126 			}
127 			fis.close();
128 			fos.close();
129 			File uploaded = new File(getUploadFile().getFileName());
130 			CoverArtManager.purge(getPlaylistId());
131 			CoverArtManager.add(getPlaylistId(), new FileInputStream(uploaded));
132 		} catch (IOException ioe) {
133 			ioe.printStackTrace();
134 		} finally {
135 			if (fis != null) {
136 				try {
137 					fis.close();
138 				} catch (IOException ioe) {
139 				}
140 			}
141 			if (fos != null) {
142 				try {
143 					fos.close();
144 				} catch (IOException ioe) {
145 				}
146 			}
147 		}
148 	}
149 	
150 	/**
151 	 * Ask the Mc Service to return a list of possible cover art for this
152 	 * playlist.
153 	 * 
154 	 * @return A list of cover art candidates for persistent playlists.
155 	 */
156 	public Collection<CoverArtSearchResult> getCoverArtList() {
157 		if (isPersistentPlaylist()) {
158 			return getMcService().getCoverArtList(getPlaylist());
159 		} else {
160 			String bandname = null;
161 			if(isPersistentPlaylist()) {
162 		    	try {
163 		    		bandname = getPlaylist().getSongs().iterator().next().getSong().getBand().getName();
164 		    	} catch (Exception e) {
165 		    		bandname = "Various";
166 		    	}
167 			} else {
168 				bandname = getBandName();
169 			}
170 			return getMcService().getCoverArtList(bandname, getPlaylist().getName());
171 		}
172 	}
173 
174 	/**
175 	 * Set the selected image as the cover art image for the playlist if the playlist is persistent.  
176 	 * <p>Returns to the page passed to coverArtedit by the <code>returnPage</code> property. Sets the <code>playlistId</code>
177 	 * and the <code>coverArt</code> property on the return page before returning. 
178 	 * 
179 	 * @param cycle The Tapestry MVC cycle.
180 	 * @param selected The selected cover art image.
181 	 */
182 	public ILink selectCoverArt(IRequestCycle cycle, CoverArtSearchResult selected) {
183 		try {
184 			if(isPersistentPlaylist()) {
185 				getMcService().setPlaylistCoverArt(getPlaylist(),selected);
186 			}
187 			if(getReturnPage()!=null) {
188 				IEngineService service = getExternalService();
189 				ExternalServiceParameter parameter = new ExternalServiceParameter(getReturnPage(), new Object[]{getPlaylist().getId(),selected,getPlaylist().getName()});
190 				ILink link = service.getLink(false, parameter);
191 				return link;
192 			}
193 		} catch (IOException e) {
194 			// TODO: Inform the user of the problem that has occured.
195 		}
196 		return null;
197 	}
198 	
199 	/**
200 	 * Tests if a persistent playlist is loaded in this page.
201 	 * @return True if a persistent playlist is loaded, otherwise false.
202 	 */
203 	public boolean isPersistentPlaylist() {
204 		return getPlaylistId()>=0;
205 	}
206 	
207 	/**
208 	 * @return The height for the currently displayed searchresult, taking into
209 	 * account that the maximum width is 400px.
210 	 */
211 	public int getHeight() {
212 		CoverArtSearchResult casr = getCoverArtCandidate();
213 		if (casr.getWidth()>MAXDISPLAYWIDTH) {
214 			return ImageScaler.calculateNewDimension(casr.getWidth(), casr.getHeight(), MAXDISPLAYWIDTH).height;
215 		} else {
216 			return casr.getHeight();
217 		}
218 	}
219 
220 	/**
221 	 * @return The width for the currently displayed searchresult, taking into
222 	 * account that the maximum width is 400px.
223 	 */
224 	public int getWidth() {
225 		CoverArtSearchResult casr = getCoverArtCandidate();
226 		if (casr.getWidth()>MAXDISPLAYWIDTH) {
227 			return ImageScaler.calculateNewDimension(casr.getWidth(), casr.getHeight(), MAXDISPLAYWIDTH).width;
228 		} else {
229 			return casr.getWidth();
230 		}
231 	}
232 
233 }