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