use of org.hisp.dhis.system.velocity.VelocityManager in project dhis2-core by dhis2.
the class DefaultMessageService method getMessageFooter.
private String getMessageFooter(MessageConversation conversation) {
HashMap<String, Object> values = new HashMap<>(2);
String baseUrl = systemSettingManager.getInstanceBaseUrl();
if (baseUrl == null) {
return StringUtils.EMPTY;
}
Locale locale = (Locale) userSettingService.getUserSetting(UserSettingKey.UI_LOCALE, conversation.getUser());
locale = ObjectUtils.firstNonNull(locale, LocaleManager.DEFAULT_LOCALE);
values.put("responseUrl", baseUrl + MESSAGE_PATH + "?id=" + conversation.getUid());
values.put("i18n", i18nManager.getI18n(locale));
return new VelocityManager().render(values, MESSAGE_EMAIL_FOOTER_TEMPLATE);
}
use of org.hisp.dhis.system.velocity.VelocityManager in project dhis2-core by dhis2.
the class DefaultSecurityService method sendRestoreMessage.
@Override
public boolean sendRestoreMessage(UserCredentials credentials, String rootPath, RestoreOptions restoreOptions) {
if (credentials == null || restoreOptions == null) {
return false;
}
if (validateRestore(credentials) != null) {
return false;
}
RestoreType restoreType = restoreOptions.getRestoreType();
String applicationTitle = (String) systemSettingManager.getSystemSetting(SettingKey.APPLICATION_TITLE);
if (applicationTitle == null || applicationTitle.isEmpty()) {
applicationTitle = DEFAULT_APPLICATION_TITLE;
}
String[] result = initRestore(credentials, restoreOptions);
Set<User> users = new HashSet<>();
users.add(credentials.getUserInfo());
Map<String, Object> vars = new HashMap<>();
vars.put("applicationTitle", applicationTitle);
vars.put("restorePath", rootPath + RESTORE_PATH + restoreType.getAction());
vars.put("token", result[0]);
vars.put("code", result[1]);
vars.put("username", credentials.getUsername());
User user = credentials.getUserInfo();
Locale locale = (Locale) userSettingService.getUserSetting(UserSettingKey.UI_LOCALE, user);
locale = ObjectUtils.firstNonNull(locale, LocaleManager.DEFAULT_LOCALE);
I18n i18n = i18nManager.getI18n(locale);
vars.put("i18n", i18n);
rootPath = rootPath.replace("http://", "").replace("https://", "");
// -------------------------------------------------------------------------
// Render emails
// -------------------------------------------------------------------------
VelocityManager vm = new VelocityManager();
String text1 = vm.render(vars, restoreType.getEmailTemplate() + "1"), text2 = vm.render(vars, restoreType.getEmailTemplate() + "2");
String subject1 = i18n.getString(restoreType.getEmailSubject()) + " " + rootPath + " (" + i18n.getString("message").toLowerCase() + " 1 / 2)", subject2 = i18n.getString(restoreType.getEmailSubject()) + " " + rootPath + " (" + i18n.getString("message").toLowerCase() + " 2 / 2)";
// -------------------------------------------------------------------------
// Send emails
// -------------------------------------------------------------------------
emailMessageSender.sendMessage(subject1, text1, null, null, users, true);
emailMessageSender.sendMessage(subject2, text2, null, null, users, true);
return true;
}
use of org.hisp.dhis.system.velocity.VelocityManager in project dhis2-core by dhis2.
the class DefaultReportService method renderHtmlReport.
@Override
public void renderHtmlReport(Writer writer, String uid, Date date, String ou) {
Report report = getReport(uid);
OrganisationUnit organisationUnit = null;
List<OrganisationUnit> organisationUnitHierarchy = new ArrayList<>();
List<OrganisationUnit> organisationUnitChildren = new ArrayList<>();
List<String> periods = new ArrayList<>();
I18nFormat format = i18nManager.getI18nFormat();
if (ou != null) {
organisationUnit = organisationUnitService.getOrganisationUnit(ou);
if (organisationUnit != null) {
organisationUnitHierarchy.add(organisationUnit);
OrganisationUnit parent = organisationUnit;
while (parent.getParent() != null) {
parent = parent.getParent();
organisationUnitHierarchy.add(parent);
}
organisationUnitChildren.addAll(organisationUnit.getChildren());
}
}
Calendar calendar = PeriodType.getCalendar();
if (report != null && report.hasRelativePeriods()) {
if (calendar.isIso8601()) {
for (Period period : report.getRelatives().getRelativePeriods(date, format, true)) {
periods.add(period.getIsoDate());
}
} else {
periods = IdentifiableObjectUtils.getLocalPeriodIdentifiers(report.getRelatives().getRelativePeriods(date, format, true), calendar);
}
}
String dateString = DateUtils.getMediumDateString(date);
if (date != null && !calendar.isIso8601()) {
dateString = calendar.formattedDate(calendar.fromIso(date));
}
final VelocityContext context = new VelocityContext();
context.put("report", report);
context.put("organisationUnit", organisationUnit);
context.put("organisationUnitHierarchy", organisationUnitHierarchy);
context.put("organisationUnitChildren", organisationUnitChildren);
context.put("date", dateString);
context.put("periods", periods);
context.put("format", format);
context.put("encoder", ENCODER);
new VelocityManager().getEngine().getTemplate("html-report.vm").merge(context, writer);
}
use of org.hisp.dhis.system.velocity.VelocityManager in project dhis2-core by dhis2.
the class DefaultMessageService method sendCompletenessMessage.
@Override
public int sendCompletenessMessage(CompleteDataSetRegistration registration) {
DataSet dataSet = registration.getDataSet();
if (dataSet == null) {
return 0;
}
UserGroup userGroup = dataSet.getNotificationRecipients();
User sender = currentUserService.getCurrentUser();
Set<User> recipients = new HashSet<>();
if (userGroup != null) {
recipients.addAll(new HashSet<>(userGroup.getMembers()));
}
if (dataSet.isNotifyCompletingUser()) {
recipients.add(sender);
}
if (recipients.isEmpty()) {
return 0;
}
String text = new VelocityManager().render(registration, COMPLETE_TEMPLATE);
MessageConversation conversation = new MessageConversation(COMPLETE_SUBJECT, sender, MessageType.SYSTEM);
conversation.addMessage(new Message(text, null, sender));
for (User user : recipients) {
conversation.addUserMessage(new UserMessage(user));
}
if (!conversation.getUserMessages().isEmpty()) {
int id = saveMessageConversation(conversation);
invokeMessageSenders(COMPLETE_SUBJECT, text, null, sender, new HashSet<>(conversation.getUsers()), false);
return id;
}
return 0;
}
use of org.hisp.dhis.system.velocity.VelocityManager in project dhis2-core by dhis2.
the class GridUtils method render.
/**
* Render using Velocity.
*/
private static void render(Grid grid, Map<?, ?> params, Writer writer, String template) {
final VelocityContext context = new VelocityContext();
context.put(KEY_GRID, grid);
context.put(KEY_ENCODER, ENCODER);
context.put(KEY_PARAMS, params);
new VelocityManager().getEngine().getTemplate(template).merge(context, writer);
}
Aggregations