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;
|
||||
}
|
||||
|
||||
public static boolean genericResultSetPassed(MultivaluedMap<String, String> queryParams) {
|
||||
return queryParams.getFirst("genericResultSet") != null;
|
||||
}
|
||||
|
||||
public static String sqlEncodeString(String str) {
|
||||
String singleQuote = "'";
|
||||
String twoSingleQuotes = "''";
|
||||
|
||||
+18
-5
@@ -26,6 +26,7 @@ import javax.ws.rs.core.UriInfo;
|
||||
import org.mifosplatform.infrastructure.core.api.ApiParameterHelper;
|
||||
import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer;
|
||||
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.security.exception.NoAuthorizationException;
|
||||
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
||||
@@ -42,15 +43,16 @@ public class ReportsApiResource {
|
||||
private final PlatformSecurityContext context;
|
||||
private final ToApiJsonSerializer<GenericResultsetData> toApiJsonSerializer;
|
||||
private final ReadReportingService readExtraDataAndReportingService;
|
||||
private final GenericDataService genericDataService;
|
||||
|
||||
@Autowired
|
||||
public ReportsApiResource(final PlatformSecurityContext context, final ReadReportingService readExtraDataAndReportingService,
|
||||
final ToApiJsonSerializer<GenericResultsetData> toApiJsonSerializer) {
|
||||
final GenericDataService genericDataService, final ToApiJsonSerializer<GenericResultsetData> toApiJsonSerializer) {
|
||||
this.context = context;
|
||||
this.readExtraDataAndReportingService = readExtraDataAndReportingService;
|
||||
this.genericDataService = genericDataService;
|
||||
this.toApiJsonSerializer = toApiJsonSerializer;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Consumes({ MediaType.APPLICATION_JSON })
|
||||
@Produces({ MediaType.APPLICATION_JSON, "application/x-msdownload" })
|
||||
@@ -130,11 +132,22 @@ public class ReportsApiResource {
|
||||
GenericResultsetData result = this.readExtraDataAndReportingService.retrieveGenericResultset(reportName, parameterTypeValue,
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
// CSV Export
|
||||
Map<String, String> reportParams = getReportParams(queryParams, false);
|
||||
StreamingOutput result = this.readExtraDataAndReportingService.retrieveReportCSV(reportName, parameterTypeValue, reportParams);
|
||||
|
||||
+24
-10
@@ -185,35 +185,49 @@ public class GenericDataServiceImpl implements GenericDataService {
|
||||
|
||||
writer.append(doubleQuote + columnHeaders.get(j).getColumnName() + doubleQuote + ": ");
|
||||
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);
|
||||
if (currVal != null) {
|
||||
if (currColType.equals("DECIMAL") || currColType.equals("INTEGER"))
|
||||
if (currVal != null && currColType != null) {
|
||||
if (currColType.equals("DECIMAL") || currColType.equals("INTEGER")) {
|
||||
writer.append(currVal);
|
||||
else {
|
||||
} else {
|
||||
if (currColType.equals("DATE")) {
|
||||
LocalDate localDate = new LocalDate(currVal);
|
||||
writer.append("[" + localDate.getYear() + ", " + localDate.getMonthOfYear() + ", " + localDate.getDayOfMonth()
|
||||
+ "]");
|
||||
} else
|
||||
writer.append(doubleQuote + replace(currVal, doubleQuote, slashDoubleQuote) + doubleQuote);
|
||||
} else {
|
||||
|
||||
writer.append(doubleQuote + replace(currVal, doubleQuote,slashDoubleQuote) + doubleQuote);
|
||||
}
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
writer.append("null");
|
||||
|
||||
}
|
||||
if (j < (rSize - 1)) writer.append(",\n");
|
||||
}
|
||||
|
||||
if (i < (data.size() - 1))
|
||||
if (i < (data.size() - 1)) {
|
||||
writer.append("},");
|
||||
else
|
||||
}
|
||||
else {
|
||||
writer.append("}");
|
||||
}
|
||||
}
|
||||
|
||||
writer.append("\n]");
|
||||
return writer.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void dbClose(Statement db_statement, Connection db_connection) {
|
||||
// logger.debug("dbClose");
|
||||
try {
|
||||
|
||||
+7
-4
@@ -63,7 +63,7 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
||||
private final static Logger logger = LoggerFactory.getLogger(ReadReportingServiceImpl.class);
|
||||
|
||||
private Boolean noPentaho = false;
|
||||
|
||||
|
||||
@Autowired
|
||||
public ReadReportingServiceImpl(final PlatformSecurityContext context) {
|
||||
// kick off pentaho reports server
|
||||
@@ -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();
|
||||
|
||||
List<ResultsetColumnHeader> columnHeaders = result.getColumnHeaders();
|
||||
@@ -126,6 +126,8 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
||||
// String currCol;
|
||||
String currColType;
|
||||
String currVal;
|
||||
String doubleQuote = "\"";
|
||||
String twoDoubleQuotes = doubleQuote + doubleQuote;
|
||||
logger.info("NO. of Rows: " + data.size());
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
row = data.get(i).getRow();
|
||||
@@ -139,7 +141,8 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
||||
|| currColType.equals("SMALLINT") || currColType.equals("INT"))
|
||||
writer.append(currVal);
|
||||
else
|
||||
writer.append('"' + currVal + '"');
|
||||
writer.append('"' + genericDataService.replace(currVal, doubleQuote, twoDoubleQuotes) + '"');
|
||||
|
||||
}
|
||||
if (j < (rSize - 1)) writer.append(",");
|
||||
}
|
||||
@@ -148,7 +151,7 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
||||
|
||||
return writer;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public GenericResultsetData retrieveGenericResultset(final String name, final String type, final Map<String, String> queryParams) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user