2021-11-29 15:44:55 +01:00
|
|
|
package res;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2022-01-19 00:16:23 +01:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
2021-11-29 15:44:55 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2022-01-20 13:33:49 +01:00
|
|
|
import helper.HttpRequestException;
|
2022-01-13 23:14:36 +01:00
|
|
|
import helper.Tuple;
|
2021-11-29 15:44:55 +01:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-01-19 00:16:23 +01:00
|
|
|
import java.time.LocalDateTime;
|
2021-11-29 15:44:55 +01:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class DataController {
|
|
|
|
|
2021-12-20 19:26:07 +01:00
|
|
|
public static long USER_ID = -1;
|
|
|
|
|
2022-01-10 13:22:59 +01:00
|
|
|
private static final String ALL_EVENTS_ENDPOINT = "http://localhost:8080/event/all";
|
|
|
|
private static final String ADD_EVENT_ENDPOINT = "http://localhost:8080/event/add";
|
|
|
|
private static final String DELETE_EVENT_ENDPOINT = "http://localhost:8080/event/del";
|
2022-01-25 19:37:49 +01:00
|
|
|
private static final String EDIT_EVENT_ENDPOINT = "http://localhost:8080/event/edit";
|
|
|
|
|
|
|
|
private static final String ALL_USER_ENDPOINT = "http://localhost:8080/user/all";
|
|
|
|
private static final String ADD_USER_ENDPOINT = "http://localhost:8080/user/add";
|
|
|
|
private static final String DELETE_USER_ENDPOINT = "http://localhost:8080/user/del";
|
|
|
|
private static final String EDIT_USER_ENDPOINT = "http://localhost:8080/user/edit";
|
2022-01-10 13:22:59 +01:00
|
|
|
|
|
|
|
private static final String LOGIN_ENDPOINT = "http://localhost:8080/user/login";
|
2022-01-20 13:33:49 +01:00
|
|
|
private static final String LOGIN_WITH_TOKEN_ENDPOINT = "http://localhost:8080/user/login-with-token";
|
2022-01-13 23:14:36 +01:00
|
|
|
private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test";
|
2021-11-29 15:44:55 +01:00
|
|
|
|
|
|
|
private final HttpRequest httpRequest;
|
|
|
|
|
2021-12-22 15:04:15 +01:00
|
|
|
public DataController() {
|
2021-11-29 15:44:55 +01:00
|
|
|
httpRequest = new HttpRequest();
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:04:15 +01:00
|
|
|
public boolean login(String username, String password) {
|
2021-12-20 19:26:07 +01:00
|
|
|
try {
|
2022-01-13 23:14:36 +01:00
|
|
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(
|
2021-12-20 19:26:07 +01:00
|
|
|
LOGIN_ENDPOINT,
|
|
|
|
"login=" + username
|
2022-01-11 17:12:37 +01:00
|
|
|
+ "&password=" + password,
|
|
|
|
false
|
2022-01-13 17:35:16 +01:00
|
|
|
);
|
2022-01-13 23:14:36 +01:00
|
|
|
String[] data = response.getValue().split("\\s+");
|
2022-01-13 17:35:16 +01:00
|
|
|
|
2022-01-13 23:14:36 +01:00
|
|
|
USER_ID = Long.parseLong(data[1]);
|
2022-01-14 19:37:06 +01:00
|
|
|
HttpRequest.TOKEN = data[0];
|
2021-12-20 19:26:07 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-13 23:14:36 +01:00
|
|
|
|
2021-12-20 19:26:07 +01:00
|
|
|
return USER_ID >= 0;
|
|
|
|
}
|
|
|
|
|
2022-01-23 21:21:16 +01:00
|
|
|
public boolean loginWithToken(long userId, String token) {
|
2021-11-29 15:44:55 +01:00
|
|
|
try {
|
2022-01-23 21:21:16 +01:00
|
|
|
HttpRequest.TOKEN = token;
|
2022-01-20 13:33:49 +01:00
|
|
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(
|
|
|
|
LOGIN_WITH_TOKEN_ENDPOINT,
|
2022-01-23 21:21:16 +01:00
|
|
|
"userId=" + userId,
|
2022-01-20 13:33:49 +01:00
|
|
|
true
|
|
|
|
);
|
2022-01-23 21:21:16 +01:00
|
|
|
|
|
|
|
System.out.println(response.getKey() + " " + response.getValue());
|
|
|
|
|
2022-01-25 20:01:22 +01:00
|
|
|
if (response.getKey() != 200) return false;
|
2021-11-29 15:44:55 +01:00
|
|
|
} catch (Exception e) {
|
2022-01-20 13:33:49 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-23 21:21:16 +01:00
|
|
|
USER_ID = userId;
|
|
|
|
HttpRequest.TOKEN = token;
|
2022-01-20 13:33:49 +01:00
|
|
|
return USER_ID >= 0;
|
|
|
|
}
|
|
|
|
|
2022-01-25 20:01:22 +01:00
|
|
|
/*********
|
|
|
|
* Event *
|
|
|
|
*********/
|
2022-01-20 13:33:49 +01:00
|
|
|
public void createEvent(Event event) throws HttpRequestException {
|
2022-01-25 20:01:22 +01:00
|
|
|
sendBasicHttpRequest(
|
|
|
|
ADD_EVENT_ENDPOINT,
|
|
|
|
event.getAsUrlParam(),
|
|
|
|
true
|
|
|
|
);
|
2021-11-29 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 13:33:49 +01:00
|
|
|
public void deleteEvent(int userId, int eventId, LocalDateTime date) throws HttpRequestException {
|
2022-01-25 20:01:22 +01:00
|
|
|
sendBasicHttpRequest(
|
|
|
|
DELETE_EVENT_ENDPOINT,
|
|
|
|
"userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(),
|
|
|
|
true
|
|
|
|
);
|
2021-11-29 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 19:37:49 +01:00
|
|
|
public void editEvent(Event oldEvent, Event event) throws HttpRequestException {
|
2022-01-25 20:01:22 +01:00
|
|
|
sendBasicHttpRequest(
|
|
|
|
EDIT_EVENT_ENDPOINT,
|
|
|
|
"eventId=" + oldEvent.getId() +
|
|
|
|
"&userId=" + oldEvent.getOwnerId() +
|
|
|
|
"&date=" + oldEvent.getDate().toLocalDate() +
|
|
|
|
"&newDate=" + event.getDate().toLocalDate() +
|
|
|
|
"&newName=" + event.getName() +
|
|
|
|
"&newStart=" + event.getStart() +
|
|
|
|
"&newEnd=" + event.getEnd() +
|
|
|
|
"&newPriority=" + event.getPriority() +
|
|
|
|
"&newIsFullDay=" + event.isFullDay() +
|
|
|
|
"&newIsPrivate=" + event.isPrivate(),
|
|
|
|
true
|
|
|
|
);
|
2022-01-25 19:37:49 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 13:33:49 +01:00
|
|
|
public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException {
|
2021-11-29 15:44:55 +01:00
|
|
|
ArrayList<Event> eventList = new ArrayList<>();
|
|
|
|
try {
|
2022-01-19 00:16:23 +01:00
|
|
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(
|
|
|
|
ALL_EVENTS_ENDPOINT,
|
|
|
|
"userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(),
|
|
|
|
true
|
|
|
|
);
|
2022-01-25 20:01:22 +01:00
|
|
|
if (response.getKey() != 200) {
|
2022-01-20 13:33:49 +01:00
|
|
|
throw new HttpRequestException(response);
|
|
|
|
}
|
2022-01-13 23:14:36 +01:00
|
|
|
String jsonResponse = response.getValue();
|
2021-11-29 15:44:55 +01:00
|
|
|
System.out.println(jsonResponse);
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
2022-01-19 00:16:23 +01:00
|
|
|
objectMapper.findAndRegisterModules();
|
2022-01-25 20:01:22 +01:00
|
|
|
eventList = (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>() {
|
|
|
|
});
|
2021-11-29 15:44:55 +01:00
|
|
|
|
|
|
|
|
2022-01-25 20:01:22 +01:00
|
|
|
} catch (HttpRequestException e) {
|
2022-01-20 13:33:49 +01:00
|
|
|
throw e;
|
2022-01-25 20:01:22 +01:00
|
|
|
} catch (Exception e) {
|
2022-01-20 13:33:49 +01:00
|
|
|
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
|
2021-11-29 15:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return eventList;
|
|
|
|
}
|
|
|
|
|
2022-01-25 20:01:22 +01:00
|
|
|
/********
|
|
|
|
* User *
|
|
|
|
********/
|
|
|
|
|
|
|
|
/*
|
|
|
|
public void createUser(User user) throws HttpRequestException {
|
|
|
|
sendBasicHttpRequest(
|
|
|
|
ADD_USER_ENDPOINT,
|
|
|
|
user.getAsUrlParam(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteUser(User user) throws HttpRequestException {
|
|
|
|
sendBasicHttpRequest(
|
|
|
|
DELETE_USER_ENDPOINT,
|
|
|
|
user.getAsUrlParam(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void editUser(User oldUser, User user) throws HttpRequestException {
|
|
|
|
sendBasicHttpRequest(
|
|
|
|
EDIT_USER_ENDPOINT,
|
|
|
|
user.getAsUrlParam(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
*/
|
2021-11-29 15:44:55 +01:00
|
|
|
|
2022-01-25 20:01:22 +01:00
|
|
|
private void sendBasicHttpRequest(String urlString, String urlParameters, boolean sendAuth) throws HttpRequestException {
|
|
|
|
try {
|
|
|
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(
|
|
|
|
urlString,
|
|
|
|
urlParameters,
|
|
|
|
sendAuth
|
|
|
|
);
|
|
|
|
if (response.getKey() != 200) {
|
|
|
|
throw new HttpRequestException(response);
|
|
|
|
}
|
|
|
|
} catch (HttpRequestException e) {
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 13:22:59 +01:00
|
|
|
}
|