View Javadoc

1   /*
2    * Created on Jun 20, 2005
3    *
4    */
5   package org.musicontroller.gui.components;
6   
7   import java.util.Date;
8   import java.util.List;
9   import java.util.Set;
10  
11  import org.apache.tapestry.BaseComponent;
12  import org.musicontroller.core.Contract_PS;
13  import org.musicontroller.core.Event;
14  import org.musicontroller.core.Playlist;
15  import org.musicontroller.core.Song;
16  import org.musicontroller.dao.Dao;
17  import org.musicontroller.security.User;
18  import org.musicontroller.service.McService;
19  import org.varienaja.util.DateTools;
20  
21  /**
22   * The component to show a list of Songs. Song-lists are very verbose. Because
23   * Hibernate loads everything lazily, it can very well be that we generate an
24   * OutOfMemoryException, while traversing through the SongList, initializing
25   * lazy-properties on our way. Because of that, this component throws objects
26   * away after rendering them. Note that this prevents OOME on huge lists, but
27   * penalizes the rendering of lists as well, because sometimes objects have
28   * to be fetched from the DB (or hopefully a 2nd-level cache) more than once.
29   * @author Varienaja
30   */
31  public abstract class SongList extends BaseComponent {
32  	public abstract User getUser();
33  	public abstract Dao getDao();
34  	
35  	public abstract Playlist getList();
36  	public abstract void setList(Playlist playlist);
37  	
38  	public abstract boolean getShowbands();
39  	
40  	public abstract Contract_PS getPlaylistcontract();
41  	public abstract void setPlaylistcontract(Contract_PS contract);
42  	
43  	public abstract int getIndex();
44  	public abstract void setIndex(int index);
45  	
46  	/**
47  	 * Getter for the MusiController services. This is injected by Spring.
48  	 * @return The Musicontroller service.
49  	 */
50  	public abstract McService getMcService();
51  	
52  	public Set<Contract_PS> getSongs() {
53  		return getList().getSongs();
54  	}
55  	
56  	public Song getSong() {
57  		return getPlaylistcontract().getSong();
58  	}
59  	
60  	private String getEventcount(Song song, User user, int kind, Date filterbegin, Date filterend) {
61  		int count = song.getEventcount(user,kind);
62  		String result = Integer.toString(count);
63  		if (filterbegin!=null) {
64  			count = song.getEventCount(user,kind,filterbegin,filterend);
65  			result += " ("+Integer.toString(count)+")";
66  		}
67  		return result;
68  	}
69  	
70  	public String getPlaycount() {
71  		return getEventcount(getSong(),getUser(),Event.played,getList().getFilterBegin(),getList().getFilterEnd());
72  	}
73  	
74  	public String getRequestcount() {
75  		return getEventcount(getSong(),getUser(),Event.requested,getList().getFilterBegin(),getList().getFilterEnd());
76  	}
77  	
78  	public String getSkipcount() {
79  		return getEventcount(getSong(),getUser(),Event.skipped,getList().getFilterBegin(),getList().getFilterEnd());
80  	}
81  	
82  	public String getLastplay() {
83  		Date d = getSong().getLastPlay(getUser());
84  		return d==null ? "" : DateTools.formatDate(d,"dd-MM-yyyy HH:mm:ss");
85  	}
86  	
87  	public String getRownumber() {
88  		Contract_PS psc = getPlaylistcontract();
89  		String result = Integer.toString(psc.getRowno());
90  
91  		Playlist previous = getList().getPrevious();
92  		if (previous!=null) {
93  			//Show current Song's position in the previous playlist between brackets.
94  			result += " [" + previous.getRownumberOf(psc.getSong()) +"]";
95  		}
96  		//TODO nice pictures or colours for +5, +10 -5, -10, etc.
97  		
98  		return result;
99  	}
100 
101 	/**
102 	 * Returns the playlist the current song appears on. This is used for
103 	 * displaying the cover art of this playlist. The result is a List containing
104 	 * just the playlist the current song is on.
105 	 * @return 
106 	 */
107 	public List<Playlist> getPlaylist() {
108 		return getMcService().getSongPlaylists(getSong());
109 	}
110 }