View Javadoc

1   package org.musicontroller;
2   
3   import java.util.List;
4   
5   import org.apache.log4j.Logger;
6   import org.musicontroller.dao.Dao;
7   import org.musicontroller.security.IUser;
8   import org.musicontroller.songselection.AdvancedRandomSongSelector;
9   import org.musicontroller.songselection.CandidateSelector;
10  
11  /**
12   * Factory for DJ objects. The factory will create a new DJ, equip it with
13   * a candidate selector and a song selector. The factory will also try to
14   * load and apply the user properties to the new DJ and its components.
15   * 
16   * @author drexler
17   * @version $Id: DJFactory.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
18   */
19  public class DJFactory {
20  	private static final Logger log = Logger.getLogger(DJFactory.class);
21  
22  	private CandidateSelector _candidateSelector;
23  	
24  	/**
25  	 * Creates a DJ object. The DJ object is fitted with
26  	 * a song selector object for selecting the songs to play, and
27  	 * a MusiController object to save the song-events, and
28  	 * a random candidate selector to select candidates for the
29  	 * songselector.
30  	 * 
31  	 * @param userid The id of the user the Dj will play songs for.
32  	 * @return The created DJ.
33  	 */
34  	public DJ create(Long userid) {
35  		if(getMusiController()==null) {
36  			log.error("Cannot create DJ: No MusiController available.");
37  			return null;			
38  		}
39  		if(getCandidateSelector()==null) {
40  			log.error("Cannot create DJ: No candidate selector available.");
41  			return null;			
42  		}
43  		Dao dao = getMusiController().getDao();
44  		DJImpl dj = new DJImpl();
45  		dao.registerSongChangeListener(dj); //FIXME deregister the listener when the DJ is finalized.
46  		IUser user = dao.getUserById(userid);
47  		if(user==null) {
48  			log.warn("There is no user with id: "+userid);
49  			return null;
50  		}
51  		dj.setUser(user);
52  		dj.setMusiController(getMusiController());
53  		
54  		AdvancedRandomSongSelector songselector = new AdvancedRandomSongSelector();
55  		songselector.setDao(dao);
56  		
57  		//We inject the Band-popularity knowledge into the songselector
58  		List<Object[]> allBands = dao.listBands(user);
59  		for (Object[] bandinfo : allBands) {
60  			Long bandid = (Long) bandinfo[0];
61  			String bandname = (String) bandinfo[1];
62  			int playcount = ((Long) bandinfo[2]).intValue();
63  			int skipcount = ((Long) bandinfo[3]).intValue();
64  			
65  			songselector.addBandKnowledge(bandid,playcount,skipcount);
66  			if (log.isDebugEnabled()) {
67  				if (skipcount>playcount) {
68  					log.debug("Negative popularity set for band: "+bandname);
69  				}
70  			}
71  		}
72  		allBands = null;
73  		
74  		//We inject the Keyword-popularity knowledge into the songselector
75  		List<Object[]> allKeywords = dao.listKeywords(user,null);
76  		for (Object[] keywordinfo : allKeywords) {
77  			Long keywordid = (Long) keywordinfo[0];
78  			String keywordname = (String) keywordinfo[1];
79  			int playcount = ((Long) keywordinfo[2]).intValue();
80  			int skipcount = ((Long) keywordinfo[3]).intValue();
81  			
82  			songselector.addKeywordKnowledge(keywordid,playcount,skipcount);
83  			if (log.isDebugEnabled()) {
84  				if (skipcount>playcount) {
85  					log.debug("Negative popularity set for keyword: "+keywordname);
86  				}
87  			}
88  		}
89  		allKeywords = null;
90  		
91  		dj.setSongSelector(songselector);
92  		CandidateSelector cs = getCandidateSelector();
93  		dj.setCandidateSelector(cs);
94  		UserProperties.applyProperties(user, dj);
95  		return dj;
96  	}
97  
98  	/**
99  	 * The MusiController object gets injected
100 	 * using Spring.
101 	 */
102 	private MusiController _controller;
103 
104 	/**
105 	 * Getter for the MusiController object.
106 	 * @return The MusiController object.
107 	 */
108 	public MusiController getMusiController() {
109 		return _controller;
110 	}
111 
112 	/**
113 	 * Setter for the MusiController object.
114 	 * @param controller The MusiController object.
115 	 */
116 	public void setMusiController(MusiController controller) {
117 		this._controller = controller;
118 	}
119 
120 	/**
121 	 * Getter for the candidate selector.
122 	 * @return The candidate selector.
123 	 */
124 	public CandidateSelector getCandidateSelector() {
125 		return _candidateSelector;
126 	}
127 
128 	/**
129 	 * Setter for the candidate selector. This selector will be shared among all DJ's.
130 	 * @param selector The candidate selector.
131 	 */
132 	public void setCandidateSelector(CandidateSelector selector) {
133 		_candidateSelector = selector;
134 	}
135 	
136 }