Search in sources :

Example 1 with ReportTemplateFormat

use of org.apache.syncope.common.lib.types.ReportTemplateFormat in project syncope by apache.

the class ResourceExplorerTopComponent method openReportEditor.

private void openReportEditor(final String name) throws IOException {
    String formatStr = (String) JOptionPane.showInputDialog(null, "Select File Format", "File format", JOptionPane.QUESTION_MESSAGE, null, PluginConstants.REPORT_TEMPLATE_FORMATS, ReportTemplateFormat.FO.name());
    if (StringUtils.isNotBlank(formatStr)) {
        ReportTemplateFormat format = ReportTemplateFormat.valueOf(formatStr);
        InputStream is = null;
        try {
            switch(format) {
                case HTML:
                    is = (InputStream) reportTemplateManagerService.getFormat(name, ReportTemplateFormat.HTML);
                    break;
                case CSV:
                    is = (InputStream) reportTemplateManagerService.getFormat(name, ReportTemplateFormat.CSV);
                    break;
                case FO:
                    is = (InputStream) reportTemplateManagerService.getFormat(name, ReportTemplateFormat.FO);
                    break;
                default:
                    LOG.log(Level.SEVERE, String.format("Format [%s] not supported", format));
                    break;
            }
        } catch (SyncopeClientException e) {
            LOG.log(Level.SEVERE, String.format("Unable to get [%s] report template in [%s] format", name, format), e);
            if (ClientExceptionType.NotFound.equals(e.getType())) {
                LOG.log(Level.SEVERE, String.format("Report template [%s] not found, create an empty one", name));
            } else {
                JOptionPane.showMessageDialog(null, String.format("Unable to get [%s] report template in [%s] format", name, format), "Connection Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            LOG.log(Level.SEVERE, String.format("Unable to get [%s] report template in [%s] format", name, format), e);
            JOptionPane.showMessageDialog(null, String.format("Unable to get [%s] report template in [%s] format", name, format), "Generic Error", JOptionPane.ERROR_MESSAGE);
        }
        String content = is == null ? StringUtils.EMPTY : IOUtils.toString(is, encodingPattern);
        String reportTemplatesDirName = System.getProperty("java.io.tmpdir") + "/Templates/Report/";
        File reportTemplatesDir = new File(reportTemplatesDirName);
        if (!reportTemplatesDir.exists()) {
            reportTemplatesDir.mkdirs();
        }
        File file = new File(reportTemplatesDirName + name + "." + format.name().toLowerCase());
        FileWriter fw = new FileWriter(file);
        fw.write(content);
        fw.flush();
        FileObject fob = FileUtil.toFileObject(file.getAbsoluteFile());
        DataObject data = DataObject.find(fob);
        data.getLookup().lookup(OpenCookie.class).open();
        data.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(final PropertyChangeEvent evt) {
                if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName())) {
                    // save item remotely
                    LOG.info(String.format("Saving Report template [%s]", name));
                    saveContent();
                }
            }
        });
    }
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) ReportTemplateFormat(org.apache.syncope.common.lib.types.ReportTemplateFormat) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) OpenCookie(org.openide.cookies.OpenCookie) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) BadLocationException(javax.swing.text.BadLocationException) IOException(java.io.IOException) DataObject(org.openide.loaders.DataObject) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 2 with ReportTemplateFormat

use of org.apache.syncope.common.lib.types.ReportTemplateFormat 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)

Example 3 with ReportTemplateFormat

use of org.apache.syncope.common.lib.types.ReportTemplateFormat in project syncope by apache.

the class ReportTemplateServiceImpl method getFormat.

@Override
public Response getFormat(final String key, final ReportTemplateFormat format) {
    String template = logic.getFormat(key, format);
    StreamingOutput sout = (os) -> os.write(template.getBytes());
    return Response.ok(sout).type(MediaType.APPLICATION_XML).build();
}
Also used : ReportTemplateFormat(org.apache.syncope.common.lib.types.ReportTemplateFormat) ReportTemplateLogic(org.apache.syncope.core.logic.ReportTemplateLogic) ReportTemplateTO(org.apache.syncope.common.lib.to.ReportTemplateTO) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) StreamingOutput(javax.ws.rs.core.StreamingOutput) StandardCharsets(java.nio.charset.StandardCharsets) ReportTemplateService(org.apache.syncope.common.rest.api.service.ReportTemplateService) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) MediaType(javax.ws.rs.core.MediaType) Response(javax.ws.rs.core.Response) Service(org.springframework.stereotype.Service) URI(java.net.URI) RESTHeaders(org.apache.syncope.common.rest.api.RESTHeaders) InputStream(java.io.InputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput)

Aggregations

InputStream (java.io.InputStream)3 ReportTemplateFormat (org.apache.syncope.common.lib.types.ReportTemplateFormat)3 IOException (java.io.IOException)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 ReportTemplateService (org.apache.syncope.common.rest.api.service.ReportTemplateService)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1 Scanner (java.util.Scanner)1 BadLocationException (javax.swing.text.BadLocationException)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 IOUtils (org.apache.commons.io.IOUtils)1