package org.gcube.vremanagement.vremodeler.impl; import java.net.URI; import java.rmi.RemoteException; import java.sql.Date; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import javax.xml.namespace.QName; import org.gcube.common.core.faults.GCUBEFault; import org.gcube.common.core.faults.GCUBEUnrecoverableFault; import org.gcube.common.core.types.VOID; import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.vremanagement.vremodeler.db.DBInterface; import org.gcube.vremanagement.vremodeler.impl.thread.DeployVRE; import org.gcube.vremanagement.vremodeler.impl.util.ModelerCollection; import org.gcube.vremanagement.vremodeler.impl.util.XMLUtil; import org.gcube.vremanagement.vremodeler.resources.MetadataFormat; import org.gcube.vremanagement.vremodeler.stubs.CheckStatusResponse; import org.gcube.vremanagement.vremodeler.stubs.CollectionArray; import org.gcube.vremanagement.vremodeler.stubs.FunctionalityIDArray; import org.gcube.vremanagement.vremodeler.stubs.GHNArray; import org.gcube.vremanagement.vremodeler.stubs.SetMDFormatArgs; import org.gcube.vremanagement.vremodeler.stubs.VRERequest; import org.globus.wsrf.ResourceException; import org.globus.wsrf.encoding.ObjectSerializer; import org.globus.wsrf.encoding.SerializationException; public class ModelerService { public static final String INPROGRESS="In Progress"; public static final String DEPLOYED="Deployed"; public static final String FAILED="Failed"; public static final String PENDING="Pending"; public static final String DEPLOYING="Deploying"; private static final GCUBELog logger = new GCUBELog(ModelerService.class.getName()); protected ModelerResource getResource() throws ResourceException{ return (ModelerResource) ModelerContext.getPortTypeContext().getWSHome().find(); } /** * * @param request void * @return VoidType * @throws RemoteException - * */ public void setVREModel(VRERequest request) throws GCUBEFault{ Date dateFrom=null, dateTo=null; ResultSet res=null; try { res=DBInterface.queryDB("select * from VRE where VRE.id='"+getResource().getId()+"'; "); if(res.next()) { dateFrom=new Date(request.getStartTime()); dateTo=new Date(request.getEndTime()); DBInterface.queryDB("update VRE set name='"+request.getVREName()+"', description='"+request.getVREDescription()+"', vredesigner='"+request.getVREDesigner()+"', vremanager= '"+request.getVREManager()+"', intervalfrom='"+dateFrom+"', intervalto='"+dateTo+"' where VRE.id='"+getResource().getId()+"'; "); }else{ dateFrom=new Date(request.getStartTime()); dateTo=new Date(request.getEndTime()); String eprToString; try { eprToString = ObjectSerializer.toString(getResource().getEPR(), new QName(ModelerContext.getPortTypeContext().getNamespace(), "ModelResourceReference")); //logger.debug("EPR: "+eprToString); } catch (SerializationException e) { //logger.error("VDL Model Service: error serializing EPR"); logger.error("VDL Model Service: error serializing EPR",e); throw new GCUBEFault(e); } DBInterface.ExecuteUpdate("insert into VRE values('"+getResource().getId()+"','"+request.getVREName()+"','"+request.getVREDescription()+"','"+request.getVREDesigner()+"','"+request.getVREManager()+"','"+dateFrom+"','"+dateTo+"','"+eprToString+"','"+INPROGRESS+"'); "); } } catch (Exception e) { logger.error(e); throw new GCUBEFault(e); } } /** * * * @param request void * @return VoidType * @throws RemoteException - */ public String getVREModel(VOID var) throws GCUBEFault{ String toReturn =null; ResultSet res=null; //logger.debug("Request DL with ID "+ID); try { String ID=getResource().getId(); DBInterface.connect(); res=DBInterface.queryDB("select * from VRE where VRE.id='"+ID+"';"); toReturn=XMLUtil.PrepareVREModelXML(res); } catch (Exception e) { logger.error("VDLModel parsing error in function getDLModel ",e); throw new GCUBEFault(e); } return toReturn; } /** * return an XML containing the DHN list * * @param request void * @return a XML String * @throws RemoteException - */ public String getGHNs(VOID var) throws GCUBEFault{ logger.info("getGHNs method"); ResultSet res; ArrayList relatedGHN=new ArrayList(); try{ DBInterface.connect(); res= DBInterface.queryDB("select * from GHN ORDER BY GHN.host;"); ResultSet resRelated= DBInterface.queryDB("select ghnid from VRERELATEDGHN where VRERELATEDGHN.vreid='"+getResource().getId()+"';"); while(resRelated.next()) relatedGHN.add(resRelated.getString(1)); }catch(Exception e) { logger.error("VDL Model: error retreiving GHNs",e); throw new GCUBEFault(e);} return XMLUtil.PrepareGHNsXML(res, relatedGHN); } /** * Set the selected DHN * * @param request array of GHN id * @return VoidType - * @throws RemoteException - */ public void setGHNs(GHNArray request) throws GCUBEFault{ try{ String instanceID=(String) getResource().getId(); DBInterface.ExecuteUpdate("DELETE FROM VRERELATEDGHN WHERE VRERELATEDGHN.vreid='"+instanceID+"';"); String dlKey=instanceID; ArrayList values= new ArrayList(request.getGHNElement().length); ArrayList row=null; for (String ghnId: request.getGHNElement()) { row= new ArrayList(2); row.add(dlKey); row.add(ghnId); values.add(row.toArray(new String[2])); } DBInterface.insertInto("VRERELATEDGHN", values.toArray(new String[0][0])); }catch(Exception e){ logger.error("error setting GHNs ",e); throw new GCUBEFault(e);} } /** * * Return an XML with all the collection selectable * * @param request void * @return the collection retrieved from the database * @throws RemoteException - */ public String getCollection(VOID var) throws GCUBEFault{ ResultSet res; ArrayList relatedCollection=new ArrayList(); try{ DBInterface.connect(); res= DBInterface.queryDB("select * from COLLECTION;"); ResultSet resRelated= DBInterface.queryDB("select collid from VRERELATEDCOLLECTION where VRERELATEDCOLLECTION.vreid='"+getResource().getId()+"';"); while(resRelated.next()){ relatedCollection.add(resRelated.getString(1)); } }catch(Exception e) { logger.error("error getting collections",e); throw new GCUBEFault(e);} return XMLUtil.PrepareCollectionXML(res, relatedCollection); } /** * * Insert in the DB the selected collection * * @param request An Array that contains the selected collections ids * @return void * @throws RemoteException - */ public void setCollection(CollectionArray request) throws GCUBEFault{ try{ logger.trace("method setCollection Called"); DBInterface.connect(); DBInterface.ExecuteUpdate("DELETE FROM VRERELATEDCOLLECTION WHERE VRERELATEDCOLLECTION.vreid='"+getResource().getId()+"';"); String dlKey=(String) getResource().getId(); ArrayList values= new ArrayList(request.getCollectionElement().length); ArrayList row=null; logger.trace("the collection set for"+ dlKey+" are "+request.getCollectionElement().length); for (int i=0; i(2); row.add(dlKey); row.add(request.getCollectionElement()[i]); values.add(row.toArray(new String[2])); } DBInterface.connect(); DBInterface.insertInto("VRERELATEDCOLLECTION", values.toArray(new String[0][0])); }catch(Exception e){ logger.error("error setting collection",e); throw new GCUBEFault(e); } } /** * Return the possibles metadata format for selected collection * * @param request void * @return a XML format string * @throws RemoteException - */ public String getMetadataRelatedToCollection(VOID var) throws GCUBEFault { Hashtable> nativeMetadataFormat; Hashtable> selectedMetadataFormat; try{ DBInterface.connect(); //retrieve all possible metadataformat for the selected collection nativeMetadataFormat= new Hashtable>(); ResultSet allPossibleMDF= DBInterface.queryDB("select rel.collid, mdf.id, mdf.name, mdf.schemauri, mdf.language, coll.name, coll.description from COLLECTION as coll, VRERELATEDCOLLECTION as rel, MCOLLECTION as mcoll, METADATAFORMAT as mdf, NATIVEMDF as native where rel.vreid='"+getResource().getId()+"' and mcoll.relatedcollectionid=rel.collid and mdf.id=native.mdfid and native.mdcollid=mcoll.id and coll.id=rel.collid;"); while (allPossibleMDF.next()){ ModelerCollection collection= new ModelerCollection(allPossibleMDF.getString(1), allPossibleMDF.getString(7), allPossibleMDF.getString(6) ); MetadataFormat mf= new MetadataFormat(allPossibleMDF.getString(2), allPossibleMDF.getString(3), new URI(allPossibleMDF.getString(4)), allPossibleMDF.getString(5)); if (nativeMetadataFormat.containsKey(collection)){ nativeMetadataFormat.get(collection).add(mf); }else { ArrayList keyList=new ArrayList(); keyList.add(mf); nativeMetadataFormat.put(collection, keyList); } } //retrieving selected metadaformat //hashtable selectedMetadataFormat= new Hashtable>(); ResultSet selectedFormat= DBInterface.queryDB("select rel.collid, mf.id, mf.name, mf.schemauri, mf.language , coll.name, coll.description from COLLECTION as coll, VRERELATEDMETADATAFORMAT as rel, METADATAFORMAT as mf where mf.id=rel.mfid and rel.vreid='"+getResource().getId()+"' and coll.id=rel.collid;"); while (selectedFormat.next()){ ModelerCollection collection= new ModelerCollection(selectedFormat.getString(1), selectedFormat.getString(7), selectedFormat.getString(6)); MetadataFormat mf= new MetadataFormat(selectedFormat.getString(2),selectedFormat.getString(3), new URI(selectedFormat.getString(4)), selectedFormat.getString(5)); if (selectedMetadataFormat.containsKey(collection)) selectedMetadataFormat.get(collection).add(mf); else { ArrayList keyList=new ArrayList(); keyList.add(mf); selectedMetadataFormat.put(collection, keyList); } } /* //recovering possible previous selection for metadataFormat while (metadataSelected.next()){ if(selectedMetadataStructure.containsKey(metadataSelected.getString(1))) selectedMetadataStructure.get(metadataSelected.getString(1)).add(metadataSelected.getString(2)); else { ArrayList temp= new ArrayList(); temp.add(metadataSelected.getString(2)); selectedMetadataStructure.put(metadataSelected.getString(1), temp); } } if(!enter) throw new GCUBEFault("no collection selected for this VRE"); else preparedWhere=preparedWhere.substring(0, preparedWhere.length()-4)+") "; //retrieving all available MDFromat totalMF = DBInterface.queryDB("select MDFORMAT.id, MDFORMAT.name, MDFORMAT.schemauri, MDFORMAT.language from MDFORMAT;"); totalMFList= new ArrayList(); while (totalMF.next()){ MetadataFormat mf= new MetadataFormat(totalMF.getString(1),totalMF.getString(2), new URI(totalMF.getString(3)), totalMF.getString(4) ); totalMFList.add(mf); } /* resDerivable= DBInterface.queryDB("select mdc.collid, mdc.collname, mdc.colldesc , collmdf.id as mdfid from (select * from DERIVABLEMDF INNER JOIN MDFORMAT on DERIVABLEMDF.mdfid= MDFORMAT.id) " + "as COLLMDF,(select COLLECTION.id as collid, COLLECTION.name as collname, COLLECTION.description as colldesc, MDCOLLECTION.id as mdid from COLLECTION INNER JOIN MDCOLLECTION " + "ON COLLECTION.id = MDCOLLECTION.relatedcollectionid) as MDC where COLLMDF.mdcollid= MDC.mdid "+preparedWhere+";"); resNative= DBInterface.queryDB("select mdc.collid, mdc.collname, mdc.colldesc , collmdf.id as mdfid from (select * from NATIVEMDF INNER JOIN MDFORMAT on NATIVEMDF.mdfid= MDFORMAT.id) " + "as COLLMDF,(select COLLECTION.id as collid, COLLECTION.name as collname, COLLECTION.description as colldesc, MDCOLLECTION.id as mdid from COLLECTION INNER JOIN MDCOLLECTION " + "ON COLLECTION.id = MDCOLLECTION.relatedcollectionid) as MDC where COLLMDF.mdcollid= MDC.mdid "+preparedWhere+";"); */ }catch(Exception e) { logger.error("ModelService: getMEtadataRelatedToCollection Error ",e); throw new GCUBEFault(e); } return XMLUtil.PrepareMCFormatXML( nativeMetadataFormat ,selectedMetadataFormat); } /** * * Sets selected Metadata Format * * @param request * @return void * @throws RemoteException - */ public void setMetadataRelatedToCollection(SetMDFormatArgs request) throws GCUBEFault { try{ DBInterface.ExecuteUpdate("DELETE FROM VRERELATEDMETADATAFORMAT WHERE VRERELATEDMETADATAFORMAT.vreid='"+getResource().getId()+"';"); //String[] singleElement; String key=(String) getResource().getId(); //logger.debug(request.getCollectionIDArray().length+" "+request.getMdFormatIDArray().length+" "+request.getCheckedArray().length+" "+request.getCheckedArray(0).getCheckedRowElement().length); ArrayList values= new ArrayList(request.getCollectionIDArray().length); ArrayList row=null; for (int i=0; i(3); row.add(key); row.add(request.getCollectionIDArray(i)); row.add(request.getMdFormatIDArray(j)); values.add(row.toArray(new String[3])); } } DBInterface.insertInto("VRERELATEDMETADATAFORMAT", values.toArray(new String[0][0])); }catch(Exception e){ logger.error("setMetadataRelateToCollection failed ",e); throw new GCUBEFault(e); } } /** * Return an xml with the selectable functionalities * String * * @param request void * @return an xml String containing selectable functionalities * @throws RemoteException - */ public String getFunctionality(VOID var) throws GCUBEFault { ResultSet resFunct=null; ResultSet selectedFuncRes=null; ResultSet selectedCSRes=null; ArrayList selectedFunctionality= new ArrayList(); ArrayList selectedCS= new ArrayList(); try { DBInterface.connect(); resFunct= DBInterface.queryDB("select * from functionality where father is null;"); selectedFuncRes= DBInterface.queryDB("select funcid from VRERELATEDFUNCT where vreid='"+getResource().getId()+"';"); while (selectedFuncRes.next()) selectedFunctionality.add(selectedFuncRes.getInt(1)); selectedCSRes= DBInterface.queryDB("select csid from VRERELATEDCS where vreid='"+getResource().getId()+"';"); while (selectedCSRes.next()) selectedCS.add(selectedCSRes.getString(1)); } catch (Exception e) { logger.error("VDLModel Service: error retrieving functionality",e); throw new GCUBEFault(e); } return XMLUtil.PrepareFunctionalityXML(resFunct, selectedFunctionality, selectedCS); } /** * * * * @return void * @throws RemoteException - */ public void setFunctionality(FunctionalityIDArray request) throws GCUBEFault { System.out.println("Set Functionality called"); try{ DBInterface.ExecuteUpdate("DELETE FROM VRERELATEDFUNCT WHERE VRERELATEDFUNCT.vreid='"+getResource().getId()+"';"); DBInterface.ExecuteUpdate("DELETE FROM VRERELATEDCS WHERE VRERELATEDCS.vreid='"+getResource().getId()+"';"); String vreKey=(String) getResource().getId(); //logger.debug("KEY: "+dlKey); if (request.getFunctionalityIDElement()!=null){ ArrayList values= new ArrayList(request.getFunctionalityIDElement().length); ArrayList row=null; for (String funct: request.getFunctionalityIDElement()) { row= new ArrayList(2); row.add(vreKey); row.add(funct); values.add(row.toArray(new String[2])); } DBInterface.insertInto("VRERELATEDFUNCT", values.toArray(new String[0][0])); }else System.out.println("requestFunciotnality is null"); if (request.getCsIDElement()!=null) { ArrayList values= new ArrayList(request.getCsIDElement().length); ArrayList row=null; for (String css:request.getCsIDElement()) { row= new ArrayList(2); row.add(vreKey); row.add(css); values.add(row.toArray(new String[2])); } DBInterface.insertInto("VRERELATEDCS", values.toArray(new String[0][0])); } }catch(Exception e){ logger.error("error setting functionality",e); throw new GCUBEFault(e);} } /** * Return an XML with Quality parameter to insert * * @param request void * @return a XML format String * @throws RemoteException - */ public String getQuality(VOID var) throws GCUBEFault{ return XMLUtil.PrepareQualityXML(); } /** * Sets Quality parameters * * @param request * @throws RemoteException - */ public void setQuality(String request) throws GCUBEFault{ } /** * * set the DL state to Pending * * @param request * @return * @throws RemoteException - */ public void setVREtoPendingState(VOID var) throws GCUBEFault{ try{ DBInterface.ExecuteUpdate("UPDATE VRE SET STATUS='"+PENDING+"' WHERE VRE.id='"+getResource().getId()+"';"); }catch(Exception e){throw new GCUBEFault(e);} } /** * * @param request * @return * @throws RemoteException - */ public void deployVRE(VOID var) throws GCUBEFault{ String resourceID; try { resourceID = getResource().getId(); } catch (ResourceException e) { logger.error("Error retrieving the Resource Requested",e); throw new GCUBEUnrecoverableFault(e); } try{ DBInterface.ExecuteUpdate("UPDATE VRE SET STATUS='"+DEPLOYING+"' WHERE VRE.id='"+resourceID+"';"); } catch (Exception e) { logger.error("DB Error ",e); throw new GCUBEUnrecoverableFault(e); } DeployVRE deployVREThread= new DeployVRE(resourceID, ServiceContext.getContext().getScope() ); ServiceContext.getContext().setScope(deployVREThread, ServiceContext.getContext().getScope()); logger.trace("Deploy VRE thread started"); deployVREThread.start(); } public CheckStatusResponse checkStatus(VOID var) throws Exception{ CheckStatusResponse res= new CheckStatusResponse(); res.setReports(new String[]{getResource().getFirstReport(), getResource().getSecondReport()}); return res; } }