Skip to content

Commit

Permalink
added JSON support (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodavid committed Apr 28, 2018
1 parent 0ebed59 commit ecc4d51
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 1 deletion.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Currently the following file-types are supported:

* Excel `.xlsx`
* comma separated values `.csv`
* JSON `.json`

In order to configure various import options, there is a UI based configuration possibility to define

Expand Down Expand Up @@ -109,6 +110,60 @@ will be triggered. Afterwards the user will see a summary of how many entities w
![import-wizard-step-5](https://github.com/mariodavid/cuba-component-data-import/blob/master/img/import-wizard-step-5.png)


## Supported file types

Multiple filetypes are supported by this application component. Information and requirements
for certain file types will be described below.

Example files can be found in the [example-data](https://github.com/mariodavid/cuba-component-data-import/blob/master/example-data) subdirectory.

### Excel - .xlsx

For the Excel files the first row has to be the column names.
Unnamed columns are not supported currently.


Example Excel file:

| Name | Description |
| ---------------- | ---------------------- |
| Users | This will be the users |
| Managers | The moderators |


### CSV - .csv

For the CSV files the first row has to be the column names.
Unnamed columns are not supported currently.

Example CSV file:
```
"Name","Description"
"Users", "This will be the users"
"Moderators", "The Moderators"
```

### JSON - .json

For the JSON files it is required to be a JSON array, where each entry in this array
is itself a JSON object, which should get imported as an entity instance.


Example JSON file:
```
[
{
"Name": "Users",
"Description": "The users of the system"
},
{
"Name": "Moderators",
"Description": "The mods of the system"
}
]
```


## Import Configuration

The basis for the import wizard is the `Import Configuration`. It is also available via `Administration > Data Import > Import Configuration`.
Expand Down
10 changes: 10 additions & 0 deletions example-data/roles.json
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"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DataConverterFactory {
switch (fileDescriptor.extension) {
case 'xlsx': return new ExcelImportDataConverter()
case 'csv': return new CsvImportDataConverter()
case 'json': return new JsonImportDataConverter()
default: throw new FileNotSupportedException()
}
}
Expand Down
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
}
*/
}
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'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
spacing="true">
<buttonsPanel align="TOP_CENTER">
<upload id="importFileUploadBtn"
accept=".csv,.xlsx"
dropZone="dropZone"
uploadButtonCaption="msg://uploadBtnCaption"
uploadButtonDescription="msg://uploadBtnDescription"
Expand Down

0 comments on commit ecc4d51

Please sign in to comment.