View Javadoc

1   package org.musicontroller.core.jobs;
2   
3   import java.util.Date;
4   
5   import org.apache.log4j.Logger;
6   import org.hibernate.Session;
7   import org.hibernate.SessionFactory;
8   import org.musicontroller.core.Playlist;
9   import org.musicontroller.dao.Dao;
10  import org.springframework.orm.hibernate3.SessionHolder;
11  import org.springframework.transaction.support.TransactionSynchronizationManager;
12  import org.varienaja.comments.CommentService;
13  import org.varienaja.util.DenseSet;
14  
15  /**
16   * The ReviewRefreshJob refreshes all Playlist-reviews. For each Playlist, the
17   * reviews are retrieved again, and new reviews will be added. Every review
18   * will be scanned for occurences of strings that happen to be 'linkable'.
19   * 
20   * A 'linkable' string is for instance 'The Beatles'. Whenever such a
21   * string is found, a link to the Band 'The Beatles' is inserted, provided of
22   * course, such a Band is in the MusiController database.
23   * 
24   * @author Varienaja
25   */
26  public class ReviewRefreshJob {
27  	private static final Logger log = Logger.getLogger(ReviewRefreshJob.class);
28  	private SessionFactory _sessionFactory;
29  	private Dao _dao;
30  
31  	public Dao getDao() {
32  		return _dao;
33  	}
34  
35  	public void setDao(Dao _dao) {
36  		this._dao = _dao;
37  	}
38  	
39  	public SessionFactory getSessionFactory() {
40  		return _sessionFactory;
41  	}
42  
43  	public void setSessionFactory(SessionFactory factory) {
44  		_sessionFactory = factory;
45  	}
46  	
47  	/**
48  	 * Starts the ReviewRefreshJob. Refreshes all playlist-reviews. This method
49  	 * creates a Session per Playlist, to guarantee that not too many object
50  	 * will hog resources.
51  	 * @return The amount of playlists that were processed.
52  	 */
53  	public int execute() {
54  		int count = 0;
55  		log.info("Review refresh job started.");
56  		CommentService service = new CommentService();
57  		
58  		Session session = null;
59  		session = _sessionFactory.openSession();
60  		TransactionSynchronizationManager.bindResource(_sessionFactory, new SessionHolder(session));
61  		DenseSet playlistIDs = getPlaylistIDs();
62  		session.close();
63  		TransactionSynchronizationManager.unbindResource(_sessionFactory);
64  		
65  		while (count<playlistIDs.size()) {
66  			long id = playlistIDs.get(count++);
67  			session = _sessionFactory.openSession();
68  			TransactionSynchronizationManager.bindResource(_sessionFactory, new SessionHolder(session));
69  			
70  			Playlist playlist = _dao.getPlaylistById(id, null);
71  			if (service.refreshComments(playlist)) {
72  				//Set the changed property of the playlist, so it 
73  				//gets refreshed when re-indexing takes place
74  				playlist.setChanged(new Date());
75  				_dao.save(playlist);
76  			}
77  			
78  			session.close();
79  			TransactionSynchronizationManager.unbindResource(_sessionFactory);
80  		}
81  		
82  		log.info("Review refresh job finished, "+count+" playlists were processed.");
83  		return count;
84  	}
85  	
86  	
87  	/**
88  	 * Helper method which returns a DenseSet, containing the ID of each
89  	 * existing Playlist.
90  	 * @return A DenseSet with playlist-id's.
91  	 */
92  	private DenseSet getPlaylistIDs() {
93  		DenseSet ids = new DenseSet();
94  		for (Playlist playlist : _dao.listPlaylists()) {
95  			ids.add(playlist.getId());
96  		}
97  		return ids;
98  	}
99  		
100 }