View Javadoc

1   package org.musicontroller.importer;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.Date;
7   import java.util.HashMap;
8   import java.util.HashSet;
9   import java.util.List;
10  import java.util.Map;
11  import java.util.Set;
12  import java.util.TreeSet;
13  
14  import org.apache.log4j.Logger;
15  import org.varienaja.util.DateTools;
16  
17  /**
18   * This class contains information about an uploaded music archive file.
19   * <p>The inner class PlaylistImportProperties is used for setting instruction for the importer
20   * per playlist name.
21   *  
22   * @author Hans Drexler
23   * @version $Id: MusicArchiveBean.java,v 1.1 2010/03/16 18:55:43 varienaja Exp $
24   */
25  public class MusicArchiveBean implements Serializable {
26  
27  	private static final long serialVersionUID = 20071205181533L;
28  
29  	private static Logger _log = Logger.getLogger(MusicArchiveBean.class);
30  	
31  	/**
32  	 * Stores the release date of the playlist contained in the music archive.
33  	 */
34  	private Date _releaseDate = null;
35  	
36  	/**
37  	 * Stores aditional information about playlists in the archive, like release date and
38  	 * cover art.
39  	 */
40  	private Map<String,PlaylistImportProperties> _playlistPropertiesMap;
41  	
42  	/**
43  	 * The (file)name of this archive.
44  	 */
45  	private String _archiveName;
46  	
47  	/**
48  	 * The list of songs contained in this archive.
49  	 */
50  	private List<MusicArchiveEntryBean> _entries;
51  	
52  	/**
53  	 * The default constructor initializes the entries and the playlist properties to an empty set
54  	 * and all other properties to <tt>null</tt>.
55  	 */
56  	public MusicArchiveBean() {
57  		_playlistPropertiesMap = new HashMap<String,PlaylistImportProperties>();
58  		_releaseDate = null;
59  		_archiveName = null;
60  		setEntries(new ArrayList<MusicArchiveEntryBean>());
61  	}
62  	
63  	/**
64  	 * Get the per playlist properties of the specified playlist.
65  	 * @return The per playlist import properties of the specified playlist or null.
66  	 */
67  	public PlaylistImportProperties getPlaylistProperties(String playlistName) {
68  		if(playlistName==null) {
69  			return null;
70  		} 
71  		return _playlistPropertiesMap.get(playlistName);
72  	}
73  
74  	public Set<PlaylistImportProperties> getPlaylistPropertySet() {
75  		Collection<PlaylistImportProperties> allProperties = _playlistPropertiesMap.values();
76  		Set<PlaylistImportProperties> result = new HashSet<PlaylistImportProperties>(allProperties);
77  		_log.debug("Returned a set of "+result.size()+" playlist import properties.");
78  		return result;
79  	}
80  	
81  	/**
82  	 * Returns a set of all distinct playlist names in the archive.
83  	 * @return A set of all distinct playlist names in the archive.
84  	 */
85  	public Set<String> getPlaylistNames() {
86  		return _playlistPropertiesMap.keySet();
87  	}
88  	
89  	/**
90  	 * Setter for the per playlist import properties of the specified playlist.
91  	 * @param properties The per playlist import properties.
92  	 */
93  	public void setPlaylistProperties(String playlistName, PlaylistImportProperties properties) {
94  		if(playlistName==null) {
95  			return;
96  		}		
97  		_playlistPropertiesMap.put(playlistName, properties);
98  	}
99  
100 	/**
101 	 * Getter for the music archive name.
102 	 * @return The music archive name.
103 	 */
104 	public String getArchiveName() {
105 		return _archiveName;
106 	}
107 
108 	/**
109 	 * Setter for the music archive name.
110 	 * @param name The music archive name.
111 	 */
112 	public void setArchiveName(String name) {
113 		_archiveName = name;
114 	}
115 
116 	/**
117 	 * Getter for the list of music entries in this archive.
118 	 * @return The list of music entries.
119 	 */
120 	private List<MusicArchiveEntryBean> getEntries() {
121 		return _entries;
122 	}
123 
124 	/**
125 	 * Setter for the list of entries in the archive.
126 	 * @param entries The list of entries.
127 	 */
128 	private void setEntries(List<MusicArchiveEntryBean> entries) {
129 		this._entries = entries;
130 	}
131 	
132 	
133 	public Set<MusicArchiveEntryBean> getEntrySet() {
134 		Set<MusicArchiveEntryBean> result = new TreeSet<MusicArchiveEntryBean>();
135 		result.addAll(_entries);
136 		return result;
137 	}
138 	
139 	/**
140 	 * Returns the minimum value of releaseDate of all entries, or the value
141 	 * of release date that has been set.
142 	 * @return
143 	 */
144 	public Date getReleaseDate() {
145 		Date result = _releaseDate;
146 		if (result==null) { //Not initialized, get minimum releasedate from entries.
147 			for (MusicArchiveEntryBean entry : _entries) {
148 				Date test = entry.getReleaseDate();
149 				if (test!=null) {
150 					if (result==null || test.before(result)) {
151 						result = test;
152 					}
153 				}
154 			}
155 		}
156 		return result;
157 	}
158 	
159 	/**
160 	 * Sets the release date value of this bean.
161 	 * @param releaseDate The release date.
162 	 */
163 	public void setReleaseDate(Date releaseDate) {
164 		_releaseDate = releaseDate;
165 	}
166 	
167 	/**
168 	 * Adds an entry to the music archive.
169 	 * @param entry The entry to add. Null values are ignored. If the playlist name
170 	 * 				is null, "" is used as the playlist name.
171 	 */
172 	public void addEntry(MusicArchiveEntryBean entry) {
173 		if(entry==null) {
174 			return;
175 		}
176 		if(entry.getPlaylistName()==null) {
177 			entry.setPlaylistName("");
178 		}
179 		getEntries().add(entry);
180 		analyseMusicArchive();
181 	}
182 	
183 	/**
184 	 * Analyses the entries in the music archive to create a list of different playlists. If the archive
185 	 * already contains an entry for a playlist in the music archive, that entry is not changed.
186 	 * Maintain information on every playlist found:
187 	 * <ul>
188 	 * <li> Release date
189 	 * <li> Cover art 
190 	 * </ul> 
191 	 * @param archive The music archive to analyse.
192 	 */
193 	public void analyseMusicArchive() {
194 		// Build a Map of different Playlist names in the archive, and set up their properties.
195 		Set<String> playlistNames = new HashSet<String>();
196 		for (MusicArchiveEntryBean entry : getEntries()) {
197 			String key = entry.getPlaylistName();
198 			playlistNames.add(key);
199 			if(getPlaylistProperties(key)==null) {
200 				// No entry yet, construct one. We can't set a cover art here, since that would require
201 				// Tapestry functionality.
202 				PlaylistImportProperties pip = new PlaylistImportProperties();
203 				pip.setPlaylistName(key);
204 				pip.setReleaseDate(DateTools.currentDate());
205 				pip.setAddToExistingPlaylist(false);
206 				setPlaylistProperties(key, pip);
207 			}
208 		}
209 		// Remove playlist properties of playlist names that are no longer in the list.
210 		for(PlaylistImportProperties pp: getPlaylistPropertySet()) {
211 			if (!playlistNames.contains(pp.getPlaylistName())) {
212 				_playlistPropertiesMap.remove(pp.getPlaylistName());
213 			}
214 		}
215 	}
216 
217 	
218 }