Search in sources :

Example 1 with DocumentationItem

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();
}
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)

Example 2 with DocumentationItem

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;
}
Also used : Locale(java.util.Locale) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DocumentationItem(com.serotonin.m2m2.util.DocumentationItem) FileInputStream(java.io.FileInputStream) StringWriter(java.io.StringWriter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

DocumentationItem (com.serotonin.m2m2.util.DocumentationItem)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)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 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1