use of com.remswork.project.alice.exception.StudentException in project classify-system by anverliedoit.
the class StudentServiceImpl method addStudent.
@Override
public Student addStudent(final Student student, final long sectionId) throws StudentException {
try {
return new AsyncTask<String, Student, Student>() {
@Override
protected Student doInBackground(String... args) {
try {
String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("?sectionId=").concat(String.valueOf(sectionId));
Gson gson = new Gson();
URL url = new URL(link);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Accept", "application/json");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(gson.toJson(student));
writer.flush();
writer.close();
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == 201) {
InputStream inputStream = httpURLConnection.getInputStream();
String jsonData = "";
int data;
while ((data = inputStream.read()) != -1) {
jsonData += (char) data;
}
return gson.fromJson(jsonData, Student.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 : Student");
Log.i("ServiceTAG", "Status : " + message.getStatus());
Log.i("ServiceTAG", "Type : " + message.getType());
Log.i("ServiceTAG", "Message : " + message.getMessage());
return null;
} else
throw new StudentException("Server Error");
} catch (StudentException 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;
}
}
use of com.remswork.project.alice.exception.StudentException in project classify-system by anverliedoit.
the class StudentDaoImpl method updateStudentById.
@Override
public Student updateStudentById(long id, Student newStudent, long sectionId) throws StudentException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
Student student = session.get(Student.class, id);
if (student == null)
throw new StudentDaoException("Student with id : " + id + " does not exist.");
if (newStudent == null)
newStudent = new Student();
if (newStudent.getStudentNumber() < 1)
student.setStudentNumber(newStudent.getStudentNumber());
if (!(newStudent.getFirstName() != null ? newStudent.getFirstName() : "").trim().isEmpty())
student.setFirstName(newStudent.getFirstName());
if (!(newStudent.getLastName() != null ? newStudent.getLastName() : "").trim().isEmpty())
student.setLastName(newStudent.getLastName());
if (!(newStudent.getMiddleName() != null ? newStudent.getMiddleName() : "").trim().isEmpty())
student.setMiddleName(newStudent.getMiddleName());
if (!(newStudent.getGender() != null ? newStudent.getGender() : "").trim().isEmpty()) {
if (!(newStudent.getGender().trim().equalsIgnoreCase("Male") || newStudent.getGender().trim().equalsIgnoreCase("Female")))
throw new StudentDaoException("Student's gender is invalid");
student.setGender(newStudent.getGender());
}
if (newStudent.getAge() != 0) {
if (newStudent.getAge() <= 14 || newStudent.getAge() > 60)
throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
student.setAge(newStudent.getAge());
}
if (sectionId > 0) {
Section section = sectionDao.getSectionById(sectionId);
if (section.getId() == (student.getSection() != null ? student.getSection().getId() : 0))
throw new StudentDaoException("Can't update student's section with same section");
student.setSection(section);
student = (Student) session.merge(student);
}
session.getTransaction().commit();
session.close();
return student;
} catch (StudentDaoException | SectionException e) {
session.close();
throw new StudentException(e.getMessage());
}
}
Aggregations