Java Examples

Examples of programs integrating with Vault and Fortress API. Also, information on integrating with NAnt, CruiseControl.Net and other third-party tools

Moderator: SourceGear

Post Reply
Beth
Posts: 8550
Joined: Wed Jun 21, 2006 8:24 pm
Location: SourceGear
Contact:

Java Examples

Post by Beth » Mon Apr 30, 2018 1:16 pm

API examples in Java

Code: Select all

//Note:  Make sure you have updated API files in your build path. If you don't you will receive errors.

import VaultClientIntegrationLib.DateSortOption;
import VaultClientIntegrationLib.FortressItemExpanded;
import VaultClientIntegrationLib.GetOperations;
import VaultClientIntegrationLib.GetOptions;
import VaultClientIntegrationLib.ItemTrackingOperations;
import VaultClientIntegrationLib.ServerOperations;
import VaultClientIntegrationLib.UnchangedHandler;

import java.io.Console;
import java.io.StringWriter;

import system.IO.TextWriter;
import system.Xml.XmlTextWriter;
import MantisLib.MantisItemExpanded;
import VaultClientIntegrationLib.*;
import VaultClientOperationsLib.*;
import VaultLib.*;

public class JavaAPIExamples {
	

	void BasicSourceControl()
    {
        String url = "http://vaultdemo.sourcegear.com/vaultservice";
        String username = "guest5";
        String password = "guest5";
        String repository = "Initial Repository";

        // Set the login options and login/connect to a repository.
        ServerOperations.client.LoginOptions.URL = url;
        ServerOperations.client.LoginOptions.User = username;
        ServerOperations.client.LoginOptions.Password = password;
        ServerOperations.client.LoginOptions.Repository = repository;
        ServerOperations.Login();

        //Create a folder.
        String pathToANewFolder = "$/path/to/a/newFolder";
        ServerOperations.ProcessCommandCreateFolder(pathToANewFolder);

        //Add two files to the new folder.
        String pathToFile1 = "c:/path/to/a/file";
        String pathToFile2 = "c:/path/to/another/file";
        String folderToAddItemsTo = "$/path/to/a/folder/";
        ServerOperations.client.AutoCommit = true;
        ServerOperations.ProcessCommandAdd(folderToAddItemsTo, new String[] { pathToFile1, pathToFile2 });
        
        // Set the login options and login/connect to a repository.
        ServerOperations.client.LoginOptions.URL = url;
        ServerOperations.client.LoginOptions.User = username;
        ServerOperations.client.LoginOptions.Password = password;
        ServerOperations.client.LoginOptions.Repository = repository;
        ServerOperations.Login();

        // Set a working folder.
        String repositoryFolderPath = "$/path/to/a/repository/folder/";
        String diskPath = "c:/path/to/a/working/folder/";
        
        boolean bCreateDiskPath = true;
        ServerOperations.SetWorkingFolder(repositoryFolderPath, diskPath, bCreateDiskPath);

        // Do a get on the working folder.
        GetOptions getOptions = new GetOptions();
        getOptions.Recursive = true;
        GetOperations.ProcessCommandGet(new String[] { diskPath }, getOptions);

        // Exclusively check out a file.
        getOptions = new GetOptions();
        String filePath = "$/path/to/a/file";
        boolean bExclusive = true;
        boolean bGetLatest = true;
        ServerOperations.ProcessCommandCheckout(new String[] { filePath }, bExclusive, bGetLatest, getOptions);

        // coList is a list of all the check outs. Output it in any preferrred manner.
        VaultClientCheckOutList coList = ServerOperations.ProcessCommandListCheckOuts();

        
        // Check in the file we just checked out, set the unchanged handler to check in the item (the default unchanged op is Undo Checkout).
        boolean bKeepCheckedOut = false;
        ServerOperations.ProcessCommandCheckIn(new String[] { filePath }, UnchangedHandler.Checkin, bKeepCheckedOut, LocalCopyType.Leave);

        //Query the file history so we can view the check in.
        boolean bRecursive = false;
        int beginVersion = -1;
        int endVersion = -1;
        VaultHistoryItem[] items = ServerOperations.ProcessCommandHistory(filePath, bRecursive, DateSortOption.desc, null, null, VaultDate.EmptyDate().toString(), VaultDate.EmptyDate().toString(), null, null, beginVersion, endVersion, 10);

	//Now you can output the items in your preferred manner.              
    }
	
    public static void WildcardTasks()
    {
        String url = "http://VaultServer/VaultService";
        String username = "username";
        String password = "password";
        String repository = "Your Repository";

        // Set the login options and login/connect to a repository.
        ServerOperations.client.LoginOptions.URL = url;
        ServerOperations.client.LoginOptions.User = username;
        ServerOperations.client.LoginOptions.Password = password;
        ServerOperations.client.LoginOptions.Repository = repository;
        ServerOperations.Login();

        ServerOperations.client.AutoCommit = true;

        // add some files to use with wildcard tasks
        String pathToFile1 = "c:/path/to/wildcard1.txt";
        String pathToFile2 = "c:/path/to/wildcard2.txt";
        String pathToFile3 = "c:/path/to/wildcardWithLetters.txt";
        String folderPath = "$/a/folder/";
        ServerOperations.ProcessCommandAdd(folderPath, new String[] { pathToFile1, pathToFile2, pathToFile3 });

        //  get all three wildcards to a nonworking folder
        String wildcardStr = "wildcard*.txt";
        GetOptions getOptions = new GetOptions();
        String nonworkingFolderPath = "c:/path/to/nonworkingFolderForWildcards/";
        GetOperations.ProcessCommandGetWildcardToNonWorkingFolder(folderPath, new String[] { wildcardStr }, getOptions, nonworkingFolderPath);

        // get wildcard1.txt and wildcard2.txt to a working folder
        wildcardStr = "wildcard?.txt";
        String workingFolderPath = "c:/path/to/aWorkingFolder/";
        GetOperations.ProcessCommandGetWildcard(workingFolderPath, new String[] {wildcardStr}, getOptions);
    }

    public static void LabelTasks()
    {
        String url = "http://VaultServer/VaultService";
        String username = "username";
        String password = "password";
        String repository = "Your Repository";

        // Set the login options and login/connect to a repository.
        ServerOperations.client.LoginOptions.URL = url;
        ServerOperations.client.LoginOptions.User = username;
        ServerOperations.client.LoginOptions.Password = password;
        ServerOperations.client.LoginOptions.Repository = repository;
        ServerOperations.Login();

        ServerOperations.client.AutoCommit = true;

        // Label the current version of a folder
        String objectPath = "$/path/to/a/folderToLabel/";
        String labelName = "LabeledFolder";
        long versionID = -1; //pass -1 to get the current version
        ServerOperations.ProcessCommandLabel(objectPath, labelName, versionID);

        // Get label to a location on disk
        GetOptions getOptions = new GetOptions();
        String destPath = "c:/path/to/labeledFolder/";
        GetOperations.ProcessCommandGetLabelToLocationOutsideWorkingFolder(objectPath, labelName, null, getOptions, destPath);
    }

    public static void AddFortressItem()
    {
        String url = "http://FortressServer/VaultService";
        String username = "username";
        String password = "password";

        // Set the login options and login/connect to a repository.
        ServerOperations.client.LoginOptions.URL = url;
        ServerOperations.client.LoginOptions.User = username;
        ServerOperations.client.LoginOptions.Password = password;
        ServerOperations.Login();

        // List the open fortress items
        String projectName = "aProject";
        MantisItemExpanded[] openItems = ItemTrackingOperations.ProcessCommandListOpenFortressItems(projectName);

	//Now you can output the openItems in your preferred manner.
        
        // Make a new fortress item
        FortressItemExpanded newItem = new FortressItemExpanded();
        newItem.ProjectName = projectName;
        newItem.Description = "A new item.";
        newItem.Details = "Some details about the new item.";
        newItem.Status = "Open";
        newItem.Assignee = "someUser";
        newItem.Resolver = "anotherUser";
        newItem.Platform = "Unknown";
        newItem.ItemType = "Bug";
        newItem.TimeEstimate = "Unknown";
        newItem.Priority = "Low";
        newItem.Validate();  // Call Validate after setting all your Strings for your item and before you pass the item to a method.

        
        // Add the new item
        ItemTrackingOperations.ProcessCommandAddFortressItem(newItem);

        // List the open fortress items again
        openItems = ItemTrackingOperations.ProcessCommandListOpenFortressItems(projectName);

	//Now you can output the items in your preferred manner.

    }
}
Beth Kieler
SourceGear Technical Support

Post Reply