View Javadoc

1   package org.musicontroller.gui.edit;
2   import java.util.List;
3   import java.util.Set;
4   
5   import org.apache.log4j.Logger;
6   import org.apache.tapestry.IExternalPage;
7   import org.apache.tapestry.IRequestCycle;
8   import org.apache.tapestry.event.PageBeginRenderListener;
9   import org.apache.tapestry.event.PageEndRenderListener;
10  import org.apache.tapestry.event.PageEvent;
11  import org.apache.tapestry.html.BasePage;
12  import org.musicontroller.core.AIBag;
13  import org.musicontroller.core.AIRelation;
14  import org.musicontroller.core.Playlist;
15  import org.musicontroller.core.Song;
16  import org.musicontroller.dao.BagAndKeywordUtils;
17  import org.musicontroller.dao.Dao;
18  import org.musicontroller.service.McService;
19  
20  /**
21   * This screen implements editing the properties of Songs.
22   * @author deksels
23   * @version $Id: SongEdit.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
24   */
25  public abstract class SongEdit extends BasePage implements IExternalPage, PageBeginRenderListener, PageEndRenderListener {
26  	private static final Logger log = Logger.getLogger(SongEdit.class);
27  
28  	public abstract Long getSongId();
29  	public abstract void setSongId(Long id);
30  
31  	public abstract void setSongName(String name);
32  	public abstract void setSongLength(int length);
33  	public abstract void setSongKeywords(String keywords);
34  	public abstract void setBandName(String bandname);
35  	
36  	public abstract String getSongName();
37  	public abstract int getSongLength();
38  	public abstract String getSongKeywords();
39  	public abstract String getBandName();
40  	
41  	public abstract Dao getDao();
42  	
43  	public abstract List<AiRelationBean> getAiRelationList();
44  	public abstract void setAiRelationList(List<AiRelationBean> relations);
45  	
46  	private Song _song;
47  	
48  	public abstract McService getMcService();
49  	
50  	public void pageBeginRender(PageEvent event) {
51  		Song song = getSong();
52  		if(song==null) {
53  			return;
54  		}
55  		setBandName(song.getBand().getName());
56  		setSongName(song.getName());
57  		setSongLength(song.getLength()/1000);
58  		setSongKeywords(song.listKeywords());
59  		// Map the artist_id to a set of all instruments played by the artist.
60  		List<AiRelationBean> beanlist = BagAndKeywordUtils.buildAiRelationBeanList(Playlist.createInstance(song));
61  		setAiRelationList(beanlist);
62      }
63  	
64  	public void pageEndRender(PageEvent e) {
65  			_song = null;
66  	}
67  
68  	public void activateExternalPage(Object[] args, IRequestCycle cycle) {
69  		Long songid = (Long) args[0];
70  		setSongId(songid);
71  	}
72  
73  	public Song getSong() {
74  		if (_song==null) {
75  			_song = getDao().getSongById(getSongId());
76  			log.debug("Queried song");
77  		} else {
78  			log.debug("Got song from cache.");
79  		}
80  		return _song;
81  	}
82  
83  	/**
84  	 * Returns the list of playlists this song occurs in.
85  	 * @return List of playlists containing this song.
86  	 */
87  	public List<Playlist> getPlaylists() {
88  		return getDao().listPlaylists(getSong());
89  	}
90  
91  	/**
92  	 * Submits the changes made.
93  	 * 
94  	 * @param cycle The Tapestry MVC cycle.
95  	 */
96  	public void save(IRequestCycle cycle) {
97  		getMcService().setSongProperties(getSong(), getSongName(), getBandName(), 
98  				getSongLength()*1000, getSongKeywords());
99  	}
100 		
101 	/**
102 	 * Read the changed artist names and instrument lists from the
103 	 * bean and commit those changes. Builds a list of artists and
104 	 * sets of instruments that corresponds to the values entered by
105 	 * the user. Then it adds missing relations and removes relations
106 	 * that no longer exist.
107 	 * Each artist should at least have a last name.
108 	 * @param cycle The Tapestry MVC cycle.
109 	 */
110 	public void editAiRelation(IRequestCycle cycle) {
111 		Set<AIRelation> relations = BagAndKeywordUtils.buildAiRelationList(getAiRelationList());
112 		AIBag newBag = BagAndKeywordUtils.getAIBag(relations);
113 		Song song = getSong();
114 		song.setAibag(newBag);
115 		getDao().save(song);
116 	}
117 	
118 	/**
119 	 * When a User hits a button invoking this listener, a guess is made for the performers in this song.
120 	 * The guess is: select the most used s_ai-relation for the band of this song.
121 	 * @param cycle The Tapestry MVC-Cycle
122 	 */
123 	public void guessPerformers(IRequestCycle cycle) {
124 		Song song = getSong();
125 		AIBag aibag = getDao().getMostUsedAIBag(song.getBand());
126 		song.setAibag(aibag);
127 		getDao().save(song);
128 	}
129 	
130 	public void addMusician(IRequestCycle cycle, String artistfirstname, String artistlastname, String instruments) {
131 		getMcService().addMusician(getSong(), artistfirstname, artistlastname, instruments);
132 	}
133 
134 	public void deleteMusician(IRequestCycle cycle, String artistfirstname, String artistlastname, String instruments) {
135 		getMcService().deleteMusician(getSong(), artistfirstname, artistlastname, instruments);
136 	}
137 
138 }