/*
 * Copyright 2010 PC-NG Inc. All rights reserved.
 * PC-NG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nexiwave.search.client.sample;

import java.io.File;

import javax.activation.DataHandler;

import com.nexiwave.speechindexing.client.SpeechIndexingStub;
import com.nexiwave.speechindexing.client.SpeechIndexingStub.AddResponse;
import com.nexiwave.speechindexing.client.SpeechIndexingStub.AudioUploadData;

public class UploadAudioSample {

	public long addByURL(String email, String passwd) throws Exception {
		// testing add audio:
		SpeechIndexingStub.Add req = new SpeechIndexingStub.Add();

		SpeechIndexingStub.AuthData authData = new SpeechIndexingStub.AuthData();
		authData.setEmail(email);
		authData.setPasswd(passwd);

		req.setAuthData(authData);

		AudioUploadData uploadData = new AudioUploadData();
		uploadData.setMediaURL("http://www.mycompany.com/audio/123.mp3");
		req.setData(uploadData);

		SpeechIndexingStub stub = new SpeechIndexingStub(
				"https://api.nexiwave.com/SpeechIndexing/services/SpeechIndexing");

		// make the call:
		AddResponse resAdd = stub.add(req);

		// obtain response:
		long newRecordingId = resAdd.get_return();
		System.out.println("add result " + newRecordingId);

		if (newRecordingId <= 0)
			throw new RuntimeException("Hmm, invalid recording id returned.");

		return newRecordingId;

	}

	public long addLocalFile(String email, String passwd, File audio) throws Exception {
		// testing add audio:
		SpeechIndexingStub.Add req = new SpeechIndexingStub.Add();

		SpeechIndexingStub.AuthData authData = new SpeechIndexingStub.AuthData();
		authData.setEmail(email);
		authData.setPasswd(passwd);

		req.setAuthData(authData);

		AudioUploadData uploadData = new AudioUploadData();
		uploadData.setMediaFileData(new DataHandler(audio.toURI().toURL()));
		req.setData(uploadData);

		SpeechIndexingStub stub = new SpeechIndexingStub(
				"https://api.nexiwave.com/SpeechIndexing/services/SpeechIndexing");

		// make the call:
		AddResponse resAdd = stub.add(req);

		// obtain response:
		long newRecordingId = resAdd.get_return();
		System.out.println("add result " + newRecordingId);

		if (newRecordingId <= 0)
			throw new RuntimeException("Hmm, invalid recording id returned.");

		return newRecordingId;

	}

}
