fix prob with linefeeds on csv export & allow reports to have normal json output
This commit is contained in:
+4
@@ -125,6 +125,10 @@ public class ApiParameterHelper {
|
|||||||
return genericResultSet;
|
return genericResultSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean genericResultSetPassed(MultivaluedMap<String, String> queryParams) {
|
||||||
|
return queryParams.getFirst("genericResultSet") != null;
|
||||||
|
}
|
||||||
|
|
||||||
public static String sqlEncodeString(String str) {
|
public static String sqlEncodeString(String str) {
|
||||||
String singleQuote = "'";
|
String singleQuote = "'";
|
||||||
String twoSingleQuotes = "''";
|
String twoSingleQuotes = "''";
|
||||||
|
|||||||
+16
-3
@@ -26,6 +26,7 @@ import javax.ws.rs.core.UriInfo;
|
|||||||
import org.mifosplatform.infrastructure.core.api.ApiParameterHelper;
|
import org.mifosplatform.infrastructure.core.api.ApiParameterHelper;
|
||||||
import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer;
|
import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer;
|
||||||
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
|
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
|
||||||
|
import org.mifosplatform.infrastructure.dataqueries.service.GenericDataService;
|
||||||
import org.mifosplatform.infrastructure.dataqueries.service.ReadReportingService;
|
import org.mifosplatform.infrastructure.dataqueries.service.ReadReportingService;
|
||||||
import org.mifosplatform.infrastructure.security.exception.NoAuthorizationException;
|
import org.mifosplatform.infrastructure.security.exception.NoAuthorizationException;
|
||||||
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
||||||
@@ -42,15 +43,16 @@ public class ReportsApiResource {
|
|||||||
private final PlatformSecurityContext context;
|
private final PlatformSecurityContext context;
|
||||||
private final ToApiJsonSerializer<GenericResultsetData> toApiJsonSerializer;
|
private final ToApiJsonSerializer<GenericResultsetData> toApiJsonSerializer;
|
||||||
private final ReadReportingService readExtraDataAndReportingService;
|
private final ReadReportingService readExtraDataAndReportingService;
|
||||||
|
private final GenericDataService genericDataService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ReportsApiResource(final PlatformSecurityContext context, final ReadReportingService readExtraDataAndReportingService,
|
public ReportsApiResource(final PlatformSecurityContext context, final ReadReportingService readExtraDataAndReportingService,
|
||||||
final ToApiJsonSerializer<GenericResultsetData> toApiJsonSerializer) {
|
final GenericDataService genericDataService, final ToApiJsonSerializer<GenericResultsetData> toApiJsonSerializer) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.readExtraDataAndReportingService = readExtraDataAndReportingService;
|
this.readExtraDataAndReportingService = readExtraDataAndReportingService;
|
||||||
|
this.genericDataService = genericDataService;
|
||||||
this.toApiJsonSerializer = toApiJsonSerializer;
|
this.toApiJsonSerializer = toApiJsonSerializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Consumes({ MediaType.APPLICATION_JSON })
|
@Consumes({ MediaType.APPLICATION_JSON })
|
||||||
@Produces({ MediaType.APPLICATION_JSON, "application/x-msdownload" })
|
@Produces({ MediaType.APPLICATION_JSON, "application/x-msdownload" })
|
||||||
@@ -130,7 +132,18 @@ public class ReportsApiResource {
|
|||||||
GenericResultsetData result = this.readExtraDataAndReportingService.retrieveGenericResultset(reportName, parameterTypeValue,
|
GenericResultsetData result = this.readExtraDataAndReportingService.retrieveGenericResultset(reportName, parameterTypeValue,
|
||||||
reportParams);
|
reportParams);
|
||||||
|
|
||||||
final String json = this.toApiJsonSerializer.serializePretty(prettyPrint, result);
|
String json = "";
|
||||||
|
final boolean genericResultSetIsPassed = ApiParameterHelper.genericResultSetPassed(uriInfo.getQueryParameters());
|
||||||
|
final boolean genericResultSet = ApiParameterHelper.genericResultSet(uriInfo.getQueryParameters());
|
||||||
|
if (genericResultSetIsPassed) {
|
||||||
|
if (genericResultSet) {
|
||||||
|
json = this.toApiJsonSerializer.serializePretty(prettyPrint, result);
|
||||||
|
} else {
|
||||||
|
json = this.genericDataService.generateJsonFromGenericResultsetData(result);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
json = this.toApiJsonSerializer.serializePretty(prettyPrint, result);
|
||||||
|
}
|
||||||
|
|
||||||
return Response.ok().entity(json).type(MediaType.APPLICATION_JSON).build();
|
return Response.ok().entity(json).type(MediaType.APPLICATION_JSON).build();
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-10
@@ -185,28 +185,42 @@ public class GenericDataServiceImpl implements GenericDataService {
|
|||||||
|
|
||||||
writer.append(doubleQuote + columnHeaders.get(j).getColumnName() + doubleQuote + ": ");
|
writer.append(doubleQuote + columnHeaders.get(j).getColumnName() + doubleQuote + ": ");
|
||||||
currColType = columnHeaders.get(j).getColumnDisplayType();
|
currColType = columnHeaders.get(j).getColumnDisplayType();
|
||||||
|
final String colType = columnHeaders.get(j).getColumnType();
|
||||||
|
if (currColType == null && colType.equalsIgnoreCase("INT")) {
|
||||||
|
currColType = "INTEGER";
|
||||||
|
}
|
||||||
|
if (currColType == null && colType.equalsIgnoreCase("VARCHAR")) {
|
||||||
|
currColType = "VARCHAR";
|
||||||
|
}
|
||||||
|
if (currColType == null && colType.equalsIgnoreCase("DATE")) {
|
||||||
|
currColType = "DATE";
|
||||||
|
}
|
||||||
currVal = row.get(j);
|
currVal = row.get(j);
|
||||||
if (currVal != null) {
|
if (currVal != null && currColType != null) {
|
||||||
if (currColType.equals("DECIMAL") || currColType.equals("INTEGER"))
|
if (currColType.equals("DECIMAL") || currColType.equals("INTEGER")) {
|
||||||
writer.append(currVal);
|
writer.append(currVal);
|
||||||
else {
|
} else {
|
||||||
if (currColType.equals("DATE")) {
|
if (currColType.equals("DATE")) {
|
||||||
LocalDate localDate = new LocalDate(currVal);
|
LocalDate localDate = new LocalDate(currVal);
|
||||||
writer.append("[" + localDate.getYear() + ", " + localDate.getMonthOfYear() + ", " + localDate.getDayOfMonth()
|
writer.append("[" + localDate.getYear() + ", " + localDate.getMonthOfYear() + ", " + localDate.getDayOfMonth()
|
||||||
+ "]");
|
+ "]");
|
||||||
} else
|
} else {
|
||||||
writer.append(doubleQuote + replace(currVal, doubleQuote, slashDoubleQuote) + doubleQuote);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
writer.append("null");
|
|
||||||
|
|
||||||
|
writer.append(doubleQuote + replace(currVal, doubleQuote,slashDoubleQuote) + doubleQuote);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
writer.append("null");
|
||||||
|
}
|
||||||
if (j < (rSize - 1)) writer.append(",\n");
|
if (j < (rSize - 1)) writer.append(",\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < (data.size() - 1))
|
if (i < (data.size() - 1)) {
|
||||||
writer.append("},");
|
writer.append("},");
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
writer.append("}");
|
writer.append("}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.append("\n]");
|
writer.append("\n]");
|
||||||
|
|||||||
+5
-2
@@ -108,7 +108,7 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static StringBuffer generateCsvFileBuffer(final GenericResultsetData result) {
|
private StringBuffer generateCsvFileBuffer(final GenericResultsetData result) {
|
||||||
StringBuffer writer = new StringBuffer();
|
StringBuffer writer = new StringBuffer();
|
||||||
|
|
||||||
List<ResultsetColumnHeader> columnHeaders = result.getColumnHeaders();
|
List<ResultsetColumnHeader> columnHeaders = result.getColumnHeaders();
|
||||||
@@ -126,6 +126,8 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
|||||||
// String currCol;
|
// String currCol;
|
||||||
String currColType;
|
String currColType;
|
||||||
String currVal;
|
String currVal;
|
||||||
|
String doubleQuote = "\"";
|
||||||
|
String twoDoubleQuotes = doubleQuote + doubleQuote;
|
||||||
logger.info("NO. of Rows: " + data.size());
|
logger.info("NO. of Rows: " + data.size());
|
||||||
for (int i = 0; i < data.size(); i++) {
|
for (int i = 0; i < data.size(); i++) {
|
||||||
row = data.get(i).getRow();
|
row = data.get(i).getRow();
|
||||||
@@ -139,7 +141,8 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
|||||||
|| currColType.equals("SMALLINT") || currColType.equals("INT"))
|
|| currColType.equals("SMALLINT") || currColType.equals("INT"))
|
||||||
writer.append(currVal);
|
writer.append(currVal);
|
||||||
else
|
else
|
||||||
writer.append('"' + currVal + '"');
|
writer.append('"' + genericDataService.replace(currVal, doubleQuote, twoDoubleQuotes) + '"');
|
||||||
|
|
||||||
}
|
}
|
||||||
if (j < (rSize - 1)) writer.append(",");
|
if (j < (rSize - 1)) writer.append(",");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user