/* Generated by Together */
package multimonster.systemadministration;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import multimonster.common.Codec;
import multimonster.common.Format;
import multimonster.common.FormatId;
import multimonster.common.InputOption;
import multimonster.common.OutputOption;
import multimonster.common.Protocol;
import multimonster.common.ProtocolId;
import multimonster.common.Structure;
import multimonster.common.UserIdentifier;
import multimonster.common.media.MIIdentifier;
import multimonster.common.media.MOIdentifier;
import multimonster.common.media.MediaInstance;
import multimonster.common.media.MediaObject;
import multimonster.common.media.MetaData;
import multimonster.common.media.MetaDataAccess;
import multimonster.exceptions.MultiMonsterException;
import org.apache.log4j.Logger;
public class MOManager {
private Logger log;
public MOManager() {
log = Logger.getLogger(this.getClass());
}
/**
* creates a new MediaObject in the DB the mediaObject-input must contain
* the essential meta-Data
*
* @param mediaObject
* @param user
* @return
*/
public MOIdentifier addMediaObject(MediaObject mediaObject,
UserIdentifier user) throws MultiMonsterException {
ResultSet result = null;
// check Userrights
// check if all required data has been supplied
MetaData meta = mediaObject.getMetaData();
if (meta == null) {
throw new MultiMonsterException("No Metadata supplied!");
}
QueryManager qmnr = new QueryManager();
int connNr = qmnr.reserveConnection();
// build statement
String query = buildStatement_addMO(meta);
qmnr.dbOpInsert(query, connNr);
result = qmnr.dbOpExec("SELECT LAST_INSERT_ID();", connNr);
if (result == null) {
throw new MultiMonsterException("Insert of MediaObject failed!");
}
int moNr = 0;
try {
result.next();
moNr = result.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
qmnr.bringBackConn(connNr);
return new MOIdentifier(moNr);
}
public void remMediaObject(MOIdentifier id, UserIdentifier user)
throws MultiMonsterException {
log.debug("remMediaObject()");
// check Userrights
// check if MOIdentifier is ok
if (id == null || id.getMoNumber() == 0) {
throw new MultiMonsterException("MOIdentifier is empty");
}
QueryManager qmnr = new QueryManager();
int connNr = qmnr.reserveConnection();
//build statement
String query = buildStatement_remMO(id);
qmnr.dbOpInsert(query, connNr);
qmnr.bringBackConn(connNr);
}
/**
* doesn't provide a new MOIdentifier
*/
public void modifyMediaObject(MediaObject mediaObject, UserIdentifier user) {
}
public MediaInstance getMediaInstance(MOIdentifier mOId, FormatId fId)
throws MultiMonsterException {
log.debug("getMediaInstance() - search MediaInstance");
if (mOId == null || mOId.getMoNumber() == 0) {
log.error("Fehlerhafter Parameter MOIdentifier.");
throw new MultiMonsterException(
"Fehlerhafter Parameter MOIdentifier.");
}
MIIdentifier mID = new MIIdentifier();
QueryManager qmngr = new QueryManager();
// demand connection
int connNr = qmngr.reserveConnection();
if (connNr == -1) {
//throw new DBNotAvailableException("DB not available!");
}
// got connection
String query = "select id, location, vid_format, res_horiz "
+ "from mediainstance " + "where mediainstance.moid = "
+ mOId.getMoNumber() + " and vid_format = '" + fId.getId() + "';";
log.debug("Query for getMediaInstance: " + query);
ResultSet result = qmngr.dbOpExec(query, connNr);
if (result == null) {
throw new MultiMonsterException("Search for mediainstance failed!");
}
try {
if (result.next() == true) {
MediaInstance mediaInstance = null;
mID.setMINumber(result.getInt("id"));
mID.setLocation(result.getString("location"));
Format form = getFormat(new FormatId(result
.getString("vid_format")));
form.setResolutionHorizontal(result.getInt("res_horiz"));
mediaInstance = new MediaInstance(mID, mOId, form);
//getFormat(new FormatId(FormatId.fId_NO_CONVERSION)));
result.close();
qmngr.bringBackConn(connNr);
return mediaInstance;
} else {
// zu diesem MediaObject existiert keine MI mit diesem Format
// welche MI wird f�r Konvertierung verwendet?
query = "select mi.id, mi.location, mi.vid_format, mi.res_horiz "
+ "from plugin_converter pc, videoformat vf, mediainstance mi "
+ "where pc.to_format = '" + fId.getId() + "' "
+ "and vf.codec_id = pc.from_codec "
+ "and vf.struc_id = pc.from_structure "
+ "and vf.id = mi.vid_format "
+ "and mi.moid = " + mOId.getMoNumber() + " ;" ;
log.debug("Query f�r getMediainstance: " + query);
result = qmngr.dbOpExec(query, connNr);
if (result.next() == true) {
MediaInstance mediaInstance = null;
mID.setMINumber(result.getInt("id"));
mID.setLocation(result.getString("location"));
// new machen
Format form = getFormat(new FormatId(result
.getString("vid_format")));
form.setResolutionHorizontal(result.getInt("res_horiz"));
mediaInstance = new MediaInstance(mID, mOId, form);
result.close();
qmngr.bringBackConn(connNr);
return mediaInstance;
}
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
log.error("Fehler beim suchen in mediainstance!");
return null;
}
}
public MIIdentifier getSourceMediaInstance(MOIdentifier moid)
throws MultiMonsterException {
ResultSet result = null;
String fileName = null;
int miNumber = 0;
if (moid == null || moid.getMoNumber() == 0) {
throw new MultiMonsterException("MediaObjectIdentifier is null!");
}
QueryManager qmnr = new QueryManager();
int connNr = qmnr.reserveConnection();
// build statement
String query = buildStatement_getSourceMI(moid);
result = qmnr.dbOpExec(query, connNr);
try {
if (result.next()) {
fileName = result.getString("location");
miNumber = result.getInt("id");
}
} catch (SQLException e) {
log.error("Query failed: " + query);
throw new MultiMonsterException("Query failed: " + query);
} finally {
qmnr.bringBackConn(connNr);
}
MIIdentifier mIId = new MIIdentifier(fileName, miNumber);
return mIId;
}
public void addMediaInstance(MediaInstance instance, MetaData meta)
throws MultiMonsterException {
ResultSet result = null;
int mo_number = 0;
MOIdentifier moid;
// TODO check Userrights
// check if all required data has been supplied
if (instance == null)
throw new MultiMonsterException("Instance is null!");
else if (meta == null)
throw new MultiMonsterException("Meta is null!");
else if (instance.getMOId() == null) {
throw new MultiMonsterException("MOID in Instance is null!");
} else {
moid = instance.getMOId();
mo_number = moid.getMoNumber();
}
QueryManager qmnr = new QueryManager();
int connNr = qmnr.reserveConnection();
// check first if there already exist an instance
int source = 0;
String count_query = "select count(*) from mediainstance where moid = "
+ mo_number + ";";
result = qmnr.dbOpExec(count_query, connNr);
try {
result.next();
int count = result.getInt(1);
if (count == 0)
source = 1;
log.debug("Mediainstance is source: " + source);
} catch (SQLException e2) {
log.error("Query failed: " + count_query);
}
// build statement
String query = buildStatement_addMI(moid, instance, meta, source);
log.debug("Query for inserting MI: " + query);
try {
qmnr.dbOpInsert(query, connNr);
} catch (MultiMonsterException e1) {
log.error(e1.getMessage());
}
result = qmnr.dbOpExec("SELECT LAST_INSERT_ID();", connNr);
try {
if (result.next()) {
int id = result.getInt(1);
log.debug("Mediainstance mit ID=" + id + " inserted.");
}
result.close();
} catch (SQLException e) {
throw new MultiMonsterException(
"Error while inserting an mediainstance!");
}
qmnr.bringBackConn(connNr);
}
public void remMediaInstance(MIIdentifier id) {
}
public InputOption[] getInputOptions(UserIdentifier user) {
// TODO implement correctly (by JM)
InputOption[] ios = null;
try {
QueryManager qmngr = new QueryManager();
int connNr = qmngr.reserveConnection();
ResultSet result = qmngr
.dbOpExec(
"select pr.protocolID, pr.longname "
+ "from plugin_proxy pl, protocol pr "
+ "where pl.protocolID = pr.protocolID and pl.isInput=1;",
connNr);
if (result != null) {
ArrayList io_arr = new ArrayList();
while (result.next()) {
// (1) = pr.protocolID
ProtocolId pId = new ProtocolId(result.getString(1));
// (2) = pr.longname
Protocol p = new Protocol(pId, result.getString(2));
io_arr.add(new InputOption(p));
}
ios = new InputOption[io_arr.size()];
ios = (InputOption[]) io_arr.toArray(ios);
} else {
log.error("The result for InputOptions was null!");
}
} catch (SQLException e) {
log.error("unable to get InputOptions", e);
}
return ios;
}
public OutputOption[] getOutputOptions(UserIdentifier user, MOIdentifier id) {
// TODO take respect of given user
try {
QueryManager qmngr = new QueryManager();
int connNr = qmngr.reserveConnection();
String query = "select pc.to_format, prot.protocolID, prot.longname "
+ "from mediaobject mo, mediainstance mi, videoformat vf, "
+ "plugin_converter pc, protocol prot, plugin_proxy pp, videoformat pc_vf "
+ "where mo.id = "
+ id.getMoNumber()
+ " and mo.id = mi.moid "
+ " and mi.vid_format = vf.id"
+ " and vf.codec_id = pc.from_codec"
+ " and vf.struc_id = pc.from_structure"
+ " and prot.protocolID = pp.protocolID"
+ " and pc_vf.id = pc.to_format"
+ " and pp.isInput = 0 "
+ " group by pc.to_format, pc_vf.formatName, prot.protocolID;";
ResultSet result = qmngr.dbOpExec(query, connNr);
log.debug("Statement for getOutputOptions " + query);
Vector outputList = new Vector();
if (result != null) {
while (result.next()) {
OutputOption outputOption = extract_OutputOption(result);
outputList.add(outputOption);
}
//Source Format is added to OutputOptions
// but when it is already contained in converted set
// don't take it once more
// list of already contained formatIDs
String formatIDList = "";
for(int i = 0; i < outputList.size(); i++) {
if(i!=0) {
formatIDList += ", ";
}
String listitem = ((OutputOption) outputList.get(i)).getFormat().getFormatId().getId();
formatIDList += "'" + listitem + "'";
}
log.debug("formatIDList which is taken out: " + query);
query = "select mi.vid_format, prot.protocolID, prot.longname "
+ "from mediaobject mo, mediainstance mi, videoformat vf, protocol prot, plugin_proxy pp "
+ "where mo.id = " + id.getMoNumber() + " "
+ "and mo.id = mi.moid "
+ "and mi.vid_format = vf.id "
+ "and prot.protocolID = pp.protocolID "
+ "and pp.isInput = 0 ";
if(outputList.size() > 0) {
query += "and mi.vid_format not in (" + formatIDList + ");";
}
log.debug("Query for source Format: " + query);
result = qmngr.dbOpExec(query, connNr);
if (result != null) {
while (result.next()) {
OutputOption outputOption = extract_OutputOption(result);
outputList.add(outputOption);
}
result.close();
}
// release Connection
qmngr.bringBackConn(connNr);
OutputOption[] alloptions = new OutputOption[outputList.size()];
alloptions = (OutputOption[]) outputList.toArray(alloptions);
log.debug("OutputOptions number of results: "
+ alloptions.length);
return alloptions;
} else {
// release Connection
qmngr.bringBackConn(connNr);
log.debug("The result for OutputOptions was null!");
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* welche Plugins stehen in der edit-Komponente zur verf�gung
*/
public void getFilterOptions() {
}
/**
* holt zu einer FormatID alle in der DB gespeicherten werte
*
* @param fId
* @return
*/
public Format getFormat(FormatId fId) {
Format format = null;
String formatID = fId.getId();
QueryManager qmngr = new QueryManager();
int connNr = qmngr.reserveConnection();
String query = "select codec_id, struc_id, res_lines, framerate, bitrate, formatName " +
"from videoformat vf "
+ "where vf.id = '" + formatID + "';";
ResultSet result = qmngr.dbOpExec(query, connNr);
log.debug("Statement for getFormat: " + query);
if (result != null) {
try {
if (result.next()) {
// FormatID aus DB holen und Objekt daraus erzeugen
String codec_id = result.getString("codec_id");
String struc_id = result.getString("struc_id");
int res_lines = result.getInt("res_lines");
int framerate = result.getInt("framerate");
int bitrate = result.getInt("bitrate");
String formatName = result.getString("formatName");
format = new Format(fId);
format.setBitrate(bitrate);
format.setFps(framerate);
format.setResolutionVertical(res_lines);
format.setCodec(new Codec(codec_id));
format.setStructure(new Structure(struc_id));
format.setDescription(formatName);
} else {
log.error("gefordertes Format existiert nicht!");
}
result.close();
} catch (SQLException e) {
log.error("Fehler beim holen von Format-Details!");
}
}
qmngr.bringBackConn(connNr);
return format;
// format = new Format(fId);
//
// if (fId.getId().equals(FormatId.fId_MPEG_1)) {
// format.setDescription("MPEG-1");
// } else if (fId.getId().equals(FormatId.fId_MPEG_1_LOW)) {
// format.setDescription("MPEG-1 Low Res");
// format.setResolutionHorizontal(320);
// format.setResolutionVertical(180);
// format.setBitrate(500);
// } else if (fId.getId().equals(FormatId.fId_MPEG_1_MID)) {
// format.setDescription("MPEG-1 Mid Res");
// format.setResolutionHorizontal(640);
// format.setResolutionVertical(360);
// format.setBitrate(1000);
// } else if (fId.getId().equals(FormatId.fId_MPEG_1_HI)) {
// format.setDescription("MPEG-1 HI Res");
// format.setResolutionHorizontal(640);
// format.setResolutionVertical(360);
// format.setBitrate(2000);
// } else if (fId.getId().equals(FormatId.fId_MPEG_2)) {
// format.setDescription("MPEG-2");
// } else if (fId.getId().equals(FormatId.fId_MPEG_4)) {
// format.setDescription("MPEG-4");
// } else {
// format.setDescription("unknown");
// }
}
public MetaDataAccess getMetaData(MOIdentifier mOId) {
try {
//String titleSearch = criteria.getTitle();
MetaDataAccess metaData = null;
QueryManager qmngr = new QueryManager();
int connNr = qmngr.reserveConnection();
ResultSet result = qmngr.dbOpExec(
"select mo.id, mo.title, mo.outline, mo.duration, mo.release_date "
+ "from mediaobject mo "
+ "where mediaobject.id = " + mOId.getMoNumber()
+ " ;", connNr);
if (result != null) {
if (result.next()) {
metaData = extract_Metadata(result);
}
result.close();
// release Connection
qmngr.bringBackConn(connNr);
return metaData;
} else {
// release Connection
qmngr.bringBackConn(connNr);
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/** **************************************************************** */
/** ******************* Helper Methods ***************************** */
/** **************************************************************** */
/**
* erzeugt aus einem passenden Recordset ein OutputOption Objekt
*/
private OutputOption extract_OutputOption(ResultSet result) {
String formatID = "";
String protocol = "";
String protocolName = "";
try {
formatID = result.getString(1);
protocol = result.getString(2);
protocolName = result.getString(3);
} catch (SQLException e) {
log.error("Fehler beim extrahieren der OutputOptions!");
return null;
}
log.debug("Values for OutputOptions extracted: " + formatID + ", " + protocol);
// two values successfully extracted
Format format = getFormat(new FormatId(formatID));
Protocol prot = new Protocol(new ProtocolId(protocol), protocolName);
OutputOption output = new OutputOption(format, prot);
return output;
}
/**
* erzeugt aus einem passenden Recordset das MetaDataAccess-Objekt
*/
private MetaDataAccess extract_Metadata(ResultSet result) {
//return null;
String title = "";
String outline = "";
String duration = "";
try {
int monr = result.getInt(1);
title = result.getString(2);
outline = result.getString(3);
duration = result.getString(4);
} catch (SQLException e) {
log.error("Fehler beim Zugriff auf Metadaten resultset!");
return null;
}
MetaDataAccess metadata = new MetaDataAccess();
metadata.setValue("Title", title);
metadata.setValue("Outline", outline);
metadata.setValue("Duration", duration);
return metadata;
}
private String buildStatement_addMO(MetaData meta)
throws MultiMonsterException {
String stmt = null;
// fields for DB
String title;
String outline;
if (meta != null) {
title = meta.getTitle();
outline = meta.getOutline();
if (title != null && title != "" && outline != null && outline != "") {
stmt = "insert into mediaobject values(null, '" + title
+ "', '" + outline + "', '" + "00:00:00" + "', NOW() );";
} else {
throw new MultiMonsterException("To less Metadata supplied!");
}
} else {
throw new MultiMonsterException("No Metadata supplied!");
}
return stmt;
}
private String buildStatement_remMO(MOIdentifier id)
throws MultiMonsterException {
String stmt = null;
if (id == null || id.getMoNumber() == 0) {
throw new MultiMonsterException("Empty MOIdentifier");
}
int moid = id.getMoNumber();
stmt = "delete from mediaobject where id = " + moid + ";";
return stmt;
}
private String buildStatement_addMI(MOIdentifier id, MediaInstance mi,
MetaData meta, int source) throws MultiMonsterException {
String stmt = null;
int moid = 0;
int resolution_vert = 0;
int resolution_hori = 0;
int bitrate = 0;
int framerate = 0;
String location = "";
String duration = "";
String vid_format = "UNKNOWN";
FormatId formatID = null;
int frames = 0;
if (id == null || id.getMoNumber() == 0) {
throw new MultiMonsterException("Empty MOIdentifier");
} else {
moid = id.getMoNumber();
}
if (mi == null || mi.getFormat() == null || mi.getFormat().getCodec() == null || mi.getFormat().getStructure() == null) {
log.info("Missing Argument in Format-Container");
//throw new MultiMonsterException("Missing Argument in Format-Container");
} else {
resolution_vert = mi.getFormat().getResolutionVertical();
resolution_hori = mi.getFormat().getResolutionHorizontal();
bitrate = mi.getFormat().getBitrate();
framerate = mi.getFormat().getFps();
formatID = getFormatbyProperties(mi.getFormat().getCodec()
.toString(), mi.getFormat().getStructure().toString(),
resolution_vert, framerate, bitrate);
vid_format = formatID.getId();
}
if (mi == null || mi.getIdentifier() == null) {
//throw new MultiMonsterException("Empty Identifier");
log.info("Empty Identifier");
} else {
location = mi.getIdentifier().getLocation();
}
if (meta == null || meta.getDuration() == null) {
//throw new MultiMonsterException("Empty Metadata");
log.info("Empty Metadata");
} else {
duration = meta.getDuration().getDuration();
frames = meta.getNumOfFrames();
}
stmt = "insert into mediainstance values(null, " + moid + ", '"
+ vid_format + "', " + resolution_hori + ", " + frames + ", '" + location + "', "
+ "'" + duration + "', " + source + ");";
return stmt;
}
/**
* schaut in der DB nach ob ein entsprechendes Format schon existiert wenn
* ja => dann wird die id ausgegeben wenn nein => neues format anlegen und
* id rausgeben.
*
* @return
*/
private FormatId getFormatbyProperties(String codec, String structure,
int res_lines, int framerate, int bitrate) {
FormatId formatid = null;
QueryManager qmngr = new QueryManager();
int connNr = qmngr.reserveConnection();
String query = "select vf.id " + "from videoformat vf "
+ "where vf.codec_id = '" + codec + "'"
+ " and vf.struc_id = '" + structure + "'"
+ " and vf.res_lines = " + res_lines + " and vf.framerate = "
+ framerate + " and vf.bitrate = " + bitrate + ";";
ResultSet result = qmngr.dbOpExec(query, connNr);
log.debug("Statement for getFormatbyProperties: " + query);
if (result != null) {
try {
if (result.next()) {
// FormatID aus DB holen und Objekt daraus erzeugen
String dbFormatString = result.getString("id");
formatid = new FormatId(dbFormatString);
} else {
// DB query-result ist leer => system kennt dieses Format
// nicht
// => das Format neu anlegen
String vidid = "in_" + codec + structure + res_lines
+ framerate + bitrate;
String vidname = vidid;
query = "insert into videoformat values( " + "'" + vidid
+ "', '" + codec + "', '" + structure + "', "
+ res_lines + ", " + framerate + ", " + bitrate
+ ", '" + vidname + "');";
log.debug("Neues Format anlegen: " + query);
qmngr.dbOpInsert(query, connNr);
formatid = new FormatId(vidid);
}
} catch (SQLException e) {
log.error("Fehler beim Lesen aus Result!");
} catch (MultiMonsterException e) {
log.error("Fehler beim Insert aufgetreten!");
}
} else {
log.error("Fehler bei der Format feststellung!");
}
try {
result.close();
} catch (SQLException e) {
log.error("Fehler beim schliessen des result-sets");
}
qmngr.bringBackConn(connNr);
return formatid;
}
private String buildStatement_getSourceMI(MOIdentifier id)
throws MultiMonsterException {
String stmt = null;
int moid;
if (id == null || id.getMoNumber() == 0) {
throw new MultiMonsterException("Empty MOIdentifier");
} else {
moid = id.getMoNumber();
}
stmt = "select id, location from mediainstance where moid = " + moid
+ " and source = 1";
return stmt;
}
}