Switch to unified view

a b/recommendation-engine/src/main/java/eu/alfred/event_manager/client/EventManagerClient.java
1
package eu.alfred.event_manager.client;
2
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.List;
6
7
import org.codehaus.jackson.map.ObjectMapper;
8
9
import eu.alfred.event_manager.model.AnalyticsParameters;
10
import eu.alfred.event_manager.model.Event;
11
import eu.alfred.recommendations.util.DataTransformationsHandler;
12
import eu.alfred.recommendations.util.WebServiceClient;
13
14
/**
15
 * This class serves as a client for the services provided by the Event Manager.
16
 * It provides methods for performing CRUD operation on events and adding/deleting participants from events.
17
 * 
18
 * @author tasos
19
 *
20
 */
21
public class EventManagerClient {
22
23
  private final String baseUrl = "http://80.241.219.213:1999/Alfred/";
24
  private DataTransformationsHandler dtHandler;
25
  private WebServiceClient wsClient;
26
  
27
  public EventManagerClient() {
28
      dtHandler = new DataTransformationsHandler();
29
      wsClient = new WebServiceClient();
30
  }
31
  
32
  /**
33
   * Retrieves an event entry from the EM, with the specified ID.
34
   * 
35
   * @param eventId A String with the unique identification of the event. 
36
   * @return The event with the specified ID, if successfully retrieved. 
37
   */
38
  public Event getEvent(String eventId) {     
39
      String requestUrl = baseUrl +"Event?EventID="+eventId;
40
      
41
      // There can be only one event with a specific id. We use "get(0)" because of the generic "doGetRequest"
42
      // utility method which returns a List<Object>
43
      Event event = (Event)wsClient.doGetRequest(requestUrl, Event.class).get(0);
44
      
45
      return event;       
46
  }
47
  
48
  /**
49
   * Creates a new entry of an event if the event doesn't exist, or updates the event if already present.
50
   * 
51
   * @param newEvent The Event object for the new event entry.
52
   * @return The new Event entry created if the operation was successful, or null otherwise.
53
   */
54
  public Event addEvent(Event newEvent) {
55
56
      String eventAsJson = dtHandler.mapObjectToJson(newEvent);   
57
      String result = wsClient.doPostRequestToCreate(baseUrl + "AddEvent", eventAsJson);
58
      
59
      ObjectMapper mapper = new ObjectMapper();
60
      try {
61
          return (Event) mapper.readValue(result,Event.class);
62
      } catch (IOException e) { 
63
          e.printStackTrace();
64
      }
65
      return null;
66
  }
67
  
68
  /**
69
   * Deletes an event entry with the specified ID.
70
   * 
71
   * @param eventId A String with the unique identification of the event to be deleted.
72
   * @return The Event that was deleted with the operation.
73
   */
74
  public Object deleteEvent(String eventId) { 
75
      String requestUrl = baseUrl +"DeleteEvent?EventID=" + eventId;
76
      
77
      Event deletedEvent = (Event) wsClient.doDeleteRequestEM(requestUrl, Event.class);
78
      
79
      // Why the hell Dimitri's service returns an event that was just now deleted?
80
      return deletedEvent;
81
  }
82
  
83
  /**
84
   * Retrieves a list of events from the Event Manager, based on parameters described in an AnalyticsParameters object.
85
   * The AnalyticsParameters object makes use of calculations of the OLAP cubes defined in the EM.
86
   * 
87
   * @param parameters The AnalyticsParameters object with the parameters for filtering the events to be retrieved.
88
   * @return A List of Event objects which satisfy the defined parameters.
89
   */
90
  public List<Event> getEvents(AnalyticsParameters parameters) { 
91
      
92
      String paramsAsJson = dtHandler.mapObjectToJson(parameters);
93
94
      List<Event> events = (List<Event>)(List<?>) wsClient.doPostRequestToRetrieve(baseUrl + 
95
                                                                  "QueryEvents", paramsAsJson, Event.class);
96
      
97
      return events;
98
  }
99
      
100
   /**
101
    * Adds a user with a specified Id as a participant in an event with a specified Id.
102
    * 
103
    * @param eventId The unique identification of the event in which the user will participate.
104
    * @param participantId The unique identification of the user who will participate in the event
105
    * @return A Boolean indicating the success or failure of the operation
106
    */
107
   public Boolean addParticipant(String eventId, String participantId) {
108
      
109
      Boolean operationResult= wsClient.doGetRequest(baseUrl + "AddParticipant?EventID="+eventId+"&UserID="+participantId);
110
      
111
      return operationResult;
112
  }
113
  
114
   /**
115
    * Deletes a user entry as a participant in an event. 
116
    * 
117
    * @param eventId he unique identification of the event from which the user will be removed as a participant.
118
    * @param participantId The unique identification of the user who will be removed as a participant in the event
119
    * @return A Boolean indicating the success or failure of the operation
120
    */
121
  public Boolean deleteParticipant(String eventId, String participantId) { 
122
              
123
      String operationResult= wsClient.doDeleteRequest(baseUrl + "AddParticipant?EventID="+eventId+"&UserID="+participantId);
124
      
125
      if(operationResult=="true")
126
          return true;
127
      
128
      return false;
129
  }
130
   
131
}