package ro.dcsi.internship;
import org.apache.commons.cli.*;

public class UserSyncApp {

  //App name
  private final static String appName="usersync";
  private final static String usage = appName + " <src> <dest>";

  //Option strings
  private final static String forgeRockShortOption="f";
  private final static String forgeRockLongOption="forgerock";
  private final static String csvShortOption="c";
  private final static String csvLongOption="csv";


  //Print help
  private static void printHelp(Options options){
    //Print Help
    HelpFormatter help = new HelpFormatter();
    help.printHelp(usage,options);
  }

  //Parse option for UserDao
  private static UserDao parseDaoInfo(Option daoInfo){

    //Parse CSV Dao
    if(daoInfo.getOpt().equals(csvShortOption) || (daoInfo.hasLongOpt() && daoInfo.getLongOpt().equals(csvLongOption)))
      return new CsvUserDao(daoInfo.getValue());

    //Parse ForgeRockDao
    if(daoInfo.getOpt().equals(forgeRockShortOption) || (daoInfo.hasLongOpt() && daoInfo.getLongOpt().equals(forgeRockLongOption)))
      return new ForgeRockUserDao(new OpenIdConfig(daoInfo.getValue(0),daoInfo.getValue(1),daoInfo.getValue(2)));

    return new CsvUserDao("");
  }


  public static void main(String[] args) {

    //Create option group
    Options options = new Options();

    //Creating options
    Option forgeRock = Option.builder(forgeRockShortOption)
            .hasArg(true)
            .argName("server addr> <username> <password")
            .longOpt(forgeRockLongOption)
            .desc("Option to specify forgeRock server")
            .numberOfArgs(3)
            .valueSeparator(' ')
            .build();
    Option csv = Option.builder(csvShortOption)
            .hasArg(true)
            .argName("file")
            .longOpt(csvLongOption)
            .desc("Option to Specify CSV file")
            .numberOfArgs(1)
            .valueSeparator(' ')
            .build();

    //Add options to group
    options.addOption(forgeRock);
    options.addOption(csv);

    //Create Necessary stuff
    CommandLine cmd;
    CommandLineParser parser = new DefaultParser();
    UserDao src;
    UserDao dest;

    //Parse input
    try {
      cmd = parser.parse(options, args);

      //Get options
      Option parsedOptions[] = cmd.getOptions();

      //check number of option
      if(parsedOptions.length == 2){
        //Parse first option
        src = parseDaoInfo(parsedOptions[0]);
        //Parse second option
        dest = parseDaoInfo(parsedOptions[1]);

        //Actual function
        UserSync function = new UserSync();
        function.copyUsers(src, dest);
      }
      printHelp(options);
      throw new RuntimeException("Invalid number of options");
    }
    catch (ParseException e){
      printHelp(options);
      throw new RuntimeException("Invalid Arguments",e);
    }

  }
}

@@ -21,16 +21,17 @@
 rnship;%0A
+%0A
 import o
@@ -134,17 +134,19 @@
  appName
-=
+ = 
 %22usersyn
@@ -284,17 +284,19 @@
 rtOption
-=
+ = 
 %22f%22;%0A  p
@@ -341,17 +341,19 @@
 ngOption
-=
+ = 
 %22forgero
@@ -401,17 +401,19 @@
 rtOption
-=
+ = 
 %22c%22;%0A  p
@@ -456,17 +456,19 @@
 tion
-=
+ = 
 %22csv%22;%0A%0A
 %0A  /
@@ -463,17 +463,16 @@
 %22csv%22;%0A%0A
-%0A
   //Prin
@@ -526,16 +526,17 @@
 options)
+ 
 %7B%0A    //
@@ -617,16 +617,17 @@
 p(usage,
+ 
 options)
@@ -715,16 +715,17 @@
 daoInfo)
+ 
 %7B%0A%0A    /
@@ -737,32 +737,33 @@
 e CSV Dao%0A    if
+ 
 (daoInfo.getOpt(
@@ -941,16 +941,17 @@
 o%0A    if
+ 
 (daoInfo
@@ -980,32 +980,40 @@
 RockShortOption)
+%0A       
  %7C%7C (daoInfo.has
@@ -1147,16 +1147,17 @@
 alue(0),
+ 
 daoInfo.
@@ -1168,16 +1168,17 @@
 alue(1),
+ 
 daoInfo.
@@ -1229,17 +1229,16 @@
 );%0A  %7D%0A%0A
-%0A
   public
@@ -1423,47 +1423,21 @@
 ion)
-%0A            .hasArg(true)%0A            
+.hasArg(true)
 .arg
@@ -1478,36 +1478,32 @@
 sword%22)%0A        
-    
 .longOpt(forgeRo
@@ -1511,37 +1511,24 @@
 kLongOption)
-%0A            
 .desc(%22Optio
@@ -1558,29 +1558,16 @@
 server%22)
-%0A            
 .numberO
@@ -1574,29 +1574,16 @@
 fArgs(3)
-%0A            
 .valueSe
@@ -1587,36 +1587,32 @@
 eSeparator(' ')%0A
-    
         .build()
@@ -1664,47 +1664,21 @@
 ion)
-%0A            .hasArg(true)%0A            
+.hasArg(true)
 .arg
@@ -1689,29 +1689,16 @@
 (%22file%22)
-%0A            
 .longOpt
@@ -1709,28 +1709,24 @@
 LongOption)%0A
-    
         .des
@@ -1756,29 +1756,16 @@
 V file%22)
-%0A            
 .numberO
@@ -1772,29 +1772,16 @@
 fArgs(1)
-%0A            
 .valueSe
@@ -1796,21 +1796,8 @@
 ' ')
-%0A            
 .bui
@@ -2210,16 +2210,17 @@
       if
+ 
 (parsedO
@@ -2238,16 +2238,17 @@
 th == 2)
+ 
 %7B%0A      
@@ -2605,20 +2605,16 @@
 );%0A    %7D
-%0A   
  catch (
@@ -2630,16 +2630,17 @@
 ption e)
+ 
 %7B%0A      
@@ -2712,16 +2712,17 @@
 uments%22,
+ 
 e);%0A    

format style