package eu.dnetlib.clients;

import eu.dnetlib.clients.enabling.islookup.ws.ISLookUpClient;
import eu.dnetlib.clients.enabling.isregistry.ws.ISRegistryClient;
import eu.dnetlib.domain.enabling.SecurityProfile;
import eu.dnetlib.domain.functionality.UserProfile;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
import gr.uoa.di.driver.enabling.ISRegistry;
import gr.uoa.di.driver.enabling.ISRegistryException;
import gr.uoa.di.driver.enabling.islookup.ISLookUpImpl;
import gr.uoa.di.driver.enabling.registry.ISRegistryImpl;
import gr.uoa.di.driver.enabling.registry.SecurityAwareRegistryImpl;
import gr.uoa.di.driver.util.StaticServiceLocator;
import gr.uoa.di.driver.xml.SecurityProfileXmlConverter;
import gr.uoa.di.driver.xml.UserProfileXmlConverter;

import javax.xml.bind.JAXBException;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestISRegistry {

	private static String registryAddress = "http://dl114.madgik:8280/is/services/isRegistry";
	private static String lookupAdress = " http://dl114.madgik:8280/is/services/isLookUp";
	private static ISRegistry<UserProfile> userProfileRegistry = null;
	
	private static ISRegistryService registryEndpoint = null; 

	@BeforeClass
	public static void setup() throws JAXBException {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(ISRegistryService.class);
		factory.setAddress(registryAddress);
		registryEndpoint = (ISRegistryService) factory
				.create();

		factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(ISLookUpService.class);
		factory.setAddress(lookupAdress);
		ISLookUpService lookupEndpoint = (ISLookUpService) factory.create();

		ISRegistryClient registryClient = new ISRegistryClient();
		registryClient.setIsRegistry(registryEndpoint);

		ISLookUpClient lookupClient = new ISLookUpClient();
		lookupClient.setIsLookUp(lookupEndpoint);

		StaticServiceLocator<eu.dnetlib.api.enabling.ISRegistryService> registryLocator = new StaticServiceLocator<eu.dnetlib.api.enabling.ISRegistryService>();
		registryLocator.setService(registryClient);
		StaticServiceLocator<eu.dnetlib.api.enabling.ISLookUpService> lookupLocator = new StaticServiceLocator<eu.dnetlib.api.enabling.ISLookUpService>();
		lookupLocator.setService(lookupClient);

		ISRegistryImpl<UserProfile> registryImpl = new ISRegistryImpl<UserProfile>();

		registryImpl.setConverter(new UserProfileXmlConverter());
		registryImpl.setResourceUri("http://I.dont.matter.com");
		registryImpl.setRegistryLocator(registryLocator);

		ISRegistryImpl<SecurityProfile> securityProfileRegistry = new ISRegistryImpl<SecurityProfile>();
		securityProfileRegistry.setConverter(new SecurityProfileXmlConverter());
		securityProfileRegistry.setRegistryLocator(registryLocator);
		securityProfileRegistry.setResourceUri("http://I.dont.matter.com");

		ISLookUpImpl<SecurityProfile> securityProfileLookup = new ISLookUpImpl<SecurityProfile>();
		securityProfileLookup.setConverter(new SecurityProfileXmlConverter());
		securityProfileLookup.setLookupLocator(lookupLocator);

		SecurityAwareRegistryImpl<UserProfile> secureRegistry = new SecurityAwareRegistryImpl<UserProfile>();

		secureRegistry.setRegistry(registryImpl);
		secureRegistry.setRegistryServiceLocator(registryLocator);
		secureRegistry.setSecurityRegistry(securityProfileRegistry);
		secureRegistry.setSecurityLookUp(securityProfileLookup);

		userProfileRegistry = secureRegistry;
	}

//	@Test
	public void test() {
		try {
			UserProfile profile = new UserProfile();

			profile.setEmail("antleb@di.uoa.gr");

			System.out.println(userProfileRegistry.save(profile).getResourceId());
			System.out.println(profile.getSecurityProfile().getResourceId());
			
			userProfileRegistry.delete(profile);
		} catch (ISRegistryException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void tt() throws eu.dnetlib.enabling.is.registry.rmi.ISRegistryException {
		registryEndpoint.addOrUpdateResourceType("CSWHarvestResourceType", "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">    <xs:element name=\"RESOURCE_PROFILE\">        <xs:complexType>            <xs:sequence>                <xs:element name=\"HEADER\" type=\"HEADERType\"/>                <xs:element name=\"BODY\" type=\"BODYType\"/>            </xs:sequence>        </xs:complexType>    </xs:element>    <xs:complexType name=\"HEADERType\">        <xs:all>            <xs:element name=\"RESOURCE_IDENTIFIER\" type=\"RESOURCE_IDENTIFIERType\"/>            <xs:element name=\"RESOURCE_TYPE\" type=\"RESOURCE_TYPEType\"/>            <xs:element name=\"RESOURCE_KIND\" type=\"RESOURCE_KINDType\"/>            <xs:element name=\"RESOURCE_URI\" type=\"RESOURCE_URIType\"/>            <xs:element name=\"DATE_OF_CREATION\" type=\"DATE_OF_CREATIONType\"/>            <xs:element name=\"PROTOCOL\" type=\"xs:string\"/>        </xs:all>    </xs:complexType>    <xs:complexType name=\"BODYType\">        <xs:sequence>            <xs:element name=\"CONFIGURATION\" type=\"CONFIGURATIONType\"/>            <xs:element name=\"STATUS\" type=\"STATUSType\"/>            <xs:element name=\"QOS\" type=\"QOSType\"/>            <xs:element name=\"SECURITY_PARAMETERS\" type=\"xs:string\"/>            <xs:element name=\"BLACKBOARD\" type=\"BLACKBOARDType\"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name=\"BLACKBOARDType\">        <xs:sequence>            <xs:element name=\"LAST_REQUEST\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"1\"/>            <xs:element name=\"LAST_RESPONSE\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"1\"/>            <xs:element name=\"MESSAGE\" type=\"MESSAGEType\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name=\"CONFIGURATIONType\">        <xs:all>            <xs:element name=\"MAX_SIZE_OF_DATASTRUCTURE\" type=\"xs:integer\"/>            <xs:element name=\"AVAILABLE_DISKSPACE\" type=\"xs:string\"/>            <xs:element name=\"MAX_NUMBER_OF_DATASTRUCTURE\" type=\"xs:integer\"/>          <xs:element name=\"CSW_IDENTIFIER\" type=\"xs:string\" />               <xs:element name=\"DATE_FROM\" type=\"xs:string\" />            <xs:element name=\"DATE_TO\" type=\"xs:string\" />              <xs:element name=\"OBJECT_TYPE\" type=\"xs:string\" />        </xs:all>    </xs:complexType>    <xs:complexType name=\"LAST_UPDATEType\">        <xs:attribute name=\"value\" type=\"xs:dateTime\" use=\"required\"/>    </xs:complexType>    <xs:complexType mixed=\"true\" name=\"STATUSType\">        <xs:all>            <xs:element name=\"HANDLED_DATASTRUCTURE\" type=\"xs:string\" minOccurs=\"0\"/>            <xs:element name=\"USED_DISKSPACE\" type=\"xs:string\" minOccurs=\"0\"/>            <xs:element name=\"NUMBER_OF_OBJECTS\" type=\"xs:integer\"/>            <xs:element name=\"LAST_UPDATE\" type=\"LAST_UPDATEType\" minOccurs=\"0\"/>        </xs:all>    </xs:complexType>    <xs:simpleType name=\"ACTION_STATUSType\">        <xs:restriction base=\"xs:string\">            <xs:enumeration value=\"DONE\"/>            <xs:enumeration value=\"ONGOING\"/>            <xs:enumeration value=\"FAILED\"/>            <xs:enumeration value=\"WAITING\"/>            <xs:enumeration value=\"ASSIGNED\"/>        </xs:restriction>    </xs:simpleType>      <xs:complexType name=\"RESOURCE_TYPEType\">        <xs:attribute name=\"value\" use=\"required\">            <xs:simpleType>                <xs:restriction base=\"xs:string\">                    <xs:enumeration value=\"CSWHarvestResourceType\"/>                </xs:restriction>            </xs:simpleType>        </xs:attribute>    </xs:complexType>    <xs:complexType name=\"RESOURCE_URIType\">        <xs:attribute name=\"value\" type=\"xs:string\" use=\"required\"/>    </xs:complexType>    <xs:complexType name=\"RESOURCE_KINDType\">        <xs:attribute name=\"value\" use=\"required\">            <xs:simpleType>                <xs:restriction base=\"xs:string\">                    <xs:enumeration value=\"CSWHarvestResourceResources\"/>                </xs:restriction>            </xs:simpleType>        </xs:attribute>    </xs:complexType>    <xs:complexType name=\"RESOURCE_IDENTIFIERType\">        <xs:attribute name=\"value\" type=\"xs:string\" use=\"required\"/>    </xs:complexType>    <xs:complexType name=\"DATE_OF_CREATIONType\">        <xs:attribute name=\"value\" type=\"xs:dateTime\" use=\"required\"/>    </xs:complexType>    <xs:complexType name=\"QOSType\">        <xs:sequence>            <xs:element name=\"AVAILABILITY\" type=\"xs:string\"/>            <xs:element name=\"CAPACITY\" type=\"xs:string\"/>            <xs:element name=\"RESPONSE_TIME\" type=\"xs:string\" minOccurs=\"0\"/>            <xs:element name=\"THROUGHPUT\" type=\"xs:double\"/>        </xs:sequence>    </xs:complexType>    <xs:complexType name=\"PARAMETERSType\">        <xs:simpleContent>            <xs:extension base=\"xs:string\">                <xs:attribute name=\"name\" type=\"xs:string\" use=\"required\"/>            </xs:extension>        </xs:simpleContent>    </xs:complexType>    <xs:complexType name=\"MESSAGEType\">        <xs:sequence>            <xs:element name=\"ACTION\" type=\"ACTIONType\"/>            <xs:element name=\"PARAMETERS\" type=\"PARAMETERSType\" maxOccurs=\"unbounded\"/>            <xs:element name=\"ACTION_STATUS\" type=\"ACTION_STATUSType\"/>        </xs:sequence>        <xs:attribute name=\"id\" type=\"xs:string\" use=\"required\"/>    </xs:complexType>    <xs:simpleType name=\"ACTIONType\">        <xs:restriction base=\"xs:string\">            <xs:enumeration value=\"CREATE\"/>            <xs:enumeration value=\"DELETE\"/>            <xs:enumeration value=\"UPDATE\"/>            <xs:enumeration value=\"MANAGE\"/>            <xs:enumeration value=\"RELEASE\"/>            <xs:enumeration value=\"CANCEL\"/>        </xs:restriction>    </xs:simpleType></xs:schema>");
	}
	
	public static void main(String[] args) throws JAXBException {
		
		new TestISRegistry().test();
	}
}