package eu.alfred.event_manager.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
import eu.alfred.event_manager.model.AnalyticsParameters;
import eu.alfred.event_manager.model.Event;
import eu.alfred.recommendations.util.DataTransformationsHandler;
import eu.alfred.recommendations.util.WebServiceClient;
/**
* This class serves as a client for the services provided by the Event Manager.
* It provides methods for performing CRUD operation on events and adding/deleting participants from events.
*
* @author tasos
*
*/
public class EventManagerClient {
private final String baseUrl = "http://80.241.219.213:1999/Alfred/";
private DataTransformationsHandler dtHandler;
private WebServiceClient wsClient;
public EventManagerClient() {
dtHandler = new DataTransformationsHandler();
wsClient = new WebServiceClient();
}
/**
* Retrieves an event entry from the EM, with the specified ID.
*
* @param eventId A String with the unique identification of the event.
* @return The event with the specified ID, if successfully retrieved.
*/
public Event getEvent(String eventId) {
String requestUrl = baseUrl +"Event?EventID="+eventId;
// There can be only one event with a specific id. We use "get(0)" because of the generic "doGetRequest"
// utility method which returns a List<Object>
Event event = (Event)wsClient.doGetRequest(requestUrl, Event.class).get(0);
return event;
}
/**
* Creates a new entry of an event if the event doesn't exist, or updates the event if already present.
*
* @param newEvent The Event object for the new event entry.
* @return The new Event entry created if the operation was successful, or null otherwise.
*/
public Event addEvent(Event newEvent) {
String eventAsJson = dtHandler.mapObjectToJson(newEvent);
String result = wsClient.doPostRequestToCreate(baseUrl + "AddEvent", eventAsJson);
ObjectMapper mapper = new ObjectMapper();
try {
return (Event) mapper.readValue(result,Event.class);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Deletes an event entry with the specified ID.
*
* @param eventId A String with the unique identification of the event to be deleted.
* @return The Event that was deleted with the operation.
*/
public Object deleteEvent(String eventId) {
String requestUrl = baseUrl +"DeleteEvent?EventID=" + eventId;
Event deletedEvent = (Event) wsClient.doDeleteRequestEM(requestUrl, Event.class);
// Why the hell Dimitri's service returns an event that was just now deleted?
return deletedEvent;
}
/**
* Retrieves a list of events from the Event Manager, based on parameters described in an AnalyticsParameters object.
* The AnalyticsParameters object makes use of calculations of the OLAP cubes defined in the EM.
*
* @param parameters The AnalyticsParameters object with the parameters for filtering the events to be retrieved.
* @return A List of Event objects which satisfy the defined parameters.
*/
public List<Event> getEvents(AnalyticsParameters parameters) {
String paramsAsJson = dtHandler.mapObjectToJson(parameters);
List<Event> events = (List<Event>)(List<?>) wsClient.doPostRequestToRetrieve(baseUrl +
"QueryEvents", paramsAsJson, Event.class);
return events;
}
/**
* Adds a user with a specified Id as a participant in an event with a specified Id.
*
* @param eventId The unique identification of the event in which the user will participate.
* @param participantId The unique identification of the user who will participate in the event
* @return A Boolean indicating the success or failure of the operation
*/
public Boolean addParticipant(String eventId, String participantId) {
Boolean operationResult= wsClient.doGetRequest(baseUrl + "AddParticipant?EventID="+eventId+"&UserID="+participantId);
return operationResult;
}
/**
* Deletes a user entry as a participant in an event.
*
* @param eventId he unique identification of the event from which the user will be removed as a participant.
* @param participantId The unique identification of the user who will be removed as a participant in the event
* @return A Boolean indicating the success or failure of the operation
*/
public Boolean deleteParticipant(String eventId, String participantId) {
String operationResult= wsClient.doDeleteRequest(baseUrl + "AddParticipant?EventID="+eventId+"&UserID="+participantId);
if(operationResult=="true")
return true;
return false;
}
}