1 package org.musicontroller.dao;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.Map.Entry;
13
14 import org.musicontroller.core.AIBag;
15 import org.musicontroller.core.AIRelation;
16 import org.musicontroller.core.Artist;
17 import org.musicontroller.core.Band;
18 import org.musicontroller.core.Contract_PS;
19 import org.musicontroller.core.Instrument;
20 import org.musicontroller.core.Keyword;
21 import org.musicontroller.core.KeywordCycleException;
22 import org.musicontroller.core.Keywordbag;
23 import org.musicontroller.core.Playlist;
24 import org.musicontroller.core.Song;
25 import org.musicontroller.gui.edit.AiRelationBean;
26 import org.musicontroller.gui.edit.TrackList;
27 import org.varienaja.util.StringUtil;
28
29
30
31
32
33
34 public class BagAndKeywordUtils {
35
36
37
38
39
40 private static Dao _dao;
41
42
43
44
45
46
47
48
49
50
51
52
53 public static Keywordbag addKeywordToBag(Keywordbag bag, Keyword keyword) {
54 if(bag==null || keyword==null) {
55 return null;
56 }
57 if (bag.getKeywords().contains(keyword)) {
58 return bag;
59 }
60
61 Iterator<Keywordbag> i = getBags().iterator();
62 while (i.hasNext()) {
63 Keywordbag candidate = i.next();
64 if (candidate.getKeywords().size()==bag.getKeywords().size()+1) {
65 if (candidate.getKeywords().contains(keyword)) {
66 if (candidate.getKeywords().containsAll(bag.getKeywords())) {
67 return candidate;
68 }
69 }
70 }
71 }
72
73 Keywordbag newOne = new Keywordbag();
74 newOne.getKeywords().addAll(bag.getKeywords());
75 newOne.getKeywords().add(keyword);
76 _dao.save(newOne);
77 return newOne;
78 }
79
80
81
82
83
84
85
86
87
88
89 public static Keywordbag removeKeyWordFromBag(Keywordbag bag, Keyword keyword) {
90 if(bag==null || keyword==null) {
91 return null;
92 }
93 if (!bag.getKeywords().contains(keyword.getId())) {
94 return bag;
95 }
96
97 List<Keyword> tmp = bag.getKeywords();
98 tmp.remove(keyword);
99
100 Iterator<Keywordbag> i = getBags().iterator();
101 while (i.hasNext()) {
102 Keywordbag candidate = i.next();
103 if (candidate.getKeywords().size()==tmp.size()) {
104 if (!candidate.getKeywords().contains(keyword.getId())) {
105 if (candidate.getKeywords().containsAll(tmp)) {
106 return candidate;
107 }
108 }
109 }
110 }
111
112
113 Keywordbag newOne = new Keywordbag();
114 newOne.getKeywords().addAll(tmp);
115 _dao.save(newOne);
116 return newOne;
117 }
118
119
120
121
122
123
124
125
126
127
128 public static void addKeywordToSong(Song song, Keyword kw) {
129 if(song==null || kw==null) {
130 return;
131 }
132 Keywordbag bag = song.getKeywordbag();
133 if(bag==null) {
134 bag = new Keywordbag();
135 }
136 Keywordbag newBag = BagAndKeywordUtils.addKeywordToBag(bag,kw);
137 song.setKeywordbag(newBag);
138 _dao.save(kw);
139 _dao.save(newBag);
140 }
141
142
143
144
145
146
147
148
149
150
151 public static Keyword getKeyword(String keywordname) {
152 if(keywordname==null) {
153 return null;
154 }
155 Keyword kw = _dao.searchKeyword(keywordname);
156 if (kw==null) {
157 kw = new Keyword();
158 kw.setName(keywordname);
159 try {
160 kw.setParent(null);
161 } catch (KeywordCycleException e) {
162
163 }
164 _dao.save(kw);
165 }
166 return kw;
167 }
168
169
170
171
172
173
174
175
176 public static Keywordbag getKeywordBag(String songKeywords) {
177
178 List<Keyword> keywordList = getKeywordList(songKeywords);
179 return getKeywordBag(keywordList);
180 }
181
182
183
184
185
186
187
188 public static Keywordbag getKeywordBag(Collection<Keyword> keywords) {
189 if(keywords==null) {
190 keywords = new ArrayList<Keyword>();
191 }
192 Keywordbag result = _dao.getKeywordsBag(keywords);
193
194 if(result==null) {
195 result = new Keywordbag();
196 result.getKeywords().addAll(keywords);
197 _dao.save(result);
198 }
199 return result;
200 }
201
202
203
204
205
206
207
208 public static List<Keyword> getKeywordList(String keywords) {
209 Collection<String> setOfKeywords = StringUtil.getIndividualWords(keywords);
210 List<Keyword> keywordList = new ArrayList<Keyword>();
211 for (String kws : setOfKeywords) {
212 if(kws.trim().length()>0) {
213 keywordList.add(getKeyword(kws));
214 }
215 }
216 return keywordList;
217 }
218
219
220
221
222
223
224
225
226
227 @SuppressWarnings("unchecked")
228 public static AIBag getAIBag(Set<AIRelation> relations) {
229 AIBag result = null;
230
231 String hql = "from AIBag bag " +
232 "left join bag.relations rel" +
233 " where rel.artist_id=:artistid and rel.instrument_id=:instrid";
234 Set<Long> remaining_candidates = new HashSet<Long>();
235 for(AIRelation rel: relations) {
236 Set<Long> bag_currentRelation = new HashSet<Long>();
237 long artistid = rel.getArtist_id();
238 long instrid = rel.getInstrument_id();
239 Map<String,Object> params = new HashMap<String,Object>();
240 params.put("artistid", artistid);
241 params.put("instrid", instrid);
242 List<AIBag> bags = _dao.search(hql, params, 0);
243 for(AIBag candidate: bags) {
244
245 if(candidate.getRelations().size()==relations.size()) {
246 long bag_candidate_id = candidate.getId();
247 bag_currentRelation.add(bag_candidate_id);
248 }
249 }
250 if(remaining_candidates.isEmpty()) {
251 remaining_candidates.addAll(bag_currentRelation);
252 } else {
253 Set<Long> removed = new HashSet<Long>();
254 for(long bag2_id: remaining_candidates) {
255 if(!bag_currentRelation.contains(bag2_id)) {
256 removed.add(bag2_id);
257 }
258 }
259 remaining_candidates.removeAll(removed);
260 }
261
262 if(remaining_candidates.isEmpty()) {
263 break;
264 }
265 }
266
267 if(remaining_candidates.isEmpty()) {
268 AIBag targetBag = new AIBag();
269 targetBag.getRelations().addAll(relations);
270 _dao.save(targetBag);
271 result = targetBag;
272 } else {
273 long result_bag_id = remaining_candidates.iterator().next();
274 result = _dao.getAIBagById(result_bag_id);
275 }
276 return result;
277 }
278
279
280
281
282
283
284
285 public static String listKeywords(Keywordbag bag) {
286 StringBuilder result = new StringBuilder();
287 boolean putComma = false;
288 if(bag!=null) {
289 for(Keyword kw: bag.getKeywords()) {
290 String kwname = kw.getName();
291
292 if (kwname==null || kwname.length()<1) {
293 continue;
294 }
295 if(putComma) {
296 result.append(",");
297 } else {
298 putComma = true;
299 }
300 String kwName = kw.getName().toLowerCase();
301 result.append(StringUtil.capitalize(kwName));
302 }
303 }
304 return result.toString();
305 }
306
307
308
309
310
311 public static List<Keywordbag> getBags() {
312 return _dao.listKeywordbags();
313 }
314
315
316
317
318
319 public static List<AIBag> getAIBags() {
320 return _dao.listAIBags();
321 }
322
323
324
325
326
327 public Dao getDao() {
328 return _dao;
329 }
330
331
332
333
334
335 public void setDao(Dao dao) {
336 _dao = dao;
337 }
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 public static List<AiRelationBean> buildAiRelationBeanList(Playlist playlist) {
373 List<AiRelationBean> result = new ArrayList<AiRelationBean>();
374 if(playlist==null) {
375 return result;
376 }
377 Map<Long, Map<Instrument, TrackList>> relations = new HashMap<Long,Map<Instrument,TrackList>>();
378 for(Contract_PS songcontract: playlist.getSongs()) {
379 Song song = songcontract.getSong();
380 int songindex = songcontract.getRowno();
381 if(song.getAibag()!=null) {
382
383 for(AIRelation candidaterel : song.getAibag().getRelations()) {
384 long artistid = candidaterel.getArtist_id();
385 long instsrid = candidaterel.getInstrument_id();
386 Instrument instrument = _dao.getInstrumentById(instsrid);
387
388
389 Map<Instrument,TrackList> entryset = relations.get(artistid);
390 if(entryset==null) {
391 entryset = new HashMap<Instrument,TrackList>();
392 relations.put(artistid,entryset);
393 }
394 TrackList list = entryset.get(instrument);
395 if(list==null) {
396 list = new TrackList();
397 entryset.put(instrument, list);
398 }
399 list.add(songindex);
400 }
401 }
402 }
403
404
405 for(Entry<Long,Map<Instrument,TrackList>> entry : relations.entrySet()) {
406 Map<Instrument,TrackList> instruments = entry.getValue();
407 Map<TrackList,List<Instrument>> reversedHashEntry = new HashMap<TrackList,List<Instrument>>();
408 for(Entry<Instrument,TrackList> innerEntry : instruments.entrySet()) {
409 TrackList tracks = innerEntry.getValue();
410 List<Instrument> reversedEntry = reversedHashEntry.get(tracks);
411 if(reversedEntry==null) {
412 reversedEntry = new ArrayList<Instrument>();
413 reversedHashEntry.put(tracks, reversedEntry);
414 }
415 reversedEntry.add(innerEntry.getKey());
416 }
417
418 Artist artist = BagAndKeywordUtils._dao.getArtistById(entry.getKey());
419 for(Entry<TrackList,List<Instrument>> innerEntry : reversedHashEntry.entrySet()) {
420 AiRelationBean entrybean = new AiRelationBean();
421 entrybean.setFirstName(artist.getFirstname());
422 entrybean.setLastName(artist.getLastname());
423 entrybean.setTracklist(innerEntry.getKey());
424 List<Instrument> instrumentsOfThisTrackList = innerEntry.getValue();
425 StringBuilder sb = new StringBuilder();
426 boolean first = true;
427 for(Instrument instrument : instrumentsOfThisTrackList) {
428 if(first) {
429 first = false;
430 } else {
431 sb.append(",");
432 }
433 sb.append(instrument.getName());
434 }
435 entrybean.setInstruments(sb.toString());
436 result.add(entrybean);
437 }
438 }
439
440 Collections.sort(result);
441 return result;
442 }
443
444
445
446
447
448
449
450
451 public static Set<AIRelation> buildAiRelationList(List<AiRelationBean> source) {
452 Set<AIRelation> relations = new HashSet<AIRelation>();
453 for(AiRelationBean bean : source) {
454 String artistfirstname = bean.getFirstName()==null ? null : bean.getFirstName().trim();
455 String artistlastname = bean.getLastName()==null ? null : bean.getLastName().trim();
456 String instruments = bean.getInstruments()==null ? null : bean.getInstruments().trim();
457 if(artistlastname==null) {
458
459 continue;
460 }
461 Artist theArtist = _dao.searchArtist(artistfirstname, artistlastname);
462 if (theArtist==null && (instruments!=null)) {
463
464 theArtist = new Artist();
465 theArtist.setFirstname(artistfirstname);
466 theArtist.setLastname(artistlastname);
467 _dao.save(theArtist);
468 }
469
470 List<Instrument> instrumentList = BagAndKeywordUtils.splitInstrumentList(instruments);
471
472
473 for(Instrument instr : instrumentList) {
474 AIRelation airel = new AIRelation();
475 airel.setArtist_id(theArtist.getId());
476 airel.setInstrument_id(instr.getId());
477 relations.add(airel);
478 }
479 }
480 return relations;
481 }
482
483
484
485
486
487
488
489 public static List<Instrument> splitInstrumentList(String instruments) {
490 List<Instrument> result = new ArrayList<Instrument>();
491 Collection<String> instrSet = StringUtil.getIndividualWords(instruments);
492 for(String instrname: instrSet) {
493 Instrument instr = _dao.searchInstrument(instrname);
494 if(instr==null) {
495 instr = new Instrument();
496 instr.setName(instrname);
497 _dao.save(instr);
498 }
499 result.add(instr);
500 }
501 return result;
502 }
503
504
505
506
507
508
509
510
511
512
513
514 public static Set<AIRelation> createRelations(String artistfirstname, String artistlastname, Band band, String instruments) {
515
516 artistfirstname = StringUtil.capitalize(artistfirstname);
517 artistlastname = StringUtil.capitalize(artistlastname);
518
519 Artist artistThisName = _dao.searchArtist(artistfirstname,artistlastname);
520 if (artistThisName==null) {
521
522 artistThisName = new Artist();
523 artistThisName.setFirstname(artistfirstname);
524 artistThisName.setLastname(artistlastname);
525 artistThisName.addBand(band);
526 _dao.save(artistThisName);
527 }
528
529
530 Set<AIRelation> relations = new HashSet<AIRelation>();
531 List<Instrument> instrumentList = BagAndKeywordUtils.splitInstrumentList(instruments);
532 for (Instrument i : instrumentList) {
533 AIRelation relToAdd = new AIRelation();
534 relToAdd.setInstrument_id(i.getId());
535 relToAdd.setArtist_id(artistThisName.getId());
536 relations.add(relToAdd);
537 }
538 return relations;
539 }
540
541 }