package org.gcube.vremanagement.vremodeler.resources.handlers; import java.sql.ResultSet; import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.vremanagement.vremodeler.db.DBInterface; import org.gcube.vremanagement.vremodeler.resources.MFRelationNative; import org.gcube.vremanagement.vremodeler.resources.MetadataFormat; public class MetadataFormatHandler implements ResourceHandler { private static GCUBELog logger= new GCUBELog(MetadataFormatHandler.class); private static final String nameFieldDb="NAME"; private static final String schemaFieldDb="SCHEMAURI"; private static final String languageFieldDb="LANGUAGE"; public static final String tableName="METADATAFORMAT"; private String relatedMCollectionID; public MetadataFormatHandler(String relatedMCollectionId){ this.relatedMCollectionID= relatedMCollectionId; } public void add(MetadataFormat resource) throws Exception { String id; if ((id=exists(resource))!=null){ new NativeMetadataFormatHandler().add(new MFRelationNative(this.relatedMCollectionID, id)); }else{ DBInterface.connect(); DBInterface.insertIntoListable(tableName, resource); new NativeMetadataFormatHandler().add(new MFRelationNative(this.relatedMCollectionID, resource.getId())); } } public void drop(String resourceId) throws Exception { // TODO Auto-generated method stub } public void initialize() throws Exception {} /** * this method returns the id of the MetadataFormat if this metadataFormat exists, null otherwise */ private static String exists(MetadataFormat metadataFormat) throws Exception{ DBInterface.connect(); ResultSet result; if (metadataFormat.getLanguage().compareTo(MetadataFormat.ANY_LANGUAGE)==0){ result=DBInterface.queryDB("SELECT ID from "+tableName+" WHERE "+nameFieldDb+"='"+metadataFormat.getName()+"' AND "+schemaFieldDb+"='"+metadataFormat.getSchemaURI()+"'"); }else result=DBInterface.queryDB("SELECT ID from "+tableName+" WHERE "+nameFieldDb+"='"+metadataFormat.getName()+"' AND "+schemaFieldDb+"='"+metadataFormat.getSchemaURI()+"' AND ("+languageFieldDb+"='"+MetadataFormat.ANY_LANGUAGE+"' OR "+languageFieldDb+"='"+metadataFormat.getLanguage()+"')"); if (result.next()) { logger.trace("the metadataFormat already exists"); return result.getString(1); }else return null; } }