package org.gcube.vremanagement.vremodeler.impl; 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.XMLUtil; import org.gcube.vremanagement.vremodeler.portallayout.CollectionResourceCreation; import org.gcube.vremanagement.vremodeler.portallayout.LayoutCreation; 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.getMessage()); 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); e.printStackTrace(); 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;"); 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); } DBInterface.InsertInto("VRERELATEDGHN", values); }catch(Exception e){ logger.error("error setting GHNs "+e); e.printStackTrace(); 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("VDL Model: error on DB"); e.printStackTrace(); 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); } DBInterface.connect(); DBInterface.InsertInto("VRERELATEDCOLLECTION", values); }catch(Exception e){ e.printStackTrace(); 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 { ResultSet resDerivable, resNative, collectionRelated, totalMF; String preparedWhere=""; Hashtable> selectedMetadataStructure= new Hashtable>(); boolean enter=false; try{ DBInterface.connect(); collectionRelated= DBInterface.queryDB("select VRERELATEDCOLLECTION.collid from VRERELATEDCOLLECTION where VRERELATEDCOLLECTION.vreid='"+getResource().getId()+"';"); //select only the resuls related with selected collection preparedWhere= " and ( "; while (collectionRelated.next()){ enter=true; preparedWhere+= " mdc.collid='"+collectionRelated.getString(1)+"' or "; } ResultSet metadataSelected= DBInterface.queryDB("select collid, mfid from VRERELATEDMETADATAFORMAT where vreid='"+getResource().getId()+"';"); //recovering possible previsious 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)+") "; totalMF = DBInterface.queryDB("select MDFORMAT.id, MDFORMAT.name, MDFORMAT.schemauri, MDFORMAT.language from MDFORMAT;"); 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) { e.printStackTrace(); logger.error("ModelService: getMEtadataRelatedToCollection Error "+e.getMessage()); throw new GCUBEFault(e); } return XMLUtil.PrepareMCFormatXML( resNative,resDerivable,totalMF , selectedMetadataStructure); } /** * * 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); } } DBInterface.InsertInto("VRERELATEDMETADATAFORMAT", values); }catch(Exception e){ logger.error("setMetadataRelateToCollectio failed "); e.printStackTrace(); 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.printStackTrace(); 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); } DBInterface.InsertInto("VRERELATEDFUNCT", values); }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); } DBInterface.InsertInto("VRERELATEDCS", values); } }catch(Exception e){ e.printStackTrace(); 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"); throw new GCUBEUnrecoverableFault(e); } try{ DBInterface.ExecuteUpdate("UPDATE VRE SET STATUS='"+DEPLOYING+"' WHERE VRE.id='"+resourceID+"';"); } catch (GCUBEFault e) { logger.error("HSQLDB Error "+e); throw new GCUBEUnrecoverableFault(e); } //only for test LayoutCreation lc= new LayoutCreation(resourceID, "TestScenarioPub"); CollectionResourceCreation crc= new CollectionResourceCreation(resourceID, "TestScenarioPub"); try { lc.createAndPublishLayout(); crc.createAndPublish(); } catch (Exception e) { e.printStackTrace(); } /* 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().getReportFirstStep(), getResource().getReportSecondStep()}); return res; } }