-
Notifications
You must be signed in to change notification settings - Fork 15
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
1 parent
0ebed59
commit ecc4d51
Showing
6 changed files
with
178 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"Name": "Users", | ||
"Description": "The users of the system" | ||
}, | ||
{ | ||
"Name": "Moderators", | ||
"Description": "The mods of the system" | ||
} | ||
] |
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
66 changes: 66 additions & 0 deletions
66
modules/global/src/de/diedavids/cuba/dataimport/converter/JsonImportDataConverter.groovy
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,66 @@ | ||
package de.diedavids.cuba.dataimport.converter | ||
|
||
import de.diedavids.cuba.dataimport.dto.DataRow | ||
import de.diedavids.cuba.dataimport.dto.DataRowImpl | ||
import de.diedavids.cuba.dataimport.dto.ImportData | ||
import de.diedavids.cuba.dataimport.dto.ImportDataImpl | ||
import groovy.json.JsonSlurper | ||
|
||
class JsonImportDataConverter implements ImportDataConverter { | ||
@Override | ||
ImportData convert(String content) { | ||
def result = new ImportDataImpl() | ||
|
||
def json = new JsonSlurper().parseText(content) | ||
|
||
|
||
json.each { | ||
result.columns = new ArrayList(it.keySet()) | ||
addToTableData(result, it) | ||
} | ||
|
||
result | ||
} | ||
|
||
private DataRow addToTableData(ImportDataImpl importData, Map row) { | ||
def dataRow = DataRowImpl.ofMap(row) | ||
importData.rows << dataRow | ||
dataRow | ||
} | ||
|
||
@Override | ||
ImportData convert(File file) { | ||
convert(file.text) | ||
} | ||
|
||
/* | ||
@Override | ||
ImportData convert(String content) { | ||
def result = new ImportDataImpl() | ||
def csvRows = parseCSV(content) | ||
csvRows.each { PropertyMapper row -> | ||
DataRow dataRow = addToTableData(result, row) | ||
result.columns = dataRow.columnNames | ||
} | ||
result | ||
} | ||
@Override | ||
ImportData convert(File file) { | ||
convert(file.text) | ||
} | ||
private Iterator parseCSV(String content) { | ||
new CsvParser().parse(content) | ||
} | ||
private DataRow addToTableData(ImportDataImpl importData, PropertyMapper row) { | ||
def dataRow = DataRowImpl.ofMap(row.toMap()) | ||
importData.rows << dataRow | ||
dataRow | ||
} | ||
*/ | ||
} |
46 changes: 46 additions & 0 deletions
46
...les/global/test/de/diedavids/cuba/dataimport/converter/JsonImportDataConverterSpec.groovy
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,46 @@ | ||
package de.diedavids.cuba.dataimport.converter | ||
|
||
import de.diedavids.cuba.dataimport.dto.ImportData | ||
import spock.lang.Specification | ||
|
||
class JsonImportDataConverterSpec extends Specification { | ||
private sut | ||
private String JSON_STRING = '''[ | ||
{ | ||
"Name": "Mark", | ||
"Lastname": "Andersson" | ||
}, | ||
{ | ||
"Name": "Pete", | ||
"Lastname": "Hansen" | ||
} | ||
]''' | ||
|
||
|
||
void setup() { | ||
sut = new JsonImportDataConverter() | ||
} | ||
|
||
def "convert contains the correct amount of columns"() { | ||
|
||
when: | ||
ImportData result = sut.convert(JSON_STRING) | ||
then: | ||
result.columns.size() == 2 | ||
} | ||
|
||
def "convert contains two DataRows"() { | ||
when: | ||
ImportData result = sut.convert(JSON_STRING) | ||
then: | ||
result.rows.size() == 2 | ||
} | ||
|
||
def "convert contains the correct values for the DataRows"() { | ||
when: | ||
ImportData result = sut.convert(JSON_STRING) | ||
then: | ||
result.rows[0].Name == 'Mark' | ||
result.rows[0].Lastname == 'Andersson' | ||
} | ||
} |
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