Search in sources :

Example 1 with StudentServiceException

use of com.remswork.project.alice.web.service.exception.StudentServiceException in project classify-system by anverliedoit.

the class StudentServiceImpl method getStudentById.

@Override
public Student getStudentById(long id) throws StudentException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        uri.append("/");
        uri.append(id);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Response response = target.request().get();
        if (response.getStatus() == 200) {
            return (Student) response.readEntity(Student.class);
        } else if (response.getStatus() == 404) {
            Message message = (Message) response.readEntity(Message.class);
            throw new StudentServiceException(message.getMessage());
        } else
            throw new StudentServiceException("The request might invalid or server is down");
    } catch (StudentServiceException e) {
        throw new StudentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) StudentException(com.remswork.project.alice.exception.StudentException) Message(com.remswork.project.alice.model.support.Message) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Student(com.remswork.project.alice.model.Student) StudentServiceException(com.remswork.project.alice.web.service.exception.StudentServiceException)

Example 2 with StudentServiceException

use of com.remswork.project.alice.web.service.exception.StudentServiceException in project classify-system by anverliedoit.

the class StudentServiceImpl method updateStudentById.

@Override
public Student updateStudentById(long id, Student newStudent, long sectionId) throws StudentException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        uri.append("/");
        uri.append(id);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Builder builder = target.queryParam("sectionId", sectionId).request();
        builder.accept("application/json");
        Response response = builder.put(Entity.json(newStudent));
        if (response.getStatus() == 200) {
            return (Student) response.readEntity(Student.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new StudentServiceException(message.getMessage());
        } else
            throw new StudentServiceException("The request might invalid or server is down");
    } catch (StudentServiceException e) {
        throw new StudentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) StudentException(com.remswork.project.alice.exception.StudentException) Message(com.remswork.project.alice.model.support.Message) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Student(com.remswork.project.alice.model.Student) StudentServiceException(com.remswork.project.alice.web.service.exception.StudentServiceException)

Example 3 with StudentServiceException

use of com.remswork.project.alice.web.service.exception.StudentServiceException in project classify-system by anverliedoit.

the class StudentServiceImpl method addStudent.

@Override
public Student addStudent(Student Student, long sectionId) throws StudentException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Builder builder = target.queryParam("sectionId", sectionId).request();
        builder.accept("application/json");
        Response response = builder.post(Entity.json(Student));
        if (response.getStatus() == 201) {
            return (Student) response.readEntity(Student.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new StudentServiceException(message.getMessage());
        } else
            throw new StudentServiceException("The request might invalid or server is down");
    } catch (StudentServiceException e) {
        throw new StudentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) StudentException(com.remswork.project.alice.exception.StudentException) Message(com.remswork.project.alice.model.support.Message) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Student(com.remswork.project.alice.model.Student) StudentServiceException(com.remswork.project.alice.web.service.exception.StudentServiceException)

Example 4 with StudentServiceException

use of com.remswork.project.alice.web.service.exception.StudentServiceException in project classify-system by anverliedoit.

the class StudentServiceImpl method getStudentList.

@Override
public List<Student> getStudentList() throws StudentException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Response response = target.request().get();
        if (response.getStatus() == 200) {
            return (List<Student>) response.readEntity(new GenericType<List<Student>>() {
            });
        } else if (response.getStatus() == 404) {
            Message message = (Message) response.readEntity(Message.class);
            throw new StudentServiceException(message.getMessage());
        } else
            throw new StudentServiceException("The request might invalid or server is down");
    } catch (StudentServiceException e) {
        throw new StudentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) StudentException(com.remswork.project.alice.exception.StudentException) Message(com.remswork.project.alice.model.support.Message) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Student(com.remswork.project.alice.model.Student) StudentServiceException(com.remswork.project.alice.web.service.exception.StudentServiceException)

Example 5 with StudentServiceException

use of com.remswork.project.alice.web.service.exception.StudentServiceException in project classify-system by anverliedoit.

the class StudentServiceImpl method deleteStudentById.

@Override
public Student deleteStudentById(long id) throws StudentException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        uri.append("/");
        uri.append(id);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Builder builder = target.request();
        builder.accept("application/json");
        Response response = builder.delete();
        if (response.getStatus() == 200) {
            return (Student) response.readEntity(Student.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new StudentServiceException(message.getMessage());
        } else
            throw new StudentServiceException("The request might invalid or server is down");
    } catch (StudentServiceException e) {
        throw new StudentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) StudentException(com.remswork.project.alice.exception.StudentException) Message(com.remswork.project.alice.model.support.Message) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Student(com.remswork.project.alice.model.Student) StudentServiceException(com.remswork.project.alice.web.service.exception.StudentServiceException)

Aggregations

StudentException (com.remswork.project.alice.exception.StudentException)6 Student (com.remswork.project.alice.model.Student)6 Message (com.remswork.project.alice.model.support.Message)6 StudentServiceException (com.remswork.project.alice.web.service.exception.StudentServiceException)6 Client (javax.ws.rs.client.Client)6 WebTarget (javax.ws.rs.client.WebTarget)6 Response (javax.ws.rs.core.Response)6 ClientBuilder (javax.ws.rs.client.ClientBuilder)3 Builder (javax.ws.rs.client.Invocation.Builder)3 List (java.util.List)1 GenericType (javax.ws.rs.core.GenericType)1