package dnb.analyze.nfo;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import dnb.analyze.MatchResult;
import dnb.data.Label;
import dnb.data.MediaType;
import dnb.data.NfoDate;
import dnb.data.ReleaseData;
import dnb.data.Repository;
import dnb.data.impl.AlbumDataHibernateImpl;
import dnb.data.impl.CatalogNumberHibernateImpl;
/**
* From analyze to data package => Converts <code>MatchResult</code> to <code>AlbumData</code>.
* XXX split up: general stuff & NFO type specific: for DEF and sour
*/
public class NfoDataFactory {
// XXX define non-numbers as date separators 2 get all types
private static final Pattern DATE = Pattern.compile("(.*?)/(.*?)/(.*?)");
// 28/11/2008
public static ReleaseData fromNfo(NfoLyzeResult result, Repository repository) {
Map<NfoField, MatchResult> m = result.getMatches();
switch(result.getNfoType()) {
case SOUR:
return fromSourNfo(m, repository);
case DEF:
return fromDefNfo(m, repository);
default:
return null;
}
}
private static ReleaseData fromSourNfo(Map<NfoField, MatchResult> m, Repository repository) {
ReleaseData d = new AlbumDataHibernateImpl();
storeCommonFields(d, m, repository);
storeDates(d, m, 3, 2, 1);
if (m.containsKey(NfoField.MEDIA_TYPE_VINYL)
&& !m.get(NfoField.MEDIA_TYPE_VINYL).getMatch().trim().isEmpty()) {
d.setMediaType(MediaType.VINYL);
} else if (m.containsKey(NfoField.MEDIA_TYPE_TAPE)
&& !m.get(NfoField.MEDIA_TYPE_TAPE).getMatch().trim().isEmpty()) {
d.setMediaType(MediaType.TAPE);
} else if (m.containsKey(NfoField.MEDIA_TYPE_CD)
&& !m.get(NfoField.MEDIA_TYPE_CD).getMatch().trim().isEmpty()) {
d.setMediaType(MediaType.CD);
} else if (m.containsKey(NfoField.MEDIA_TYPE_RADIO)
&& !m.get(NfoField.MEDIA_TYPE_RADIO).getMatch().trim().isEmpty()) {
d.setMediaType(MediaType.RADIO);
} else {
d.setMediaType(MediaType.UNKNOWN);
}
return d;
}
private static ReleaseData fromDefNfo(Map<NfoField, MatchResult> m, Repository repository) {
ReleaseData d = new AlbumDataHibernateImpl();
storeCommonFields(d, m, repository);
storeDates(d, m, 1, 2, 3);
if (m.containsKey(NfoField.MEDIA_TYPE)) {
String mt = m.get(NfoField.MEDIA_TYPE).getMatch();
if (mt.equalsIgnoreCase("WEB")) {
d.setMediaType(MediaType.WEB);
} else if (mt.equalsIgnoreCase("CDDA")) {
d.setMediaType(MediaType.CD);
} else {
d.setMediaType(MediaType.UNKNOWN);
System.err.println("***********!!!!!!!!!!!!!!!UNKNOWN DEF SOURCE:" + mt);
}
}
return d;
}
private static void storeCommonFields(ReleaseData target,
Map<NfoField, MatchResult> matchSource, Repository repository) {
if(matchSource.containsKey(NfoField.ARTIST)) {
target.setArtist(repository.getArtist(matchSource.get(NfoField.ARTIST).getMatch()));
}
if(matchSource.containsKey(NfoField.ALBUM_TITLE)) {
target.setTitle(matchSource.get(NfoField.ALBUM_TITLE).getMatch());
}
if (matchSource.containsKey(NfoField.GENRE)) {
target.setGenre(repository.getGenre(matchSource.get(NfoField.GENRE).getMatch()));
}
Label l = null;
if(matchSource.containsKey(NfoField.RECORD_LABEL)) {
l = repository.getLabel(matchSource.get(NfoField.RECORD_LABEL).getMatch());
target.setRecordLabel(l);
}
if(matchSource.containsKey(NfoField.LABEL_CODE)
&& matchSource.containsKey(NfoField.LABEL_CODE_NO)
&& matchSource.containsKey(NfoField.LABEL_CODE_SUFFIX)) {
target.setLabelCode(new CatalogNumberHibernateImpl(matchSource.get(NfoField.LABEL_CODE).getMatch(),
Integer.parseInt(matchSource.get(NfoField.LABEL_CODE_NO).getMatch()),
matchSource.get(NfoField.LABEL_CODE_SUFFIX).getMatch()));
if (l != null) {
l.addCode(matchSource.get(NfoField.LABEL_CODE).getMatch());
}
}
}
private static void storeDates(ReleaseData target, Map<NfoField, MatchResult> matchSource,
int dayGroupIndex, int monthGroupIndex, int yearGroupIndex) {
if (matchSource.containsKey(NfoField.RELEASE_DATE)) {
Matcher mt = DATE.matcher(matchSource.get(NfoField.RELEASE_DATE).getMatch());
if (mt.matches()) {
target.setReleaseDate(new NfoDate(mt.group(dayGroupIndex),
mt.group(monthGroupIndex), mt.group(yearGroupIndex)));
}
}
if (matchSource.containsKey(NfoField.STREET_DATE)) {
Matcher mt = DATE.matcher(matchSource.get(NfoField.STREET_DATE).getMatch());
if (mt.matches()) {
target.setStreetDate(new NfoDate(mt.group(3), mt.group(2), mt.group(1)));
}
}
}
}