use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionServiceImpl method deleteSectionById.
@Override
public Section deleteSectionById(long id) throws SectionException {
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.request();
builder.accept("application/json");
Response response = builder.delete();
if (response.getStatus() == 200) {
return (Section) response.readEntity(Section.class);
} else if (response.getStatus() == 400) {
Message message = (Message) response.readEntity(Message.class);
throw new SectionServiceException(message.getMessage());
} else
throw new SectionServiceException("The request might invalid or server is down");
} catch (SectionServiceException e) {
throw new SectionException(e.getMessage());
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionServiceImpl method getSectionList.
@Override
public List<Section> getSectionList() throws SectionException {
try {
StringBuilder uri = new StringBuilder();
uri.append(targetProperties.getDomain());
uri.append("/");
uri.append(targetProperties.getBaseUri());
uri.append("/");
uri.append(payload);
Client client = ClientBuilder.newClient();
WebTarget target = client.target(uri.toString());
Response response = target.request().get();
if (response.getStatus() == 200) {
return (List<Section>) response.readEntity(new GenericType<List<Section>>() {
});
} else if (response.getStatus() == 404) {
Message message = (Message) response.readEntity(Message.class);
throw new SectionServiceException(message.getMessage());
} else
throw new SectionServiceException("The request might invalid or server is down");
} catch (SectionServiceException e) {
throw new SectionException(e.getMessage());
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionController method updateSectionById.
@RequestMapping(value = "update", method = RequestMethod.POST)
public String updateSectionById(@RequestParam("id") long id, @RequestParam("name") String name, @RequestParam("departmentId") long departmentId, ModelMap modelMap) {
try {
Section newSection = new Section(name);
try {
sectionService.updateSectionById(id, newSection, departmentId);
} catch (SectionException e) {
try {
e.printStackTrace();
departmentId = 0;
sectionService.updateSectionById(id, newSection, departmentId);
} catch (SectionException e2) {
e2.printStackTrace();
}
}
List<Section> sectionList = sectionService.getSectionList();
List<Department> departmentList = departmentService.getDepartmentList();
modelMap.put("sectionList", sectionList);
modelMap.put("departmentList", departmentList);
return "section";
} catch (SectionException | DepartmentException e) {
e.printStackTrace();
return "error";
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionController method addSection.
@RequestMapping(value = "add", method = RequestMethod.POST)
public String addSection(@RequestParam("name") String name, @RequestParam("departmentId") long departmentId, ModelMap modelMap) {
try {
Section section = new Section(name);
sectionService.addSection(section, departmentId);
List<Section> sectionList = sectionService.getSectionList();
List<Department> departmentList = departmentService.getDepartmentList();
modelMap.put("sectionList", sectionList);
modelMap.put("departmentList", departmentList);
return "section";
} catch (SectionException | DepartmentException e) {
e.printStackTrace();
return "error";
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionServiceImpl method addSection.
@Override
public Section addSection(final Section section, final long departmentId) throws SectionException {
try {
return new AsyncTask<String, Section, Section>() {
@Override
protected Section doInBackground(String... args) {
try {
String link = "".concat(domain).concat("/").concat(baseUri).concat("/").concat(payload).concat("?departmentId=").concat(String.valueOf(departmentId));
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(section));
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, Section.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 : Section");
Log.i("ServiceTAG", "Status : " + message.getStatus());
Log.i("ServiceTAG", "Type : " + message.getType());
Log.i("ServiceTAG", "Message : " + message.getMessage());
return null;
} else
throw new SectionException("Server Error");
} catch (SectionException 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;
}
}
Aggregations