2009-09-30 11:29:45 +02:00
|
|
|
package org.gcube.vremanagement.vremodeler.consumers;
|
|
|
|
|
|
|
|
import java.io.StringReader;
|
2009-12-21 16:28:25 +01:00
|
|
|
import java.io.StringWriter;
|
2009-09-30 11:29:45 +02:00
|
|
|
|
|
|
|
import javax.xml.namespace.QName;
|
|
|
|
|
|
|
|
import org.gcube.common.core.contexts.GHNContext;
|
|
|
|
import org.gcube.common.core.informationsystem.client.AtomicCondition;
|
|
|
|
import org.gcube.common.core.informationsystem.client.ISClient;
|
|
|
|
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery;
|
|
|
|
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericResourceQuery;
|
|
|
|
import org.gcube.common.core.informationsystem.notifier.ISNotifier.BaseNotificationConsumer;
|
|
|
|
import org.gcube.common.core.informationsystem.notifier.ISNotifier.NotificationEvent;
|
2009-11-27 17:20:12 +01:00
|
|
|
import org.gcube.common.core.resources.GCUBEGenericResource;
|
2009-09-30 11:29:45 +02:00
|
|
|
import org.gcube.common.core.scope.GCUBEScope;
|
|
|
|
import org.gcube.common.core.utils.logging.GCUBELog;
|
|
|
|
import org.gcube.vremanagement.vremodeler.impl.ServiceContext;
|
|
|
|
import org.gcube.vremanagement.vremodeler.resources.handlers.FunctionalityHandler;
|
2009-11-27 17:20:12 +01:00
|
|
|
import org.gcube.vremanagement.vremodeler.resources.handlers.GenericResourceHandler;
|
2009-09-30 11:29:45 +02:00
|
|
|
import org.gcube.vremanagement.vremodeler.resources.kxml.KGCUBEGenericFunctionalityResource;
|
|
|
|
|
2009-11-27 17:20:12 +01:00
|
|
|
public class GenericResourceConsumer extends BaseNotificationConsumer{
|
2009-09-30 11:29:45 +02:00
|
|
|
|
|
|
|
private GCUBELog logger= new GCUBELog(GHNConsumer.class);
|
|
|
|
|
|
|
|
public static final QName functionalityQName= new QName("http://gcube-system.org/namespaces/informationsystem/registry","GenericResource");
|
|
|
|
|
2009-11-27 17:20:12 +01:00
|
|
|
private String functionalityResourceId;
|
2009-09-30 11:29:45 +02:00
|
|
|
private GCUBEScope scope;
|
|
|
|
|
2009-11-27 17:20:12 +01:00
|
|
|
public GenericResourceConsumer(GCUBEScope scope, String resourceId){
|
2009-09-30 11:29:45 +02:00
|
|
|
super();
|
|
|
|
this.scope=scope;
|
2009-11-27 17:20:12 +01:00
|
|
|
this.functionalityResourceId= resourceId;
|
2009-09-30 11:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onNotificationReceived(NotificationEvent event){
|
|
|
|
try{
|
|
|
|
ServiceContext.getContext().setScope(this.scope);
|
|
|
|
String id= event.getPayload().getMessage()[0].getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
|
|
|
|
String operation=event.getPayload().getMessage()[0].getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
|
2009-10-14 11:57:31 +02:00
|
|
|
logger.info("notification received for genericResource "+id+" and operation "+operation);
|
2009-11-27 17:20:12 +01:00
|
|
|
|
2009-12-21 16:28:25 +01:00
|
|
|
if (id.compareTo(this.functionalityResourceId)==0 && (operation.compareTo("update")==0)){
|
2009-10-14 11:57:31 +02:00
|
|
|
logger.trace("notification received for functionalityResource with id "+id+" in scope "+scope.toString());
|
2009-09-30 11:29:45 +02:00
|
|
|
KGCUBEGenericFunctionalityResource resource= new KGCUBEGenericFunctionalityResource();
|
|
|
|
ISClient client=GHNContext.getImplementation(ISClient.class);
|
|
|
|
GCUBEGenericResourceQuery query= client.getQuery(GCUBEGenericResourceQuery.class);
|
|
|
|
query.addAtomicConditions(new AtomicCondition("/Profile/Name","FuctionalitiesResource"), new AtomicCondition("/Profile/SecondaryType","VREModelerResource"));
|
|
|
|
GCUBEGenericQuery genericQuery= client.getQuery(GCUBEGenericQuery.class);
|
|
|
|
genericQuery.setExpression(query.getExpression());
|
2009-10-14 11:57:31 +02:00
|
|
|
Thread.sleep(60000);
|
2009-09-30 11:29:45 +02:00
|
|
|
resource.load(new StringReader(client.execute(genericQuery, ServiceContext.getContext().getScope()).get(0).toString()));
|
2009-10-14 11:57:31 +02:00
|
|
|
//FunctionalityHandler
|
2009-09-30 11:29:45 +02:00
|
|
|
FunctionalityHandler functionalityHandler= new FunctionalityHandler();
|
|
|
|
functionalityHandler.add(resource);
|
2009-11-27 17:20:12 +01:00
|
|
|
}else if (operation.compareTo("create")==0){
|
|
|
|
logger.trace("notification received for generic resource with operation create");
|
|
|
|
ISClient client=GHNContext.getImplementation(ISClient.class);
|
|
|
|
GCUBEGenericResourceQuery query= client.getQuery(GCUBEGenericResourceQuery.class);
|
2009-12-21 16:28:25 +01:00
|
|
|
query.addAtomicConditions(new AtomicCondition("/ID",id));
|
2009-11-27 17:20:12 +01:00
|
|
|
GCUBEGenericResource resource =client.execute(query, ServiceContext.getContext().getScope()).get(0);
|
2009-12-21 16:28:25 +01:00
|
|
|
|
|
|
|
if (resource.getName().compareTo("FuctionalitiesResource")==0){
|
|
|
|
FunctionalityHandler functionalityHandler= new FunctionalityHandler();
|
|
|
|
KGCUBEGenericFunctionalityResource functResource=new KGCUBEGenericFunctionalityResource();
|
|
|
|
StringWriter sw= new StringWriter();
|
|
|
|
resource.store(sw);
|
|
|
|
functResource.load(new StringReader(sw.toString()));
|
|
|
|
functionalityHandler.add(functResource);
|
|
|
|
} else
|
|
|
|
if (ServiceContext.getContext().getSecondaryTypeGenericResourceRequired().contains(resource.getSecondaryType()))
|
|
|
|
new GenericResourceHandler().add(resource);
|
2009-11-27 17:20:12 +01:00
|
|
|
}else if (operation.compareTo("destroy")==0){
|
|
|
|
logger.trace("notification received for generic resource with operation destroy");
|
|
|
|
new GenericResourceHandler().drop(id);
|
2009-09-30 11:29:45 +02:00
|
|
|
}
|
|
|
|
|
2009-10-14 11:57:31 +02:00
|
|
|
}catch(Exception e){logger.error("error in functionality notification",e);}
|
2009-09-30 11:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|