use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherDaoImpl method updateTeacherById.
@Override
public Teacher updateTeacherById(long id, Teacher newTeacher, long departmentId) throws TeacherException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
if (newTeacher == null)
newTeacher = new Teacher();
Teacher teacher = session.get(Teacher.class, id);
if (teacher == null)
throw new TeacherDaoException("Teacher with id : " + id + " does not exist.");
if (!(newTeacher.getFirstName() != null ? newTeacher.getFirstName() : "").trim().isEmpty())
teacher.setFirstName(newTeacher.getFirstName());
if (!(newTeacher.getLastName() != null ? newTeacher.getLastName() : "").trim().isEmpty())
teacher.setLastName(newTeacher.getLastName());
if (!(newTeacher.getEmail() != null ? newTeacher.getEmail() : "").trim().isEmpty())
teacher.setEmail(newTeacher.getEmail());
if (!(newTeacher.getMiddleName() != null ? newTeacher.getMiddleName() : "").trim().isEmpty())
teacher.setMiddleName(newTeacher.getMiddleName());
if (departmentId > 0) {
Department department = departmentDao.getDepartmentById(departmentId);
if (department.getId() == (teacher.getDepartment() != null ? teacher.getDepartment().getId() : 0))
throw new TeacherDaoException("Can't update teacher's department with same department");
teacher.setDepartment(department);
teacher = (Teacher) session.merge(teacher);
}
session.getTransaction().commit();
session.close();
return teacher;
} catch (TeacherDaoException | DepartmentException e) {
session.close();
throw new TeacherException(e.getMessage());
}
}
use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherServiceImpl method getTeacherById.
@Override
public Teacher getTeacherById(final long id) throws TeacherException {
try {
return (Teacher) new AsyncTask<Long, Teacher, Teacher>() {
@Override
protected Teacher doInBackground(Long... params) {
try {
URL url = new URL(baseUri + "/" + params[0]);
Gson gson = new Gson();
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == 200) {
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() == 404) {
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(id).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 getTeacherList.
@Override
public List<Teacher> getTeacherList() throws TeacherException {
try {
final List<Teacher> teacherList = new ArrayList<>();
return new AsyncTask<String, List<Teacher>, List<Teacher>>() {
@Override
protected List<Teacher> doInBackground(String... params) {
try {
Gson gson = new Gson();
URL url = new URL(baseUri);
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++) {
teacherList.add(gson.fromJson(jsonArray.get(ctr).toString(), Teacher.class));
}
return teacherList;
} else if (httpURLConnection.getResponseCode() == 404) {
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++) {
teacherList.add(gson.fromJson(jsonArray.get(ctr).toString(), Teacher.class));
}
Message message = gson.fromJson(jsonData, Message.class);
Log.i("ServiceTAG", "Status : " + message.getStatus());
Log.i("ServiceTAG", "Type : " + message.getType());
Log.i("ServiceTAG", "Message : " + message.getMessage());
return teacherList;
} else
throw new TeacherException("Server Error");
} catch (IOException e1) {
e1.printStackTrace();
return teacherList;
} catch (JSONException e1) {
e1.printStackTrace();
return teacherList;
} catch (TeacherException e1) {
e1.printStackTrace();
return teacherList;
}
}
}.execute("").get();
} catch (InterruptedException e) {
e.printStackTrace();
return new ArrayList<Teacher>();
} catch (ExecutionException e) {
e.printStackTrace();
return new ArrayList<Teacher>();
}
}
use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherServiceImpl method updateTeacherById.
@Override
public Teacher updateTeacherById(long id, Teacher newTeacher, long departmentId) throws TeacherException {
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.queryParam("departmentId", departmentId).request();
builder.accept("application/json");
Response response = builder.put(Entity.json(newTeacher));
if (response.getStatus() == 200) {
return (Teacher) response.readEntity(Teacher.class);
} else if (response.getStatus() == 400) {
Message message = (Message) response.readEntity(Message.class);
throw new TeacherServiceException(message.getMessage());
} else
throw new TeacherServiceException("The request might invalid or server is down");
} catch (TeacherServiceException e) {
throw new TeacherException(e.getMessage());
}
}
use of com.remswork.project.alice.exception.TeacherException in project classify-system by anverliedoit.
the class TeacherServiceImpl method getTeacherById.
@Override
public Teacher getTeacherById(long id) throws TeacherException {
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());
Response response = target.request().get();
if (response.getStatus() == 200) {
return (Teacher) response.readEntity(Teacher.class);
} else if (response.getStatus() == 404) {
Message message = (Message) response.readEntity(Message.class);
throw new TeacherServiceException(message.getMessage());
} else
throw new TeacherServiceException("The request might invalid or server is down");
} catch (TeacherServiceException e) {
throw new TeacherException(e.getMessage());
}
}
Aggregations