-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
155 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/homihq/db2rest/rest/create/BulkCreateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/homihq/db2rest/rest/create/bulk/CSVDataProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/homihq/db2rest/rest/create/bulk/DataProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/homihq/db2rest/rest/create/bulk/JSONDataProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters