Download this file

EventManagerClient.java    132 lines (104 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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;
}
}