-
Notifications
You must be signed in to change notification settings - Fork 1
[BI-1199] - Choose file format for germplasm export #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
666a2a1
[BI-1199] - Modal file selection fixes
HMS17 625ab68
[BI-1199] - Enable csv and xls export
HMS17 ec9412f
[BI-1199] - Cleanup
HMS17 dbc3ffe
[BI-1199] - Code review fixes 1
HMS17 b9f5041
[BI-1199] - Path to Query param
HMS17 9f0af1b
[BI-1199] - Numerical to Int/Double differentiation
HMS17 1233349
[BI-1199] - Unit test fix
HMS17 835b3c8
Merge branch 'develop' into feature/BI-1199
HMS17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
src/main/java/org/breedinginsight/brapps/importer/model/exports/FileType.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * See the NOTICE file distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.breedinginsight.brapps.importer.model.exports; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| Defines MediaType parameters for file export | ||
| */ | ||
| @Getter | ||
| public enum FileType { | ||
| XLS("xls", ".xls", "application/vnd.ms-excel"), | ||
| XLSX("xlsx", ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), | ||
| CSV("csv", ".csv", "text/csv"); | ||
|
|
||
| private String name; | ||
| private String extension; | ||
| private String mimeType; | ||
|
|
||
| FileType(String name, String extension, String mimeType) { | ||
| this.name = name; | ||
| this.extension = extension; | ||
| this.mimeType = mimeType; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
src/main/java/org/breedinginsight/services/writers/CSVWriter.java
This file contains hidden or 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,80 @@ | ||
| /* | ||
| * See the NOTICE file distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.breedinginsight.services.writers; | ||
|
|
||
| import io.micronaut.http.MediaType; | ||
| import io.micronaut.http.server.types.files.StreamedFile; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.commons.csv.CSVFormat; | ||
| import org.apache.commons.csv.CSVPrinter; | ||
| import org.breedinginsight.brapps.importer.model.exports.FileType; | ||
| import org.breedinginsight.model.Column; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.io.*; | ||
| import java.util.*; | ||
|
|
||
| /* | ||
| * This csv writer creates a csv file with the first row as column names and subsequent rows | ||
| * as data. | ||
| */ | ||
| @Slf4j | ||
| public class CSVWriter { | ||
|
|
||
| public static StreamedFile writeToDownload(List<Column> columns, List<Map<String, Object>> data, FileType extension) throws IOException { | ||
| try (ByteArrayOutputStream out = writeToCSV(columns, data)) { | ||
| InputStream inputStream = new ByteArrayInputStream(out.toByteArray()); | ||
| MediaType fileVal = new MediaType(extension.getMimeType(), extension.getName()); | ||
| StreamedFile downloadFile = new StreamedFile(inputStream, fileVal); | ||
| return downloadFile; | ||
| } catch (IOException e) { | ||
| log.info(e.getMessage()); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| //Writes to csv with desired columns and data | ||
| public static ByteArrayOutputStream writeToCSV(List<Column> columns, List<Map<String, Object>> data) { | ||
|
|
||
| String[] headers = columns.stream().map(x -> x.getValue() ).toArray(String[]::new); | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| try { | ||
| CSVFormat format = CSVFormat.EXCEL.withHeader(headers); | ||
| CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format); | ||
|
|
||
| ArrayList<Object> rowVals; | ||
| Object cellValue; | ||
| for (int i = 0; i < data.size(); i++) { | ||
| rowVals = new ArrayList<>(); | ||
| for (Column column: columns) { | ||
| if(data.get(i).containsKey(column.getValue())){ | ||
| cellValue = data.get(i).get(column.getValue()); | ||
| rowVals.add(cellValue); | ||
| } else { | ||
| rowVals.add(""); | ||
| } | ||
| } | ||
| csvPrinter.printRecord(rowVals); | ||
| } | ||
| csvPrinter.close(); | ||
| } catch (IOException e) { | ||
|
|
||
| } | ||
| return out; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.