1
0
Fork 0
 Conflicts:
	src/main/java/GUI/Agenda/Agenda.java
	src/main/java/GUI/Agenda/TableModel.java
	src/main/java/GUI/ProjectionHandler.java
This commit is contained in:
Tergel TSAGAAN 2021-01-12 15:25:55 +01:00
commit d5a71e5a50
6 changed files with 108 additions and 37 deletions

View file

@ -0,0 +1,84 @@
package Config;
import Exceptions.InvalidConfig;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Map;
public class Config {
static private final JSONObject template = new JSONObject(
Map.ofEntries(
Map.entry("db",
new JSONObject(Map.ofEntries(Map.entry("host", ""),
Map.entry("database", ""),
Map.entry("user", ""),
Map.entry("password", "")))
),
Map.entry("startDate", LocalDate.of(2021, 5, 11).toString()),
Map.entry("endDate", LocalDate.of(2021, 5, 22).toString())
)
);
static private void createConfig() {
File f = new File("./config.json");
try {
FileWriter fw = new FileWriter(f);
fw.write(template.toJSONString());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
static private boolean checkConfig(JSONObject jsonObject, JSONObject template) {
for (Object k : template.keySet().toArray())
if (!jsonObject.containsKey(k) ||
(template.get(k).getClass() == JSONObject.class &&
!checkConfig((JSONObject) jsonObject.get(k), (JSONObject) template.get(k))))
return false;
return true;
}
static private JSONObject getConfig() throws InvalidConfig {
try {
File f = new File("./config.json");
if (!f.exists())
createConfig();
JSONObject config = (JSONObject) new JSONParser().parse(new FileReader(f));
if (checkConfig(config, template))
return config;
else
throw new InvalidConfig();
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return null;
}
static public JSONObject getDBAuth() throws InvalidConfig {
return (JSONObject) Config.getConfig().get("db");
}
static public LocalDate getStartDate() {
try {
return LocalDate.parse((String) Config.getConfig().get("startDate"));
} catch (InvalidConfig e) {
return null;
}
}
static public LocalDate getEndDate() {
try {
return LocalDate.parse((String) Config.getConfig().get("endDate"));
} catch (InvalidConfig e) {
return null;
}
}
}

View file

@ -1,13 +1,12 @@
package DB; package DB;
import Config.Config;
import Exceptions.AlreadyOnTable; import Exceptions.AlreadyOnTable;
import Exceptions.InvalidConfig;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.*; import java.sql.*;
import java.sql.Date; import java.sql.Date;
@ -16,39 +15,15 @@ import java.util.*;
public class DB { public class DB {
@Getter static private Connection connection; @Getter static private Connection connection;
static private JSONObject getAuth() {
try {
File f = new File("./config.json");
if (!f.exists()) {
FileWriter fw = new FileWriter(f);
fw.write(
new JSONObject(
Map.ofEntries(
Map.entry("host", ""),
Map.entry("database", ""),
Map.entry("user", ""),
Map.entry("password", "")
)
).toJSONString()
);
fw.close();
}
return (JSONObject) new JSONParser().parse(new FileReader(f));
} catch (ParseException | IOException e) {
e.printStackTrace();
return null;
}
}
static public boolean connect() { static public boolean connect() {
try { try {
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
JSONObject auth = getAuth(); JSONObject auth = Config.getDBAuth();
if (auth != null) if (auth != null)
connection = DriverManager.getConnection("jdbc:mariadb://"+auth.get("host")+"/"+auth.get("database")+"?user="+auth.get("user")+"&password="+auth.get("password")); connection = DriverManager.getConnection("jdbc:mariadb://"+auth.get("host")+"/"+auth.get("database")+"?user="+auth.get("user")+"&password="+auth.get("password"));
return true; return true;
} }
} catch (SQLException e) { } catch (SQLException | InvalidConfig e) {
return false; return false;
} }
return false; return false;

View file

@ -0,0 +1,7 @@
package Exceptions;
public class InvalidConfig extends Exception {
public InvalidConfig() {
super("Configuration file is invalid");
}
}

View file

@ -1,5 +1,6 @@
package GUI.Agenda; package GUI.Agenda;
import Config.Config;
import DB.Competition; import DB.Competition;
import DB.Projection; import DB.Projection;
import DB.Slot; import DB.Slot;
@ -23,7 +24,6 @@ import java.util.Arrays;
public class Agenda extends JPanel { public class Agenda extends JPanel {
private JPanel agendaPanel; private JPanel agendaPanel;
private Object headers[]; private Object headers[];
private int totalDay;
private int currentPage; private int currentPage;
private JTable table; private JTable table;
private CompetType competionType; private CompetType competionType;
@ -36,7 +36,6 @@ public class Agenda extends JPanel {
this.agendaPanel = agendaPanel; this.agendaPanel = agendaPanel;
this.slots = competionType.getSlots(); this.slots = competionType.getSlots();
this.headers = slots.toArray(); this.headers = slots.toArray();
this.totalDay = 11;
this.currentPage = currentPage; this.currentPage = currentPage;
this.competionType = competionType; this.competionType = competionType;
this.agendaPanel.removeAll(); this.agendaPanel.removeAll();
@ -65,7 +64,8 @@ public class Agenda extends JPanel {
} }
private int[] dayToPage() { private int[] dayToPage() {
int day = this.totalDay; int totalDay = Config.getEndDate().compareTo(Config.getStartDate());
int day = totalDay;
int count = 0; int count = 0;
while (day >= 5) { while (day >= 5) {
day = day - 5; day = day - 5;
@ -87,7 +87,7 @@ public class Agenda extends JPanel {
return headers[index]; return headers[index];
} }
}; };
TableModel dm = new TableModel(day, currentPage, headers.length); TableModel dm = new TableModel(day, currentPage);
table = new JTable(dm) { table = new JTable(dm) {
@Override @Override
public boolean getScrollableTracksViewportHeight() { public boolean getScrollableTracksViewportHeight() {

View file

@ -1,5 +1,7 @@
package GUI.Agenda; package GUI.Agenda;
import Config.Config;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;
@ -7,15 +9,17 @@ import java.util.Arrays;
public class TableModel extends AbstractTableModel { public class TableModel extends AbstractTableModel {
private LocalDate[] columnNames; private LocalDate[] columnNames;
private Object[][] data; private Object[][] data;
private int headerSize;
private int day; private int day;
public TableModel(int day, int currentPage, int headerSize) { public TableModel(int day, int currentPage) {
this.day = day; this.day = day;
columnNames = new LocalDate[this.day]; columnNames = new LocalDate[this.day];
data = new Object[5][this.day]; data = new Object[5][this.day];
LocalDate startDate = Config.getStartDate();
for (int i = 0; i < this.day; i++) { for (int i = 0; i < this.day; i++) {
this.columnNames[i] = LocalDate.of(2021, 05, (i + 11) + (5 * (currentPage))); this.columnNames[i] = startDate.plusDays(i + 5 * (currentPage));
} }
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
Arrays.fill(data[i], ""); Arrays.fill(data[i], "");

View file

@ -1,5 +1,6 @@
package GUI; package GUI;
import Config.Config;
import DB.*; import DB.*;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import Exceptions.ProjectionNotSpecified; import Exceptions.ProjectionNotSpecified;
@ -117,7 +118,7 @@ public class ProjectionHandler extends JDialog {
roomComboBox.removeAllItems(); roomComboBox.removeAllItems();
Competition competition = (Competition) competitionComboBox.getSelectedItem(); Competition competition = (Competition) competitionComboBox.getSelectedItem();
Set<Date> dates = competition.getProjections().stream().map(p -> p.getStartDate()).collect(Collectors.toSet()); Set<Date> dates = competition.getProjections().stream().map(p -> p.getStartDate()).collect(Collectors.toSet());
LocalDate[] localDates = LocalDate.of(2021, 5, 11).datesUntil(LocalDate.of(2021, 5, 22)).toArray(LocalDate[]::new); LocalDate[] localDates = Config.getStartDate().datesUntil(Config.getEndDate()).toArray(LocalDate[]::new);
if (competition.getDays() != 0 && dates.size() >= competition.getDays() && !(projectionType == ProjectionType.EDIT && projection.getCompetition().getName().equals(competition.getName()))) if (competition.getDays() != 0 && dates.size() >= competition.getDays() && !(projectionType == ProjectionType.EDIT && projection.getCompetition().getName().equals(competition.getName())))
localDates = dates.stream().map(Date::toLocalDate).toArray(LocalDate[]::new); localDates = dates.stream().map(Date::toLocalDate).toArray(LocalDate[]::new);