View Javadoc

1   package org.musicontroller.gui;
2   
3   import java.io.IOException;
4   import java.util.List;
5   import java.util.Map;
6   import java.util.TreeMap;
7   
8   import org.apache.log4j.Logger;
9   import org.apache.tapestry.IExternalPage;
10  import org.apache.tapestry.IRequestCycle;
11  import org.apache.tapestry.event.PageBeginRenderListener;
12  import org.apache.tapestry.event.PageEndRenderListener;
13  import org.apache.tapestry.event.PageEvent;
14  import org.apache.tapestry.html.BasePage;
15  import org.farng.mp3.MP3File;
16  import org.farng.mp3.TagException;
17  import org.musicontroller.core.Playlist;
18  import org.musicontroller.core.Song;
19  import org.musicontroller.security.User;
20  import org.musicontroller.service.McService;
21  import org.varienaja.util.DateTools;
22  
23  public abstract class SongInfo extends BasePage implements IExternalPage {
24  	private static final Logger LOG = Logger.getLogger(SongInfo.class);
25  	
26  	public abstract Long getSongId();
27  	public abstract void setSongId(Long id);
28  	
29  	public abstract int getBitrate();
30  	public abstract void setBitrate(int br);
31  	
32  	public abstract User getUser();
33  	
34  	private Song _song;
35  	private Map<Integer, Integer> _bitrates;
36  	private int _maxFramecount;
37  	
38  	public abstract McService getMcService();
39  	
40  	public SongInfo() {
41  		super();
42  		addPageBeginRenderListener(
43  			new PageBeginRenderListener() {
44  		        public void pageBeginRender(PageEvent e) {
45  		        	_song = null;
46  		        	_bitrates = null;
47  		        }
48  			}		
49  		);
50  		
51  		this.addPageEndRenderListener(
52  			new PageEndRenderListener() {
53  				public void pageEndRender(PageEvent e) {
54  					_song = null;
55  					_bitrates = null;
56  				}
57  			}
58  		);
59  	}
60  	
61  	public void activateExternalPage(Object[] args, IRequestCycle cycle) {
62  		Long songid = (Long) args[0];
63  		setSongId(songid);
64  	}
65  	
66  	public Song getSong() {
67  		if (_song==null) {
68  			_song = getMcService().getSongById(getSongId());
69  			LOG.debug("Queried song");
70  		} else {
71  			LOG.debug("Got song from cache.");
72  		}
73  		return _song;
74  	}
75  
76  	/**
77  	 * Returns <tt>true</tt> if this songs has at least 1 neighbour or false otherwise.
78       * TODO expensive query performed twice?
79  	 * @return <tt>true</tt> if this songs has at least 1 neighbour or false otherwise.
80  	 */
81  	public boolean hasNeighbours() {
82  		return getMcService().getSongNeighbours(getSong(),getUser()).getSongs().size()>0;
83  	}
84  	
85  	/**
86  	 * Returns a playlist with all song neighbours of the song displayed.
87  	 * @return A Playlist containing all songs neighboring the song displayed.
88  	 */
89  	public Playlist getNeighbours() {
90  		return getMcService().getSongNeighbours(getSong(),getUser());
91  	}
92  	
93  	public List<Playlist> getPlaylists() {
94  		return getMcService().getSongPlaylists(getSong());
95  	}
96  
97  	public String getInserted() {
98  		return DateTools.formatTimestamp(getSong().getInserted());
99  	}
100 	
101 	public Integer[] getBitrates() {
102 		if (_bitrates == null) {
103 			_bitrates = new TreeMap<Integer, Integer>();
104 			try {
105 				if (!getSong().getLink().getFile().exists()) {
106 					LOG.error("File does not exist: " + getSong().getLink().getUrl());
107 					throw new IOException("File does not exist, cannot extract MP3info!");
108 				}
109 				MP3File file = new MP3File(getSong().getLink().getFile().getAbsolutePath());
110 				int[] rates = file.getFramecountPerBitrate();
111 				_bitrates.put(32, rates[0]);
112 				_bitrates.put(40, rates[1]);
113 				_bitrates.put(48, rates[2]);
114 				_bitrates.put(56, rates[3]);
115 				_bitrates.put(64, rates[4]);
116 				_bitrates.put(80, rates[5]);
117 				_bitrates.put(96, rates[6]);
118 				_bitrates.put(112, rates[7]);
119 				_bitrates.put(128, rates[8]);
120 				_bitrates.put(160, rates[9]);
121 				_bitrates.put(192, rates[10]);
122 				_bitrates.put(224, rates[11]);
123 				_bitrates.put(256, rates[12]);
124 				_bitrates.put(320, rates[13]);
125 				
126 				_maxFramecount = 1;
127 				for (int i=0; i<rates.length; i++) {
128 					if (rates[i]>_maxFramecount) {
129 						_maxFramecount = rates[i];
130 					}
131 				}
132 			} catch (IOException e) {
133 				LOG.error("Error during bitrate-extraction: " + e);
134 			} catch (TagException e) {
135 				LOG.error("Error during bitrate-extraction: " + e);
136 			}
137 		}
138 		Integer[] result = new Integer[0];
139 		return _bitrates.keySet().toArray(result); 
140 	}
141 	
142 	public String getFramecount() {
143 		return Integer.toString(_bitrates.get(getBitrate()));
144 	}
145 	
146 	public String getSize() {
147 		int sze = _bitrates.get(getBitrate());
148 		
149 		int pct = 200*sze / _maxFramecount; //Make sure max size is 200.
150 		
151 		return Integer.toString(pct);
152 	}
153 	
154 	
155 }