Quantcast
Channel:
Viewing all articles
Browse latest Browse all 43911

Forum Post: RE: checkout issue

$
0
0
This was a very timely post as I just started working on using the SDK to check out files this morning. This post helped me as I wasn't aware of the findProject, findView, and findLabel API's so I spent a couple hours this morning writing my own which worked but is using the SDK API's is much easier... :-) Anyway, in case it help Faisal, below is some code that checks out files with labels.  I am just playing with the API so the code is not that pretty or fully bullet proof but should give you a good head start. Two things of note: I had to cast the "v.getViewMembers(s.getTypes().FILE).getAt(0);" call from Anil's post.  It would not compile that way, has to be File file = (File)v.getViewMembers(s.getTypes().FILE).getAt(0); The CheckoutOptions doesn't seem to work.  I am trying to use it to filter the files by a given Label but when I make the checkout call, it gets the file even if it doesn't have that label.  Not a big deal as I created a little loop that does what I want but would be nice to know why that doesn't work (NOT trying to highjack this thread!). Code: import java.lang.*; import com.starteam.*; import com.starteam.exceptions.SDKRuntimeException; public class Checkout {    protected   String  serverAddr      = "serverAddr";            // Server TCP/IP address    protected   int     serverPort      = 49201;                    // Server port number    protected   String  userName        = "user";          // Logon user name    protected   String  userPassword    = "password";               // Logon password    protected   String  projectName     = "project";  // Project name    protected   String  viewName        = "view";  // View name    protected   String  labelName       = "label";     // Label name    public static void main(String args[]) {        Checkout checkout = new Checkout();        // Connect to the server        System.out.println("\nConnecting to StarTeam server \"" + checkout.serverAddr + "\" on port " + checkout.serverPort);        Server stServer = checkout.connectToStarTeamServer();        // Make sure we are connected to the server before continuing        if (!stServer.isConnected()) {            System.out.println("\nNot connected to StarTeam server, cannot continue...");            return;        }        // Get the project object        System.out.println("\nSearching for project \"" + checkout.projectName + "\"");        Project project = stServer.findProject(checkout.projectName);        // Make sure the project is not null        if (project == null) {            System.out.println("\nNo match found for project \"" + checkout.projectName + "\"!\nCannot continue");            // Disconnect from the server and return            stServer.disconnect();            return;        }        else {            System.out.println("Found project \"" + checkout.projectName + "\"");        }        // Get the view object        System.out.println("\nSearching for view \"" + checkout.viewName + "\"");        View view = project.findView(checkout.viewName);        // Make sure the view is not null        if (view == null) {            System.out.println("\nNo match found for view \"" + checkout.viewName + "\"!\nCannot continue");            // Disconnect from the server and return            stServer.disconnect();            return;        }        else {            System.out.println("Found view \"" + checkout.viewName + "\"");        }        // Get the label object        System.out.println("\nSearching for label \"" + checkout.labelName + "\"");        Label label = view.findLabel(checkout.labelName);        // Make sure the label is not null        if (label == null) {            System.out.println("\nNo match found for label \"" + checkout.labelName + "\"!\nCannot continue");            // Disconnect from the server and return            stServer.disconnect();            return;        }        else {            System.out.println("Found label \"" + checkout.labelName + "\"");        }        System.out.println("\nChecking out files...");        // Check them out        checkout.checkoutFiles(stServer, project, view, label);        // Disconnect from the server        stServer.disconnect();    }    // Creates a StarTeam server object, connects to the server, and logs in    protected Server connectToStarTeamServer() {        // Simplest constructor, uses default encryption algorithm and compression level.        Server stServer = new Server(serverAddr, serverPort);        // Connect to the server and logon using specified user name and password.        try {            stServer.logOn(userName, userPassword);        } catch (SDKRuntimeException exception) {            System.out.println("\n" + exception.getLocalizedMessage());            System.out.println(exception.getCause());        }        return(stServer);    }    // Check them out    protected boolean checkoutFiles(Server stServer, Project project, View view, Label label) {        // The CheckoutOptions object set the default view of the specified project        CheckoutOptions checkoutOptions = new CheckoutOptions(view);        // Check out the files based on the label        // This doesn't seem to work for some reason        // It still checks out all of the files        checkoutOptions.setCheckoutByLabel(label);        // Create the checkout manager instance        CheckoutManager checkoutManager = view.createCheckoutManager();        // The file object(s)        File        file        = null;        ViewMember  viewMember  = null;        Label[]     labels      = null;        int         checkedOut  = 0;        // Get the number of files in this view        int vmcSize = ((ViewMemberCollection)view.getViewMembers(stServer.getTypes().FILE)).size();        // Print out some debug if you wish...        //System.out.println("vmCollection size is " + vmcSize);        // Loop through the files and add them to the checkout manager queue        for (int i = 0; i vmcSize; i++) {            // Get the view member object            viewMember = view.getViewMembers(stServer.getTypes().FILE).getAt(i);            // Get all of the labels            labels = viewMember.getAllLabels();            // Loop through the labels and add to checkout manager if it has the one we are looking for            for (int j = 0; j labels.length; j++) {                System.out.println("\nChecking file for label: " + labels[j].getName());                // If the current label and the label we want match...                if (labels[j].getName().equals(label.getName())) {                    // Get the file object                    file = (File)view.getViewMembers(stServer.getTypes().FILE).getAt(i);                    // Print out some information on the file if you wish...                    //System.out.println("File name is: " + file.getName());                    //System.out.println("File fullname is: " + file.getFullName());                    //System.out.println("File description is: " + file.getDescription());                    // Tell checkout manager to check it out                    // Does not actually happen yet, have to use commit for that                    // Don't really have to have checkoutOptions but since it doesn't                    // seem to work anyway, no harm leaving it                    System.out.println("File \"" + file.getFullName() + "\" will be checked out");                    checkoutManager.checkout(file, checkoutOptions);                    // Increment counter so we know how many files we are checking out                    checkedOut++;                }                else {                    // Print out some debug if you wish...                    //System.out.println("Labels do not match, skipping...");                }            }        }        // Check out the files        StringBuffer strBuf = new StringBuffer("\nChecking out " + checkedOut + " of " + vmcSize + " total files for..."                                               + "\nProject:\t" + project                                               + "\nView:\t\t" + view                                               + "\nLabel:\t\t" + label                                               + "\n\nPlease wait...");        System.out.println(new String(strBuf));        checkoutManager.commit();        System.out.println("\nDone!");        return true;    } }

Viewing all articles
Browse latest Browse all 43911

Trending Articles