Search in sources :

Example 1 with TemplateEditorInput

use of org.apache.syncope.ide.eclipse.plugin.editors.TemplateEditorInput in project syncope by apache.

the class SyncopeView method openTemplateInEditor.

protected void openTemplateInEditor(final TreeObject obj) {
    TreeParent tp = (TreeParent) vcp.getParent(obj);
    if (MAIL_TEMPLATE_LABEL.equals(tp.getName())) {
        final MailTemplateService mailTemplateService = SYNCOPE_CLIENT.getService(MailTemplateService.class);
        final String[] templateData = new String[2];
        final String[] editorTitles = { TEMPLATE_FORMAT_HTML, TEMPLATE_FORMAT_TEXT };
        final String[] editorToolTips = { obj.getName(), obj.getName() };
        Job job = new Job(LOADING_TEMPLATE_FORMAT_LABEL) {

            @Override
            protected IStatus run(final IProgressMonitor arg0) {
                templateData[0] = getStringFromTemplate(mailTemplateService, obj.getName(), MailTemplateFormat.HTML);
                templateData[1] = getStringFromTemplate(mailTemplateService, obj.getName(), MailTemplateFormat.TEXT);
                Display.getDefault().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            getViewSite().getPage().openEditor(new TemplateEditorInput(templateData, editorTitles, editorToolTips), TemplateEditor.ID);
                        } catch (final PartInitException e) {
                            e.printStackTrace();
                        }
                    }
                });
                return Status.OK_STATUS;
            }

            private String getStringFromTemplate(final MailTemplateService mailTemplateService, final String name, final MailTemplateFormat format) {
                try {
                    InputStream inpstream = (InputStream) (mailTemplateService.getFormat(name, format)).getEntity();
                    Scanner sc = new Scanner(inpstream);
                    String templateContent = sc.nextLine();
                    while (sc.hasNext()) {
                        templateContent += "\n" + sc.nextLine();
                    }
                    sc.close();
                    return (templateContent);
                } catch (final SyncopeClientException e) {
                    if (ClientExceptionType.NotFound.equals(e.getType())) {
                        return "";
                    }
                }
                return null;
            }
        };
        job.setUser(true);
        job.schedule();
    } else if (tp.getName().equals(REPORT_TEMPLATE_LABEL)) {
        final ReportTemplateService reportTemplateService = SYNCOPE_CLIENT.getService(ReportTemplateService.class);
        final String[] templateData = new String[3];
        final String[] editorTitles = { TEMPLATE_FORMAT_CSV, TEMPLATE_FORMAT_XSL_FO, TEMPLATE_FORMAT_XSL_HTML };
        final String[] editorToolTips = { obj.getName(), obj.getName(), obj.getName() };
        Job job = new Job(LOADING_TEMPLATE_FORMAT_LABEL) {

            @Override
            protected IStatus run(final IProgressMonitor arg0) {
                templateData[0] = getStringFromTemplate(reportTemplateService, obj.getName(), ReportTemplateFormat.CSV);
                templateData[1] = getStringFromTemplate(reportTemplateService, obj.getName(), ReportTemplateFormat.FO);
                templateData[2] = getStringFromTemplate(reportTemplateService, obj.getName(), ReportTemplateFormat.HTML);
                Display.getDefault().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            getViewSite().getPage().openEditor(new TemplateEditorInput(templateData, editorTitles, editorToolTips), TemplateEditor.ID);
                        } catch (final PartInitException e) {
                            e.printStackTrace();
                        }
                    }
                });
                return Status.OK_STATUS;
            }

            private String getStringFromTemplate(final ReportTemplateService reportTemplateService, final String name, final ReportTemplateFormat format) {
                try {
                    InputStream inpstream = (InputStream) (reportTemplateService.getFormat(name, format)).getEntity();
                    Scanner sc = new Scanner(inpstream);
                    String templateContent = sc.nextLine();
                    while (sc.hasNext()) {
                        templateContent += "\n" + sc.nextLine();
                    }
                    sc.close();
                    return (templateContent);
                } catch (final SyncopeClientException e) {
                    if (ClientExceptionType.NotFound.equals(e.getType())) {
                        return "";
                    }
                }
                return null;
            }
        };
        job.setUser(true);
        job.schedule();
    }
}
Also used : ReportTemplateService(org.apache.syncope.common.rest.api.service.ReportTemplateService) Scanner(java.util.Scanner) IStatus(org.eclipse.core.runtime.IStatus) MailTemplateService(org.apache.syncope.common.rest.api.service.MailTemplateService) ReportTemplateFormat(org.apache.syncope.common.lib.types.ReportTemplateFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) TemplateEditorInput(org.apache.syncope.ide.eclipse.plugin.editors.TemplateEditorInput) MailTemplateFormat(org.apache.syncope.common.lib.types.MailTemplateFormat) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Scanner (java.util.Scanner)1 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 MailTemplateFormat (org.apache.syncope.common.lib.types.MailTemplateFormat)1 ReportTemplateFormat (org.apache.syncope.common.lib.types.ReportTemplateFormat)1 MailTemplateService (org.apache.syncope.common.rest.api.service.MailTemplateService)1 ReportTemplateService (org.apache.syncope.common.rest.api.service.ReportTemplateService)1 TemplateEditorInput (org.apache.syncope.ide.eclipse.plugin.editors.TemplateEditorInput)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Job (org.eclipse.core.runtime.jobs.Job)1 PartInitException (org.eclipse.ui.PartInitException)1