package com.collab.examples;


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

import com.collab.util.TeamForgeConnection;
import com.collab.util.GroupUtil;
import com.collab.util.RoleUtil;



public class Example6
{
   private static final Logger
      logger = Logger.getLogger(Example6.class.getName());


  /**
   * Example 7 finds the logged-in users Groups and Roles.
   * <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>
   *    project:         the project to report roles from.
   * <p>
   * @param args command line arguments passed into main().
   * <p>
   * @throws 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 Example7");

      String sfUrl;
      String sfUser;
      String sfPass;
      String sfProject;

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

         TeamForgeConnection sfc = new TeamForgeConnection(sfUrl);

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

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

         logger.log(Level.INFO, "Group memberships for " + sfUser);

         GroupUtil gu = new GroupUtil(sfUrl, sessionId);
         Collection<String> groups = gu.getGroups(sfUser);
         if (groups.isEmpty()) {
             logger.log(Level.INFO, "None Found!");
         } else {
             logger.log(Level.INFO, "=======================");
             for (String group: groups) {
                 logger.log(Level.INFO, group);
             }
         }
         logger.log(Level.INFO, "");

         logger.log(Level.INFO, "Roles for " + sfUser + " in Project " + sfProject);
         RoleUtil ru = new RoleUtil(sfUrl, sessionId);
         Collection<String> roles = ru.getRoles(sfProject, sfUser);
         if (roles.isEmpty()) {
             logger.log(Level.INFO, "No roles found!");
         } else {
             logger.log(Level.INFO, "=======================");
             for (String role: roles) {
                 logger.log(Level.INFO, role);
             }
         }
         logger.log(Level.INFO, "");

         sfc.logoff(sessionId);

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

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