WIP associations parameter

This commit is contained in:
John Woodlock
2012-06-07 17:25:16 +10:00
parent bfe47b7354
commit 551cf85416
@@ -14,7 +14,6 @@ import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.map.ser.FilterProvider; import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter; import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider; import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;
import org.mifosng.platform.api.LoansApiResource;
import org.mifosng.platform.exceptions.PlatformInternalServerException; import org.mifosng.platform.exceptions.PlatformInternalServerException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -42,7 +41,6 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
String allowedFieldList, String selectedFields, String allowedFieldList, String selectedFields,
String associationFields, MultivaluedMap<String, String> queryParams) { String associationFields, MultivaluedMap<String, String> queryParams) {
logger.info("in Loan Date Convert Request: " + associationFields);
return convertRequestCommon(dataObject, filterName, allowedFieldList, return convertRequestCommon(dataObject, filterName, allowedFieldList,
selectedFields, associationFields, queryParams); selectedFields, associationFields, queryParams);
@@ -52,11 +50,11 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
String allowedFieldList, String selectedFields, String allowedFieldList, String selectedFields,
String associationFields, MultivaluedMap<String, String> queryParams) { String associationFields, MultivaluedMap<String, String> queryParams) {
// filterType E means Exclude : used where no restriction on the basic // filterType E means Exclude : has the effect of returning all fields
// fields in the java object and has the effect of returning all fields // in the dataObject but can only be used if selectedFields is "" and
// in the object // there is no fields query parameter
// filterType I mean Include : used when a specific list of fields is to // filterType I means Include : used when a specific list of fields is
// be returned. // to be returned.
String filterType = "E"; String filterType = "E";
String fieldList = ""; String fieldList = "";
@@ -69,28 +67,10 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
if (selectedFields.equals("")) if (selectedFields.equals(""))
fieldList = fields; fieldList = fields;
else { else {
Set<String> paramFieldsSet = new HashSet<String>(); Set<String> paramFieldsSet = createSetFromString(fields);
StringTokenizer st = new StringTokenizer(fields, ","); Set<String> selectedFieldsSet = createSetFromString(selectedFields);
while (st.hasMoreTokens()) { fieldList = createIncludedInStringList(paramFieldsSet,
paramFieldsSet.add(st.nextToken().trim()); selectedFieldsSet);
}
Set<String> selectedFieldsSet = new HashSet<String>();
st = new StringTokenizer(selectedFields, ",");
while (st.hasMoreTokens()) {
selectedFieldsSet.add(st.nextToken().trim());
}
Boolean first = true;
for (String paramField : paramFieldsSet) {
if (selectedFieldsSet.contains(paramField)) {
if (first) {
fieldList = paramField;
first = false;
} else {
fieldList += "," + paramField;
}
}
}
} }
} else { } else {
fieldList = selectedFields; fieldList = selectedFields;
@@ -98,39 +78,90 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
} }
if (isTrue(queryParams.getFirst("template"))) { logger.info("fieldList before template is: " + fieldList
if (filterType.equals("I")) + " filter type is: " + filterType);
fieldList += "," + allowedFieldList;
} else {
if (filterType.equals("E"))
fieldList = allowedFieldList;
}
if (isPassed(queryParams.getFirst("associations"))) { fieldList = updateListForTemplate(fieldList, allowedFieldList,
logger.info("is associations"); queryParams.getFirst("template"), filterType);
// assume ALL for moment
if (filterType.equals("I"))
fieldList += "," + associationFields;
logger.info("fieldList before assocs is: " + fieldList
+ " filter type is: " + filterType);
} else { fieldList = updateListForAssociations(fieldList, associationFields,
queryParams.getFirst("associations"), filterType);
logger.info("no associations"); logger.info("fieldList to be processed is: " + fieldList
if (filterType.equals("E")) { + " filter type is: " + filterType);
if (fieldList.equals(""))
fieldList = associationFields;
else
fieldList += "," + associationFields;
}
}
logger.info("fieldList to be process is: " + fieldList + " filter type is: " + filterType);
return convertDataObjectJSON(dataObject, filterName, filterType, return convertDataObjectJSON(dataObject, filterName, filterType,
fieldList, isTrue(queryParams.getFirst("pretty"))); fieldList, isTrue(queryParams.getFirst("pretty")));
} }
private String updateListForTemplate(String fieldList,
String allowedFieldList, String param, String filterType) {
if (isTrue(param)) {
if (filterType.equals("I")) {
return fieldList + "," + allowedFieldList;
}
return fieldList;
}
//No template query param provided
if (filterType.equals("E")) {
fieldList = allowedFieldList; //exclude
}
return fieldList;
}
private String updateListForAssociations(String fieldList,
String associationFields, String param, String filterType) {
if (isPassed(param)) {
logger.info("is associations");
if (param.equalsIgnoreCase("ALL")) {
if (filterType.equals("I")) {
return fieldList + "," + associationFields;
}
return fieldList;
}
Set<String> fullAssociationsSet = createSetFromString(associationFields);
Set<String> paramAssociationsSet = createSetFromString(param);
if (filterType.equals("I")) {
String selectedAssociationFields = createIncludedInStringList(
paramAssociationsSet, fullAssociationsSet);
return fieldList + "," + selectedAssociationFields;
}
String unSelectedAssociationFields = createNotIncludedInStringList(
paramAssociationsSet, fullAssociationsSet);
if (fieldList.equals("")) {
return unSelectedAssociationFields;
}
return fieldList + "," + unSelectedAssociationFields;
}
// No association parameter provided
// If Exclude - then add the list of associationFields to the original
// fieldList
// If filter is Include just return the original fieldList value
if (filterType.equals("E")) {
if (fieldList.equals("")) {
return associationFields;
}
return fieldList + "," + associationFields;
}
return fieldList;
}
private String convertDataObjectJSON(Object dataObject, String filterName, private String convertDataObjectJSON(Object dataObject, String filterName,
String filterType, String fields, boolean prettyOutput) { String filterType, String fields, boolean prettyOutput) {
@@ -276,6 +307,49 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
return filters; return filters;
} }
private Set<String> createSetFromString(String string) {
Set<String> set = new HashSet<String>();
StringTokenizer st = new StringTokenizer(string, ",");
while (st.hasMoreTokens()) {
set.add(st.nextToken().trim());
}
return set;
}
private String createIncludedInStringList(Set<String> candidates,
Set<String> matchAgainst) {
String stringList = "";
Boolean first = true;
for (String candidate : candidates) {
if (matchAgainst.contains(candidate)) {
if (first) {
stringList = candidate;
first = false;
} else {
stringList += "," + candidate;
}
}
}
return stringList;
}
private String createNotIncludedInStringList(Set<String> candidates,
Set<String> matchAgainst) {
String stringList = "";
Boolean first = true;
for (String candidate : candidates) {
if (!(matchAgainst.contains(candidate))) {
if (first) {
stringList = candidate;
first = false;
} else {
stringList += "," + candidate;
}
}
}
return stringList;
}
private Boolean isTrue(String param) { private Boolean isTrue(String param) {
if (param != null && param.equalsIgnoreCase("true")) if (param != null && param.equalsIgnoreCase("true"))
return true; return true;