Skip to content

Commit

Permalink
Methods implemented for retrieving calendar objects for specific user
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkncelik committed Jun 26, 2018
1 parent 3d442c5 commit d5bfb2a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 7 deletions.
@@ -0,0 +1,26 @@
package edu.itu.cavabunga.client.controller;

import edu.itu.cavabunga.client.service.CavabungaClientService;
import edu.itu.cavabunga.client.service.ParticipantRestService;
import edu.itu.cavabunga.lib.entity.Component;
import edu.itu.cavabunga.lib.entity.Participant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/")
public class ClientController {
@Autowired
private CavabungaClientService cavabungaClientService;

@GetMapping
public List<Component> test(){
return this.cavabungaClientService.retrieveCalendarsByOwner("testuser");
}

}
3 changes: 3 additions & 0 deletions src/main/java/edu/itu/cavabunga/client/http/HttpAdapter.java
Expand Up @@ -38,6 +38,9 @@ public static String doRequest(String requestUrl, RequestMethod requestMethod, S
}
in.close();
connection.disconnect();
if(connection.getResponseCode() != 200){
throw new ClientException("Cavabunga server return with http code: " + connection.getResponseCode());
}
return result.toString();
}catch (Exception e){
throw new ClientException("There is a problem about http connection: " + e.getMessage());
Expand Down
@@ -0,0 +1,71 @@
package edu.itu.cavabunga.client.service;

import edu.itu.cavabunga.client.exception.ClientException;
import edu.itu.cavabunga.lib.entity.Component;
import edu.itu.cavabunga.lib.entity.Participant;
import edu.itu.cavabunga.lib.entity.component.Calendar;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Data
@Service
public class CavabungaClientService {
private ComponentRestService componentRestService;
private ParameterRestService parameterRestService;
private ParticipantRestService participantRestService;
private PropertyRestService propertyRestService;

@Autowired
public CavabungaClientService(ComponentRestService componentRestService,
ParameterRestService parameterRestService,
ParticipantRestService participantRestService,
PropertyRestService propertyRestService){
this.componentRestService = componentRestService;
this.parameterRestService = parameterRestService;
this.participantRestService = participantRestService;
this.propertyRestService = propertyRestService;
}

public List<Participant> retrieveParticipant(String userName){
try {
return participantRestService.recieveParticipantFromServer("participant/" + userName, RequestMethod.GET);
}catch (Exception e){
throw new ClientException("Couldnt recieve participant with username: " + userName + " ,message:" + e.getMessage());
}
}

public List<Component> retrieveComponentById(Long id){
try {
return componentRestService.recieveComponentFromServer("component/" + id.toString(), RequestMethod.GET);
}catch (Exception e){
throw new ClientException("Couldnt recieve component with id of " + id.toString() + " ,message:" + e.getMessage());
}
}

public List<Component> retrieveComponentsByOwner(String userName){
try {
return componentRestService.recieveComponentFromServer("participant/" + userName + "/components", RequestMethod.GET);
}catch (Exception e){
throw new ClientException("Coulnd recieve participants components username: " + userName + " ,message:" + e.getMessage());
}
}

public List<Component> retrieveCalendarsByOwner(String userName){
List<Component> calendars = new ArrayList<>();
try {
for (Component c : componentRestService.recieveComponentFromServer("participant/" + userName + "/components", RequestMethod.GET)){
if(c instanceof Calendar){
calendars.add(c);
}
}
return calendars;
}catch (Exception e){
throw new ClientException("Coulnd recieve participants components username: " + userName + " ,message:" + e.getMessage());
}
}
}
Expand Up @@ -32,13 +32,13 @@ public ComponentRestService(HttpAdapter httpAdapter,
public String sendComponentToServer(edu.itu.cavabunga.lib.entity.Component component,
RequestMethod requestMethod,
String apiUri){
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod,
this.jsonObjectMapper.mapComponentToJson(component));
}

public List<edu.itu.cavabunga.lib.entity.Component> recieveComponentFromServer(String apiUri, RequestMethod requestMethod){
return this.jsonObjectMapper.mapFromJsonToComponentResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
return this.jsonObjectMapper.mapFromJsonToComponentResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod, ""));
}
}
Expand Up @@ -31,14 +31,14 @@ public ParameterRestService(HttpAdapter httpAdapter,
public String sendParameterToServer(Parameter parameter,
RequestMethod requestMethod,
String apiUri){
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod,
this.jsonObjectMapper.mapParameterToJson(parameter));
}

public List<Parameter> recieveParameterFromServer(String apiUri,
RequestMethod requestMethod){
return this.jsonObjectMapper.mapFromJsonToParameterResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
return this.jsonObjectMapper.mapFromJsonToParameterResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod, ""));
}
}
Expand Up @@ -34,7 +34,7 @@ public ParticipantRestService(HttpAdapter httpAdapter,
public String sendParticipantToServer(Participant participant,
RequestMethod requestMethod,
String apiUri){
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":"+ this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod,
this.jsonObjectMapper.mapParticipantToJson(participant));
}
Expand Down
Expand Up @@ -32,13 +32,13 @@ public PropertyRestService(HttpAdapter httpAdapter,
public String sendPropertyToServer(Property property,
RequestMethod requestMethod,
String apiUri){
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod,
this.jsonObjectMapper.mapPropertyToJson(property));
}

public List<Property> recievePropertyFromServer(String apiUri, RequestMethod requestMethod){
return this.jsonObjectMapper.mapFromJsonToPropertyResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
return this.jsonObjectMapper.mapFromJsonToPropertyResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod, ""));
}
}

0 comments on commit d5bfb2a

Please sign in to comment.