Skip to content

Commit

Permalink
#151 - csv bulk upload added.
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Jan 18, 2024
1 parent db4fcf7 commit daeba26
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 16 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.16.1</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>


<dependency>
Expand Down Expand Up @@ -252,4 +262,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.homihq.db2rest.rest.create;

import com.homihq.db2rest.exception.GenericDataAccessException;
import com.homihq.db2rest.rest.create.bulk.DataProcessor;
import com.homihq.db2rest.rest.create.dto.CreateBulkResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequiredArgsConstructor
public class BulkCreateController {

private final CreateService createService;

private final List<DataProcessor> dataProcessors;

@ResponseStatus(HttpStatus.CREATED)
@PostMapping (value= "/{tableName}/bulk",
consumes = {"application/json", "text/csv"}
)
public CreateBulkResponse save(@PathVariable String tableName,
@RequestHeader(name = "Content-Profile" , required = false) String schemaName,
@RequestParam(name = "tsid", required = false) String tsid,
@RequestParam(name = "tsidType", required = false, defaultValue = "number") String tsidType,
HttpServletRequest request) throws Exception{

DataProcessor dataProcessor = dataProcessors.stream()
.filter(d -> d.handle(request.getContentType()))
.findFirst().orElseThrow(() -> new GenericDataAccessException("Unable to process content type : "
+ request.getContentType()));

List<Map<String,Object>> data =
dataProcessor.getData(request.getInputStream());

log.info("### data -> {}", data);

int [] rows =
createService.saveBulk(schemaName, tableName, data, tsid, tsidType);

return new CreateBulkResponse(rows);
}
}
13 changes: 0 additions & 13 deletions src/main/java/com/homihq/db2rest/rest/create/CreateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,4 @@ public CreateResponse save(@PathVariable String tableName,
return new CreateResponse(rows);
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping ( "/{tableName}/bulk")
public CreateBulkResponse saveBulk(@PathVariable String tableName,
@RequestHeader(name = "Content-Profile") String schemaName,
@RequestBody List<Map<String,Object>> data,
@RequestParam(name = "tsid", required = false) String tsid,
@RequestParam(name = "tsidType", required = false, defaultValue = "number") String tsidType) {

int [] rows =
createService.saveBulk(schemaName, tableName, data, tsid, tsidType);

return new CreateBulkResponse(rows);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.homihq.db2rest.rest.create.bulk;


import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Component
@Slf4j
public class CSVDataProcessor implements DataProcessor{

private final CsvMapper csvMapper = new CsvMapper();

@Override
public List<Map<String, Object>> getData(InputStream inputStream) throws Exception{


CsvSchema schema = CsvSchema.emptySchema().withHeader();

ObjectReader oReader = csvMapper.readerFor(Map.class).with(schema);
List<Map<String, Object>> data = new ArrayList<>();
try (Reader reader = new InputStreamReader(inputStream)) {
MappingIterator<Map<String, Object>> mi = oReader.readValues(reader);
while (mi.hasNext()) {
Map<String, Object> current = mi.next();
data.add(current);

}
}

return data;
}

@Override
public boolean handle(String contentType) {
return StringUtils.equalsIgnoreCase(contentType, "text/csv");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.homihq.db2rest.rest.create.bulk;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

public interface DataProcessor {

boolean handle(String contentType);

List<Map<String,Object>> getData(InputStream inputStream) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.homihq.db2rest.rest.create.bulk;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;
import java.util.Map;


@Component
@Slf4j
public class JSONDataProcessor implements DataProcessor{

private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public List<Map<String, Object>> getData(InputStream inputStream) throws Exception{

log.info("#### Json Data ###");
return objectMapper.readValue(inputStream, new TypeReference<List<Map<String, Object>>>(){});
}

@Override
public boolean handle(String contentType) {
return StringUtils.equalsIgnoreCase(contentType, "application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void createError() throws Exception {
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
"special_features" : "Commentaries",
"country" : "INDIA"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void createError() throws Exception {
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
"special_features" : "Commentaries",
"country" : "INDIA"
},
Expand Down

0 comments on commit daeba26

Please sign in to comment.