vre-modeler/src/org/gcube/vremanagement/vremodeler/consumers/GenericResourceConsumer.java

84 lines
4.4 KiB
Java
Raw Normal View History

package org.gcube.vremanagement.vremodeler.consumers;
import java.io.StringReader;
import java.io.StringWriter;
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;
import org.gcube.common.core.resources.GCUBEGenericResource;
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;
import org.gcube.vremanagement.vremodeler.resources.handlers.GenericResourceHandler;
import org.gcube.vremanagement.vremodeler.resources.kxml.KGCUBEGenericFunctionalityResource;
public class GenericResourceConsumer extends BaseNotificationConsumer{
private GCUBELog logger= new GCUBELog(GHNConsumer.class);
public static final QName functionalityQName= new QName("http://gcube-system.org/namespaces/informationsystem/registry","GenericResource");
private String functionalityResourceId;
private GCUBEScope scope;
public GenericResourceConsumer(GCUBEScope scope, String resourceId){
super();
this.scope=scope;
this.functionalityResourceId= resourceId;
}
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();
logger.info("notification received for genericResource "+id+" and operation "+operation);
if (id.compareTo(this.functionalityResourceId)==0 && (operation.compareTo("update")==0)){
logger.trace("notification received for functionalityResource with id "+id+" in scope "+scope.toString());
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());
Thread.sleep(60000);
resource.load(new StringReader(client.execute(genericQuery, ServiceContext.getContext().getScope()).get(0).toString()));
//FunctionalityHandler
FunctionalityHandler functionalityHandler= new FunctionalityHandler();
functionalityHandler.add(resource);
}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);
query.addAtomicConditions(new AtomicCondition("/ID",id));
GCUBEGenericResource resource =client.execute(query, ServiceContext.getContext().getScope()).get(0);
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);
}else if (operation.compareTo("destroy")==0){
logger.trace("notification received for generic resource with operation destroy");
new GenericResourceHandler().drop(id);
}
}catch(Exception e){logger.error("error in functionality notification",e);}
}
}