use of com.remswork.project.alice.model.Teacher in project classify-system by anverliedoit.
the class TeacherResource method getAll.
@GET
public Response getAll() {
try {
TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
List<Teacher> teacherList = teacherService.getTeacherList();
for (Teacher teacher : teacherList) {
teacher.addLink(resourceLink.self(teacher.getId()));
if (teacher.getDepartment() != null)
teacher.getDepartment().addLink(departmentResourceLinks.self(teacher.getDepartment().getId()));
}
GenericEntity<List<Teacher>> entity = new GenericEntity<List<Teacher>>(teacherList) {
};
return Response.status(Response.Status.OK).entity(entity).build();
} catch (TeacherException e) {
e.printStackTrace();
Message message = new Message(404, "Not Found", e.getMessage());
return Response.status(Response.Status.NOT_FOUND).entity(message).build();
}
}
use of com.remswork.project.alice.model.Teacher in project classify-system by anverliedoit.
the class TeacherResource method deleteTeacherById.
@DELETE
@Path("{teacherId}")
public Response deleteTeacherById(@PathParam("teacherId") long id) {
try {
TeacherResourceLinks resourceLink = new TeacherResourceLinks(uriInfo);
DepartmentResourceLinks departmentResourceLinks = new DepartmentResourceLinks(uriInfo);
Teacher teacher = teacherService.deleteTeacherById(id);
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.model.Teacher 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.model.Teacher 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.model.Teacher 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());
}
}
Aggregations