1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.modules.process;
22 
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 
28 import net.sourceforge.atunes.model.ILocalAudioObject;
29 import net.sourceforge.atunes.model.IUnknownObjectChecker;
30 import net.sourceforge.atunes.model.IWebServicesHandler;
31 
32 /**
33  * The Class SetGenresProcess.
34  */
35 public class SetGenresProcess extends AbstractChangeTagProcess {
36 
37 	private Map<ILocalAudioObject, String> filesAndGenres;
38 
39 	private IWebServicesHandler webServicesHandler;
40 
41 	private IUnknownObjectChecker unknownObjectChecker;
42 
43 	/**
44 	 * @param unknownObjectChecker
45 	 */
setUnknownObjectChecker(final IUnknownObjectChecker unknownObjectChecker)46 	public void setUnknownObjectChecker(final IUnknownObjectChecker unknownObjectChecker) {
47 		this.unknownObjectChecker = unknownObjectChecker;
48 	}
49 
50 	/**
51 	 * @param webServicesHandler
52 	 */
setWebServicesHandler(final IWebServicesHandler webServicesHandler)53 	public void setWebServicesHandler(final IWebServicesHandler webServicesHandler) {
54 		this.webServicesHandler = webServicesHandler;
55 	}
56 
57 	@Override
retrieveInformationBeforeChangeTags()58 	protected void retrieveInformationBeforeChangeTags() {
59 		super.retrieveInformationBeforeChangeTags();
60 		this.filesAndGenres = getGenresForFiles(getFilesToChange());
61 	}
62 
63 	@Override
changeTag(final ILocalAudioObject file)64 	protected void changeTag(final ILocalAudioObject file) throws IOException {
65 		String genre = this.filesAndGenres.get(file);
66 		// If file has already genre setted, avoid
67 		if (!file.getGenre(unknownObjectChecker).equals(genre)) {
68 			getTagHandler().setGenre(file, genre);
69 		}
70 	}
71 
72 	/**
73 	 * Gets the genres for files.
74 	 *
75 	 * @param files
76 	 *            the files
77 	 *
78 	 * @return the genres for files
79 	 */
getGenresForFiles(final Collection<ILocalAudioObject> files)80 	private Map<ILocalAudioObject, String> getGenresForFiles(final Collection<ILocalAudioObject> files) {
81 		Map<ILocalAudioObject, String> result = new HashMap<ILocalAudioObject, String>();
82 
83 		Map<String, String> tagCache = new HashMap<String, String>();
84 
85 		for (ILocalAudioObject f : files) {
86 			if (!unknownObjectChecker.isUnknownArtist(f.getArtist(unknownObjectChecker))) {
87 				String tag = null;
88 				if (tagCache.containsKey(f.getArtist(unknownObjectChecker))) {
89 					tag = tagCache.get(f.getArtist(unknownObjectChecker));
90 				} else {
91 					tag = webServicesHandler.getArtistTopTag(f.getArtist(unknownObjectChecker));
92 					tagCache.put(f.getArtist(unknownObjectChecker), tag);
93 					// Wait one second to avoid IP banning
94 					try {
95 						Thread.sleep(1000);
96 					} catch (InterruptedException e) {
97 						// Nothing to do
98 					}
99 				}
100 				if (tag != null) {
101 					result.put(f, tag);
102 				}
103 			}
104 		}
105 
106 		return result;
107 	}
108 }
109