john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

OxygenFile

// 2012-08-06 johnpfeiffer
// TODO: refactor so that the static factory method uses abstract (change the weird creation loop -> then usage through object)

import java.io.FileNotFoundException;
import java.util.List;

import com.oxygen.sdk.O2Agent;
import com.oxygen.sdk.O2File;
import com.oxygen.sdk.O2FolderItem;
import com.oxygen.sdk.O2ItemContainer;
import com.oxygen.sdk.O2Space;
import com.oxygen.sdk.exception.O2NetworkException;
import com.oxygen.sdk.exception.O2RuleException;
import com.oxygen.sdk.exception.O2UnexpectedException;

public class OxygenFile
{
    private static final String CLASSVERSION = "0.23";
    O2File file;

    OxygenFile( O2File newFile )
    {
        file = newFile;
    }

    protected static O2File getInstanceByNameFromSpace( O2Agent agent , String targetName , O2Space space ) throws O2NetworkException , O2UnexpectedException ,
            O2RuleException
    {
        O2FolderItem searchResult = null;

        if( agent == null )
        {
            throw new IllegalArgumentException( "Oxygen agent cannot be null" );
        }
        if( targetName == null )
        {
            throw new IllegalArgumentException( "targetName cannot be null" );
        }
        if( targetName.equals( "/" ) )
        {
            throw new IllegalArgumentException( "targetName cannot be just a slash" );
        }
        if( space == null )
        {
            throw new IllegalArgumentException( "Space cannot be null" );
        }
        List <O2FolderItem> foldersAndFiles = agent.listContainerItems( space );
        if( foldersAndFiles == null )
        {
            throw new IllegalArgumentException( "Space cannot be null: " + space );
        }
        if( !foldersAndFiles.isEmpty() )
        {
            if( targetName.startsWith( "/" ) )
            {
                targetName = targetName.substring( 1 );
            }
            String sourceSplit[] = targetName.split( "/" );
            if( sourceSplit.length == 1 ) // no slashes
            {
                searchResult = findItemByName( foldersAndFiles , targetName );
            }else
            {
                int max = sourceSplit.length;
                System.out.println( "DEBUG: " + max + " levels deep to search for " + targetName );
                int searchDepth = 0;
                do
                {
                    String currentTargetName = sourceSplit[searchDepth];

                    System.out.println( "DEBUG: searching level " + searchDepth + " for " + currentTargetName );

                    searchResult = findItemByName( foldersAndFiles , currentTargetName );

                    System.out.println( "DEBUG: found " + searchResult.getName() );

                    searchDepth++;

                    if( searchResult.getType() != O2ItemContainer.Type.O2File )
                    {
                        foldersAndFiles = agent.listContainerItems( searchResult );
                    }

                }while( searchResult != null && searchDepth != max );
            }

        }
        return (O2File) searchResult;
    }

    // returns last match of result
    private static O2FolderItem findItemByName( List <O2FolderItem> foldersAndFiles , String targetName )
    {
        O2FolderItem result = null;
        for( O2FolderItem element : foldersAndFiles )
        {
            if( targetName.equals( element.getName() ) )
            {
                result = element;
            }
        }
        return result;
    }

    protected static O2File getInstanceByIdFromSpace( O2Agent agent , String targetId , O2Space space ) throws O2NetworkException , O2UnexpectedException, FileNotFoundException
    {
        if( agent == null )
        {
            throw new IllegalArgumentException( "ERROR: Oxygen agent cannot be null" );
        }
        if( targetId == null )
        {
            throw new IllegalArgumentException( "ERROR: targetId cannot be null" );
        }
        if( targetId.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: targetId cannot be empty" );
        }
        if( space == null )
        {
            throw new IllegalArgumentException( "ERROR: Space cannot be null" );
        }
        O2FolderItem searchResult = null;
        O2File fileFound = null;
        try
        {
            searchResult = agent.getFolderItem( targetId );
            if( searchResult.getType() == O2ItemContainer.Type.O2File )
            {
                fileFound = (O2File) searchResult;
            }
        }catch( ArrayIndexOutOfBoundsException e )  //not sure why it's this particular Exception
        {
            throw new IllegalArgumentException( "ERROR: fileId in an incorrect format " + targetId );
        }catch( O2RuleException e )
        {
            throw new FileNotFoundException( "ERROR: Space or File not found: " + targetId + " from " + space.getName() );
        }
        return fileFound;
    }


    // last name match initializes the object, otherwise null
    // TODO: nested recursion search
    // TODO: duplicate file names in multiple folders or spaces?
    protected static O2File getInstanceByName( O2Agent agent , String targetName ) throws O2NetworkException , O2UnexpectedException , O2RuleException
    {
        O2File fileConstructor = null;
        if( agent == null )
        {
            throw new IllegalArgumentException( "Oxygen agent cannot be null" );
        }
        if( targetName == null )
        {
            throw new IllegalArgumentException( "targetName cannot be null" );
        }
        List <O2Space> spaceList = null;
        spaceList = agent.listSpaces();
        for( O2Space currentSpace : spaceList )
        {
            List <O2FolderItem> foldersAndFiles = agent.listContainerItems( currentSpace );
            for( O2FolderItem element : foldersAndFiles )
            {
                if( element.getType() == O2ItemContainer.Type.O2File )
                {
                    if( targetName.equals( element.getName() ) )
                    {
                        fileConstructor = (O2File) element;
                    }
                }
            }
        }
        return fileConstructor;
    }

    // last name match initializes the object, otherwise null
    // TODO: nested recursion search
    // TODO: duplicate file names in multiple folders or spaces?
    protected O2File createFromId( O2Agent agent , String targetId ) throws O2NetworkException , O2UnexpectedException , O2RuleException
    {
        O2File fileConstructor = null;
        if( agent == null )
        {
            throw new IllegalArgumentException( "Oxygen agent cannot be null" );
        }
        if( targetId == null )
        {
            throw new IllegalArgumentException( "targetId cannot be null" );
        }
        List <O2Space> spaceList = null;
        spaceList = agent.listSpaces();
        for( O2Space currentSpace : spaceList )
        {
            List <O2FolderItem> foldersAndFiles = agent.listContainerItems( currentSpace );
            for( O2FolderItem element : foldersAndFiles )
            {
                if( element.getType() == O2ItemContainer.Type.O2File )
                {
                    if( targetId.equals( element.getID() ) )
                    {
                        fileConstructor = (O2File) element;
                    }
                }
            }
        }
        return fileConstructor;
    }

    protected String version()
    {
        return CLASSVERSION;
    }

    protected O2File get()
    {
        return file;
    }

    protected String getName()
    {
        return file.getName();
    }

    protected String getId()
    {
        return file.getID();
    }

    protected long getCreatedTimestamp()
    {
        return file.getCreatedTimestamp();
    }

    protected String getLastModifiedBy()
    {
        return file.getLastModifiedBy();
    }

    protected long getSizeInBytes()
    {
        return file.getLength();
    }

} // end class

  • « Bash for loop examples files in a directory loop count
  • getoxygenfileinfo »

Published

Aug 8, 2012

Category

java-oxygencloud

~549 words

Tags

  • java 252
  • oxygencloud 19
  • oxygenfile 2