use of com.serotonin.m2m2.web.mvc.rest.v1.model.help.RelatedHelpItemModel in project ma-modules-public by infiniteautomation.
the class HelpRestController method getHelp.
@ApiOperation(value = "Get Help", notes = "request help via the internal Mango help id", response = HelpModel.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/by-id/{helpId}")
public ResponseEntity<HelpModel> getHelp(@PathVariable String helpId, HttpServletRequest request) {
RestProcessResult<HelpModel> result = new RestProcessResult<HelpModel>(HttpStatus.OK);
this.checkUser(request, result);
if (result.isOk()) {
HelpModel model = new HelpModel();
DocumentationItem item = Common.documentationManifest.getItem(helpId);
if (item == null) {
result.addRestMessage(this.getDoesNotExistMessage());
} else {
model.setId(item.getId());
model.setTitle(new TranslatableMessage(item.getKey()).translate(Common.getTranslations()));
// Find the file appropriate for the locale.
Locale locale = ControllerUtils.getLocale(request);
File file = Common.documentationManifest.getDocumentationFile(item, locale.getLanguage(), locale.getCountry(), locale.getVariant());
// Read the content.
try {
Reader in = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
StringWriter out = new StringWriter();
StreamUtils.transfer(in, out);
in.close();
model.setContent(out.toString());
List<RelatedHelpItemModel> relatedItems = new ArrayList<RelatedHelpItemModel>();
for (String relatedId : item.getRelated()) {
DocumentationItem relatedItem = Common.documentationManifest.getItem(relatedId);
if (relatedItem == null)
throw new RuntimeException("Related document '" + relatedId + "' not found");
relatedItems.add(new RelatedHelpItemModel(relatedItem.getId(), new TranslatableMessage(relatedItem.getKey()).translate(Common.getTranslations())));
}
model.setRelatedList(relatedItems);
return result.createResponseEntity(model);
} catch (FileNotFoundException e) {
result.addRestMessage(HttpStatus.NOT_FOUND, new TranslatableMessage("dox.fileNotFound", file.getPath()));
} catch (IOException e) {
result.addRestMessage(HttpStatus.NOT_FOUND, new TranslatableMessage("dox.readError", e.getClass().getName(), e.getMessage()));
}
}
}
return result.createResponseEntity();
}
Aggregations