diff --git a/src/main/java/com/rgsystem/Main.java b/src/main/java/com/rgsystem/Main.java index 3489b91..e0602ae 100644 --- a/src/main/java/com/rgsystem/Main.java +++ b/src/main/java/com/rgsystem/Main.java @@ -8,6 +8,8 @@ import com.rgsystem.input.CommandLineInputs; import com.rgsystem.ui.CmdLineUI; import com.rgsystem.ui.UI; +import com.rgsystem.validation.CMDInputValidations; +import com.rgsystem.validation.InputValidations; public class Main { @@ -20,7 +22,8 @@ public static void main(String[] args){ ); Database database = new SQLDatabase(); - Inputs inputs = new CommandLineInputs(args); + InputValidations validations = new CMDInputValidations(); + Inputs inputs = new CommandLineInputs(args, validations); UI ui = new CmdLineUI(); ReportGeneratorApp app = new ReportGeneratorApp(connection, database, inputs, ui); diff --git a/src/main/java/com/rgsystem/report/EmailBody.java b/src/main/java/com/rgsystem/emails/EmailBody.java similarity index 92% rename from src/main/java/com/rgsystem/report/EmailBody.java rename to src/main/java/com/rgsystem/emails/EmailBody.java index c90c503..ac08e4a 100644 --- a/src/main/java/com/rgsystem/report/EmailBody.java +++ b/src/main/java/com/rgsystem/emails/EmailBody.java @@ -1,4 +1,4 @@ -package com.rgsystem.report; +package com.rgsystem.emails; import java.io.File; diff --git a/src/main/java/com/rgsystem/report/EmailBodyGenerator.java b/src/main/java/com/rgsystem/emails/EmailBodyGenerator.java similarity index 93% rename from src/main/java/com/rgsystem/report/EmailBodyGenerator.java rename to src/main/java/com/rgsystem/emails/EmailBodyGenerator.java index 27cc23a..1421d2c 100644 --- a/src/main/java/com/rgsystem/report/EmailBodyGenerator.java +++ b/src/main/java/com/rgsystem/emails/EmailBodyGenerator.java @@ -1,4 +1,4 @@ -package com.rgsystem.report; +package com.rgsystem.emails; import java.io.File; diff --git a/src/main/java/com/rgsystem/input/CommandLineInputs.java b/src/main/java/com/rgsystem/input/CommandLineInputs.java index 592565a..5d4ff38 100644 --- a/src/main/java/com/rgsystem/input/CommandLineInputs.java +++ b/src/main/java/com/rgsystem/input/CommandLineInputs.java @@ -1,11 +1,15 @@ package com.rgsystem.input; +import com.rgsystem.validation.InputValidations; + public class CommandLineInputs implements Inputs { private final String[] args; + private final InputValidations validations; - public CommandLineInputs(String[] args) { + public CommandLineInputs(String[] args, InputValidations validations) { this.args = args; + this.validations = validations; } @Override @@ -13,14 +17,7 @@ public String getReportType() throws InvalidInputException { String reportType = args[0]; //Make sure to validate the arguments before using... - if (reportType == null) { - throw new InvalidInputException("Please provide the report type as an argument"); - } - - if (!(reportType.equals("daily-sales") || reportType.equals("user-signups"))) { - throw new InvalidInputException - ("Please provide daily-sales or user-signups as the report type for first argument"); - } + validations.validateReportType(reportType); return reportType; } @@ -30,14 +27,7 @@ public String getStartDate() throws InvalidInputException { String startDate = args[1]; //Make sure to validate the arguments before using... - if (startDate == null) { - throw new InvalidInputException("Please provide the start date as the second argument"); - } - - /*if(!(isDateValid(startDate))){ - throw new InvalidInputException("Please provide the start date in dd/mm/yyyy format"); - } */ - + validations.validateDate(startDate); return startDate; } @@ -47,14 +37,7 @@ public String getEndDate() throws InvalidInputException { String endDate = args[2]; //Make sure to validate the arguments before using... - if (endDate == null) { - throw new InvalidInputException("Please provide the end date as the third argument"); - } - - /*if(!(isDateValid(endDate))){ - throw new InvalidInputException("Please provide the end date in dd/mm/yyyy format"); - }*/ - + validations.validateDate(endDate); return endDate; } @@ -63,15 +46,7 @@ public String getOutputFormat() throws InvalidInputException { String outputFormat = args[3]; //Make sure to validate the arguments before using... - if (outputFormat == null) { - throw new InvalidInputException("Please provide the report type as an argument"); - } - - if (!(outputFormat.equals("email") || outputFormat.equals("file"))) { - throw new InvalidInputException - ("Please provide mail or file as the report output format for fourth argument"); - } - + validations.validateOutputFormat(outputFormat); return outputFormat; } @@ -81,14 +56,7 @@ public String getUserEmail() throws InvalidInputException { String userEmail = args[4]; //Make sure to validate the arguments before using... - if (userEmail == null) { - throw new InvalidInputException("Please provide the user email as the fifth argument"); - } - - /*if(!(isEmailValid(userEmail))){ - throw new InvalidInputException("Please provide a valid user email as the fifth argument"); - }*/ - + validations.validateEmail(userEmail); return userEmail; } diff --git a/src/main/java/com/rgsystem/output/outputs/EmailOutput.java b/src/main/java/com/rgsystem/output/outputs/EmailOutput.java index c551f83..687a556 100644 --- a/src/main/java/com/rgsystem/output/outputs/EmailOutput.java +++ b/src/main/java/com/rgsystem/output/outputs/EmailOutput.java @@ -6,8 +6,8 @@ import com.rgsystem.output.ExcelFileOutput; import com.rgsystem.output.Outputs; import com.rgsystem.output.WorkBookWriter; -import com.rgsystem.report.EmailBody; -import com.rgsystem.report.EmailBodyGenerator; +import com.rgsystem.emails.EmailBody; +import com.rgsystem.emails.EmailBodyGenerator; import com.rgsystem.report.Period; import org.apache.poi.xssf.usermodel.XSSFWorkbook; diff --git a/src/main/java/com/rgsystem/validation/CMDInputValidations.java b/src/main/java/com/rgsystem/validation/CMDInputValidations.java new file mode 100644 index 0000000..7efb3af --- /dev/null +++ b/src/main/java/com/rgsystem/validation/CMDInputValidations.java @@ -0,0 +1,51 @@ +package com.rgsystem.validation; + +import com.rgsystem.input.InvalidInputException; + +public class CMDInputValidations implements InputValidations{ + + @Override + public void validateReportType(String reportType) throws InvalidInputException { + + if (reportType == null) { + throw new InvalidInputException("Please provide the report type"); + + }else if (!(reportType.equals("daily-sales") || reportType.equals("user-signups"))){ + throw new InvalidInputException + ("Please provide daily-sales or user-signups as the report type for first argument"); + } + } + + @Override + public void validateDate(String date) throws InvalidInputException { + + if (date == null) { + throw new InvalidInputException("Please provide the start and end dates"); + }else if (!(date.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})"))){ + throw new InvalidInputException("Please provide the date in dd/mm/yyyy format"); + } + + } + + @Override + public void validateOutputFormat(String outputFormat) throws InvalidInputException { + if (outputFormat == null) { + throw new InvalidInputException("Please provide the report type as an argument"); + }else if(!(outputFormat.equals("email") || outputFormat.equals("file"))) { + throw new InvalidInputException + ("Please provide mail or file as the report output format for fourth argument"); + } + } + + @Override + public void validateEmail(String email) throws InvalidInputException { + + String regex = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"; + + if (email == null) { + throw new InvalidInputException("Please provide the user email as the fifth argument"); + }else if(!(email.matches(regex))){ + throw new InvalidInputException("Please provide a valid user email as the fifth argument"); + } + } +} diff --git a/src/main/java/com/rgsystem/validation/InputValidations.java b/src/main/java/com/rgsystem/validation/InputValidations.java new file mode 100644 index 0000000..cb2f3e1 --- /dev/null +++ b/src/main/java/com/rgsystem/validation/InputValidations.java @@ -0,0 +1,12 @@ +package com.rgsystem.validation; + +import com.rgsystem.input.InvalidInputException; + +public interface InputValidations { + + void validateReportType(String reportType) throws InvalidInputException; + void validateDate(String date) throws InvalidInputException; + void validateOutputFormat(String outputFormat) throws InvalidInputException; + void validateEmail(String email) throws InvalidInputException; + +}