SimpleInterface.java :  » Source-Control » sourcejammer » org » sourcejammer » client » simpletools » Java Open Source

Java Open Source » Source Control » sourcejammer 
sourcejammer » org » sourcejammer » client » simpletools » SimpleInterface.java
/*
 *  Copyright (C) 2001, 2002 Robert MacGrogan
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *
 * $Archive: SourceJammer$
 * $FileName: SimpleInterface.java$
 * $FileID: 3958$
 *
 * Last change:
 * $AuthorName: Rob MacGrogan$
 * $Date: 9/11/03 11:14 AM$
 * $Comment: Add setConfDirectory(String) method.$
 */

package org.sourcejammer.client.simpletools;

import org.sourcejammer.client.gui.CommandCentral;
import org.sourcejammer.client.gui.conf.GuiConf;
import org.sourcejammer.util.AppConfig;
import org.sourcejammer.util.RepeatingResponse;
import org.sourcejammer.project.view.Project;
import org.sourcejammer.client.HistoryTypeMapper;
import org.sourcejammer.util.SourceJammerConnectionException;
import org.sourcejammer.client.gui.GUICommandException;
import org.sourcejammer.client.NoSessionException;
import org.sourcejammer.client.SourceJammerClient;
import org.sourcejammer.client.DisplayTextLibrary;
import org.sourcejammer.project.NodeIterator;
import org.sourcejammer.project.view.NodeInfo;

import java.net.MalformedURLException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * Title: $FileName: SimpleInterface.java$<br>
 * @author $AuthorName: Rob MacGrogan$<br>
 * @version $VerNum: 12$<br>
 *
 * $Description: $<br>
 * $KeyWordsOff: $<br><br>
 * 
 * 
 * This singleton class provides a simplified interface into the SourceJammer client
 * functionality for the purpose of building plugins to integrate SourceJammer into
 * 3rd party development tools such as JBuilder and Eclipse.<br><br>
 *
 * To use this class, get the singleton instance, call the <code>setConfDirectory()</code>
 * method to set the configuration, call the <code>connect()</code> method to
 * connect to an archive, and then call the provided methods as needed.
 *
 */
public class SimpleInterface {

  private static SimpleInterface instance = new SimpleInterface();

  /**
   * Returns the singleton instance.
   */
  public static SimpleInterface getInstance(){
    return instance;
  }

  private SimpleInterface(){
  }

  /**
   * Sets the configuration directory. Should be the directory that contains the
   * conf.xml and other sourcejammer configuration files. This method must be called
   * before any of the other methods of this class can be used.
   */
  public void setConfDirectory(java.io.File confDir)
          throws IOException{
    AppConfig.getInstance(confDir.getAbsolutePath());
    SourceJammerClient client = SourceJammerClient.getInstance(confDir.getAbsolutePath());
    DisplayTextLibrary.initializeInstance(client.getDisplayTextPropertiesFileName());
  }

  /**
   * Sets the configuration directory. Should be the directory that contains the
   * conf.xml and other sourcejammer configuration files. This method must be called
   * before any of the other methods of this class can be used.
   */
  public void setConfDirectory(String confDir)
          throws IOException{
    java.io.File flDir = new java.io.File(confDir);
    setConfDirectory(flDir);
  }


  /**
   * Connects the SimpleInterface to the specified archive. Returns the root
   * project for the archive.
   *
   * @param archiveName -- Name of archive to connect to.
   * @param url -- URL of SourceJammer server.
   * @param userName -- name of user to use when connecting.
   * @param password -- user's password.
   */
  public Project connect(String archiveName, String url, String userName, String password)
          throws MalformedURLException, IOException,
                 SourceJammerConnectionException, GUICommandException,
                 NoSessionException{
    CommandCentral.getInstance().setServerURL(url);
    CommandCentral.getInstance().setArchiveConnectInfo(archiveName, userName, password);
    return CommandCentral.getInstance().connectToArchive();
  }

  /**
   * Returns the Project object for the specified project.
   */
  public Project getProjectObj(long projectID)
          throws SourceJammerConnectionException, GUICommandException{
    return CommandCentral.getInstance().retrieveProject(projectID);
  }

  /**
   * Get File object for the specified file.
   */
  public org.sourcejammer.project.view.File getFileObj(long fileID)
          throws SourceJammerConnectionException, GUICommandException{
    NodeInfo flInfo = new NodeInfo();
    flInfo.setUniqueID(fileID);
    return CommandCentral.getInstance().getFileInfo(flInfo, null);
  }


  /**
   * Check in the file. Uses default after checkin action from SourceJammerClient (clientconf.xml).
   */
  public void checkInFile(long fileID, String fileName, java.io.File targetDir, String comment)
          throws IOException, SourceJammerConnectionException,
                 GUICommandException{
    checkInFile(fileID, fileName, targetDir, comment, SourceJammerClient.getInstance().getOnCheckInFileAction());
  }

  /**
   * Check in the file. afterCheckInAction specifies action to perform on local
   * copy of file after it is checked in. Possible options are the same as
   * after add action options on addFile().
   */
  public void checkInFile(long fileID, String fileName, java.io.File targetDir, String comment, String afterCheckInAction)
          throws IOException, SourceJammerConnectionException,
                 GUICommandException{

    NodeInfo flInfo = new NodeInfo();
    flInfo.setUniqueID(fileID);
    flInfo.setNodeName(fileName);
    flInfo.setNodeType(AppConfig.NodeTypes.FILE);
    CommandCentral.getInstance().checkInFile(flInfo, targetDir, comment, afterCheckInAction, null);
  }


  /**
   * Get the file to the specified target directory without checking it out.
   * 
   * @param fileID -- unique id of the file.
   * @param fileName -- name of the file (File.java, for example).
   * @param targetDir -- directory whe the file will be &quot;got&quot; to.
   * @param eolType -- the eol type to apply to all text files. This value must be
   *           in AppConfig.EndOfLineType.
   * @param printMessage -- if <code>true</code>, messages will be printed to output stream.
   * @param setReadOnly -- if <code>true</code>, file will be set to read-only after it's been downloaded.
   */
  public void getFile(long fileID, String fileName, java.io.File targetDir,
                       int eolType, boolean printMessages, boolean setReadOnly)
          throws IOException, SourceJammerConnectionException,
                 GUICommandException{
    NodeInfo flInfo = new NodeInfo();
    flInfo.setFileName(fileName);
    flInfo.setUniqueID(fileID);
    CommandCentral.getInstance().getFile(flInfo, targetDir, eolType, printMessages, setReadOnly, new RepeatingResponse(), null);
  }

  /**
   * Streams the requested file into the passed in OutputStream.
   * 
   * @param fileID -- unique id of the file.
   * @param eolType -- the eol type to apply to all text files. This value must be
   *           in AppConfig.EndOfLineType.
   * @param stmOut -- an OutputStream to receive the file.
   */
  public void getFileToStream(long fileID, int eolType, OutputStream stmOut)
          throws GUICommandException, IOException, SourceJammerConnectionException{
    CommandCentral.getInstance().getFileToStream(fileID, stmOut, eolType);
  }

  /**
   * Check out the file.
   * @param fileID -- unique id of the file.
   * @param fileName -- name of the file (File.java, for example).
   * @param targetDir -- directory whe the file will be &quot;got&quot; to.
   * @param eolType -- the eol type to apply to all text files. This value must be
   *           in AppConfig.EndOfLineType.
   */
  public void checkOutFile(long fileID, String fileName, java.io.File targetDir, int eolType)
          throws SourceJammerConnectionException, GUICommandException,
                 IOException{
    CommandCentral.getInstance().checkOutFile(fileID, fileName, targetDir, eolType, null);
  }

  /**
   * Check out the file and stream file to the passed in OutputStream.
   * 
   * @param fileID -- unique id of the file.
   * @param eolType -- the eol type to apply to all text files. This value must be
   *           in AppConfig.EndOfLineType.
   * @param stmOut -- an OutputStream to receive the file.
   */
  public void checkOutFileToStream(long fileID, int eolType, OutputStream stmOut)
          throws SourceJammerConnectionException, GUICommandException,
                 IOException{
    CommandCentral.getInstance().checkOutFileToStream(fileID, stmOut, eolType);
  }


  /**
   * Add specified file to specified project in SourceJammer archive.
   *
   * @param projectID -- id of parent project for file being added.
   * @param fileName -- full name of file.
   * @param localPath -- java.io.File object pointing file to be added.
   * @param historyType -- History type. Must be a value in AppConfig.FileHistoryStorageTypes.
   * @param fileType -- File type. Must be a value in AppConfig.FileTypes, but not LABEL.
   * @param description -- a description of the file.
   */
  public void addFile(long projectID, String fileName,  java.io.File localPath,
                      int historyType, int fileType, String description)
          throws SourceJammerConnectionException, GUICommandException,
                 IOException{
    addFile(projectID, fileName, localPath, historyType, fileType, description,
            SourceJammerClient.getInstance().getOnAddFileAction());
  }

  /**
   * Add specified file to specified project in SourceJammer archive.
   *
   * @param projectID -- id of parent project for file being added.
   * @param fileName -- full name of file.
   * @param localPath -- java.io.File object pointing file to be added.
   * @param historyType -- History type. Must be a value in AppConfig.FileHistoryStorageTypes.
   * @param fileType -- File type. Must be a value in AppConfig.FileTypes, but not LABEL.
   * @param description -- a description of the file.
   * @param afterAddAction -- Action to take on local file after it has been added.
   *       Choices are in SourceJammerClient--lfa_DELETE, lfa_NO_SPECIAL_ACTION, and lfa_SET_READ_ONLY.
   */
  public void addFile(long projectID, String fileName,  java.io.File localPath,
                      int historyType, int fileType, String description, String afterAddAction)
          throws SourceJammerConnectionException, GUICommandException,
                 IOException{
    CommandCentral.getInstance().addFile(projectID, fileName,  localPath.getAbsolutePath(),
                                          fileType, historyType, description,
                                          afterAddAction, null);
  }


  /**
   * Move a file from one project to another within the SourceJammer archive.
   * 
   * @param fileID -- unique ID of the file to be moved.
   * @param fromProjectID -- unique ID of current parent project of file to be moved.
   * @param toProjectID -- uniquie ID of new parent project of file to be moved.
   */
  public void moveFile(long fileID, long fromProjectID, long toProjectID)
          throws SourceJammerConnectionException, GUICommandException{
    NodeInfo nd = new NodeInfo();
    nd.setUniqueID(fileID);
    CommandCentral.getInstance().moveFile(nd, fromProjectID, toProjectID, null);
  }

  /**
   * Copy a file from one project to another within the SourceJammer archive.
   * 
   * @param fileID -- unique ID of the file to be copied.
   * @param fromProjectID -- unique ID of current parent project of file to be copied.
   * @param toProjectID -- uniquie ID of new parent project of file to be copied.
   */
  public void copyFile(long fileID, long fromProjectID, long toProjectID)
          throws SourceJammerConnectionException, GUICommandException{
    NodeInfo nd = new NodeInfo();
    nd.setUniqueID(fileID);
    CommandCentral.getInstance().copyFile(nd, fromProjectID, toProjectID, null);
  }


  /**
   * Copy a project and all of its contents to a new location within a SourceJammer archive. All
   * files within new project will start over with no history. Existing project will not
   * be changed.
   * 
   * @param projectID -- unique id of project to be copied.
   * @param toProjectID -- unique id of new parent project of project to be copied.
   * @param newProjectName -- new name to use for new project that is created by this copy action.
   *      If null, current name of project to be copied will be used.
   */
  public void copyProject(long projectID, long toProjectID, String newProjectName)
          throws SourceJammerConnectionException, GUICommandException{
    CommandCentral.getInstance().copyProject(projectID, toProjectID, newProjectName);
  }


  /**
   * Move a project and all of its contents to a new location within a SourceJammer archive. All
   * files will maintain their exact same history once move operation is complete.
   * 
   * @param projectID -- unique id of project to be moved.
   * @param toProjectID -- unique id of new parent project of project to be moved.
   * @param newProjectName -- new name to use for new project that is created by this move action.
   *      If null, current name of project to be moved will be used.
   */
  public void moveProject(long projectID, long toProjectID, String newProjectName)
          throws SourceJammerConnectionException, GUICommandException{
    CommandCentral.getInstance().moveProject(projectID, toProjectID, newProjectName);
  }


  /**
   * Add specified file to specified project in SourceJammer archive. File type and
   * history type will be set automatically using HistoryTypeMapper.
   *
   * @param projectID -- id of parent project for file being added.
   * @param fileName -- full name of file.
   * @param localPath -- java.io.File object pointing file to be added.
   * @param description -- a description of the file.
   */
  public void addFile(long projectID, String fileName, java.io.File localPath,
                      String description)
          throws SourceJammerConnectionException, GUICommandException,
                 IOException{
    addFile(projectID, fileName, localPath, description, SourceJammerClient.getInstance().getOnAddFileAction());
  }

  /**
   * Add specified file to specified project in SourceJammer archive. File type and
   * history type will be set automatically using HistoryTypeMapper.
   *
   * @param projectID -- id of parent project for file being added.
   * @param fileName -- full name of file.
   * @param localPath -- java.io.File object pointing file to be added.
   * @param description -- a description of the file.
   * @param afterAddAction -- Action to take on local file after it has been added.
   *       Choices are in SourceJammerClient--lfa_DELETE, lfa_NO_SPECIAL_ACTION, and lfa_SET_READ_ONLY.
   */
  public void addFile(long projectID, String fileName, java.io.File localPath,
                      String description, String afterAddAction)
          throws SourceJammerConnectionException, GUICommandException,
                 IOException{
    HistoryTypeMapper typeMapper = HistoryTypeMapper.getInstance();
    String sType = typeMapper.getDefaultHistoryTypeForFile(fileName);
      //Interpret type to file type and history type.
    int historyType = -1;
    int fileType = -1;
    if (sType.equals(HistoryTypeMapper.TEXT_LIST)){
      fileType = AppConfig.FileTypes.TEXT;
      historyType = AppConfig.FileHistoryStorageTypes.DIFF;
    }
    else{
      fileType = AppConfig.FileTypes.BINARY;
      if (sType.equals(HistoryTypeMapper.BIN_COMPRESSED_LIST)){
        historyType = AppConfig.FileHistoryStorageTypes.ZIP;
      }
      else if(sType.equals(HistoryTypeMapper.BIN_CURR_ONLY_LIST)){
        historyType = AppConfig.FileHistoryStorageTypes.CURR_VERSION_SOURCE_ONLY;
      }
      else if(sType.equals(HistoryTypeMapper.BIN_DIFF_LIST)){
        historyType = AppConfig.FileHistoryStorageTypes.DIFF;
      }
      else if(sType.equals(HistoryTypeMapper.BIN_FULL_LIST)){
        historyType = AppConfig.FileHistoryStorageTypes.FULL_SOURCE;
      }
      else{
        throw new org.sourcejammer.util.BadMethodArgumentException("Unrecognized history type: " + sType);
      }
    }
   addFile(projectID, fileName, localPath, historyType, fileType, description, afterAddAction);
  }

  /**
   * Add a new project to the archive.
   */
  public void addProject(long parentProjectID, String projectName)
          throws SourceJammerConnectionException, GUICommandException{
    CommandCentral.getInstance().makeProject(parentProjectID, projectName);
  }


  /**
   * Gets the specified label to the content directory.
   *
   * @param labelID -- id of the label.
   * @param verNumber -- version number of the label that you want to get. Use
   *           <code>-1</code> to use the latest version of the label.
   * @param targetDir -- directory where the files will be downloaded to.
   * @param buildSubDires -- if <code>true</code>, builds all needed subdirectories.
   *           If <code>false</code>, does not download files that need to go into
   *           sub directories that do not already exist.
   * @param eolType -- the eol type to apply to all text files. This value must be
   *           in AppConfig.EndOfLineType.
   */
   public void getLabel(long labelID, int verNumber, java.io.File targetDir,
                       boolean buildSubDirs, int eolType)
          throws GUICommandException, SourceJammerConnectionException, IOException{

     getLabel(labelID, verNumber, targetDir, buildSubDirs, eolType, isDefaultReadOnlyOnGet());
   }

   private boolean isDefaultReadOnlyOnGet(){
    boolean isReadOnly = false;
    String s = SourceJammerClient.getInstance().getOnGetFileAction();
    if (s.equals(SourceJammerClient.lfa_SET_READ_ONLY)){
      isReadOnly = true;
    }
    return isReadOnly;
   }

   public void getLabel(long labelID, int verNumber, java.io.File targetDir,
                       boolean buildSubDirs, int eolType, boolean setReadOnly)
          throws GUICommandException, SourceJammerConnectionException, IOException{
    if (verNumber < 0){
      //get the latest version number.
      NodeInfo flInfo = new NodeInfo();
      flInfo.setUniqueID(labelID);
      org.sourcejammer.project.view.File labelFile = CommandCentral.getInstance().getFileInfo(flInfo, null);
      verNumber = labelFile.childCount();
    }
    CommandCentral.getInstance().getLabel(labelID, verNumber, targetDir, buildSubDirs, eolType,
              setReadOnly, new RepeatingResponse());
  }


  /**
   * Gets the specified label to the content directory. All required subdirectories
   * will be created if they do not already exist. All text files will be returned
   * with the default EOL type specified in the clientconf.xml file. The latest
   * version of the label will be used.
   */
  public void getLabel(long labelID, java.io.File targetDir)
          throws GUICommandException, SourceJammerConnectionException, IOException{
    int eolType = SourceJammerClient.getInstance().getDefaultEOLType();
    getLabel(labelID, -1, targetDir, true, eolType);
  }


  /**
   * Returns unique id of File based on full sourcejammer path.
   */
  public long getFileUniqueID(String sjPath)
          throws GUICommandException, SourceJammerConnectionException{
    return CommandCentral.getInstance().getFileUniqueID(sjPath);
  }

  /**
   * Returns unique id of Project based on full sourcejammer path.
   */
  public long getProjectUniqueID(String sjPath)
          throws GUICommandException, SourceJammerConnectionException{
    return CommandCentral.getInstance().getFileUniqueID(sjPath);
  }

  /**
   * Disconnect.
   */
  public void disconnect()
          throws GUICommandException, SourceJammerConnectionException{
    CommandCentral.getInstance().disconnect();
  }

  public void setOut(PrintStream out){
    CommandCentral.getInstance().setAlternateOut(out);
  }

  /**
   * Retrieves all files in the specified Project to the specified targetDirectory.
   */
  public void getProject(java.io.File targetDir, Project proj,
                         boolean bRecursive, boolean bBuildSubDirs,
                         int iEOLType, boolean makeReadOnly,
                         boolean verifyCheckIn)
          throws GUICommandException, SourceJammerConnectionException, IOException{
    boolean bSkipThisProj = false;
    if (! targetDir.exists()){
      if (bBuildSubDirs){
        targetDir.mkdir();
      }
      else {
        bSkipThisProj = true;
      }
    }
    if (! bSkipThisProj){
      NodeIterator oChildren = proj.childNodes();
      CommandCentral oCommand = CommandCentral.getInstance();
      while(oChildren.hasMoreNodes()){
        NodeInfo nd = (NodeInfo)oChildren.getNextNode();
        if (nd.getNodeType() == AppConfig.NodeTypes.FILE){
          if (verifyCheckIn && nd.isCheckedOut()){
            String msg = "File (" + nd.getNodeName() + ") is checked out to user (" +
                         nd.getCheckedOutToUser() + ") on date (" +
                         nd.getCheckedOutDate().toString() + ").  Halting project get...";
            throw new GUICommandException(msg);
          }
          oCommand.getFile(nd, targetDir, iEOLType, makeReadOnly, new RepeatingResponse(), null);
        }
        else if (nd.getNodeType() == AppConfig.NodeTypes.PROJECT &&
                 bRecursive ){
          //This also may be a temporary fix.
          java.io.File flChildDirectory = new java.io.File(targetDir, nd.getNodeName());
          Project ndChildProject = oCommand.retrieveProject(nd.getUniqueID());
          getProject(flChildDirectory, ndChildProject, bRecursive, bBuildSubDirs, iEOLType, makeReadOnly, verifyCheckIn);
        }
      }
    }
  }

  /**
   * Return GuiConf object for current archive/user. GuiConf is used to 
   * obtain the user's default directory for each project in the archive.
   */
  public GuiConf getGuiConf(){
    return CommandCentral.getInstance().getGuiConf();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.