package com.collab.examples;


import java.rmi.RemoteException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.collab.util.TeamForgeConnection;
import com.collab.util.ProjectUtil;
import com.collabnet.ce.soap60.webservices.cemain.ProjectSoapRow;


/**
 * Example2 shows how one might go about retrieving a list of projects
 * in which a given user (passed in on the command line) is a member.
 * <p>
 * This example illustrates how a developer might retrieve and then
 * loop through a list of projects.  It further shows how information
 * on a given user might be retrieved within the context of a given
 * project.
 * <p>
 */
public class Example2
{
   private static final Logger
      logger = Logger.getLogger(Example2.class.getName());


  /**
   * Example 2 prints out a list of projects of which the user passed
   * on the command line is a member.
   * <p>
   * The program expects the following arguments:
   * <p>
   *    teamforge-url: the URL of the TeamForge server
   * <p>
   *    username:        a valid TeamForge user
   * <p>
   *    password:        the password of the TeamForge user
   * <p>
   *    username:        the login account for which we will print
   *                     out all projects in which the user has
   *                     administrative permissions
   * <p>
   * @param args command line arguments passed into main().
   * <p>
   * @throws java.rmi.RemoteException any errors logging in or out of TeamForge will
   *                         be thrown from this method
   */
   public static void main(String args[])
		throws RemoteException
	{

      logger.log(Level.INFO, "running Example2");

      String sfUrl;
      String sfUser;
      String sfPass;
      String sfFindMe;

      if (args.length != 4)
      {
         logger.log(Level.SEVERE, "Usage: teamforge-url username password user-search-term");
      }
      else
      {
         sfUrl    = args[0];
         sfUser   = args[1];
         sfPass   = args[2];
         sfFindMe = args[3];

         TeamForgeConnection sfc = new TeamForgeConnection(sfUrl);

         String sessionId = sfc.login(sfUser,sfPass);

         logger.log(Level.INFO, "successfully connected to TeamForge");

         ProjectUtil projs = new ProjectUtil(sfUrl, sessionId);

         logger.log(Level.INFO, "retrieving all projects of which " + sfFindMe + " is a member");

         List<ProjectSoapRow> mbrProjects = projs.getProjectList(sfFindMe);

         logger.log(Level.INFO, "Project list");
         logger.log(Level.INFO, "------------");

         for (ProjectSoapRow i : mbrProjects)
            logger.log(Level.INFO, i.getTitle() + "  " + i.getId());

         sfc.logoff(sessionId);

         logger.log(Level.INFO, "successfully disconnected from TeamForge");
      }

      logger.log(Level.INFO, "exiting Example2");
   }
}
