package com.dstsystems.hackathon.autotempo.service;

import com.dstsystems.hackathon.autotempo.models.ExchangeUserProfileModel;
import com.dstsystems.hackathon.autotempo.models.TempoUserProfileModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Created by user on 06/11/2015.
 */
public class UserProfileServiceImpl implements UserProfileService {

    @Override
    public ExchangeUserProfileModel getExchangeUserProfile(String pathFileName) throws IOException {
        ExchangeUserProfileModel userProfile = new ExchangeUserProfileModel();

        Properties properties = readProperties(pathFileName);
        Enumeration enuKeys = properties.keys();

        while (enuKeys.hasMoreElements()) {
            String key = (String) enuKeys.nextElement();
            String value = properties.getProperty(key);
            switch (key) {
                case "exchange.userName":
                    userProfile.setUsername(value);
                    break;
                case "exchange.password":
                    userProfile.setPassword(value);
                    break;
                case "exchange.URL":
                    userProfile.setURL(value);
                    break;
                default:
                    break;
            }
        }

        return userProfile;
    }

    @Override
    public TempoUserProfileModel getTempoUserProfile(String pathFileName) throws IOException {
        TempoUserProfileModel userProfile = new TempoUserProfileModel();

        Properties properties = readProperties(pathFileName);
        Enumeration enuKeys = properties.keys();

        while (enuKeys.hasMoreElements()) {
            String key = (String) enuKeys.nextElement();
            String value = properties.getProperty(key);
            switch (key) {
                case "tempo.userName":
                    userProfile.setUsername(value);
                    break;
                case "tempo.password":
                    userProfile.setPassword(value);
                    break;
                case "tempo.URL":
                    userProfile.setURL(value);
                    break;
                default:
                    break;
            }
        }

        return userProfile;
    }

    private Properties readProperties(String pathFileName) throws IOException {
        Properties properties = new Properties();
        try (FileInputStream fileInput = new FileInputStream(pathFileName);) {
            properties.load(fileInput);
            return properties;
        }
    }

}

@@ -198,29 +198,8 @@
 l;%0A%0A
-import java.io.File;%0A
 impo
@@ -275,92 +275,20 @@
 til.
-Enumeration;%0Aimport java.util.Properties;%0A%0A/**%0A * Created by user on 06/11/2015.%0A */
+Properties;%0A
 %0Apub
@@ -472,88 +472,8 @@
 n %7B%0A
-        ExchangeUserProfileModel userProfile = new ExchangeUserProfileModel();%0A%0A
     
@@ -522,32 +522,33 @@
 (pathFileName);%0A
+%0A
         Enumerat
@@ -544,254 +544,133 @@
    E
-numeration enuKeys = properties.keys();%0A%0A        while (enuKeys.hasMoreElements()) %7B%0A            String key = (String) enuKeys.nextElement();%0A            String value = properties.getProperty(key);%0A            switch (key) %7B%0A                case 
+xchangeUserProfileModel userProfile = new ExchangeUserProfileModel();%0A        userProfile.setUsername(properties.getProperty(
 %22exc
@@ -680,38 +680,28 @@
 ge.userName%22
-:%0A            
+));%0A
         user
@@ -715,73 +715,40 @@
 .set
-Username(value);%0A                    break;%0A                case 
+Password(properties.getProperty(
 %22exc
@@ -758,39 +758,36 @@
 ge.password%22
-:%0A     
+));%0A
         
        userP
@@ -766,39 +766,32 @@
 ord%22));%0A        
-       
 userProfile.setP
@@ -793,238 +793,52 @@
 .set
-Password(value);%0A                    break;%0A                case %22exchange.URL%22:%0A                    userProfile.setURL(value);%0A                    break;%0A                default:%0A                    break;%0A            %7D%0A        %7D
+URL(properties.getProperty(%22exchange.URL%22));
 %0A%0A  
@@ -983,82 +983,8 @@
 n %7B%0A
-        TempoUserProfileModel userProfile = new TempoUserProfileModel();%0A%0A
     
@@ -1041,24 +1041,25 @@
 eName);%0A
+%0A
         
 Enumerat
@@ -1054,58 +1054,81 @@
     
-Enumeration enuKeys = properties.keys
+TempoUserProfileModel userProfile = new TempoUserProfileModel
 ();%0A
-%0A
         
 whil
@@ -1127,205 +1127,55 @@
     
-while (enuKeys.hasMoreElements()) %7B%0A            String key = (String) enuKeys.nextElement();%0A            String value = properties.getProperty(key);%0A            switch (key) %7B%0A                case 
+userProfile.setUsername(properties.getProperty(
 %22tem
@@ -1186,30 +1186,20 @@
 serName%22
-:%0A            
+));%0A
         
@@ -1217,73 +1217,40 @@
 .set
-Username(value);%0A                    break;%0A                case 
+Password(properties.getProperty(
 %22tem
@@ -1261,30 +1261,20 @@
 assword%22
-:%0A            
+));%0A
         
@@ -1292,235 +1292,49 @@
 .set
-Password(value);%0A                    break;%0A                case %22tempo.URL%22:%0A                    userProfile.setURL(value);%0A                    break;%0A                default:%0A                    break;%0A            %7D%0A        %7D
+URL(properties.getProperty(%22tempo.URL%22));
 %0A%0A  
@@ -1570,17 +1570,16 @@
 ileName)
-;
 ) %7B%0A    

Refactor UserProfileServiceImpl