Commit bc239f9a authored by Benjamin Jedrocha's avatar Benjamin Jedrocha
Browse files

Debug Page et début clean code

parent 63651ad6
package com.oxyl.newrofactory;
import com.oxyl.newrofactory.persistence.DBManager;
import com.oxyl.newrofactory.service.TerminalManager;
import com.oxyl.newrofactory.ui.CLI;
public class Main {
public static void main(String[] args) {
DBManager db = DBManager.Instance();
db.init();
new TerminalManager().start();
new CLI().start();
}
}
package com.oxyl.newrofactory.persistence;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import com.oxyl.newrofactory.mapper.InternMapping;
......@@ -32,7 +30,6 @@ public class Queries {
return InternMapping.map(rs);
} catch (SQLException e) {
System.out.println(e.getCause());
;
}
return List.of();
}
......@@ -50,11 +47,11 @@ public class Queries {
}
public static List<Object> SQLGetPromotions(int startAt, int howMany) {
String select = "select id, name from promotion" + "LIMIT ? OFFSET ?";
String select = "select id, name from promotion LIMIT ? OFFSET ?";
try (PreparedStatement selectStatement = connection.prepareStatement(select)) {
ResultSet rs = selectStatement.executeQuery();
selectStatement.setInt(1, howMany);
selectStatement.setInt(2, startAt);
ResultSet rs = selectStatement.executeQuery();
return PromotionMapping.map(rs);
} catch (SQLException e) {
e.printStackTrace();
......@@ -79,9 +76,9 @@ public class Queries {
try (PreparedStatement insertIntern = connection.prepareStatement(insertString)) {
insertIntern.setString(1, intern.getFirstName());
insertIntern.setString(2, intern.getLastName());
insertIntern.setTimestamp(3, Timestamp.valueOf(LocalDateTime.of(intern.getArrival(), LocalTime.of(0, 0))));
insertIntern.setTimestamp(4,
Timestamp.valueOf(LocalDateTime.of(intern.getFormationOver(), LocalTime.of(0, 0))));
insertIntern.setDate(3, intern.getArrival() == null ? null : Date.valueOf(intern.getArrival()));
insertIntern.setDate(4,
intern.getFormationOver() == null ? null : Date.valueOf((intern.getFormationOver())));
insertIntern.setLong(5, intern.getPromotionId());
ResultSet rs = insertIntern.executeQuery();
return InternMapping.map(rs);
......@@ -92,14 +89,14 @@ public class Queries {
}
public static void SQLUpdateIntern(Intern intern) {
String updateString = "update intern set first_name = ?, last_name = ?, arrival = ?, formation_over = ?, promotion_id = ? WHERE id = ?";
String updateString = "update intern set first_name = ?, last_name = ?, arrival = ?, "
+ "formation_over = ?, promotion_id = ? WHERE id = ?";
try (PreparedStatement updateIntern = connection.prepareStatement(updateString)) {
updateIntern.setString(1, intern.getFirstName());
updateIntern.setString(2, intern.getLastName());
updateIntern.setTimestamp(3, intern.getArrival() == null ? null
: Timestamp.valueOf(LocalDateTime.of(intern.getArrival(), LocalTime.of(0, 0))));
updateIntern.setTimestamp(4, intern.getFormationOver() == null ? null
: Timestamp.valueOf(LocalDateTime.of(intern.getFormationOver(), LocalTime.of(0, 0))));
updateIntern.setDate(3, intern.getArrival() == null ? null : Date.valueOf(intern.getArrival()));
updateIntern.setDate(4,
intern.getFormationOver() == null ? null : Date.valueOf((intern.getFormationOver())));
updateIntern.setLong(5, intern.getPromotionId());
updateIntern.setLong(6, intern.getId());
updateIntern.executeUpdate();
......
......@@ -11,6 +11,8 @@ public class Page {
private int maxPageLoaded = -1;
private boolean isAllLoaded = false;
private final int numberOfElementsByPage;
private final BiFunction<Integer, Integer, List<Object>> selectOnDB;
......@@ -19,19 +21,29 @@ public class Page {
this.numberOfElementsByPage = numberOfElementsByPage;
this.selectOnDB = selectOnDB;
}
public int getCurrentPage() {
if (currentPage == -1) {
throw new IllegalStateException("Need to do next() at least one time before used this method !");
}
return currentPage;
}
public List<Object> next() {
currentPage++;
if (currentPage > maxPageLoaded) {
list.addAll(selectOnDB.apply(numberOfElementsByPage * currentPage, numberOfElementsByPage));
if (!isAllLoaded && currentPage == maxPageLoaded) {
currentPage++;
List<Object> result = selectOnDB.apply(numberOfElementsByPage * currentPage, numberOfElementsByPage);
if (result.size() < numberOfElementsByPage) {
isAllLoaded = true;
}
list.addAll(result);
maxPageLoaded++;
} else if (isAllLoaded && currentPage != maxPageLoaded) {
currentPage++;
}
return list.subList(numberOfElementsByPage * currentPage,
numberOfElementsByPage * currentPage + numberOfElementsByPage);
Math.min(numberOfElementsByPage * currentPage + numberOfElementsByPage, list.size()));
}
public List<Object> previous() {
......
package com.oxyl.newrofactory.persistence;
package com.oxyl.newrofactory.service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
......@@ -6,8 +6,7 @@ import java.time.format.DateTimeParseException;
import java.util.List;
import com.oxyl.newrofactory.model.Intern;
import com.oxyl.newrofactory.service.Page;
import com.oxyl.newrofactory.service.UIApplier;
import com.oxyl.newrofactory.persistence.Queries;
import com.oxyl.newrofactory.ui.AskUI;
import com.oxyl.newrofactory.ui.PrintUI;
......@@ -46,8 +45,28 @@ public class UIAppliers {
public static UIApplier codeGetPromotions() {
return (print, ask) -> {
// List<Object> promotions = Queries.SQLGetPromotions();
// print.printList(promotions);
Page page = new Page(10, (t, u) -> Queries.SQLGetPromotions(t, u));
String in = "";
print.printList(page.next());
while (!in.equals("q()")) {
print.printMessage("next : Suivant, prev : Retour, q() : Quitter, page numéro : " + page.getCurrentPage());
in = ask.text();
print.printMessage("\n\n");
switch (in) {
case "next":
print.printList(page.next());
break;
case "prev":
if(page.getCurrentPage() == 0) {
print.printMessage("Action impossible");
break;
}
print.printList(page.previous());
break;
case "q()":
return;
}
}
};
}
......
package com.oxyl.newrofactory.persistence;
package com.oxyl.newrofactory.service;
import com.oxyl.newrofactory.service.NumberChoiceUIApplier;
public class NumberChoiceUIApplierGenerator {
public class UIStates {
public static NumberChoiceUIApplier defaultChoices() {
NumberChoiceUIApplier choices = new NumberChoiceUIApplier();
......
package com.oxyl.newrofactory.service;
package com.oxyl.newrofactory.ui;
import java.util.Optional;
import com.oxyl.newrofactory.persistence.NumberChoiceUIApplierGenerator;
import com.oxyl.newrofactory.ui.AskUI;
import com.oxyl.newrofactory.ui.PrintUI;
import com.oxyl.newrofactory.ui.TerminalPrintUI;
import com.oxyl.newrofactory.ui.TerminalScannerUI;
import com.oxyl.newrofactory.service.NumberChoiceUIApplier;
import com.oxyl.newrofactory.service.UIApplier;
import com.oxyl.newrofactory.service.UIStates;
public class TerminalManager {
public class CLI {
private static final String QUIT = "q()";
private final NumberChoiceUIApplier choices = NumberChoiceUIApplierGenerator.defaultChoices();
private final NumberChoiceUIApplier choices = UIStates.defaultChoices();
private final AskUI askUI = new TerminalScannerUI();
......
package com.oxyl.newrofactory.ui;
public class Home implements State {
@Override
public StateStatus execute(String input) {
// TODO Auto-generated method stub
return null;
}
}
package com.oxyl.newrofactory.ui;
public interface State {
StateStatus execute(String input);
}
package com.oxyl.newrofactory.ui;
public enum StateStatus {
FILL_STRING, FILL_INT, DONE
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment