package com.itstherules.mediacentre.controller;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.blinkenlights.jid3.v1.ID3V1Tag.Genre;
import com.itstherules.mediacentre.Extension;
import com.itstherules.mediacentre.controller.dispatcher.AlbumCallDispatcher;
import com.itstherules.mediacentre.controller.dispatcher.ArtistCallDispatcher;
import com.itstherules.mediacentre.controller.dispatcher.CallDispatcher;
import com.itstherules.mediacentre.controller.dispatcher.CommentCallDispatcher;
import com.itstherules.mediacentre.controller.dispatcher.GenreCallDispatcher;
import com.itstherules.mediacentre.controller.dispatcher.YearCallDispatcher;
import com.itstherules.mediacentre.freemarker.method.NiceifyName;
import com.itstherules.mediacentre.model.FileMp3Converter;
import com.itstherules.stream.model.DirectoriesModel;
import com.itstherules.stream.model.Mp3Genres;
import com.itstherules.stream.model.ItemsModel;
import com.itstherules.stream.model.Mp3;
import com.itstherules.stream.model.ParameterBasedMp3;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateModelException;
public class AlbumMetaDataController extends BaseController {
@Override
public void show(Map<String, Object> parameters, HttpServletResponse response) throws IOException {
common(parameters);
merge("albummetadata/show.freemarker", parameters, response.getWriter());
}
@Override
public void edit(Map<String, Object> parameters, HttpServletResponse response) throws IOException {
common(parameters);
parameters.put("genres", new Mp3Genres().asList());
merge("albummetadata/edit.freemarker", parameters, response.getWriter());
}
private void common(Map<String, Object> parameters) {
DirectoriesModel model = new DirectoriesModel("music/"+parameters.get("directory"));
File directory = model.getDirectory();
String parent = new File(directory.getParent()).getName();
List<File> files = new ItemsModel("music/"+parameters.get("directory"),"", Extension.mp3.toString()).asList();
List<Mp3> mp3s = new FileMp3Converter().convert(files);
String artist = value(mp3s, parent, new ArtistCallDispatcher());
String album = value(mp3s, directory.getName(), new AlbumCallDispatcher());
String comment = value(mp3s, "MediaCentre", new CommentCallDispatcher());
String genre = value(mp3s, Genre.Undefined.toString(), new GenreCallDispatcher());
String year = value(mp3s, String.valueOf(GregorianCalendar.getInstance().get(Calendar.YEAR)), new YearCallDispatcher());
parameters.put("album", album);
parameters.put("artist", artist);
parameters.put("comment", comment);
parameters.put("genre", genre);
parameters.put("year", year);
}
private String value(List<Mp3> mp3s, String defaultValue, CallDispatcher dispatcher) {
Set<String> values = new HashSet<String>();
for (Mp3 mp3 : mp3s) {
values.add(dispatcher.dispatch(mp3));
}
boolean empty = true;
StringBuffer buffer = new StringBuffer();
for (String value : values) {
if(!"".equals(value)) {
empty = false;
buffer.append(value);
buffer.append(" ");
}
}
String value = buffer.toString().trim();
if(!empty) return value;
if(!value.contains(Genre.Undefined.toString())) return "-empty- "+defaultValue;
return defaultValue;
}
@Override
public void update(Map<String, Object> parameters, HttpServletResponse response) throws IOException {
Object albumDirectory = parameters.get("full");
List<File> files = new ItemsModel("music/"+albumDirectory,"", Extension.mp3.toString()).asList();
List<Mp3> mp3s = new FileMp3Converter().convert(files);
for (Mp3 mp3 : mp3s) {
String track;
try {
track = ((SimpleScalar) new NiceifyName().exec(Collections.singletonList(mp3.getName()))).getAsString();
} catch (TemplateModelException e) {
throw new RuntimeException("programattic error");
}
if("true".equalsIgnoreCase((String) parameters.get("updateTrackName"))){
parameters.put("title", track);
}
parameters.put("full", albumDirectory+"/"+mp3.getName());
new ParameterBasedMp3(parameters).persist(new DirectoriesModel("music").getDirectoryPath());
}
response.sendRedirect("../show/index.html?directory="+albumDirectory);
}
}