package fr.irisa.ProfileRegistry.client;
import ihe.net.profileregistry.client.ProfileRegistryStub;

import java.io.*;
import java.net.URL;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;

public class ProfileRegistryMainClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ihe.net.profileregistry.client.ProfileRegistryStub stub = null;
		if(args.length<1)
		{

			System.out.println("Available services are:\t"+ "queryRepositoryForProfiles()\t"+"getProfile(string profileoid)\t"+"queryRepository(String profileoid)\t");
			System.out.println("Give an oid and we'll test the 3 services\t");
			System.exit(0);	
		}
		else
		{
			try{
				stub = new ProfileRegistryStub("http://wella.irisa.fr:8080/axis2/services/ProfileRegistry");
				ProfileRegistryStub.QueryRepositoryForProfiles myquery = new ProfileRegistryStub.QueryRepositoryForProfiles();
				//ProfileRegistryStub.QueryRepositoryForProfilesResponse myresponse;
				int valeur = stub.queryRepositoryForProfiles(myquery).getOut().getOID_Element().length;
				for(int i=0; i<valeur;i++)
					System.out.println(stub.queryRepositoryForProfiles(myquery).getOut().getOID_Element()[i].getOID_Type());

				ProfileRegistryStub.GetProfile myprofile = new ProfileRegistryStub.GetProfile();
				ProfileRegistryStub.OID_Type profile_oid = new ProfileRegistryStub.OID_Type();
				profile_oid.setOID_Type(args[0]);
				myprofile.setIn(profile_oid);
				ProfileRegistryStub.GetProfileResponse  profileresponse = stub.getProfile(myprofile);
				System.out.println("profileresponse\t"+profileresponse.getOut().getProfile_Type());

				//To get the content of the profile from an URL define here (don't forget to add in the server code

				URL profileurl = new URL("http://wella.irisa.fr/"+profileresponse.getOut().getProfile_Type());
				BufferedReader in = new BufferedReader(new InputStreamReader(profileurl.openStream()));
				String str;
				while ((str = in.readLine()) != null) {
					System.out.println(str);
				}
				in.close();




				ProfileRegistryStub.QueryRepository isIn = new  ProfileRegistryStub.QueryRepository();
				isIn.setIn(profile_oid);
				System.out.println("isIn\t"+stub.queryRepository(isIn).getOut());

			}catch (AxisFault e){	
				//stub can not be create, check your URI 
				e.printStackTrace();	}
			catch (RemoteException e1) {
				//related to the transaction with the stub

				e1.printStackTrace();
			}catch(IOException e){
				e.printStackTrace();
			}

		}


	}

}
