package org.gcube.data.analysis.statisticalmanager;

import org.gcube.common.homelibrary.home.Home;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.exceptions.UserNotFoundException;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
import org.gcube.data.analysis.statisticalmanager.exception.HLManagementException;
import org.gcube.data.analysis.statisticalmanager.util.ScopeUtils;
import org.gcube.data.analysis.statisticalmanager.util.ScopeUtils.ScopeBean;
import org.gcube.data.analysis.statisticalmanager.util.ServiceUtil;

public class WSCheck {

	/**
	 * @param args
	 * @throws UserNotFoundException 
	 * @throws HomeNotFoundException 
	 * @throws InternalErrorException 
	 * @throws WorkspaceFolderNotFoundException 
	 * @throws ItemAlreadyExistException 
	 * @throws InsufficientPrivilegesException 
	 * @throws ItemNotFoundException 
	 * @throws HLManagementException 
	 */
	public static void main(String[] args) throws InsufficientPrivilegesException, ItemAlreadyExistException, WorkspaceFolderNotFoundException, InternalErrorException, HomeNotFoundException, UserNotFoundException, ItemNotFoundException, HLManagementException {
//		ScopeProvider.instance.set("/gcube/devNext/NextNext");
		ScopeUtils.setAuthorizationSettings(new ScopeBean("/d4science.research-infrastructures.eu/gCubeApps/ScalableDataMining",null));
		
		String user="fabio.sinibaldi";
		
//		ServiceUtil.getWorkspaceSMFolder(ServiceUtil.getWorkspaceHome("fabio.sinibaldi")).
//			createExternalFileItem("testFile", "test csv", "text/csv", WSCheck.class.getResourceAsStream("/testCsv.csv"));

		
		
		Home home=ServiceUtil.getWorkspaceHome(user);
		Workspace ws = home.getWorkspace();
//		WorkspaceFolder appFolder=ws.getApplicationArea();
		WorkspaceFolder appFolder = ServiceUtil.getWorkspaceSMFolder(home);
//		String path="/Workspace/.applications/StatisticalManager/Warnings generated by the MaxEnt process07_10_2015_18_52_3058bc96ef-c6dc-4d2d-8817-caeea1ad6e14CMP3226.txt";
//		System.out.println("Listing "+appFolder.getPath());
		listFolder(appFolder,false);
		System.out.println("OK");
		
	}

	private static void listFolder(WorkspaceFolder toList,boolean recursive) throws InternalErrorException{
		System.out.println("********** FOLDER "+toList.getPath());
		for(WorkspaceItem item:toList.getChildren())
			if(item.isFolder()&&recursive)				
				listFolder((WorkspaceFolder) item,recursive);
			else {
				System.out.println(item.getPath());
			}
	}
	
}
