Search in sources :

Example 1 with DepartmentServiceException

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

the class DepartmentServiceImpl method getDepartmentById.

@Override
public Department getDepartmentById(long id) throws DepartmentException {
    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 (Department) response.readEntity(Department.class);
        } else if (response.getStatus() == 404) {
            Message message = (Message) response.readEntity(Message.class);
            throw new DepartmentServiceException(message.getMessage());
        } else
            throw new DepartmentServiceException("The request might invalid or server is down");
    } catch (DepartmentServiceException e) {
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) Department(com.remswork.project.alice.model.Department) Message(com.remswork.project.alice.model.support.Message) DepartmentException(com.remswork.project.alice.exception.DepartmentException) DepartmentServiceException(com.remswork.project.alice.web.service.exception.DepartmentServiceException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 2 with DepartmentServiceException

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

the class DepartmentServiceImpl method getDepartmentList.

@Override
public List<Department> getDepartmentList() throws DepartmentException {
    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<Department>) response.readEntity(new GenericType<List<Department>>() {
            });
        } else if (response.getStatus() == 404) {
            Message message = (Message) response.readEntity(Message.class);
            throw new DepartmentServiceException(message.getMessage());
        } else
            throw new DepartmentServiceException("The request might invalid or server is down");
    } catch (DepartmentServiceException e) {
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Department(com.remswork.project.alice.model.Department) Message(com.remswork.project.alice.model.support.Message) DepartmentException(com.remswork.project.alice.exception.DepartmentException) DepartmentServiceException(com.remswork.project.alice.web.service.exception.DepartmentServiceException) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 3 with DepartmentServiceException

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

the class DepartmentServiceImpl method addDepartment.

@Override
public Department addDepartment(Department department) throws DepartmentException {
    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.request();
        builder.accept("application/json");
        Response response = builder.post(Entity.json(department));
        if (response.getStatus() == 201) {
            return (Department) response.readEntity(Department.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new DepartmentServiceException(message.getMessage());
        } else
            throw new DepartmentServiceException("The request might invalid or server is down");
    } catch (DepartmentServiceException e) {
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) Department(com.remswork.project.alice.model.Department) Message(com.remswork.project.alice.model.support.Message) DepartmentException(com.remswork.project.alice.exception.DepartmentException) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) DepartmentServiceException(com.remswork.project.alice.web.service.exception.DepartmentServiceException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 4 with DepartmentServiceException

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

the class DepartmentServiceImpl method updateDepartmentById.

@Override
public Department updateDepartmentById(long id, Department newDepartment) throws DepartmentException {
    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.put(Entity.json(newDepartment));
        if (response.getStatus() == 200) {
            return (Department) response.readEntity(Department.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new DepartmentServiceException(message.getMessage());
        } else
            throw new DepartmentServiceException("The request might invalid or server is down");
    } catch (DepartmentServiceException e) {
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) Department(com.remswork.project.alice.model.Department) Message(com.remswork.project.alice.model.support.Message) DepartmentException(com.remswork.project.alice.exception.DepartmentException) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) DepartmentServiceException(com.remswork.project.alice.web.service.exception.DepartmentServiceException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 5 with DepartmentServiceException

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

the class DepartmentServiceImpl method deleteDepartmentById.

@Override
public Department deleteDepartmentById(long id) throws DepartmentException {
    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 (Department) response.readEntity(Department.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new DepartmentServiceException(message.getMessage());
        } else
            throw new DepartmentServiceException("The request might invalid or server is down");
    } catch (DepartmentServiceException e) {
        throw new DepartmentException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) Department(com.remswork.project.alice.model.Department) Message(com.remswork.project.alice.model.support.Message) DepartmentException(com.remswork.project.alice.exception.DepartmentException) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) DepartmentServiceException(com.remswork.project.alice.web.service.exception.DepartmentServiceException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Aggregations

DepartmentException (com.remswork.project.alice.exception.DepartmentException)5 Department (com.remswork.project.alice.model.Department)5 Message (com.remswork.project.alice.model.support.Message)5 DepartmentServiceException (com.remswork.project.alice.web.service.exception.DepartmentServiceException)5 Client (javax.ws.rs.client.Client)5 WebTarget (javax.ws.rs.client.WebTarget)5 Response (javax.ws.rs.core.Response)5 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