Search in sources :

Example 21 with Schedule

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

the class ScheduleServiceImpl method getScheduleList.

@Override
public List<Schedule> getScheduleList() throws ScheduleException {
    final List<Schedule> scheduleList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Schedule>, List<Schedule>>() {

            @Override
            protected List<Schedule> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload);
                    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) ArrayList(java.util.ArrayList) 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)

Example 22 with Schedule

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

the class ClassServiceImpl method getScheduleById.

@Override
public Schedule getScheduleById(long classId, long scheduleId) throws ClassException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        uri.append("/");
        uri.append(classId);
        uri.append("/");
        uri.append("schedule");
        uri.append("/");
        uri.append(scheduleId);
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Response response = target.request().get();
        if (response.getStatus() == 200) {
            return (Schedule) response.readEntity(Schedule.class);
        } else if (response.getStatus() == 404) {
            Message message = (Message) response.readEntity(Message.class);
            throw new ClassServiceException(message.getMessage());
        } else
            throw new ClassServiceException("The request might invalid or server is down");
    } catch (ClassServiceException e) {
        throw new ClassException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) ClassServiceException(com.remswork.project.alice.web.service.exception.ClassServiceException) Message(com.remswork.project.alice.model.support.Message) Schedule(com.remswork.project.alice.model.Schedule) ClassException(com.remswork.project.alice.exception.ClassException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 23 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(long classId) throws ClassException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        uri.append("/");
        uri.append(classId);
        uri.append("/");
        uri.append("schedule");
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Response response = target.request().get();
        if (response.getStatus() == 200) {
            return (Set<Schedule>) response.readEntity(new GenericType<Set<Schedule>>() {
            });
        } else if (response.getStatus() == 404) {
            Message message = (Message) response.readEntity(Message.class);
            throw new ClassServiceException(message.getMessage());
        } else
            throw new ClassServiceException("The request might invalid or server is down");
    } catch (ClassServiceException e) {
        throw new ClassException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) ClassServiceException(com.remswork.project.alice.web.service.exception.ClassServiceException) GenericType(javax.ws.rs.core.GenericType) Set(java.util.Set) Message(com.remswork.project.alice.model.support.Message) Schedule(com.remswork.project.alice.model.Schedule) ClassException(com.remswork.project.alice.exception.ClassException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 24 with Schedule

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

the class ClassServiceImpl method addScheduleById.

@Override
public Schedule addScheduleById(long classId, long scheduleId) throws ClassException {
    try {
        StringBuilder uri = new StringBuilder();
        uri.append(targetProperties.getDomain());
        uri.append("/");
        uri.append(targetProperties.getBaseUri());
        uri.append("/");
        uri.append(payload);
        uri.append("/");
        uri.append(classId);
        uri.append("/");
        uri.append("schedule");
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(uri.toString());
        Builder builder = target.queryParam("scheduleId", scheduleId).request(MediaType.APPLICATION_JSON);
        Response response = builder.post(Entity.xml(new Schedule()));
        if (response.getStatus() == 200) {
            return (Schedule) response.readEntity(Schedule.class);
        } else if (response.getStatus() == 400) {
            Message message = (Message) response.readEntity(Message.class);
            throw new ClassServiceException(message.getMessage());
        } else
            throw new ClassServiceException("The request might invalid or server is down");
    } catch (ClassServiceException e) {
        throw new ClassException(e.getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) ClassServiceException(com.remswork.project.alice.web.service.exception.ClassServiceException) 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) ClassException(com.remswork.project.alice.exception.ClassException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 25 with Schedule

use of com.remswork.project.alice.model.Schedule 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)

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