Search in sources :

Example 26 with DepartmentException

use of com.remswork.project.alice.exception.DepartmentException in project classify-system by anverliedoit.

the class DepartmentServiceImpl method deleteDepartmentById.

@Override
public Department deleteDepartmentById(final long id) throws DepartmentException {
    try {
        return new AsyncTask<String, Department, Department>() {

            @Override
            protected Department doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat(String.valueOf(id));
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("DELETE");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.connect();
                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        return gson.fromJson(jsonData, Department.class);
                    } else if (httpURLConnection.getResponseCode() == 400) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String jsonData = "";
                        int data;
                        while ((data = inputStream.read()) != -1) {
                            jsonData += (char) data;
                        }
                        Message message = gson.fromJson(jsonData, Message.class);
                        Log.i("ServiceTAG", "Service : Department");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new DepartmentException("Server Error");
                } catch (DepartmentException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }.execute((String) null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Message(com.remswork.project.alice.model.support.Message) DepartmentException(com.remswork.project.alice.exception.DepartmentException) InputStream(java.io.InputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL)

Example 27 with DepartmentException

use of com.remswork.project.alice.exception.DepartmentException in project classify-system by anverliedoit.

the class SectionDaoImpl method updateSectionById.

@Override
public Section updateSectionById(long id, Section newSection, long departmentId) throws SectionException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Section section = session.get(Section.class, id);
        if (section == null)
            throw new SectionDaoException("Section with id : " + id + " does not exist");
        if (newSection == null)
            newSection = new Section();
        if (!(newSection.getName() != null ? newSection.getName() : "").trim().isEmpty())
            section.setName(newSection.getName().trim());
        if (departmentId > 0) {
            Department department = departmentDao.getDepartmentById(departmentId);
            if (department.getId() == (section.getDepartment() != null ? section.getDepartment().getId() : 0))
                throw new SectionDaoException("Can't update section's department with same department");
            section.setDepartment(department);
        }
        session.getTransaction().commit();
        session.close();
        return section;
    } catch (SectionDaoException | DepartmentException e) {
        session.close();
        throw new SectionException(e.getMessage());
    }
}
Also used : Department(com.remswork.project.alice.model.Department) DepartmentException(com.remswork.project.alice.exception.DepartmentException) SectionException(com.remswork.project.alice.exception.SectionException) Section(com.remswork.project.alice.model.Section) SectionDaoException(com.remswork.project.alice.dao.exception.SectionDaoException) Session(org.hibernate.Session)

Example 28 with DepartmentException

use of com.remswork.project.alice.exception.DepartmentException in project classify-system by anverliedoit.

the class SectionDaoImpl method addSection.

@Override
public Section addSection(Section section, long departmentId) throws SectionException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (section == null)
            throw new SectionDaoException("You tried to add section with a null value");
        if (section.getName() == null)
            throw new SectionDaoException("Section's first name is required");
        if (section.getName().trim().equals(""))
            throw new SectionDaoException("Section can't have an empty name");
        if (departmentId < 1)
            throw new SectionDaoException("Query param : departmentId is required");
        Department department = departmentDao.getDepartmentById(departmentId);
        section.setDepartment(department);
        section = (Section) session.merge(section);
        session.getTransaction().commit();
        session.close();
        return section;
    } catch (SectionDaoException | DepartmentException e) {
        session.close();
        throw new SectionException(e.getMessage());
    }
}
Also used : Department(com.remswork.project.alice.model.Department) DepartmentException(com.remswork.project.alice.exception.DepartmentException) SectionException(com.remswork.project.alice.exception.SectionException) SectionDaoException(com.remswork.project.alice.dao.exception.SectionDaoException) Session(org.hibernate.Session)

Example 29 with DepartmentException

use of com.remswork.project.alice.exception.DepartmentException in project classify-system by anverliedoit.

the class DepartmentResource method addDepartment.

@POST
public Response addDepartment(Department department) {
    try {
        DepartmentResourceLinks resourceLinks = new DepartmentResourceLinks(uriInfo);
        department = departmentService.addDepartment(department);
        department.addLink(resourceLinks.self(department.getId()));
        return Response.status(Response.Status.CREATED).entity(department).build();
    } catch (DepartmentException e) {
        e.printStackTrace();
        Message message = new Message(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).entity(message).build();
    }
}
Also used : DepartmentException(com.remswork.project.alice.exception.DepartmentException) Message(com.remswork.project.alice.model.support.Message) DepartmentResourceLinks(com.remswork.project.alice.resource.links.DepartmentResourceLinks)

Example 30 with DepartmentException

use of com.remswork.project.alice.exception.DepartmentException in project classify-system by anverliedoit.

the class DepartmentResource method getDepartmentList.

@GET
public Response getDepartmentList() {
    try {
        DepartmentResourceLinks resourceLinks = new DepartmentResourceLinks(uriInfo);
        List<Department> departmentList = departmentService.getDepartmentList();
        for (Department d : departmentList) d.addLink(resourceLinks.self(d.getId()));
        GenericEntity<List<Department>> entity = new GenericEntity<List<Department>>(departmentList) {
        };
        return Response.status(Response.Status.OK).entity(entity).build();
    } catch (DepartmentException e) {
        e.printStackTrace();
        Message message = new Message(404, "Not Found", e.getMessage());
        return Response.status(Response.Status.NOT_FOUND).entity(message).build();
    }
}
Also used : Department(com.remswork.project.alice.model.Department) DepartmentException(com.remswork.project.alice.exception.DepartmentException) Message(com.remswork.project.alice.model.support.Message) DepartmentResourceLinks(com.remswork.project.alice.resource.links.DepartmentResourceLinks) List(java.util.List)

Aggregations

DepartmentException (com.remswork.project.alice.exception.DepartmentException)30 Department (com.remswork.project.alice.model.Department)22 Message (com.remswork.project.alice.model.support.Message)15 Session (org.hibernate.Session)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 AsyncTask (android.os.AsyncTask)5 Gson (com.google.gson.Gson)5 DepartmentDaoException (com.remswork.project.alice.dao.exception.DepartmentDaoException)5 SectionException (com.remswork.project.alice.exception.SectionException)5 Section (com.remswork.project.alice.model.Section)5 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 DepartmentServiceException (com.remswork.project.alice.web.service.exception.DepartmentServiceException)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5 ExecutionException (java.util.concurrent.ExecutionException)5 Client (javax.ws.rs.client.Client)5 WebTarget (javax.ws.rs.client.WebTarget)5 Response (javax.ws.rs.core.Response)5