View Javadoc

1   package org.musicontroller.core;
2   
3   import java.text.SimpleDateFormat;
4   import java.util.Date;
5   import java.util.HashSet;
6   import java.util.Iterator;
7   import java.util.Set;
8   import java.util.TreeSet;
9   
10  public class Playlist extends LinkableAbs {
11  	private Date releasedate,filterbegin,filterend;
12  	private String comments;
13  	private Link link;
14  	private Set<Contract_PS> songs;
15  	private Playlist previous;
16  	
17  	public Playlist() {
18  		comments="";
19  		link=null;
20  		songs=new TreeSet<Contract_PS>(); //To preserve ordering (see mapping-file), this is a TreeSet
21  		filterbegin=null;
22  		filterend=null;
23  		previous=null;
24  	}
25  	
26  	/**
27  	 * Adds a song to the playlist, with the indicated index number. If
28  	 * the playlist already contains a song with the same index number then
29  	 * the song will be added with index number 0.
30  	 * @param song The song to add. Nothing is added if this is NULL.
31  	 * @param index The requested index number.
32  	 */
33  	public void addSong(Song song,int index) {
34  		if(song==null) {
35  			return;
36  		}
37  		if (index>0) {
38  			Iterator<Contract_PS> i = songs.iterator();
39  			while (i.hasNext()) {
40  				Contract_PS c = i.next();
41  				if (c.getRowno()==index) {
42  					index=0; // This index number is taken. Use 0 instead.
43  					break;
44  				}
45  			}
46  		}
47  		
48  		// Do not add the same song twice at index 0.
49  		if(index==0) {
50  			Iterator<Contract_PS> i = songs.iterator();
51  			while (i.hasNext()) {
52  				Contract_PS c = i.next();
53  				if (c.getRowno()==0) {
54  					Song index0Song = c.getSong();
55  					if(index0Song.equals(song)) {
56  						return;
57  					}
58  				}
59  			}
60  		}
61  
62  		Contract_PS contract = new Contract_PS();
63  		contract.setPlaylist(this);
64  		contract.setSong(song);
65  		contract.setRowno(index);
66  		songs.add(contract);
67  	}
68  	
69  	public String getComments() {
70  		return comments;
71  	}
72  	
73  	public void setComments(String comments) {
74  		this.comments = comments;
75  	}
76  	
77  	public Link getLink() {
78  		return link;
79  	}
80  	
81  	public void setLink(Link link) {
82  		this.link = link;
83  	}
84  	
85  	public Date getReleasedate() {
86  		return releasedate;
87  	}
88  	
89  	public String getFormattedReleasedate() {
90  		if (getReleasedate()==null) {
91  			return "";
92  		} else {
93  			//TODO Doesn't really belong here...
94  			SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
95  			df.applyPattern("dd-MM-yyyy");
96  			return df.format(getReleasedate());
97  		}
98  	}
99  	
100 	public void setReleasedate(Date releasedate) {
101 		this.releasedate = releasedate;
102 	}
103 
104 	public Set<Contract_PS> getSongs() {
105 		return songs;
106 	}
107 	
108 
109 	public void setSongs(Set<Contract_PS> songs) {
110 		if (songs!=null) this.songs = songs;
111 	}
112 	
113 	public Date getFilterBegin() {
114 		return filterbegin;
115 	}
116 	
117 	/**
118 	 * You can set this value to indicate that this playlist's Songs' Events should be filtered
119 	 * to a certain period. This makes it possible to show the amount of plays in one month
120 	 * for instance.
121 	 * @param filter
122 	 */
123 	public void setFilterBegin(Date filter) {
124 		this.filterbegin = filter;
125 	}
126 	
127 	public Date getFilterEnd() {
128 		return filterend;
129 	}
130 	
131 	public void setFilterEnd(Date filter) {
132 		this.filterend = filter;
133 	}
134 	
135 	/**
136 	 * You can set this property to a non-null value, to indicate the previous state
137 	 * of this playlist.
138 	 * @param playlist Another Playlist
139 	 */
140 	public void setPrevious(Playlist playlist) {
141 		this.previous = playlist;
142 	}
143 	
144 	public Playlist getPrevious() {
145 		return previous;
146 	}
147 	
148 	/**
149 	 * Returns the rownumber of the given Song in this Playlist.
150 	 * @param song The Song
151 	 * @return The Rownumber, or 0 if the Song is not in this Playlist.
152 	 */
153 	public int getRownumberOf(Song song) {
154 		int result = 0;
155 		if (song!=null) {
156 			for (Contract_PS psc : songs) {
157 				if (song.equals(psc.getSong())) return psc.getRowno();
158 			}
159 		}
160 		return result;
161 	}
162 
163 	/**
164 	 * Returns the song at song index i in the playlist, or null if there
165 	 * is no song at song index i. If the requested song index is 0, this
166 	 * returns one of the songs listed at index 0, if any.
167 	 * 
168 	 * @param i The requested song index.
169 	 * @return The song at song index 0, or null if there is no song at that
170 	 *         index.
171 	 */
172 	public Song getSongAtIndex(int i) {
173 		for (Contract_PS psc : songs) {
174 			if(psc.getRowno()==i) {
175 				return psc.getSong();
176 			}
177 		}
178 		return null;
179 	}
180 
181 	/**
182 	 * Constructs a playlist containing the song passed in the parameter. 
183 	 * @param song The song to be entered in the playlist.
184 	 * @return A playlist containing the song at index number 0. The playlist has no name.
185 	 */
186 	public static Playlist createInstance(Song song) {
187 		Playlist playlist = new Playlist();
188 		playlist.addSong(song, 0);
189 		return playlist;
190 	}
191 	
192 	/**
193 	 * Explicitly specify that two playlist are equal only if they have the same id.
194 	 * This makes it possible to have two playslists with the same
195 	 * name (and other properties).
196 	 */
197 	public boolean equals(Object o) {
198 		if(o instanceof Playlist) {
199 			Playlist p = (Playlist) o;
200 			return getId() == p.getId();
201 		} else {
202 			return false;
203 		}
204 	}
205 	
206 	/*
207 	 * (non-Javadoc)
208 	 * @see java.lang.Object#hashCode()
209 	 */
210 	@Override
211 	public int hashCode() {
212 		return Long.valueOf(getId()).hashCode();
213 	}
214 
215 	public String getType() {
216 		return "P";
217 	}
218 
219 	/**
220 	 * Returns the name of the Band performing on this Playlist. If there is more
221 	 * than one Band performing on this Playlist, the result will be "Various".
222 	 * @return The name of the Band.
223 	 */
224 	public String getBandname() {
225 		String result = "Various";
226 		Set<String> names = new HashSet<String>();
227 		for (Contract_PS cps : getSongs()) {
228 			names.add(cps.getSong().getBand().getName());
229 			if (names.size()>1) {
230 				return result;
231 			}
232 		}
233 		return names.size()>0 ? names.iterator().next() : result;
234 	}
235 	
236 	/*
237 	 * (non-Javadoc)
238 	 * @see java.lang.Object#toString()
239 	 */
240 	@Override
241 	public String toString() {
242 		StringBuilder sb = new StringBuilder();
243 		sb.append("[");
244 		sb.append(getId());
245 		sb.append("] ");
246 		sb.append(getName());
247 		for (Contract_PS contract : songs) {
248 			sb.append("\n");
249 			String rowno = Integer.toString(contract.getRowno());
250 			for (int i=0;i<5-rowno.length();i++) {
251 				sb.append(" ");
252 			}
253 			sb.append(rowno);
254 			sb.append(" ");
255 			sb.append(contract.getSong());
256 		}
257 		
258 		return sb.toString();
259 	}
260 
261 	/**
262 	 * Adds a Song to the Playlist at a certain index, if the Playlist does
263 	 * not already contain that Song. If the Playlist already contains the Song,
264 	 * nothing happens.
265 	 * Songs having a rownumber equal to or greater than the specified index, 
266 	 * will shift one position.
267 	 * @param song The Song.
268 	 * @param index The index-position for the new Song.
269 	 */
270 	public void addSongIfNew(Song song, int index) {
271 		int maxRowno = 0;
272 		for (Contract_PS contract : getSongs()) {
273 			if (contract.getRowno()>maxRowno) {
274 				maxRowno = contract.getRowno();
275 			}
276 			if (contract.getSong().equals(song)) {
277 				return;
278 			}
279 		}
280 		
281 		//Shift all Playlist-Song-Contracts which have an index >= index to make
282 		//room for the new Song.
283 		for (Contract_PS psc : songs) {
284 			int idx = psc.getRowno();
285 			if (idx>=index) {
286 				psc.setRowno(idx+1);
287 			}
288 		}
289 		addSong(song, index);
290 	}
291 
292 }