some comments around template implementation.

This commit is contained in:
Keith Woodlock
2013-09-23 16:16:18 +01:00
parent 72a0a547aa
commit 1ca11846b4
5 changed files with 46 additions and 50 deletions
@@ -45,13 +45,15 @@ import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Path("/templates")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@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 String RESOURCE_NAME_FOR_PERMISSION = "template";
private final PlatformSecurityContext context;
private final DefaultToApiJsonSerializer<Template> toApiJsonSerializer;
@@ -77,12 +79,12 @@ public class TemplatesApiResource {
}
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String getTemplates(@DefaultValue("-1") @QueryParam("typeId") final int typeId,
public String retrieveAll(@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);
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCE_NAME_FOR_PERMISSION);
// FIXME - we dont use the ORM when doing fetches - we write SQL and fetch through JDBC returning data to be serialized to JSON
List<Template> templates = new ArrayList<Template>();
if (typeId != -1 && entityId != -1) {
@@ -97,10 +99,9 @@ public class TemplatesApiResource {
@GET
@Path("template")
@Produces({ MediaType.APPLICATION_JSON })
public String getTemplatesByTemplate(@Context final UriInfo uriInfo) {
public String template(@Context final UriInfo uriInfo) {
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCEN_NAME_FOR_PERMISSION);
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCE_NAME_FOR_PERMISSION);
final TemplateData templateData = TemplateData.template();
@@ -108,14 +109,22 @@ public class TemplatesApiResource {
return this.templateDataApiJsonSerializer.serialize(settings, templateData, this.RESPONSE_TEMPLATES_DATA_PARAMETERS);
}
@POST
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);
}
@GET
@Path("{templateId}")
@Produces({ MediaType.APPLICATION_JSON })
public String getTemplate(@PathParam("templateId") final Long templateId, @Context final UriInfo uriInfo) {
public String retrieveOne(@PathParam("templateId") final Long templateId, @Context final UriInfo uriInfo) {
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCEN_NAME_FOR_PERMISSION);
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCE_NAME_FOR_PERMISSION);
final Template template = this.templateService.getById(templateId);
final Template template = this.templateService.findOneById(templateId);
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, template, this.RESPONSE_TEMPLATES_DATA_PARAMETERS);
@@ -123,25 +132,18 @@ public class TemplatesApiResource {
@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);
this.context.authenticatedUser().validateHasReadPermission(this.RESOURCE_NAME_FOR_PERMISSION);
final TemplateData template = TemplateData.template(this.templateService.getById(templateId));
final TemplateData template = TemplateData.template(this.templateService.findOneById(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();
@@ -153,7 +155,6 @@ public class TemplatesApiResource {
@DELETE
@Path("{templateId}")
@Produces({ MediaType.APPLICATION_JSON })
public String deleteTemplate(@PathParam("templateId") final Long templateId) {
final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteTemplate(templateId).build();
@@ -163,25 +164,13 @@ public class TemplatesApiResource {
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);
final Template template = this.templateService.findOneById(templateId);
@SuppressWarnings("unchecked")
final
@@ -202,5 +191,4 @@ public class TemplatesApiResource {
parametersMap.putAll(result);
return this.templateMergeService.compile(template, parametersMap);
}
}
}
@@ -14,6 +14,7 @@ import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.springframework.data.jpa.domain.AbstractPersistable;
@@ -44,23 +45,27 @@ public class Template extends AbstractPersistable<Long> {
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.name = StringUtils.defaultIfEmpty(name, null);
this.entity = entity;
this.type = type;
this.text = text;
this.text = StringUtils.defaultIfEmpty(text, null);
this.mappers = mappers;
}
protected Template() {}
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()));
@@ -116,4 +121,4 @@ public class Template extends AbstractPersistable<Long> {
public void setText(final String text) {
this.text = text;
}
}
}
@@ -24,7 +24,7 @@ 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_MAPPERS = "mappers";
private static final String PROPERTY_ENTITY = "entity";
private static final String PROPERTY_TYPE = "type";
@@ -37,7 +37,7 @@ public class JpaTemplateDomainService implements TemplateDomainService {
}
@Override
public Template getById(final Long id) {
public Template findOneById(final Long id) {
final Template template = this.templateRepository.findOne(id);
if (template == null) { throw new TemplateNotFoundException(id); }
return template;
@@ -46,6 +46,8 @@ public class JpaTemplateDomainService implements TemplateDomainService {
@Transactional
@Override
public CommandProcessingResult createTemplate(final JsonCommand command) {
// FIXME - no validation here of the data in the command object, is name, text populated etc
// FIXME - handle cases where data integrity constraints are fired from database when saving.
final Template template = Template.fromJson(command);
this.templateRepository.saveAndFlush(template);
@@ -55,8 +57,10 @@ public class JpaTemplateDomainService implements TemplateDomainService {
@Transactional
@Override
public CommandProcessingResult updateTemplate(final Long templateId, final JsonCommand command) {
final Template template = getById(templateId);
System.out.println("COMM: " + command);
// FIXME - no validation here of the data in the command object, is name, text populated etc
// FIXME - handle cases where data integrity constraints are fired from database when saving.
final Template template = findOneById(templateId);
template.setName(command.stringValueOfParameterNamed(PROPERTY_NAME));
template.setText(command.stringValueOfParameterNamed(PROPERTY_TEXT));
template.setEntity(TemplateEntity.values()[command.integerValueSansLocaleOfParameterNamed(PROPERTY_ENTITY)]);
@@ -78,13 +82,14 @@ public class JpaTemplateDomainService implements TemplateDomainService {
@Transactional
@Override
public CommandProcessingResult removeTemplate(final Long templateId) {
final Template template = getById(templateId);
final Template template = findOneById(templateId);
this.templateRepository.delete(template);
return new CommandProcessingResultBuilder().withEntityId(templateId).build();
}
@Transactional
@Override
public Template updateTemplate(final Template template) {
return this.templateRepository.saveAndFlush(template);
@@ -14,7 +14,7 @@ public interface TemplateDomainService {
List<Template> getAllByEntityAndType(TemplateEntity entity, TemplateType type);
Template getById(Long id);
Template findOneById(Long id);
Template updateTemplate(Template template);
@@ -72,7 +72,6 @@ public class TemplateMergeService {
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) {
@@ -92,7 +91,6 @@ public class TemplateMergeService {
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 {
@@ -150,7 +148,7 @@ public class TemplateMergeService {
}
} catch (final IOException e) {
e.printStackTrace();
// FIXME - HANDLE THIS CASE
} finally {
if (br != null) {
try {