use of com.serotonin.m2m2.util.DocumentationItem 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();
}
use of com.serotonin.m2m2.util.DocumentationItem in project ma-core-public by infiniteautomation.
the class MiscDwr method getDocumentationItem.
@DwrPermission(anonymous = true)
public Map<String, Object> getDocumentationItem(String documentId) {
Map<String, Object> result = new HashMap<String, Object>();
DocumentationItem item = Common.documentationManifest.getItem(documentId);
if (item == null)
result.put("error", translate("dox.idNotFound", documentId));
else {
// Find the file appropriate for the locale.
Locale locale = getLocale();
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();
addDocumentationItem(result, item);
result.put("content", out.toString());
List<Map<String, Object>> related = new ArrayList<Map<String, Object>>();
for (String relatedId : item.getRelated()) {
Map<String, Object> map = new HashMap<String, Object>();
related.add(map);
DocumentationItem relatedItem = Common.documentationManifest.getItem(relatedId);
if (relatedItem == null)
throw new RuntimeException("Related document '" + relatedId + "' not found");
addDocumentationItem(map, relatedItem);
}
result.put("relatedList", related);
} catch (FileNotFoundException e) {
result.put("error", translate("dox.fileNotFound", file.getPath()));
} catch (IOException e) {
result.put("error", translate("dox.readError", e.getClass().getName(), e.getMessage()));
}
}
return result;
}
Aggregations