- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
@OnEvent("search")
@ReportGritter(title = "message:error", text = "message:database_error")
Results search() {
final List<Result> results = new ArrayList<Result>();
int count = 0;
final List<?> search1 = this.bands.search(this.query);
final String group1 = HSSearch.GROUP_BANDS;
if (!search1.isEmpty()) {
results.addAll(this.transform(search1, group1));
count += search1.size();
}
final List<?> search2 = this.albums.search(this.query);
final String group2 = HSSearch.GROUP_ALBUMS;
if (!search2.isEmpty()) {
results.addAll(this.transform(search2, group2));
count += search2.size();
}
final List<?> search3 = this.tracks.search(this.query);
final String group3 = HSSearch.GROUP_TRACKS;
if (!search3.isEmpty()) {
results.addAll(this.transform(search3, group3));
count += search3.size();
}
this.results.setResults(results);
this.results.setCount(count);
return this.results;
}
private <E> String toTitle(final E item) {
if (item instanceof BandEntity) {
return ((BandEntity) item).getTitle();
}
if (item instanceof AlbumEntity) {
final AlbumEntity aitem = (AlbumEntity) item;
return String.format("%s (%d)", aitem.getTitle(), Integer.valueOf(aitem.getYear()));
}
if (item instanceof TrackEntity) {
return ((TrackEntity) item).getTitle();
}
return item.toString();
}
private <E> String toUrl(final E item) {
if (item instanceof BandEntity) {
final BandEntity bitem = (BandEntity) item;
return this.links.createPageRenderLinkWithContext(Band.class, bitem.getLetter(), bitem.getAlias())
.toAbsoluteURI();
}
if (item instanceof AlbumEntity) {
final AlbumEntity aitem = (AlbumEntity) item;
return this.links.createPageRenderLinkWithContext(Album.class, aitem.getBand().getLetter(),
aitem.getBand().getAlias(), aitem.getAlias()).toAbsoluteURI();
}
if (item instanceof TrackEntity) {
final TrackEntity titem = (TrackEntity) item;
return this.links.createPageRenderLinkWithContext(Album.class, titem.getAlbum().getBand().getLetter(),
titem.getAlbum().getBand().getAlias(), titem.getAlbum().getAlias()).toAbsoluteURI();
}
return this.links.createPageRenderLinkWithContext("").toAbsoluteURI();
}
private <E> List<Result> transform(final List<E> search, final String groupName) {
final ArrayList<Result> res = new ArrayList<Result>();
if (!search.isEmpty()) {
final String group = this.messages.get(groupName);
res.add(new Result(group));
for (final E item : search) {
res.add(new Result(group, this.toTitle(item), this.toUrl(item)));
}
}
return res;
}
DRY in Action.
мое домашнее творчество.
3.14159265 06.06.2013 20:00 # +5
> if (item instanceof BandEntity) {
Мешать женерики с кастами и instanceof. Явно что-то не то с проектировкой.
Да тот же полиморфизм. Сдеалать toTitle и toUrl в каждом классе.
Lure Of Chaos 07.06.2013 14:33 # 0
someone 08.06.2013 10:37 # 0
Lure Of Chaos 08.06.2013 11:09 # 0
someone 07.06.2013 06:55 # 0
roman-kashitsyn 07.06.2013 10:03 # +1
inkanus-gray 07.06.2013 20:43 # +2
— Потому что может!