Search in sources :

Example 86 with GradingFactorException

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

the class ActivityDaoImpl method addActivityResult.

@Override
public ActivityResult addActivityResult(int score, long activityId, long studentId) throws GradingFactorException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Activity activity = session.get(Activity.class, activityId);
        Student student = session.get(Student.class, studentId);
        ActivityResult result = new ActivityResult();
        String hql = "from ActivityResult as R where R.activity.id = :activityId and R.student.id = :studentId";
        Query query = session.createQuery(hql);
        query.setParameter("activityId", activityId);
        query.setParameter("studentId", studentId);
        if (activity == null)
            throw new GradingFactorDaoException("Activity's activity with id : " + activityId + " does not exist");
        if (student == null)
            throw new GradingFactorDaoException("Activity's student with id : " + studentId + " does not exist");
        if (activityId < 1)
            throw new GradingFactorDaoException("Query param : activityId is required");
        if (studentId < 1)
            throw new GradingFactorDaoException("Query param : studentId is required");
        if (score < 0 && score > activity.getItemTotal())
            throw new GradingFactorDaoException("ActivityResult's score is invalid");
        if (query.list().size() > 0)
            throw new GradingFactorDaoException("ActivityResult's  student with id : " + studentId + " already exist");
        result.setScore(score);
        result.setActivity(activity);
        result.setStudent(student);
        session.persist(result);
        session.getTransaction().commit();
        session.close();
        return result;
    } catch (GradingFactorDaoException e) {
        session.close();
        throw new GradingFactorException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) GradingFactorException(com.remswork.project.alice.exception.GradingFactorException) GradingFactorDaoException(com.remswork.project.alice.dao.exception.GradingFactorDaoException) Session(org.hibernate.Session)

Example 87 with GradingFactorException

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

the class ActivityDaoImpl method getActivityListByClassId.

@Override
public List<Activity> getActivityListByClassId(long classId, long termId) throws GradingFactorException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Activity> activityList = new ArrayList<>();
        String hql = "from Activity where _class.id = :classId and term.id = :termId";
        Query query = session.createQuery(hql);
        query.setParameter("classId", classId);
        query.setParameter("termId", termId);
        for (Object objActivity : query.list()) activityList.add((Activity) objActivity);
        session.getTransaction().commit();
        session.close();
        return activityList;
    } catch (GradingFactorDaoException e) {
        session.close();
        throw new GradingFactorException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) GradingFactorException(com.remswork.project.alice.exception.GradingFactorException) ArrayList(java.util.ArrayList) GradingFactorDaoException(com.remswork.project.alice.dao.exception.GradingFactorDaoException) Session(org.hibernate.Session)

Example 88 with GradingFactorException

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

the class ActivityDaoImpl method getActivityResultByActivityAndStudentId.

@Override
public ActivityResult getActivityResultByActivityAndStudentId(long activityId, long studentId) throws GradingFactorException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Activity activity = session.get(Activity.class, activityId);
        Student student = session.get(Student.class, studentId);
        String hql = "from ActivityResult as R where R.activity.id = :activityId and R.student.id = :studentId";
        Query query = session.createQuery(hql);
        query.setParameter("activityId", activityId);
        query.setParameter("studentId", studentId);
        if (activity == null)
            throw new GradingFactorDaoException("Activity with id : " + activityId + " does not exist");
        if (student == null)
            throw new GradingFactorDaoException("Activity's student with id : " + studentId + " does not exist");
        if (query.list().size() < 1)
            throw new GradingFactorDaoException("No ActivityResult found. Try use query param " + "(ex. studentId=[id])");
        ActivityResult result = (ActivityResult) query.list().get(0);
        session.getTransaction().commit();
        session.close();
        return result;
    } catch (GradingFactorDaoException e) {
        session.close();
        throw new GradingFactorException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) GradingFactorException(com.remswork.project.alice.exception.GradingFactorException) GradingFactorDaoException(com.remswork.project.alice.dao.exception.GradingFactorDaoException) Session(org.hibernate.Session)

Example 89 with GradingFactorException

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

the class ActivityDaoImpl method deleteActivityById.

@Override
public Activity deleteActivityById(long id) throws GradingFactorException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        String[] table = new String[1];
        table[0] = ActivityResult.class.getSimpleName();
        Activity activity = session.get(Activity.class, id);
        if (activity == null)
            throw new GradingFactorDaoException("Activity with id : " + id + " does not exist");
        for (String cell : table) {
            String hql = "delete from ".concat(cell).concat(" where activity.id = :activityId");
            Query query = session.createQuery(hql);
            query.setParameter("activityId", id);
            query.executeUpdate();
        }
        session.delete(activity);
        session.getTransaction().commit();
        session.close();
        return activity;
    } catch (GradingFactorDaoException e) {
        session.close();
        throw new GradingFactorException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) GradingFactorException(com.remswork.project.alice.exception.GradingFactorException) GradingFactorDaoException(com.remswork.project.alice.dao.exception.GradingFactorDaoException) Session(org.hibernate.Session)

Example 90 with GradingFactorException

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

the class AttendanceServiceImpl method getAttendanceListByStudentId.

@Override
public List<Attendance> getAttendanceListByStudentId(final long studentId) throws GradingFactorException {
    final List<Attendance> attendanceList = new ArrayList<>();
    try {
        return new AsyncTask<String, List<Attendance>, List<Attendance>>() {

            @Override
            protected List<Attendance> doInBackground(String... args) {
                try {
                    String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("?studentId=").concat(String.valueOf(studentId));
                    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++) {
                            attendanceList.add(gson.fromJson(jsonArray.get(ctr).toString(), Attendance.class));
                        }
                        return attendanceList;
                    } 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 : Attendance");
                        Log.i("ServiceTAG", "Status : " + message.getStatus());
                        Log.i("ServiceTAG", "Type : " + message.getType());
                        Log.i("ServiceTAG", "Message : " + message.getMessage());
                        return attendanceList;
                    } else
                        throw new GradingFactorException("Server Error");
                } catch (GradingFactorException 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 : Attendance(com.remswork.project.alice.model.Attendance) Message(com.remswork.project.alice.model.support.Message) InputStream(java.io.InputStream) GradingFactorException(com.remswork.project.alice.exception.GradingFactorException) 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) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)302 Message (com.remswork.project.alice.model.support.Message)178 GradingFactorDaoException (com.remswork.project.alice.dao.exception.GradingFactorDaoException)104 Session (org.hibernate.Session)104 AsyncTask (android.os.AsyncTask)102 Gson (com.google.gson.Gson)102 IOException (java.io.IOException)102 InputStream (java.io.InputStream)102 HttpURLConnection (java.net.HttpURLConnection)102 URL (java.net.URL)102 ExecutionException (java.util.concurrent.ExecutionException)102 ArrayList (java.util.ArrayList)71 Query (org.hibernate.Query)64 ClassResourceLinks (com.remswork.project.alice.resource.links.ClassResourceLinks)35 JSONArray (org.json.JSONArray)32 JSONException (org.json.JSONException)32 Grade (com.remswork.project.alice.model.Grade)25 BufferedWriter (java.io.BufferedWriter)21 OutputStream (java.io.OutputStream)21 OutputStreamWriter (java.io.OutputStreamWriter)21