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
22
23
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
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
85
86
87 public List<Playlist> getPlaylists() {
88 return getDao().listPlaylists(getSong());
89 }
90
91
92
93
94
95
96 public void save(IRequestCycle cycle) {
97 getMcService().setSongProperties(getSong(), getSongName(), getBandName(),
98 getSongLength()*1000, getSongKeywords());
99 }
100
101
102
103
104
105
106
107
108
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
120
121
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 }