Search in sources :

Example 31 with Schedule

use of com.remswork.project.alice.model.Schedule in project classify-system by anverliedoit.

the class ClassServiceImpl method getScheduleList.

@Override
public Set<Schedule> getScheduleList(final long classId) throws ClassException {
    final Set<Schedule> scheduleSet = new HashSet<>();
    try {
        return new AsyncTask<String, Set<Schedule>, Set<Schedule>>() {

            @Override
            protected Set<Schedule> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("/").concat(String.valueOf(classId)).concat("/").concat("schedule");
                    URL url = new URL(link);
                    Gson gson = new Gson();
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    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;
                        }
                        JSONArray jsonArray = new JSONArray(jsonData);
                        for (int ctr = 0; ctr < jsonArray.length(); ctr++) {
                            scheduleSet.add(gson.fromJson(jsonArray.get(ctr).toString(), Schedule.class));
                        }
                        return scheduleSet;
                    } else if (httpURLConnection.getResponseCode() == 404) {
                        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 : Class");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return scheduleSet;
                    } else
                        throw new ClassException("Server Error");
                } catch (ClassException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (JSONException 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 : Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) ClassException(com.remswork.project.alice.exception.ClassException) JSONException(org.json.JSONException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) Schedule(com.remswork.project.alice.model.Schedule) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 32 with Schedule

use of com.remswork.project.alice.model.Schedule in project classify-system by anverliedoit.

the class ScheduleDaoImpl method updateScheduleById.

@Override
public Schedule updateScheduleById(long id, Schedule newSchedule) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Schedule schedule = session.get(Schedule.class, id);
        if (schedule == null)
            throw new ScheduleDaoException("Schedule with id : " + id + " does not exist");
        if (newSchedule == null)
            throw new ScheduleDaoException("You tried to update student with a null value");
        if (!(newSchedule.getDay() != null ? newSchedule.getDay() : "").trim().isEmpty())
            schedule.setDay(newSchedule.getDay());
        if (!(newSchedule.getRoom() != null ? newSchedule.getRoom() : "").trim().isEmpty())
            schedule.setRoom(newSchedule.getRoom());
        if (!(newSchedule.getTime() != null ? newSchedule.getTime() : "").trim().isEmpty()) {
            if (!timeHelperBean.isValid(newSchedule.getTime().trim()))
                throw new ScheduleDaoException("Schedule's time is invalid");
            schedule.setTime(newSchedule.getTime());
        }
        if (!(newSchedule.getPeriod() != null ? newSchedule.getPeriod() : "").trim().isEmpty()) {
            if (!timeHelperBean.isValid(newSchedule.getPeriod().trim()))
                throw new ScheduleDaoException("Schedule's period is invalid");
            schedule.setPeriod(newSchedule.getPeriod());
        }
        session.getTransaction().commit();
        session.close();
        return schedule;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Schedule(com.remswork.project.alice.model.Schedule) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Example 33 with Schedule

use of com.remswork.project.alice.model.Schedule in project classify-system by anverliedoit.

the class ScheduleDaoImpl method getScheduleList.

@Override
public List<Schedule> getScheduleList() throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Schedule> scheduleList = new ArrayList<>();
        Query query = session.createQuery("from Schedule");
        for (Object scheduleObj : query.list()) scheduleList.add((Schedule) scheduleObj);
        session.getTransaction().commit();
        session.close();
        return scheduleList;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) Schedule(com.remswork.project.alice.model.Schedule) ArrayList(java.util.ArrayList) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Example 34 with Schedule

use of com.remswork.project.alice.model.Schedule in project classify-system by anverliedoit.

the class ScheduleDaoImpl method getScheduleById.

@Override
public Schedule getScheduleById(long id) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Schedule schedule = session.get(Schedule.class, id);
        if (schedule == null)
            throw new ScheduleDaoException("Schedule with id : " + id + " does not exist");
        session.getTransaction().commit();
        session.close();
        return schedule;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Schedule(com.remswork.project.alice.model.Schedule) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Example 35 with Schedule

use of com.remswork.project.alice.model.Schedule in project classify-system by anverliedoit.

the class ScheduleResource method deleteScheduleById.

@DELETE
@Path("{scheduleId}")
public Response deleteScheduleById(@PathParam("scheduleId") long id) {
    try {
        ScheduleResourceLinks resourceLinks = new ScheduleResourceLinks(uriInfo);
        Schedule schedule = scheduleService.deleteScheduleById(id);
        schedule.addLink(resourceLinks.self(id));
        return Response.status(Response.Status.OK).entity(schedule).build();
    } catch (ScheduleException e) {
        e.printStackTrace();
        Message message = new Message(400, "Bad Request", e.getMessage());
        return Response.status(Response.Status.BAD_REQUEST).entity(message).build();
    }
}
Also used : Message(com.remswork.project.alice.model.support.Message) Schedule(com.remswork.project.alice.model.Schedule) ScheduleResourceLinks(com.remswork.project.alice.resource.links.ScheduleResourceLinks) ScheduleException(com.remswork.project.alice.exception.ScheduleException)

Aggregations

Schedule (com.remswork.project.alice.model.Schedule)36 ScheduleException (com.remswork.project.alice.exception.ScheduleException)22 Message (com.remswork.project.alice.model.support.Message)22 ClassException (com.remswork.project.alice.exception.ClassException)13 Client (javax.ws.rs.client.Client)10 WebTarget (javax.ws.rs.client.WebTarget)10 Response (javax.ws.rs.core.Response)10 ScheduleServiceException (com.remswork.project.alice.web.service.exception.ScheduleServiceException)6 ScheduleResourceLinks (com.remswork.project.alice.resource.links.ScheduleResourceLinks)5 ArrayList (java.util.ArrayList)5 ClientBuilder (javax.ws.rs.client.ClientBuilder)5 Builder (javax.ws.rs.client.Invocation.Builder)5 Session (org.hibernate.Session)5 ScheduleDaoException (com.remswork.project.alice.dao.exception.ScheduleDaoException)4 ClassScheduleListResourceLinks (com.remswork.project.alice.resource.links.ClassScheduleListResourceLinks)4 ClassServiceException (com.remswork.project.alice.web.service.exception.ClassServiceException)4 Set (java.util.Set)4 AsyncTask (android.os.AsyncTask)3 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3