Search in sources :

Example 21 with ScheduleException

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

the class ScheduleServiceImpl method getScheduleListByTeacherId.

@Override
public Set<Schedule> getScheduleListByTeacherId(final long teacherId) throws ScheduleException {
    final Set<Schedule> scheduleList = 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("1").concat("?teacherId=").concat(String.valueOf(teacherId));
                    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++) {
                            scheduleList.add(gson.fromJson(jsonArray.get(ctr).toString(), Schedule.class));
                        }
                        return scheduleList;
                    } 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 : Schedule");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return scheduleList;
                    } else
                        throw new ScheduleException("Server Error");
                } catch (ScheduleException 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) JSONException(org.json.JSONException) IOException(java.io.IOException) ScheduleException(com.remswork.project.alice.exception.ScheduleException) 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 22 with ScheduleException

use of com.remswork.project.alice.exception.ScheduleException 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 23 with ScheduleException

use of com.remswork.project.alice.exception.ScheduleException 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 24 with ScheduleException

use of com.remswork.project.alice.exception.ScheduleException 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 25 with ScheduleException

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

the class ScheduleDaoImpl method addSchedule.

@Override
public Schedule addSchedule(Schedule schedule) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (schedule == null)
            throw new ScheduleDaoException("You tried to add schedule with a null value");
        if (schedule.getDay() == null)
            throw new ScheduleDaoException("Schedule's day is required");
        if (schedule.getDay().trim().equals(""))
            throw new ScheduleDaoException("Schedule can't have an empty day");
        if (schedule.getTime() == null)
            throw new ScheduleDaoException("Schedule's time is required");
        if (schedule.getTime().trim().equals(""))
            throw new ScheduleDaoException("Schedule can't have an empty time");
        if (!timeHelperBean.isValid(schedule.getTime().trim()))
            throw new ScheduleDaoException("Schedule's time is invalid");
        if (schedule.getPeriod() == null)
            throw new ScheduleDaoException("Schedule's period is required");
        if (schedule.getPeriod().trim().equals(""))
            throw new ScheduleDaoException("Schedule can't have an empty period");
        if (!timeHelperBean.isValid(schedule.getPeriod().trim()))
            throw new ScheduleDaoException("Schedule's period is invalid");
        session.save(schedule);
        session.getTransaction().commit();
        session.close();
        return schedule;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Aggregations

ScheduleException (com.remswork.project.alice.exception.ScheduleException)28 Schedule (com.remswork.project.alice.model.Schedule)22 Message (com.remswork.project.alice.model.support.Message)18 AsyncTask (android.os.AsyncTask)6 Gson (com.google.gson.Gson)6 ScheduleResourceLinks (com.remswork.project.alice.resource.links.ScheduleResourceLinks)6 ScheduleServiceException (com.remswork.project.alice.web.service.exception.ScheduleServiceException)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 HttpURLConnection (java.net.HttpURLConnection)6 URL (java.net.URL)6 ExecutionException (java.util.concurrent.ExecutionException)6 Client (javax.ws.rs.client.Client)6 WebTarget (javax.ws.rs.client.WebTarget)6 Response (javax.ws.rs.core.Response)6 Session (org.hibernate.Session)6 ScheduleDaoException (com.remswork.project.alice.dao.exception.ScheduleDaoException)5 ArrayList (java.util.ArrayList)3 ClientBuilder (javax.ws.rs.client.ClientBuilder)3 Builder (javax.ws.rs.client.Invocation.Builder)3