use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherResource method updateTeacherById.
@PUT
@Path("{teacherId}")
public Response updateTeacherById(@PathParam("teacherId") long id, Teacher newTeacher) {
try {
TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
Teacher teacher;
if (departmentId > 0)
teacher = teacherService.updateTeacherById(id, newTeacher, departmentId);
else
teacher = teacherService.updateTeacherById(id, newTeacher);
teacher.addLink(resourceLink.self(id));
if (teacher.getDepartment() != null)
teacher.getDepartment().addLink(departmentResourceLinks.self(teacher.getDepartment().getId()));
return Response.status(Response.Status.OK).entity(teacher).build();
} catch (TeacherException e) {
e.printStackTrace();
Message message = new Message(400, "Bad Request", e.getMessage());
return Response.status(Response.Status.BAD_REQUEST).entity(message).build();
}
}
use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherResource method addTeacher.
@POST
public Response addTeacher(Teacher teacher) {
try {
TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
if (departmentId > 0)
teacher = teacherService.addTeacher(teacher, departmentId);
else
teacher = teacherService.addTeacher(teacher);
teacher.addLink(resourceLink.self(teacher.getId()));
if (teacher.getDepartment() != null)
teacher.getDepartment().addLink(departmentResourceLinks.self(teacher.getDepartment().getId()));
return Response.status(Response.Status.CREATED).entity(teacher).build();
} catch (TeacherException e) {
e.printStackTrace();
Message message = new Message(400, "Bad Request", e.getMessage());
return Response.status(Response.Status.BAD_REQUEST).entity(message).build();
}
}
use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherServiceImpl method addTeacher.
@Deprecated
@Override
public Teacher addTeacher(final Teacher teacher, final long departmentId) throws TeacherException {
try {
return new AsyncTask<Teacher, Teacher, Teacher>() {
@Override
protected Teacher doInBackground(Teacher... params) {
try {
Gson gson = new Gson();
URL url = new URL(baseUri);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
Uri.Builder builder = new Uri.Builder().appendQueryParameter("departmentId", String.valueOf(departmentId));
String query = builder.build().getEncodedQuery();
OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
writer.write(query);
writer.write(gson.toJson(teacher).toString());
writer.flush();
writer.close();
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == 201) {
InputStream inputStream = httpURLConnection.getInputStream();
String stringData = "";
int data;
while ((data = inputStream.read()) != -1) {
stringData += (char) data;
}
return gson.fromJson(stringData, Teacher.class);
} else if (httpURLConnection.getResponseCode() == 400) {
InputStream inputStream = httpURLConnection.getInputStream();
String stringData = "";
int data;
while ((data = inputStream.read()) != -1) {
stringData += (char) data;
}
Message message = gson.fromJson(stringData, Message.class);
Log.i("ServiceTAG", "Status : " + message.getStatus());
Log.i("ServiceTAG", "Type : " + message.getType());
Log.i("ServiceTAG", "Message : " + message.getMessage());
return null;
} else
throw new TeacherException("Server Error");
} catch (TeacherException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}.execute(teacher).get();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
}
}
use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherServiceImpl method addTeacher.
@Deprecated
@Override
public Teacher addTeacher(final Teacher teacher) throws TeacherException {
try {
if (teacher.getDepartment() != null) {
teacher.getDepartment().setId(0);
}
return (Teacher) new AsyncTask<Teacher, Teacher, Teacher>() {
@Override
protected Teacher doInBackground(Teacher... params) {
try {
Gson gson = new Gson();
URL url = new URL(baseUri);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
writer.write(gson.toJson(teacher).toString());
writer.flush();
writer.close();
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == 201) {
InputStream inputStream = httpURLConnection.getInputStream();
String stringData = "";
int data;
while ((data = inputStream.read()) != -1) {
stringData += (char) data;
}
return gson.fromJson(stringData, Teacher.class);
} else if (httpURLConnection.getResponseCode() == 400) {
InputStream inputStream = httpURLConnection.getInputStream();
String stringData = "";
int data;
while ((data = inputStream.read()) != -1) {
stringData += (char) data;
}
Message message = gson.fromJson(stringData, Message.class);
Log.i("ServiceTAG", "Status : " + message.getStatus());
Log.i("ServiceTAG", "Type : " + message.getType());
Log.i("ServiceTAG", "Message : " + message.getMessage());
return null;
} else
throw new TeacherException("Server Error");
} catch (TeacherException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}.execute(teacher).get();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
}
}
use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherDaoImpl method getTeacherById.
@Override
public Teacher getTeacherById(long id) throws TeacherException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
Teacher teacher = session.get(Teacher.class, id);
if (teacher == null)
throw new TeacherDaoException("Teacher with id : " + id + " does not exist");
session.getTransaction().commit();
session.close();
return teacher;
} catch (TeacherDaoException e) {
session.close();
throw new TeacherException(e.getMessage());
}
}
Aggregations