View Javadoc

1   package org.musicontroller.repair;
2   
3   
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Map.Entry;
9   
10  import org.apache.log4j.Logger;
11  import org.musicontroller.core.AIBag;
12  import org.musicontroller.core.AIRelation;
13  import org.musicontroller.core.Artist;
14  import org.musicontroller.dao.Dao;
15  
16  /**
17   * Merges all Artists in the database that are equal into one.
18   * @author drexler
19   * @version $Id: MergeArtistCopies.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
20   */
21  public class MergeArtistCopies {
22  	private static final Logger log = Logger.getLogger(MergeArtistCopies.class);
23  
24  	@SuppressWarnings("unchecked")
25  	public static void execute(Dao dao) {
26  		if(dao==null) {
27  			log.error("MergeArtistCopies.execute() invoked with a null DAO. The merge could not take place.");
28  			return;
29  		}
30  		log.info("Merge artist copy job started.");
31  		List<Long> allArtists = dao.search("select a.id from Artist a", null, 0);
32  		// The artist map will contain an entry for each distinct artist. The
33  		// entries value is a list of all copies of that artist.
34  		Map<Artist, List<Long>> artistMap = new HashMap<Artist,List<Long>>();
35  		for (long anArtistId : allArtists) {
36  			Artist artist = dao.getArtistById(anArtistId);
37  			List<Long> entry = artistMap.get(artist);
38  			if(entry==null) {
39  				// A new artist : create an empty entry
40  				entry = new ArrayList<Long>();
41  				artistMap.put(artist, entry);
42  			} else {
43  				// We have found a duplicate. Add it to the list.
44  				entry.add(anArtistId);
45  			}
46  		}
47  		// Now we have to merge the duplicates.
48  		for(Entry<Artist,List<Long>> entry : artistMap.entrySet()) {
49  			List<Long> duplicates = artistMap.get(entry.getValue());
50  			for(long duplicateId : duplicates) {
51  				mergeArtists(entry.getKey(),duplicateId,dao);
52  			}
53  		}
54  		log.info("Merge artist copy job finished.");
55  	}
56  
57  	/**
58  	 * Merge the artist duplicateId into the artist keep.
59  	 * @param keep The artist to keep.
60  	 * @param duplicateId The artist to merge.
61  	 */
62  	@SuppressWarnings("unchecked")
63  	private static void mergeArtists(Artist keep, long duplicateId,Dao dao) {
64  		String hql = "select distinct bag.id from AIBag bag left join bag.relations rel" +
65  				" where rel.artist_id=:artistid";
66  		Map<String,Object> params = new HashMap<String,Object>();
67  		params.put("artistid", duplicateId);
68  		List<Long> bagIdList = dao.search(hql, params, 0);
69  		for(long bag_id: bagIdList) {
70  			AIBag bag = dao.getAIBagById(bag_id);
71  			for(AIRelation rel: bag.getRelations()) {
72  				if(rel.getArtist_id()==duplicateId) {
73  					rel.setArtist_id(keep.getId());
74  					log.info("Changing reference to artist "+duplicateId+" into a reference to artist "+keep.getId()+".");
75  				}
76  			}
77  			dao.save(bag);
78  		}
79  		// Delete the duplicate artist.
80  		Artist duplicate = dao.getArtistById(duplicateId);
81  		dao.deleteArtist(duplicate);
82  		log.info("Artist id: "+duplicateId+" has been removed.");
83  	}
84  }