MIFOSX-117: integrate andreas work on UGD.
This commit is contained in:
+11
-2
@@ -26,6 +26,7 @@ public class CommandWrapper {
|
||||
private final String supportedEntityType;
|
||||
private final Long supportedEntityId;
|
||||
private final Long productId;
|
||||
private Long templateId;
|
||||
|
||||
public static CommandWrapper wrap(final String actionName, final String entityName, final Long resourceId, final Long subresourceId) {
|
||||
return new CommandWrapper(null, actionName, entityName, resourceId, subresourceId, null, null);
|
||||
@@ -56,13 +57,12 @@ public class CommandWrapper {
|
||||
this.json = null;
|
||||
this.transactionId = null;
|
||||
this.productId = productId;
|
||||
|
||||
}
|
||||
|
||||
public CommandWrapper(final Long officeId, final Long groupId, final Long clientId, final Long loanId, final Long savingsId,
|
||||
final String actionName, final String entityName, final Long entityId, final Long subentityId, final Long codeId,
|
||||
final String supportedEntityType, final Long supportedEntityId, final String href, final String json,
|
||||
final String transactionId, final Long productId) {
|
||||
final String transactionId, final Long productId, final Long templateId) {
|
||||
this.commandId = null;
|
||||
this.officeId = officeId;
|
||||
this.groupId = groupId;
|
||||
@@ -81,6 +81,7 @@ public class CommandWrapper {
|
||||
this.json = json;
|
||||
this.transactionId = transactionId;
|
||||
this.productId = productId;
|
||||
this.templateId = templateId;
|
||||
}
|
||||
|
||||
public Long commandId() {
|
||||
@@ -142,6 +143,10 @@ public class CommandWrapper {
|
||||
public String getTransactionId() {
|
||||
return this.transactionId;
|
||||
}
|
||||
|
||||
public Long getTemplateId() {
|
||||
return this.templateId;
|
||||
}
|
||||
|
||||
public String getEntityName() {
|
||||
return this.entityName;
|
||||
@@ -320,6 +325,10 @@ public class CommandWrapper {
|
||||
public boolean isCollateralResource() {
|
||||
return this.entityName.equalsIgnoreCase("COLLATERAL");
|
||||
}
|
||||
|
||||
public boolean isTemplateRessource() {
|
||||
return this.entityName.equalsIgnoreCase("Template");
|
||||
}
|
||||
|
||||
public boolean isApproveLoanApplication() {
|
||||
return this.actionName.equalsIgnoreCase("APPROVE") && this.entityName.equalsIgnoreCase("LOAN");
|
||||
|
||||
+29
-1
@@ -26,11 +26,12 @@ public class CommandWrapperBuilder {
|
||||
private String supportedEntityType;
|
||||
private Long supportedEntityId;
|
||||
private Long productId;
|
||||
private Long templateId;
|
||||
|
||||
public CommandWrapper build() {
|
||||
return new CommandWrapper(this.officeId, this.groupId, this.clientId, this.loanId, this.savingsId, this.actionName,
|
||||
this.entityName, this.entityId, this.subentityId, this.codeId, this.supportedEntityType, this.supportedEntityId, this.href,
|
||||
this.json, this.transactionId, this.productId);
|
||||
this.json, this.transactionId, this.productId, this.templateId);
|
||||
}
|
||||
|
||||
public CommandWrapperBuilder withLoanId(final Long withLoanId) {
|
||||
@@ -1305,6 +1306,33 @@ public class CommandWrapperBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandWrapperBuilder createTemplate() {
|
||||
this.actionName = "CREATE";
|
||||
this.entityName = "TEMPLATE";
|
||||
this.entityId = null;
|
||||
this.templateId = null;
|
||||
this.href = "/templates";
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandWrapperBuilder updateTemplate(final Long templateId) {
|
||||
this.actionName = "UPDATE";
|
||||
this.entityName = "TEMPLATE";
|
||||
this.entityId = templateId;
|
||||
this.templateId = templateId;
|
||||
this.href = "/templates/" + templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandWrapperBuilder deleteTemplate(final Long templateId) {
|
||||
this.actionName = "DELETE";
|
||||
this.entityName = "TEMPLATE";
|
||||
this.entityId = templateId;
|
||||
this.templateId = templateId;
|
||||
this.href = "/templates/" + templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandWrapperBuilder assignClientStaff(final Long clientId) {
|
||||
this.actionName = "ASSIGNSTAFF";
|
||||
this.entityName = "CLIENT";
|
||||
|
||||
+24
-9
@@ -144,7 +144,6 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
} else if (wrapper.isClientIdentifierResource()) {
|
||||
|
||||
if (wrapper.isCreate()) {
|
||||
handler = this.applicationContext.getBean("createClientIdentifierCommandHandler", NewCommandSourceHandler.class);
|
||||
} else if (wrapper.isUpdate()) {
|
||||
@@ -501,15 +500,12 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
} else if (wrapper.isCollectionSheetResource()) {
|
||||
|
||||
if (wrapper.isUpdate()) {
|
||||
handler = this.applicationContext.getBean("updateCollectionSheetCommandHandler", NewCommandSourceHandler.class);
|
||||
} else {
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
|
||||
} else if (wrapper.isReportResource()) {
|
||||
|
||||
if (wrapper.isCreate()) {
|
||||
handler = this.applicationContext.getBean("createReportCommandHandler", NewCommandSourceHandler.class);
|
||||
} else if (wrapper.isUpdate()) {
|
||||
@@ -519,9 +515,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
|
||||
} else {
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
|
||||
} else if (wrapper.isAccountingRuleResource()) {
|
||||
|
||||
if (wrapper.isCreate()) {
|
||||
handler = this.applicationContext.getBean("createAccountingRuleCommandHandler", NewCommandSourceHandler.class);
|
||||
} else if (wrapper.isUpdate()) {
|
||||
@@ -531,7 +525,6 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
|
||||
} else {
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
|
||||
} else if (wrapper.isXBRLMappingResource()) {
|
||||
if (wrapper.isUpdate()) {
|
||||
handler = this.applicationContext.getBean("updateTaxonomyMappingCommandHandler", NewCommandSourceHandler.class);
|
||||
@@ -539,7 +532,6 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
} else if (wrapper.isHolidayResource()) {
|
||||
|
||||
if (wrapper.isCreate()) {
|
||||
handler = this.applicationContext.getBean("createHolidayCommandHandler", NewCommandSourceHandler.class);
|
||||
}
|
||||
@@ -577,6 +569,29 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
|
||||
} else {
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
} else if (wrapper.isTemplateRessource()) {
|
||||
|
||||
if (wrapper.isCreate()) {
|
||||
handler = this.applicationContext.getBean("createTemplateCommandHandler", NewCommandSourceHandler.class);
|
||||
} else if (wrapper.isUpdate()) {
|
||||
handler = this.applicationContext.getBean("updateTemplateCommandHandler", NewCommandSourceHandler.class);
|
||||
} else if (wrapper.isDelete()) {
|
||||
handler = this.applicationContext.getBean("deleteTemplateCommandHandler", NewCommandSourceHandler.class);
|
||||
} else {
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
|
||||
} else if (wrapper.isTemplateRessource()) {
|
||||
if (wrapper.isCreate()) {
|
||||
handler = this.applicationContext.getBean("createTemplateCommandHandler", NewCommandSourceHandler.class);
|
||||
} else if (wrapper.isUpdate()) {
|
||||
handler = this.applicationContext.getBean("updateTemplateCommandHandler", NewCommandSourceHandler.class);
|
||||
} else if (wrapper.isDelete()) {
|
||||
handler = this.applicationContext.getBean("deleteTemplateCommandHandler", NewCommandSourceHandler.class);
|
||||
} else {
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
throw new UnsupportedCommandException(wrapper.commandName());
|
||||
@@ -584,4 +599,4 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
|
||||
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -10,6 +10,7 @@ import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
@@ -23,6 +24,7 @@ import org.mifosplatform.infrastructure.security.service.PlatformPasswordEncoder
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
/**
|
||||
* Immutable representation of a command.
|
||||
@@ -314,6 +316,12 @@ public final class JsonCommand {
|
||||
return this.fromApiJsonHelper.extractStringNamed(parameterName, this.parsedCommand);
|
||||
}
|
||||
|
||||
public Map<String, String> mapValueOfParameterNamed(final String json) {
|
||||
final Type typeOfMap = new TypeToken<Map<String, String>>() {}.getType();
|
||||
final Map<String, String> value = this.fromApiJsonHelper.extractDataMap(typeOfMap, json);
|
||||
return value;
|
||||
}
|
||||
|
||||
public boolean isChangeInBigDecimalParameterNamedDefaultingZeroToNull(final String parameterName, final BigDecimal existingValue) {
|
||||
boolean isChanged = false;
|
||||
if (parameterExists(parameterName)) {
|
||||
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
package org.mifosplatform.template.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.mifosplatform.commands.domain.CommandWrapper;
|
||||
import org.mifosplatform.commands.service.CommandWrapperBuilder;
|
||||
import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService;
|
||||
import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
|
||||
import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer;
|
||||
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
||||
import org.mifosplatform.template.data.TemplateData;
|
||||
import org.mifosplatform.template.domain.Template;
|
||||
import org.mifosplatform.template.domain.TemplateEntity;
|
||||
import org.mifosplatform.template.domain.TemplateType;
|
||||
import org.mifosplatform.template.service.TemplateDomainService;
|
||||
import org.mifosplatform.template.service.TemplateMergeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Path("/templates")
|
||||
@Component
|
||||
@Scope("singleton")
|
||||
public class TemplatesApiResource {
|
||||
|
||||
private final Set<String> RESPONSE_TEMPLATES_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id"));
|
||||
private final Set<String> RESPONSE_TEMPLATE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "entities", "types", "template"));
|
||||
private final String RESOURCEN_NAME_FOR_PERMISSION = "template";
|
||||
|
||||
private final PlatformSecurityContext context;
|
||||
private final DefaultToApiJsonSerializer<Template> toApiJsonSerializer;
|
||||
private final DefaultToApiJsonSerializer<TemplateData> templateDataApiJsonSerializer;
|
||||
private final ApiRequestParameterHelper apiRequestParameterHelper;
|
||||
private final TemplateDomainService templateService;
|
||||
private final TemplateMergeService templateMergeService;
|
||||
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
|
||||
|
||||
@Autowired
|
||||
public TemplatesApiResource(final PlatformSecurityContext context, final DefaultToApiJsonSerializer<Template> toApiJsonSerializer,
|
||||
final DefaultToApiJsonSerializer<TemplateData> templateDataApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper,
|
||||
final TemplateDomainService templateService, final TemplateMergeService templateMergeService,
|
||||
final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) {
|
||||
|
||||
this.context = context;
|
||||
this.toApiJsonSerializer = toApiJsonSerializer;
|
||||
this.templateDataApiJsonSerializer = templateDataApiJsonSerializer;
|
||||
this.apiRequestParameterHelper = apiRequestParameterHelper;
|
||||
this.templateService = templateService;
|
||||
this.templateMergeService = templateMergeService;
|
||||
this.commandsSourceWritePlatformService = commandsSourceWritePlatformService;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String getTemplates(@DefaultValue("-1") @QueryParam("typeId") final int typeId,
|
||||
@DefaultValue("-1") @QueryParam("entityId") final int entityId, @Context final UriInfo uriInfo) {
|
||||
|
||||
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCEN_NAME_FOR_PERMISSION);
|
||||
|
||||
List<Template> templates = new ArrayList<Template>();
|
||||
|
||||
if (typeId != -1 && entityId != -1) {
|
||||
templates = this.templateService.getAllByEntityAndType(TemplateEntity.values()[entityId], TemplateType.values()[typeId]);
|
||||
} else {
|
||||
templates = this.templateService.getAll();
|
||||
}
|
||||
|
||||
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
|
||||
return this.toApiJsonSerializer.serialize(settings, templates, this.RESPONSE_TEMPLATES_DATA_PARAMETERS);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("template")
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String getTemplatesByTemplate(@Context final UriInfo uriInfo) {
|
||||
|
||||
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCEN_NAME_FOR_PERMISSION);
|
||||
|
||||
final TemplateData templateData = TemplateData.template();
|
||||
|
||||
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
|
||||
return this.templateDataApiJsonSerializer.serialize(settings, templateData, this.RESPONSE_TEMPLATES_DATA_PARAMETERS);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{templateId}")
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String getTemplate(@PathParam("templateId") final Long templateId, @Context final UriInfo uriInfo) {
|
||||
|
||||
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCEN_NAME_FOR_PERMISSION);
|
||||
|
||||
final Template template = this.templateService.getById(templateId);
|
||||
|
||||
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
|
||||
return this.toApiJsonSerializer.serialize(settings, template, this.RESPONSE_TEMPLATES_DATA_PARAMETERS);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{templateId}/template")
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String getTemplateByTemplate(@PathParam("templateId") final Long templateId, @Context final UriInfo uriInfo) {
|
||||
|
||||
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCEN_NAME_FOR_PERMISSION);
|
||||
|
||||
final TemplateData template = TemplateData.template(this.templateService.getById(templateId));
|
||||
|
||||
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
|
||||
System.out.println("QUERY_PARAMETER: " + uriInfo.getQueryParameters());
|
||||
System.out.println("SETTINGS: " + settings);
|
||||
System.out.println("FOO: " + this.templateDataApiJsonSerializer.serialize(settings, template, this.RESPONSE_TEMPLATE_DATA_PARAMETERS));
|
||||
|
||||
return this.templateDataApiJsonSerializer.serialize(settings, template, this.RESPONSE_TEMPLATE_DATA_PARAMETERS);
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("{templateId}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String saveTemplate(@PathParam("templateId") final Long templateId, final String apiRequestBodyAsJson) {
|
||||
|
||||
final CommandWrapper commandRequest = new CommandWrapperBuilder().updateTemplate(templateId).withJson(apiRequestBodyAsJson).build();
|
||||
|
||||
final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
|
||||
|
||||
return this.toApiJsonSerializer.serialize(result);
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{templateId}")
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String deleteTemplate(@PathParam("templateId") final Long templateId) {
|
||||
|
||||
final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteTemplate(templateId).build();
|
||||
|
||||
final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
|
||||
|
||||
return this.toApiJsonSerializer.serialize(result);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String createTemplate(final String apiRequestBodyAsJson) {
|
||||
final CommandWrapper commandRequest = new CommandWrapperBuilder().createTemplate().withJson(apiRequestBodyAsJson).build();
|
||||
|
||||
final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);
|
||||
|
||||
return this.toApiJsonSerializer.serialize(result);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("{templateId}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.TEXT_HTML })
|
||||
public String mergeTemplate(@PathParam("templateId") final Long templateId, @Context final UriInfo uriInfo,
|
||||
final String apiRequestBodyAsJson) throws MalformedURLException, IOException {
|
||||
|
||||
final Template template = this.templateService.getById(templateId);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
final
|
||||
HashMap<String, Object> result = new ObjectMapper().readValue(apiRequestBodyAsJson, HashMap.class);
|
||||
|
||||
final MultivaluedMap<String, String> parameters = uriInfo.getQueryParameters();
|
||||
final Map<String, Object> parametersMap = new HashMap<String, Object>();
|
||||
for (final Map.Entry<String, List<String>> entry : parameters.entrySet()) {
|
||||
|
||||
if (entry.getValue().size() == 1) {
|
||||
parametersMap.put(entry.getKey(), entry.getValue().get(0));
|
||||
} else {
|
||||
parametersMap.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
parametersMap.put("BASE_URI", uriInfo.getBaseUri());
|
||||
parametersMap.putAll(result);
|
||||
return this.templateMergeService.compile(template, parametersMap);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.mifosplatform.template.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mifosplatform.template.domain.Template;
|
||||
import org.mifosplatform.template.domain.TemplateEntity;
|
||||
import org.mifosplatform.template.domain.TemplateType;
|
||||
|
||||
public class TemplateData {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private final List<Map<String, Object>> entities;
|
||||
@SuppressWarnings("unused")
|
||||
private final List<Map<String, Object>> types;
|
||||
@SuppressWarnings("unused")
|
||||
private final Template template;
|
||||
|
||||
private TemplateData(final Template template) {
|
||||
this.template = template;
|
||||
this.entities = getEntites();
|
||||
this.types = getTypes();
|
||||
}
|
||||
|
||||
public static TemplateData template(final Template template) {
|
||||
return new TemplateData(template);
|
||||
}
|
||||
|
||||
public static TemplateData template() {
|
||||
return new TemplateData(null);
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> getEntites() {
|
||||
final List<Map<String, Object>> l = new ArrayList<Map<String, Object>>();
|
||||
for (final TemplateEntity e : TemplateEntity.values()) {
|
||||
final Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("id", e.getId());
|
||||
m.put("name", e.getName());
|
||||
l.add(m);
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> getTypes() {
|
||||
final List<Map<String, Object>> l = new ArrayList<Map<String, Object>>();
|
||||
for (final TemplateType e : TemplateType.values()) {
|
||||
final Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("id", e.getId());
|
||||
m.put("name", e.getName());
|
||||
l.add(m);
|
||||
}
|
||||
return l;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OrderBy;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
|
||||
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.springframework.data.jpa.domain.AbstractPersistable;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
@Entity
|
||||
@Table(name = "m_template", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }, name = "unq_name") })
|
||||
public class Template extends AbstractPersistable<Long> {
|
||||
|
||||
@Column(name = "name", nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
@Enumerated
|
||||
@JsonSerialize(using = TemplateEntitySerializer.class)
|
||||
private TemplateEntity entity;
|
||||
|
||||
@Enumerated
|
||||
@JsonSerialize(using = TemplateTypeSerializer.class)
|
||||
private TemplateType type;
|
||||
|
||||
@Column(name = "text", columnDefinition = "longtext", nullable = false)
|
||||
private String text;
|
||||
|
||||
@OrderBy(value = "mapperorder")
|
||||
@OneToMany(targetEntity = TemplateMapper.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private List<TemplateMapper> mappers;
|
||||
|
||||
public Template(final String name, final String text, final TemplateEntity entity, final TemplateType type, final List<TemplateMapper> mappers) {
|
||||
this.name = name;
|
||||
this.entity = entity;
|
||||
this.type = type;
|
||||
this.text = text;
|
||||
this.mappers = mappers;
|
||||
}
|
||||
|
||||
protected Template() {}
|
||||
|
||||
public static Template fromJson(final JsonCommand command) {
|
||||
System.out.println("COMMAND: " + command);
|
||||
final String name = command.stringValueOfParameterNamed("name");
|
||||
final String text = command.stringValueOfParameterNamed("text");
|
||||
final TemplateEntity entity = TemplateEntity.values()[command.integerValueSansLocaleOfParameterNamed("entity")];
|
||||
final TemplateType type = TemplateType.values()[command.integerValueSansLocaleOfParameterNamed("type")];
|
||||
final JsonArray array = command.arrayOfParameterNamed("mappers");
|
||||
final List<TemplateMapper> mappersList = new ArrayList<TemplateMapper>();
|
||||
for (final JsonElement element : array) {
|
||||
mappersList.add(new TemplateMapper(element.getAsJsonObject().get("mappersorder").getAsInt(), element.getAsJsonObject()
|
||||
.get("mapperskey").getAsString(), element.getAsJsonObject().get("mappersvalue").getAsString()));
|
||||
}
|
||||
|
||||
return new Template(name, text, entity, type, mappersList);
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, String> getMappersAsMap() {
|
||||
final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
|
||||
for (final TemplateMapper mapper : getMappers()) {
|
||||
map.put(mapper.getMapperkey(), mapper.getMappervalue());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public List<TemplateMapper> getMappers() {
|
||||
return this.mappers;
|
||||
}
|
||||
|
||||
public void setMappers(final List<TemplateMapper> mappers) {
|
||||
this.mappers = mappers;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public TemplateEntity getEntity() {
|
||||
return this.entity;
|
||||
}
|
||||
|
||||
public void setEntity(final TemplateEntity entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public TemplateType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(final TemplateType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
public void setText(final String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
@JsonSerialize(using = TemplateEntitySerializer.class)
|
||||
public enum TemplateEntity {
|
||||
|
||||
@SerializedName("client")
|
||||
CLIENT(0, "client"), @SerializedName("loan")
|
||||
LOAN(1, "loan");
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
private TemplateEntity(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.JsonProcessingException;
|
||||
import org.codehaus.jackson.map.JsonSerializer;
|
||||
import org.codehaus.jackson.map.SerializerProvider;
|
||||
|
||||
public class TemplateEntitySerializer extends JsonSerializer<TemplateEntity> {
|
||||
|
||||
@Override
|
||||
public void serialize(final TemplateEntity value, final JsonGenerator generator, @SuppressWarnings("unused") final SerializerProvider provider)
|
||||
throws IOException, JsonProcessingException {
|
||||
|
||||
generator.writeStartObject();
|
||||
generator.writeFieldName("id");
|
||||
generator.writeNumber(value.getId());
|
||||
generator.writeFieldName("name");
|
||||
generator.writeString(value.getName());
|
||||
generator.writeEndObject();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class TemplateFunctions {
|
||||
|
||||
public static String now() {
|
||||
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
|
||||
final Date date = new Date();
|
||||
|
||||
return dateFormat.format(date);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.springframework.data.jpa.domain.AbstractPersistable;
|
||||
|
||||
@Entity
|
||||
@Table(name = "m_templatemappers")
|
||||
public class TemplateMapper extends AbstractPersistable<Long> {
|
||||
|
||||
@Column(name = "mapperorder")
|
||||
private int mapperorder;
|
||||
|
||||
@Column(name = "mapperkey")
|
||||
private String mapperkey;
|
||||
|
||||
@Column(name = "mappervalue")
|
||||
private String mappervalue;
|
||||
|
||||
protected TemplateMapper() {}
|
||||
|
||||
public TemplateMapper(final int mapperorder, final String mapperkey, final String mappervalue) {
|
||||
this.mapperorder = mapperorder;
|
||||
this.mapperkey = mapperkey;
|
||||
this.mappervalue = mappervalue;
|
||||
}
|
||||
|
||||
public String getMapperkey() {
|
||||
return this.mapperkey;
|
||||
}
|
||||
|
||||
public int getMapperorder() {
|
||||
return this.mapperorder;
|
||||
}
|
||||
|
||||
public void setMapperorder(final int mapperorder) {
|
||||
this.mapperorder = mapperorder;
|
||||
}
|
||||
|
||||
public void setMapperkey(final String mapperkey) {
|
||||
this.mapperkey = mapperkey;
|
||||
}
|
||||
|
||||
public String getMappervalue() {
|
||||
return this.mappervalue;
|
||||
}
|
||||
|
||||
public void setMappervalue(final String mappervalue) {
|
||||
this.mappervalue = mappervalue;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface TemplateRepository extends JpaRepository<Template, Long> {
|
||||
|
||||
List<Template> findByEntityAndType(TemplateEntity entity, TemplateType type);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
@JsonSerialize(using = TemplateTypeSerializer.class)
|
||||
public enum TemplateType {
|
||||
|
||||
@SerializedName("Document")
|
||||
DOCUMENT(0, "Document");
|
||||
|
||||
/**
|
||||
* @SerializedName("E-Mail") EMAIL(1, "E-Mail"),
|
||||
* @SerializedName("SMS") SMS(2, "SMS");
|
||||
*/
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
private TemplateType(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package org.mifosplatform.template.domain;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.JsonProcessingException;
|
||||
import org.codehaus.jackson.map.JsonSerializer;
|
||||
import org.codehaus.jackson.map.SerializerProvider;
|
||||
|
||||
public class TemplateTypeSerializer extends JsonSerializer<TemplateType> {
|
||||
|
||||
@Override
|
||||
public void serialize(final TemplateType value, final JsonGenerator generator, @SuppressWarnings("unused") final SerializerProvider provider)
|
||||
throws IOException, JsonProcessingException {
|
||||
|
||||
generator.writeStartObject();
|
||||
generator.writeFieldName("id");
|
||||
generator.writeNumber(value.getId());
|
||||
generator.writeFieldName("name");
|
||||
generator.writeString(value.getName());
|
||||
generator.writeEndObject();
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.mifosplatform.template.exception;
|
||||
|
||||
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException;
|
||||
|
||||
public class TemplateNotFoundException extends AbstractPlatformResourceNotFoundException {
|
||||
|
||||
public TemplateNotFoundException(final Long id) {
|
||||
super("error.msg.template.id.invalid", "Template with identifier " + id + " does not exist", id);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package org.mifosplatform.template.handler;
|
||||
|
||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.mifosplatform.template.service.TemplateDomainService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class CreateTemplateCommandHandler implements NewCommandSourceHandler {
|
||||
|
||||
private final TemplateDomainService templateService;
|
||||
|
||||
@Autowired
|
||||
public CreateTemplateCommandHandler(final TemplateDomainService templateService) {
|
||||
|
||||
this.templateService = templateService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public CommandProcessingResult processCommand(final JsonCommand command) {
|
||||
|
||||
return this.templateService.createTemplate(command);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package org.mifosplatform.template.handler;
|
||||
|
||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.mifosplatform.template.service.TemplateDomainService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class DeleteTemplateCommandHandler implements NewCommandSourceHandler {
|
||||
|
||||
private final TemplateDomainService templateService;
|
||||
|
||||
@Autowired
|
||||
public DeleteTemplateCommandHandler(final TemplateDomainService templateService) {
|
||||
|
||||
this.templateService = templateService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public CommandProcessingResult processCommand(final JsonCommand command) {
|
||||
|
||||
return this.templateService.removeTemplate(command.entityId());
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package org.mifosplatform.template.handler;
|
||||
|
||||
import org.mifosplatform.commands.handler.NewCommandSourceHandler;
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.mifosplatform.template.service.TemplateDomainService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class UpdateTemplateCommandHandler implements NewCommandSourceHandler {
|
||||
|
||||
private final TemplateDomainService templateService;
|
||||
|
||||
@Autowired
|
||||
public UpdateTemplateCommandHandler(final TemplateDomainService templateService) {
|
||||
|
||||
this.templateService = templateService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public CommandProcessingResult processCommand(final JsonCommand command) {
|
||||
|
||||
return this.templateService.updateTemplate(command.entityId(), command);
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package org.mifosplatform.template.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder;
|
||||
import org.mifosplatform.template.domain.Template;
|
||||
import org.mifosplatform.template.domain.TemplateEntity;
|
||||
import org.mifosplatform.template.domain.TemplateMapper;
|
||||
import org.mifosplatform.template.domain.TemplateRepository;
|
||||
import org.mifosplatform.template.domain.TemplateType;
|
||||
import org.mifosplatform.template.exception.TemplateNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
@Service
|
||||
public class JpaTemplateDomainService implements TemplateDomainService {
|
||||
|
||||
private static final String PROPERTY_NAME = "name";
|
||||
private static final String PROPERTY_TEXT = "text";
|
||||
private static final String PROPERTY_MAPPERS = "mappers";
|
||||
private static final String PROPERTY_ENTITY = "entity";
|
||||
private static final String PROPERTY_TYPE = "type";
|
||||
|
||||
@Autowired
|
||||
private TemplateRepository templateRepository;
|
||||
|
||||
@Override
|
||||
public List<Template> getAll() {
|
||||
return this.templateRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template getById(final Long id) {
|
||||
final Template template = this.templateRepository.findOne(id);
|
||||
if (template == null) { throw new TemplateNotFoundException(id); }
|
||||
return template;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public CommandProcessingResult createTemplate(final JsonCommand command) {
|
||||
final Template template = Template.fromJson(command);
|
||||
|
||||
this.templateRepository.saveAndFlush(template);
|
||||
return new CommandProcessingResultBuilder().withEntityId(template.getId()).build();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public CommandProcessingResult updateTemplate(final Long templateId, final JsonCommand command) {
|
||||
final Template template = getById(templateId);
|
||||
System.out.println("COMM: " + command);
|
||||
template.setName(command.stringValueOfParameterNamed(PROPERTY_NAME));
|
||||
template.setText(command.stringValueOfParameterNamed(PROPERTY_TEXT));
|
||||
template.setEntity(TemplateEntity.values()[command.integerValueSansLocaleOfParameterNamed(PROPERTY_ENTITY)]);
|
||||
template.setType(TemplateType.values()[command.integerValueSansLocaleOfParameterNamed(PROPERTY_TYPE)]);
|
||||
|
||||
final JsonArray array = command.arrayOfParameterNamed("mappers");
|
||||
final List<TemplateMapper> mappersList = new ArrayList<TemplateMapper>();
|
||||
for (final JsonElement element : array) {
|
||||
mappersList.add(new TemplateMapper(element.getAsJsonObject().get("mappersorder").getAsInt(), element.getAsJsonObject()
|
||||
.get("mapperskey").getAsString(), element.getAsJsonObject().get("mappersvalue").getAsString()));
|
||||
}
|
||||
template.setMappers(mappersList);
|
||||
|
||||
this.templateRepository.saveAndFlush(template);
|
||||
|
||||
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(template.getId()).build();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public CommandProcessingResult removeTemplate(final Long templateId) {
|
||||
final Template template = getById(templateId);
|
||||
|
||||
this.templateRepository.delete(template);
|
||||
|
||||
return new CommandProcessingResultBuilder().withEntityId(templateId).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template updateTemplate(final Template template) {
|
||||
return this.templateRepository.saveAndFlush(template);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Template> getAllByEntityAndType(final TemplateEntity entity, final TemplateType type) {
|
||||
|
||||
return this.templateRepository.findByEntityAndType(entity, type);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package org.mifosplatform.template.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||
import org.mifosplatform.template.domain.Template;
|
||||
import org.mifosplatform.template.domain.TemplateEntity;
|
||||
import org.mifosplatform.template.domain.TemplateType;
|
||||
|
||||
public interface TemplateDomainService {
|
||||
|
||||
List<Template> getAll();
|
||||
|
||||
List<Template> getAllByEntityAndType(TemplateEntity entity, TemplateType type);
|
||||
|
||||
Template getById(Long id);
|
||||
|
||||
Template updateTemplate(Template template);
|
||||
|
||||
CommandProcessingResult createTemplate(final JsonCommand command);
|
||||
|
||||
CommandProcessingResult updateTemplate(final Long templateId, final JsonCommand command);
|
||||
|
||||
CommandProcessingResult removeTemplate(final Long templateId);
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
package org.mifosplatform.template.service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.net.Authenticator;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.net.URL;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper;
|
||||
import org.mifosplatform.template.domain.Template;
|
||||
import org.mifosplatform.template.domain.TemplateFunctions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.github.mustachejava.DefaultMustacheFactory;
|
||||
import com.github.mustachejava.Mustache;
|
||||
import com.github.mustachejava.MustacheFactory;
|
||||
|
||||
@Service
|
||||
public class TemplateMergeService {
|
||||
|
||||
private final FromJsonHelper fromApiJsonHelper;
|
||||
private Map<String, Object> scopes;
|
||||
|
||||
@Autowired
|
||||
public TemplateMergeService(final FromJsonHelper fromApiJsonHelper) {
|
||||
this.fromApiJsonHelper = fromApiJsonHelper;
|
||||
}
|
||||
|
||||
public String compile(final Template template, final Map<String, Object> scopes) throws MalformedURLException, IOException {
|
||||
|
||||
this.scopes = scopes;
|
||||
this.scopes.put("static", new TemplateFunctions());
|
||||
|
||||
final MustacheFactory mf = new DefaultMustacheFactory();
|
||||
final Mustache mustache = mf.compile(new StringReader(template.getText()), template.getName());
|
||||
|
||||
final Map<String, Object> mappers = getCompiledMapFromMappers(template.getMappersAsMap());
|
||||
this.scopes.putAll(mappers);
|
||||
|
||||
final StringWriter stringWriter = new StringWriter();
|
||||
mustache.execute(stringWriter, this.scopes);
|
||||
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
private Map<String, Object> getCompiledMapFromMappers(final Map<String, String> data) {
|
||||
|
||||
final MustacheFactory mf = new DefaultMustacheFactory();
|
||||
|
||||
if (data != null) {
|
||||
for (final Map.Entry<String, String> entry : data.entrySet()) {
|
||||
|
||||
final Mustache mappersMustache = mf.compile(new StringReader(entry.getValue()), "");
|
||||
final StringWriter stringWriter = new StringWriter();
|
||||
|
||||
mappersMustache.execute(stringWriter, this.scopes);
|
||||
String url = stringWriter.toString();
|
||||
if (!url.startsWith("http")) {
|
||||
url = this.scopes.get("BASE_URI") + url;
|
||||
}
|
||||
System.out.println("URL: " + url);
|
||||
try {
|
||||
this.scopes.put(entry.getKey(), getMapFromUrl(url));
|
||||
} catch (final MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.scopes;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> getMapFromUrl(final String url) throws MalformedURLException, IOException {
|
||||
|
||||
final HttpURLConnection connection = getConnection(url);
|
||||
|
||||
final String response = getStringFromInputStream(connection.getInputStream());
|
||||
HashMap<String, Object> result = new HashMap<String, Object>();
|
||||
System.out.println("CONTENT_TYPE" + connection.getContentType());
|
||||
if (connection.getContentType().equals("text/plain")) {
|
||||
result.put("src", response);
|
||||
} else {
|
||||
result = new ObjectMapper().readValue(response, HashMap.class);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private HttpURLConnection getConnection(final String url) {
|
||||
|
||||
final String name = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
final String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
|
||||
|
||||
Authenticator.setDefault(new Authenticator() {
|
||||
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(name, password.toCharArray());
|
||||
}
|
||||
});
|
||||
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
TrustModifier.relaxHostChecking(connection);
|
||||
|
||||
connection.setDoInput(true);
|
||||
|
||||
} catch (final MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (final KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (final KeyStoreException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
private static String getStringFromInputStream(final InputStream is) {
|
||||
|
||||
BufferedReader br = null;
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
String line;
|
||||
try {
|
||||
|
||||
br = new BufferedReader(new InputStreamReader(is));
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (br != null) {
|
||||
try {
|
||||
br.close();
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.mifosplatform.template.service;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class TrustModifier {
|
||||
|
||||
private static final TrustingHostnameVerifier TRUSTING_HOSTNAME_VERIFIER = new TrustingHostnameVerifier();
|
||||
private static SSLSocketFactory factory;
|
||||
|
||||
/**
|
||||
* Call this with any HttpURLConnection, and it will modify the trust
|
||||
* settings if it is an HTTPS connection.
|
||||
*/
|
||||
public static void relaxHostChecking(final HttpURLConnection conn) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
|
||||
|
||||
if (conn instanceof HttpsURLConnection) {
|
||||
final HttpsURLConnection httpsConnection = (HttpsURLConnection) conn;
|
||||
final SSLSocketFactory factory = prepFactory(httpsConnection);
|
||||
httpsConnection.setSSLSocketFactory(factory);
|
||||
httpsConnection.setHostnameVerifier(TRUSTING_HOSTNAME_VERIFIER);
|
||||
}
|
||||
}
|
||||
|
||||
static synchronized SSLSocketFactory prepFactory(final HttpsURLConnection httpsConnection) throws NoSuchAlgorithmException,
|
||||
KeyStoreException, KeyManagementException {
|
||||
|
||||
if (factory == null) {
|
||||
final SSLContext ctx = SSLContext.getInstance("TLS");
|
||||
ctx.init(null, new TrustManager[] { new AlwaysTrustManager() }, null);
|
||||
factory = ctx.getSocketFactory();
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
||||
private static final class TrustingHostnameVerifier implements HostnameVerifier {
|
||||
|
||||
@Override
|
||||
public boolean verify(final String hostname, final SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static class AlwaysTrustManager implements X509TrustManager {
|
||||
|
||||
@Override
|
||||
public void checkClientTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,8 @@
|
||||
<provider>org.hibernate.ejb.HibernatePersistence</provider>
|
||||
<properties>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
|
||||
<!-- <property name="hibernate.hbm2ddl.auto" value="update"/> -->
|
||||
<property name="hibernate.jdbc.batch_size" value="100" />
|
||||
<property name="hibernate.order_inserts" value="true" />
|
||||
<!-- <property name = "hibernate.show_sql" value = "true" /> -->
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
@@ -36,7 +36,10 @@
|
||||
org.mifosplatform.portfolio.savingsaccount.*,
|
||||
org.mifosplatform.portfolio.*,
|
||||
org.mifosplatform.useradministration.*,
|
||||
org.mifosplatform.mix.*">
|
||||
org.mifosplatform.mix.*,
|
||||
org.mifosplatform.template.*,
|
||||
org.mifosplatform.template.service.*,
|
||||
org.mifosplatform.useradministration.*">
|
||||
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
|
||||
</context:component-scan>
|
||||
|
||||
@@ -51,6 +54,7 @@
|
||||
<jpa:repositories base-package="org.mifosplatform.portfolio.*" />
|
||||
<jpa:repositories base-package="org.mifosplatform.mix.domain" />
|
||||
<jpa:repositories base-package="org.mifosplatform.scheduledjobs.domain" />
|
||||
<jpa:repositories base-package="org.mifosplatform.template.domain" />
|
||||
|
||||
<import resource="infrastructure.xml" />
|
||||
|
||||
|
||||
@@ -29,12 +29,6 @@
|
||||
<beans:property name="realmName" value="Mifos Platform API" />
|
||||
</beans:bean>
|
||||
|
||||
<!--
|
||||
<beans:bean id="basicAuthenticationProcessingFilter" class="org.mifosplatform.infrastructure.security.filter.TenantAwareBasicAuthenticationFilter" depends-on="cacheManager">
|
||||
<beans:constructor-arg ref="authenticationManager" />
|
||||
<beans:constructor-arg ref="basicAuthenticationEntryPoint" />
|
||||
</beans:bean>
|
||||
-->
|
||||
<beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
|
||||
<beans:constructor-arg value="256"/>
|
||||
</beans:bean>
|
||||
@@ -49,7 +43,7 @@
|
||||
<beans:property name="saltSource" ref="saltSource" />
|
||||
</beans:bean>
|
||||
|
||||
<authentication-manager alias="authenticationManager">
|
||||
<authentication-manager alias="authenticationManager" erase-credentials="false">
|
||||
<authentication-provider ref="customAuthenticationProvider" />
|
||||
</authentication-manager>
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user