use of org.apache.syncope.common.lib.types.MailTemplateFormat in project syncope by apache.
the class MailTemplateServiceImpl method getFormat.
@Override
public Response getFormat(final String key, final MailTemplateFormat format) {
String template = logic.getFormat(key, format);
StreamingOutput sout = (os) -> os.write(template.getBytes());
return Response.ok(sout).type(format.getMediaType()).build();
}
use of org.apache.syncope.common.lib.types.MailTemplateFormat in project syncope by apache.
the class ResourceExplorerTopComponent method openMailEditor.
private void openMailEditor(final String name) throws IOException {
String formatStr = (String) JOptionPane.showInputDialog(null, "Select File Format", "File format", JOptionPane.QUESTION_MESSAGE, null, PluginConstants.MAIL_TEMPLATE_FORMATS, MailTemplateFormat.TEXT.name());
if (StringUtils.isNotBlank(formatStr)) {
MailTemplateFormat format = MailTemplateFormat.valueOf(formatStr);
String type = null;
InputStream is = null;
try {
switch(format) {
case HTML:
type = "html";
is = (InputStream) mailTemplateManagerService.getFormat(name, MailTemplateFormat.HTML);
break;
case TEXT:
type = "txt";
is = (InputStream) mailTemplateManagerService.getFormat(name, MailTemplateFormat.TEXT);
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] mail template in [%s] format", name, format), e);
if (ClientExceptionType.NotFound.equals(e.getType())) {
LOG.log(Level.SEVERE, String.format("Report template in [%s] format not found, create an empty one", format));
} 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] mail template in [%s] format", name, format), e);
JOptionPane.showMessageDialog(null, String.format("Unable to get [%s] mail template in [%s] format", name, format), "Error", JOptionPane.ERROR_MESSAGE);
}
String content = is == null ? StringUtils.EMPTY : IOUtils.toString(is, encodingPattern);
String mailTemplatesDirName = System.getProperty("java.io.tmpdir") + "/Templates/Mail/";
File mailTemplatesDir = new File(mailTemplatesDirName);
if (!mailTemplatesDir.exists()) {
mailTemplatesDir.mkdirs();
}
File file = new File(mailTemplatesDirName + name + "." + type);
FileWriter fw = new FileWriter(file);
fw.write(content);
fw.flush();
FileObject fob = FileUtil.toFileObject(file.getAbsoluteFile());
fob.setAttribute("description", "TEXT");
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 Mail template [%s]", name));
saveContent();
}
}
});
}
}
use of org.apache.syncope.common.lib.types.MailTemplateFormat 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();
}
}
Aggregations