WIP fields parameter
This commit is contained in:
@@ -18,14 +18,11 @@ import javax.ws.rs.core.Response;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.mifosng.data.EntityIdentifier;
|
||||
import org.mifosng.data.OfficeData;
|
||||
import org.mifosng.data.OfficeTemplateData;
|
||||
import org.mifosng.data.command.OfficeCommand;
|
||||
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
|
||||
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
|
||||
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
|
||||
import org.mifosng.platform.organisation.service.OfficeWritePlatformService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -35,8 +32,6 @@ import org.springframework.stereotype.Component;
|
||||
@Scope("singleton")
|
||||
public class OfficeApiResource {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(OfficeApiResource.class);
|
||||
|
||||
@Autowired
|
||||
private OfficeReadPlatformService readPlatformService;
|
||||
|
||||
@@ -45,45 +40,42 @@ public class OfficeApiResource {
|
||||
|
||||
@Autowired
|
||||
private ApiDataConversionService apiDataConversionService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private ApiJSONFormattingService jsonFormattingService;
|
||||
|
||||
@GET
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String retrieveOffices(@QueryParam("fields") String fields) {
|
||||
public String retrieveOffices(@QueryParam("fields") String fields,
|
||||
@QueryParam("pretty") String pretty) {
|
||||
|
||||
Collection<OfficeData> offices = this.readPlatformService
|
||||
.retrieveAllOffices();
|
||||
|
||||
Set<String> excludeFields = new HashSet<String>();
|
||||
excludeFields.add("officeTemplateData");
|
||||
|
||||
// can use parameters for turning this on/off etc.
|
||||
boolean prettyOutput = true;
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(offices, fields, excludeFields, prettyOutput);
|
||||
excludeFields.add("allowedParents");
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(offices,
|
||||
fields, excludeFields,
|
||||
this.jsonFormattingService.isTrue(pretty));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("template")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String retrieveOfficeTemplate() {
|
||||
public String retrieveOfficeTemplate(@QueryParam("pretty") String pretty) {
|
||||
|
||||
OfficeData officeData = new OfficeData();
|
||||
OfficeTemplateData officeTemplateData = this.readPlatformService
|
||||
OfficeData officeData = this.readPlatformService
|
||||
.retrieveNewOfficeTemplate();
|
||||
officeData.setOfficeTemplateData(officeTemplateData);
|
||||
|
||||
Set<String> excludeFields = new HashSet<String>();
|
||||
String includeFields = "officeTemplateData";
|
||||
|
||||
// can use parameters for turning this on/off etc.
|
||||
boolean prettyOutput = true;
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(officeData, includeFields, excludeFields, prettyOutput);
|
||||
String includeFields = "openingDate,allowedParents";
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(officeData,
|
||||
includeFields, excludeFields,
|
||||
this.jsonFormattingService.isTrue(pretty));
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -107,42 +99,30 @@ public class OfficeApiResource {
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String retreiveOffice(@PathParam("officeId") final Long officeId,
|
||||
@QueryParam("fields") String fields,
|
||||
@QueryParam("returnTemplateData") String returnTemplateData) {
|
||||
|
||||
Boolean isReturnTemplate = false;
|
||||
if (returnTemplateData != null
|
||||
&& returnTemplateData.equalsIgnoreCase("true")) {
|
||||
isReturnTemplate = true;
|
||||
}
|
||||
|
||||
Boolean isIncludeFields = false;
|
||||
if (!(fields == null || fields.equals(""))) {
|
||||
isIncludeFields = true;
|
||||
}
|
||||
|
||||
OfficeData office = this.readPlatformService.retrieveOffice(officeId);
|
||||
|
||||
if (isReturnTemplate) {
|
||||
OfficeTemplateData officeTemplateData = this.readPlatformService
|
||||
.retrieveExistingOfficeTemplate(officeId);
|
||||
office.setOfficeTemplateData(officeTemplateData);
|
||||
}
|
||||
@QueryParam("template") String template,
|
||||
@QueryParam("pretty") String pretty) {
|
||||
|
||||
String includeFields = fields;
|
||||
Set<String> excludeFields = new HashSet<String>();
|
||||
if (isIncludeFields) {
|
||||
if (isReturnTemplate) {
|
||||
includeFields += ",officeTemplateData";
|
||||
OfficeData office = this.readPlatformService.retrieveOffice(officeId);
|
||||
|
||||
if (this.jsonFormattingService.isTrue(template)) {
|
||||
office.setAllowedParents(this.readPlatformService
|
||||
.retrieveAllowedParents(officeId));
|
||||
|
||||
if (this.jsonFormattingService.isPassed(includeFields)) {
|
||||
includeFields += ",allowedParents";
|
||||
}
|
||||
|
||||
} else {
|
||||
if (!(isReturnTemplate)) {
|
||||
excludeFields.add("officeTemplateData");
|
||||
if (!(this.jsonFormattingService.isPassed(includeFields))) {
|
||||
excludeFields.add("allowedParents");
|
||||
}
|
||||
}
|
||||
|
||||
boolean prettyOutput = true;
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(office, includeFields, excludeFields, prettyOutput);
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(office,
|
||||
includeFields, excludeFields,
|
||||
this.jsonFormattingService.isTrue(pretty));
|
||||
}
|
||||
|
||||
@PUT
|
||||
|
||||
+4
@@ -7,4 +7,8 @@ public interface ApiJSONFormattingService {
|
||||
String convertDataObjectJSON(Object dataObject, String fields,
|
||||
Set<String> excludeFields, boolean prettyOutput);
|
||||
|
||||
Boolean isTrue(String param);
|
||||
|
||||
Boolean isPassed(String param);
|
||||
|
||||
}
|
||||
|
||||
+49
-11
@@ -20,10 +20,12 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(ApiJSONFormattingServiceImpl.class);
|
||||
|
||||
private final static Logger logger = LoggerFactory
|
||||
.getLogger(ApiJSONFormattingServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public String convertDataObjectJSON(Object dataObject, String fields, Set<String> excludeFields, boolean prettyOutput) {
|
||||
public String convertDataObjectJSON(Object dataObject, String fields,
|
||||
Set<String> excludeFields, boolean prettyOutput) {
|
||||
|
||||
try {
|
||||
String json = "";
|
||||
@@ -33,7 +35,8 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
|
||||
|
||||
if (fields == null || fields.equals("")) {
|
||||
filters = new SimpleFilterProvider().addFilter(myFilter,
|
||||
SimpleBeanPropertyFilter.serializeAllExcept(excludeFields));
|
||||
SimpleBeanPropertyFilter
|
||||
.serializeAllExcept(excludeFields));
|
||||
|
||||
} else {
|
||||
|
||||
@@ -43,18 +46,53 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
|
||||
}
|
||||
|
||||
filters = new SimpleFilterProvider().addFilter(myFilter,
|
||||
SimpleBeanPropertyFilter.filterOutAllExcept(includeFields));
|
||||
SimpleBeanPropertyFilter
|
||||
.filterOutAllExcept(includeFields));
|
||||
}
|
||||
|
||||
ObjectWriter jsonWriter = new ObjectMapper().writerWithDefaultPrettyPrinter();
|
||||
json = jsonWriter.withFilters(filters).writeValueAsString(dataObject);
|
||||
ObjectWriter jsonWriter = null;
|
||||
|
||||
if (prettyOutput) {
|
||||
jsonWriter = new ObjectMapper()
|
||||
.writerWithDefaultPrettyPrinter();
|
||||
} else {
|
||||
jsonWriter = new ObjectMapper().writer();
|
||||
}
|
||||
|
||||
json = jsonWriter.withFilters(filters).writeValueAsString(
|
||||
dataObject);
|
||||
return json;
|
||||
} catch (JsonGenerationException e) {
|
||||
throw new PlatformInternalServerException("error.msg.platform.json.generation", "An error occured whilst generating response from the platform server.", e.getMessage(), e.getStackTrace());
|
||||
throw new PlatformInternalServerException(
|
||||
"error.msg.platform.json.generation",
|
||||
"An error occured whilst generating response from the platform server.",
|
||||
e.getMessage(), e.getStackTrace());
|
||||
} catch (JsonMappingException e) {
|
||||
throw new PlatformInternalServerException("error.msg.platform.json.generation", "An error occured whilst generating response from the platform server.", e.getMessage(), e.getStackTrace());
|
||||
throw new PlatformInternalServerException(
|
||||
"error.msg.platform.json.generation",
|
||||
"An error occured whilst generating response from the platform server.",
|
||||
e.getMessage(), e.getStackTrace());
|
||||
} catch (IOException e) {
|
||||
throw new PlatformInternalServerException("error.msg.platform.json.generation", "An error occured whilst generating response from the platform server.", e.getMessage(), e.getStackTrace());
|
||||
throw new PlatformInternalServerException(
|
||||
"error.msg.platform.json.generation",
|
||||
"An error occured whilst generating response from the platform server.",
|
||||
e.getMessage(), e.getStackTrace());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isTrue(String param) {
|
||||
if (param != null && param.equalsIgnoreCase("true"))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isPassed(String param) {
|
||||
if (param == null || param.equals(""))
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -1,10 +1,10 @@
|
||||
package org.mifosng.platform.organisation.service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.mifosng.data.OfficeData;
|
||||
import org.mifosng.data.OfficeLookup;
|
||||
import org.mifosng.data.OfficeTemplateData;
|
||||
|
||||
public interface OfficeReadPlatformService {
|
||||
|
||||
@@ -14,7 +14,7 @@ public interface OfficeReadPlatformService {
|
||||
|
||||
OfficeData retrieveOffice(Long officeId);
|
||||
|
||||
OfficeTemplateData retrieveNewOfficeTemplate();
|
||||
|
||||
OfficeTemplateData retrieveExistingOfficeTemplate(Long officeId);
|
||||
OfficeData retrieveNewOfficeTemplate();
|
||||
|
||||
List<OfficeLookup> retrieveAllowedParents(Long officeId);
|
||||
}
|
||||
+7
-12
@@ -11,7 +11,6 @@ import javax.sql.DataSource;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.mifosng.data.OfficeData;
|
||||
import org.mifosng.data.OfficeLookup;
|
||||
import org.mifosng.data.OfficeTemplateData;
|
||||
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException;
|
||||
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||
import org.mifosng.platform.user.domain.AppUser;
|
||||
@@ -132,22 +131,22 @@ public class OfficeReadPlatformServiceImpl implements OfficeReadPlatformService
|
||||
}
|
||||
|
||||
@Override
|
||||
public OfficeTemplateData retrieveNewOfficeTemplate() {
|
||||
public OfficeData retrieveNewOfficeTemplate() {
|
||||
|
||||
context.authenticatedUser();
|
||||
|
||||
List<OfficeLookup> parentLookups = new ArrayList<OfficeLookup>(
|
||||
retrieveAllOfficesForLookup());
|
||||
|
||||
OfficeTemplateData officeTemplateData = new OfficeTemplateData();
|
||||
officeTemplateData.setAllowedParents(parentLookups);
|
||||
officeTemplateData.setDefaultOpeningDate(new LocalDate());
|
||||
OfficeData officeData = new OfficeData();
|
||||
officeData.setAllowedParents(parentLookups);
|
||||
officeData.setOpeningDate(new LocalDate());
|
||||
|
||||
return officeTemplateData;
|
||||
return officeData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OfficeTemplateData retrieveExistingOfficeTemplate(Long officeId) {
|
||||
public List<OfficeLookup> retrieveAllowedParents(Long officeId) {
|
||||
|
||||
context.authenticatedUser();
|
||||
|
||||
@@ -162,11 +161,7 @@ public class OfficeReadPlatformServiceImpl implements OfficeReadPlatformService
|
||||
}
|
||||
}
|
||||
|
||||
OfficeTemplateData officeTemplateData = new OfficeTemplateData();
|
||||
officeTemplateData.setAllowedParents(filterParentLookups);
|
||||
officeTemplateData.setDefaultOpeningDate(new LocalDate());
|
||||
|
||||
return officeTemplateData;
|
||||
return filterParentLookups;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user