Search in sources :

Example 16 with ScheduleException

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

the class ScheduleServiceImpl method updateScheduleById.

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

Example 17 with ScheduleException

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

the class ScheduleController method updateScheduleOnClass.

@RequestMapping(value = "c/update", method = RequestMethod.POST)
public String updateScheduleOnClass(@RequestParam("day") String day, @RequestParam("room") String room, @RequestParam("time") String time, @RequestParam("period") String period, @RequestParam("classId") long classId, @RequestParam("scheduleId") long scheduleId, ModelMap modelMap) {
    try {
        Schedule schedule = new Schedule();
        schedule.setDay(day);
        schedule.setTime(time);
        schedule.setPeriod(period);
        schedule.setRoom(room);
        schedule = scheduleService.updateScheduleById(scheduleId, schedule);
        Set<Schedule> scheduleList = classService.getScheduleList(classId);
        modelMap.put("scheduleList", scheduleList);
    } catch (ScheduleException | ClassException e) {
        e.printStackTrace();
    }
    return "schedule";
}
Also used : Schedule(com.remswork.project.alice.model.Schedule) ClassException(com.remswork.project.alice.exception.ClassException) ScheduleException(com.remswork.project.alice.exception.ScheduleException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with ScheduleException

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

the class ScheduleController method addScheduleOnClass.

@RequestMapping(value = "c/add", method = RequestMethod.POST)
public String addScheduleOnClass(@RequestParam("day") String day, @RequestParam("room") String room, @RequestParam("time") String time, @RequestParam("period") String period, @RequestParam("classId") long classId, ModelMap modelMap) {
    try {
        Schedule schedule = new Schedule();
        schedule.setDay(day);
        schedule.setTime(time);
        schedule.setPeriod(period);
        schedule.setRoom(room);
        schedule = scheduleService.addSchedule(schedule);
        classService.addScheduleById(classId, schedule.getId());
        Set<Schedule> scheduleList = classService.getScheduleList(classId);
        modelMap.put("scheduleList", scheduleList);
    } catch (ScheduleException | ClassException e) {
        e.printStackTrace();
    }
    return "schedule";
}
Also used : Schedule(com.remswork.project.alice.model.Schedule) ClassException(com.remswork.project.alice.exception.ClassException) ScheduleException(com.remswork.project.alice.exception.ScheduleException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with ScheduleException

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

the class ScheduleController method addSchedule.

@RequestMapping(value = "add", method = RequestMethod.POST)
public String addSchedule(@RequestParam("day") String day, @RequestParam("room") String room, @RequestParam("time") String time, @RequestParam("period") String period, ModelMap modelMap) {
    try {
        System.out.println("day : " + day);
        System.out.println("room : " + room);
        System.out.println("time : " + time);
        System.out.println("period : " + period);
        Schedule schedule = new Schedule();
        schedule.setDay(day);
        schedule.setTime(time);
        schedule.setPeriod(period);
        schedule.setRoom(room);
        schedule = scheduleService.addSchedule(schedule);
        modelMap.put("schedule", schedule);
    } catch (ScheduleException e) {
        e.printStackTrace();
    }
    return "schedule-add";
}
Also used : Schedule(com.remswork.project.alice.model.Schedule) ScheduleException(com.remswork.project.alice.exception.ScheduleException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with ScheduleException

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

the class ScheduleServiceImpl method deleteScheduleById.

@Override
public Schedule deleteScheduleById(final long id) throws ScheduleException {
    try {
        return new AsyncTask<String, Schedule, Schedule>() {

            @Override
            protected Schedule 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, Schedule.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 : Schedule");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return null;
                    } else
                        throw new ScheduleException("Server Error");
                } catch (ScheduleException 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) InputStream(java.io.InputStream) AsyncTask(android.os.AsyncTask) Gson(com.google.gson.Gson) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ScheduleException(com.remswork.project.alice.exception.ScheduleException) URL(java.net.URL)

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