Search in sources :

Example 1 with RelatedHelpItemModel

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();
}
Also used : Locale(java.util.Locale) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) RelatedHelpItemModel(com.serotonin.m2m2.web.mvc.rest.v1.model.help.RelatedHelpItemModel) IOException(java.io.IOException) DocumentationItem(com.serotonin.m2m2.util.DocumentationItem) HelpModel(com.serotonin.m2m2.web.mvc.rest.v1.model.help.HelpModel) FileInputStream(java.io.FileInputStream) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) StringWriter(java.io.StringWriter) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) File(java.io.File) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 DocumentationItem (com.serotonin.m2m2.util.DocumentationItem)1 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)1 HelpModel (com.serotonin.m2m2.web.mvc.rest.v1.model.help.HelpModel)1 RelatedHelpItemModel (com.serotonin.m2m2.web.mvc.rest.v1.model.help.RelatedHelpItemModel)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1