-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from statisticsnorway/feature/csv-fields
Feature/csv fields
- Loading branch information
Showing
18 changed files
with
271 additions
and
40 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
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
40 changes: 40 additions & 0 deletions
40
klass-api/src/main/java/no/ssb/klass/api/controllers/validators/CsvFieldsValidator.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,40 @@ | ||
package no.ssb.klass.api.controllers.validators; | ||
|
||
import com.fasterxml.jackson.dataformat.csv.CsvMapper; | ||
import com.fasterxml.jackson.dataformat.csv.CsvSchema; | ||
import no.ssb.klass.api.dto.CodeChangeItem; | ||
import no.ssb.klass.api.dto.CodeItem; | ||
import no.ssb.klass.api.dto.CorrespondenceItem; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
@Component | ||
public class CsvFieldsValidator { | ||
|
||
private final CsvSchema correspondenceItemSchema = new CsvMapper().schemaFor(CorrespondenceItem.RangedCorrespondenceItem.class); | ||
private final CsvSchema codeItemSchema = new CsvMapper().schemaFor(CodeItem.class); | ||
private final CsvSchema codeChangeItemSchema = new CsvMapper().schemaFor(CodeChangeItem.class); | ||
|
||
public void validateFieldsCorrespondenceItem(List<String> csvFields) { | ||
List<String> fieldsNotFound = csvFields.stream().filter(s -> correspondenceItemSchema.column(s) == null).collect(toList()); | ||
if(!fieldsNotFound.isEmpty()) { | ||
throw new IllegalArgumentException("CorrespondenceList does not contain the following field(s): " + String.join(",", fieldsNotFound)); | ||
} | ||
} | ||
public void validateFieldsCodeItem(List<String> csvFields) { | ||
List<String> fieldsNotFound = csvFields.stream().filter(s -> codeItemSchema.column(s) == null).collect(toList()); | ||
if(!fieldsNotFound.isEmpty()) { | ||
throw new IllegalArgumentException("CorrespondenceList does not contain the following field(s): " + String.join(",", fieldsNotFound)); | ||
} | ||
} | ||
public void validateFieldsChangeItemSchema(List<String> csvFields) { | ||
List<String> fieldsNotFound = csvFields.stream().filter(s -> codeChangeItemSchema.column(s) == null).collect(toList()); | ||
if(!fieldsNotFound.isEmpty()) { | ||
throw new IllegalArgumentException("CorrespondenceList does not contain the following field(s): " + String.join(",", fieldsNotFound)); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.