diff --git a/src/main/java/org/gcube/portal/wssynclibrary/shared/thredds/ThCatalogueBean.java b/src/main/java/org/gcube/portal/wssynclibrary/shared/thredds/ThCatalogueBean.java new file mode 100644 index 0000000..6860493 --- /dev/null +++ b/src/main/java/org/gcube/portal/wssynclibrary/shared/thredds/ThCatalogueBean.java @@ -0,0 +1,127 @@ +/** + * + */ + +package org.gcube.portal.wssynclibrary.shared.thredds; + +import java.io.Serializable; + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Mar 8, 2018 + */ +public class ThCatalogueBean implements Serializable, Comparable{ + + /** + * + */ + private static final long serialVersionUID = 56447135995876633L; + private String name; + private String path; + private Boolean isDefault; + + public ThCatalogueBean() { + + // TODO Auto-generated constructor stub + } + + /** + * @param name + * @param path + * @param isDefault + */ + public ThCatalogueBean(String name, String path, Boolean isDefault) { + + super(); + this.name = name; + this.path = path; + this.isDefault = isDefault; + } + + + /** + * @return the name + */ + public String getName() { + + return name; + } + + + /** + * @return the path + */ + public String getPath() { + + return path; + } + + + /** + * @return the isDefault + */ + public Boolean getIsDefault() { + + return isDefault; + } + + + /** + * @param name the name to set + */ + public void setName(String name) { + + this.name = name; + } + + + /** + * @param path the path to set + */ + public void setPath(String path) { + + this.path = path; + } + + + /** + * @param isDefault the isDefault to set + */ + public void setIsDefault(Boolean isDefault) { + + this.isDefault = isDefault; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("ThCatalogueBean [name="); + builder.append(name); + builder.append(", path="); + builder.append(path); + builder.append(", isDefault="); + builder.append(isDefault); + builder.append("]"); + return builder.toString(); + } + + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(ThCatalogueBean o) { + + if(o==null) + return -1; + + return this.getName().compareTo(o.getName()); + + } + + + + +} diff --git a/src/main/java/org/gcube/portal/wssynclibrary/thredds/ThreddsConverter.java b/src/main/java/org/gcube/portal/wssynclibrary/thredds/ThreddsConverter.java index da2f58b..d56e983 100644 --- a/src/main/java/org/gcube/portal/wssynclibrary/thredds/ThreddsConverter.java +++ b/src/main/java/org/gcube/portal/wssynclibrary/thredds/ThreddsConverter.java @@ -3,6 +3,7 @@ package org.gcube.portal.wssynclibrary.thredds; import java.util.function.Function; import org.gcube.portal.wssynclibrary.shared.thredds.Status; +import org.gcube.portal.wssynclibrary.shared.thredds.ThCatalogueBean; import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessDescriptor; import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessStatus; import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor; @@ -11,6 +12,7 @@ import org.gcube.usecases.ws.thredds.engine.impl.ProcessDescriptor; import org.gcube.usecases.ws.thredds.engine.impl.ProcessStatus; import org.gcube.usecases.ws.thredds.model.SyncFolderDescriptor; import org.gcube.usecases.ws.thredds.model.SynchFolderConfiguration; +import org.gcube.usecases.ws.thredds.model.gui.CatalogBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,11 +27,11 @@ import org.slf4j.LoggerFactory; * Feb 14, 2018 */ public class ThreddsConverter { - + public static final String THREDDS_FILTER_NC_NCML_ASC = "*.nc,*.ncml,*.asc"; private static Logger logger = LoggerFactory.getLogger(ThreddsConverter.class); - + /** The to S sync folder descriptor. */ public static Function toThSyncFolderDescriptor = new Function() { @@ -39,7 +41,7 @@ public class ThreddsConverter { logger.info("Input "+SyncFolderDescriptor.class.getSimpleName()+" is null, returning empty "+ThSyncFolderDescriptor.class.getSimpleName()); return mySync; } - + mySync.setFolderId(t.getFolderId()); mySync.setFolderPath(t.getFolderPath()); mySync.setLocked(t.isLocked()); @@ -47,43 +49,43 @@ public class ThreddsConverter { mySync.setLocalProcessDescriptor(localProcessDescriptor); ThSynchFolderConfiguration configuration = toThSynchFolderConfiguration.apply(t.getConfiguration()); mySync.setConfiguration(configuration); - + return mySync; } }; - + /** The to S sync folder descriptor. */ public static Function toSyncFolderDescriptor = new Function() { public SyncFolderDescriptor apply(ThSyncFolderDescriptor t) { - + if(t==null) { logger.warn("Input "+ThSyncFolderDescriptor.class.getSimpleName()+" is null, returning null"); return null; } - + SynchFolderConfiguration configuration = toSynchFolderConfiguration.apply(t.getConfiguration()); return new SyncFolderDescriptor(t.getFolderId(), t.getFolderPath(), configuration); } }; - + /** The to synch folder configuration. */ public static Function toSynchFolderConfiguration = new Function() { public SynchFolderConfiguration apply(ThSynchFolderConfiguration t) { - + if(t==null) { logger.warn("Input "+ThSynchFolderConfiguration.class.getSimpleName()+" is null, returning null"); return null; } - + String filter = t.getFilter()!=null && !t.getFilter().isEmpty()?t.getFilter():THREDDS_FILTER_NC_NCML_ASC; return new SynchFolderConfiguration(t.getRemotePath(), filter, t.getTargetToken(), t.getToCreateCatalogName()); } }; - - + + /** The to S synch folder configuration. */ public static Function toThSynchFolderConfiguration = new Function() { @@ -101,8 +103,8 @@ public class ThreddsConverter { return mySync; } }; - - + + /** The to S sync folder descriptor. */ public static Function toThProcessDescriptor = new Function() { @@ -112,7 +114,7 @@ public class ThreddsConverter { logger.info("Input "+ProcessDescriptor.class.getSimpleName()+" is null, returning empty "+ThProcessDescriptor.class.getSimpleName()); return mySync; } - + mySync.setFolderId(t.getFolderId()); mySync.setFolderPath(t.getFolderPath()); mySync.setProcessId(t.getProcessId()); @@ -121,8 +123,22 @@ public class ThreddsConverter { return mySync; } }; - - + + + /** The to th catalogue bean. */ + public static Function toThCatalogueBean = new Function() { + + @Override + public ThCatalogueBean apply(CatalogBean t) { + + if(t==null) + return null; + + return new ThCatalogueBean(t.getName(), t.getPath(), t.getIsDefault()); + } + }; + + /** The to th process status. */ public static Function toThProcessStatus = new Function() { @@ -132,10 +148,10 @@ public class ThreddsConverter { logger.info("Input "+ProcessStatus.class.getSimpleName()+" is null, returning empty "+ThProcessStatus.class.getSimpleName()); return mySync; } - + mySync.setCurrentMessage(t.getCurrentMessage()); mySync.setPercentCompleted(t.getPercent()); - + if(t.getErrorCount()!=null) mySync.setErrorCount(t.getErrorCount().get()); if(t.getLogBuilder()!=null) @@ -144,7 +160,7 @@ public class ThreddsConverter { mySync.setQueuedTransfers(t.getQueuedTransfers().get()); if(t.getServedTransfers()!=null) mySync.setServedTransfers(t.getServedTransfers().get()); - + if(t.getStatus()!=null) { switch (t.getStatus()) { @@ -163,7 +179,7 @@ public class ThreddsConverter { case STOPPED: mySync.setStatus(Status.STOPPED); break; - + default: break; } @@ -174,4 +190,5 @@ public class ThreddsConverter { }; + } diff --git a/src/main/java/org/gcube/portal/wssynclibrary/thredds/WorkspaceThreddsSynchronize.java b/src/main/java/org/gcube/portal/wssynclibrary/thredds/WorkspaceThreddsSynchronize.java index 4db4d9c..e44a2e2 100644 --- a/src/main/java/org/gcube/portal/wssynclibrary/thredds/WorkspaceThreddsSynchronize.java +++ b/src/main/java/org/gcube/portal/wssynclibrary/thredds/WorkspaceThreddsSynchronize.java @@ -1,8 +1,11 @@ package org.gcube.portal.wssynclibrary.thredds; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Set; import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.homelibrary.home.workspace.Properties; @@ -12,6 +15,7 @@ import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portal.wssynclibrary.shared.ItemNotSynched; import org.gcube.portal.wssynclibrary.shared.thredds.Status; import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status; +import org.gcube.portal.wssynclibrary.shared.thredds.ThCatalogueBean; import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessDescriptor; import org.gcube.portal.wssynclibrary.shared.thredds.ThProcessStatus; import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor; @@ -28,6 +32,7 @@ import org.gcube.usecases.ws.thredds.faults.WorkspaceNotSynchedException; import org.gcube.usecases.ws.thredds.model.SyncFolderDescriptor; import org.gcube.usecases.ws.thredds.model.SyncOperationCallBack; import org.gcube.usecases.ws.thredds.model.SynchFolderConfiguration; +import org.gcube.usecases.ws.thredds.model.gui.CatalogBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,22 +45,22 @@ import org.slf4j.LoggerFactory; * Feb 14, 2018 */ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronizedRepository{ - + /** The logger. */ private static Logger logger = LoggerFactory.getLogger(WorkspaceThreddsSynchronize.class); - + /** The engine. */ private SyncEngine engine=null; - + /** The instance. */ private static WorkspaceThreddsSynchronize instance = null; - + public static final String WS_SYNCH_SYNCH_STATUS = "WS-SYNCH.SYNCH-STATUS"; - + /** The map call back. */ // Fully synchronized HashMap private Map mapCallBack = Collections.synchronizedMap(new HashMap<>()); - + /** * Instantiates a new workspace thredds synchronize. */ @@ -63,7 +68,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized // GET ENGINE : SINGLETON INSTANCE engine = SyncEngine.get(); } - + /** * Gets the single instance of WorkspaceThreddsSynchronize. @@ -76,10 +81,10 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized } return instance; } - + //setSynchronizedFolder(config, folder.getId()); - - + + /* (non-Javadoc) * @see org.gcube.portal.wssynclibrary.WorkspaceSynchronizedRepository#isItemSynchronized(java.lang.String) */ @@ -87,14 +92,14 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized public ThSyncFolderDescriptor checkItemSynched(String itemId) throws ItemNotSynched, Exception { logger.debug("Perfoming checkItemSynched for id: "+itemId); try { - // WHEN OPENING A FOLDER, INVOKE CHECK TO UPDATE SYNCH STATUS + // WHEN OPENING A FOLDER, INVOKE CHECK TO UPDATE SYNCH STATUS SyncFolderDescriptor desc = engine.check(itemId, false); logger.debug("The item id: "+itemId +" is synched"); ThSyncFolderDescriptor descr = ThreddsConverter.toThSyncFolderDescriptor.apply(desc); logger.trace("CheckItemSynched for id: "+itemId+" returning descriptor: "+descr); return descr; - + }catch(WorkspaceNotSynchedException e) { String err = "The item id: "+itemId +" is not synched"; logger.trace(err); @@ -106,7 +111,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized engine.forceUnlock(itemId); //System.out.println("Workspace locked, going to force unlock.."); // MAINLY FOR TEST PURPOSES, OR WHEN SOMETHIGN GOES WRONG.. USE CAUTIOUSLY //engine.forceUnlock(folder.getId()); - + //TODO??? return null; //??? @@ -116,13 +121,13 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized throw new Exception("Sorry, an error occurred during check syncronization due to WS interection for the itemId: "+itemId); else if(e instanceof InternalException) throw new Exception("Sorry, an Internal Exception occurred during check syncronization for the itemId: "+itemId); - + throw new Exception("Sorry, an error occurred server side during chck syncronization for the itemId: "+itemId); } } - - + + /** * Sets the synchronized folder. * @@ -133,16 +138,16 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized */ public ThSyncFolderDescriptor setSynchronizedFolder(ThSynchFolderConfiguration thConfig, String itemId) throws Exception { SynchFolderConfiguration config = ThreddsConverter.toSynchFolderConfiguration.apply(thConfig); - + if(thConfig.getRemotePath()==null || thConfig.getRemotePath().isEmpty()) throw new Exception("A valid remote path must be provided"); - + if(thConfig.getToCreateCatalogName()==null || thConfig.getToCreateCatalogName().isEmpty()) throw new Exception("A valid Catalogue Name must be provided"); - + if(thConfig.getTargetToken()==null || thConfig.getTargetToken().isEmpty()) throw new Exception("A valid Target Token must be provided"); - + try { engine.setSynchronizedFolder(config, itemId); } catch (WorkspaceInteractionException | InternalException e) { @@ -152,68 +157,68 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized } return null; } - - + + /* (non-Javadoc) * @see org.gcube.portal.wssynclibrary.WorkspaceSynchronizedRepository#doSynchronization(java.lang.String) */ @Override public ThSyncStatus doSync(String itemId) throws Exception { - + try { // INVOKE SYNCHRONIZATION ON FOLDER ProcessDescriptor descriptor = engine.doSync(itemId); - + ThSyncStatus synStatus = mapCallBack.get(itemId); - + if(synStatus==null) { - + registerCallbackForId(itemId); descriptor = engine.getProcessDescriptorByFolderId(itemId); ProcessStatus status = engine.getProcessStatusByFolderId(itemId); updateMapCallback(itemId, status, descriptor); } - + logger.debug("DoSync returning status: "+synStatus); return synStatus; - + } catch (WorkspaceInteractionException | InternalException | ProcessNotFoundException e) { logger.error("Error: ",e); - + if(e instanceof WorkspaceInteractionException) throw new Exception("Sorry, an error occurred during syncronization due to WS interection for the itemId: "+itemId); else if(e instanceof InternalException) throw new Exception("Sorry, an Internal Exception occurred during syncronization for the itemId: "+itemId); - + throw new Exception("Sorry, an error occurred server side during syncronization for the itemId: "+itemId); } - - + + } - + /* (non-Javadoc) * @see org.gcube.portal.wssynclibrary.DoSyncItem#getSyncStatus(java.lang.String) */ @Override public ThSyncStatus monitorSyncStatus(String itemId) throws ItemNotSynched, Exception { - + try { //ThSyncFolderDescriptor descr = checkItemSynched(itemId); - + ThSyncStatus synStatus = mapCallBack.get(itemId); - - + + if(synStatus==null) { logger.info("The sync is not on-going...,returning process status null"); // String err = "The item id: "+itemId +" is not cached as synched"; // throw new ItemNotSynched(err); return new ThSyncStatus(null, null); } - + if(synStatus.getProcessStatus()!=null) { if(synStatus.getProcessStatus().getStatus()!=null) { if(synStatus.getProcessStatus().getStatus().equals(Status.COMPLETED)) { @@ -222,10 +227,10 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized } } } - + logger.trace("MonitorSyncStatus for item: "+itemId+" returning: "+synStatus); return synStatus; - + // } catch (ItemNotSynched e) { // String err = "The item id: "+itemId +" is not synched repository side"; // throw new ItemNotSynched(err); @@ -242,7 +247,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized public Boolean removeSync(String itemId) { return null; // TODO Auto-generated method stub - + } @@ -252,7 +257,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized @Override public Boolean stopSync(String itemId) { return false; - + } @@ -262,7 +267,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized @Override public Boolean initRepository() { return false; - + } @@ -277,7 +282,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized }catch (Exception e) { return null; } - + } @@ -290,7 +295,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized } - + /** * Gets the synched status from item property. * @@ -301,36 +306,36 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized * @throws Exception the exception */ public Sync_Status getSynchedStatusFromItemProperty(String folderId, String username) throws ItemNotSynched, Exception{ - + if(folderId==null) throw new Exception("Invalid pareter itemId is null"); - + String scope = ScopeProvider.instance.get(); if(scope == null || scope.isEmpty()) throw new Exception("You must set a valid scope into ScopeProvider instance"); - + WorkspaceFolder folder = null; try { - + logger.debug("Scope provider instancied at: "+scope); Workspace workspace = HomeLibrary.getUserWorkspace(username); folder = (WorkspaceFolder) workspace.getItem(folderId); - + }catch (Exception e) { logger.error("Error HL side: "+folderId, e); throw new Exception("Sorry an error occurred getting HL item for id: "+folderId); } - + if(folder==null) throw new Exception("Sorry an error occurred getting HL item for id: "+folderId); - + Properties properties = folder.getProperties(); - + if(properties==null || properties.getProperties()==null || properties.getProperties().size()==0) { throw new ItemNotSynched("No properties to read"); } - + String wsSyncStatus = properties.getProperties().get(WS_SYNCH_SYNCH_STATUS); logger.info("Folder id: "+folder.getId()+" has current: "+WS_SYNCH_SYNCH_STATUS +" value at: "+wsSyncStatus); try { @@ -341,7 +346,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized } } - + /** * Register callback for id. * @@ -350,11 +355,11 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized */ @Override public void registerCallbackForId(String itemId) throws Exception{ - + try { - + SyncOperationCallBack callback = new SyncOperationCallBack() { - + @Override public void onStep(ProcessStatus status, ProcessDescriptor descriptor) { logger.debug("ON STEP : "+status+" "+descriptor); @@ -365,7 +370,7 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized updateMapCallback(itemId, status, descriptor); } }; - + // REGISTER CALLBACK TO MONITOR PROGRESS logger.debug("Registering callback on itemId: "+itemId); engine.registerCallBack(itemId, callback); @@ -374,8 +379,8 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized throw new Exception("No Process found for item id: "+itemId+ ", refresh an try again"); } } - - + + /** * Update map callback. * @@ -389,4 +394,37 @@ public class WorkspaceThreddsSynchronize implements WorkspaceThreddsSynchronized mapCallBack.put(itemId, new ThSyncStatus(thDesc, thStatus)); logger.debug("Update map for "+itemId +" with new "+thStatus); } + + + + /** + * Gets the available catalogues by token. + * + * @param token the token + * @return + * @return the available catalogues by token + * @throws Exception the exception + */ + public List getAvailableCataloguesByToken(String token) throws Exception{ + + if(token==null || token.isEmpty()) + throw new Exception("Invalid parameter token null or empty"); + + logger.debug("Get Available Catalogues by token: "+token.substring(0, token.length()-5)+"XXXXX"); + Set ctlgs = engine.getAvailableCatalogsByToken(token); + + if(ctlgs==null || ctlgs.size()==0) + return new ArrayList(1); + + List listCtlgs = new ArrayList(ctlgs.size()); + for(CatalogBean bean: ctlgs){ + System.out.println(bean.getName()+" in "+bean.getPath()+" Default : "+bean.getIsDefault()); + ThCatalogueBean toBean = ThreddsConverter.toThCatalogueBean.apply(bean); + if(toBean!=null) + listCtlgs.add(toBean); + } + + Collections.sort(listCtlgs); + return listCtlgs; + } }