Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Portal
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
EIP
Labrador
Portal
Merge requests
!12
You need to sign in or sign up before continuing.
Add a CSV Service
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Add a CSV Service
csv
into
development
Overview
0
Commits
1
Pipelines
5
Changes
5
Merged
Add a CSV Service
Sára Juhošová
requested to merge
csv
into
development
Jul 28, 2020
Overview
0
Commits
1
Pipelines
5
Changes
5
0
0
Merge request reports
Compare
development
version 1
b03d666b
Jul 28, 2020
development (base)
and
latest version
latest version
9d9dc88b
1 commit,
Jul 28, 2020
version 1
b03d666b
2 commits,
Jul 28, 2020
5 files
+
268
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
src/main/java/nl/tudelft/portal/service/CSVService.java
0 → 100644
+
161
−
0
View file @ 9d9dc88b
Edit in single-file editor
Open in Web IDE
/*
* Portal - A site for direct access to the Labrador system as a teacher or admin
* Copyright (C) 2020- Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package
nl.tudelft.portal.service
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.Reader
;
import
java.net.MalformedURLException
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.UrlResource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
com.opencsv.*
;
import
com.opencsv.exceptions.CsvValidationException
;
@Service
public
class
CSVService
{
@Value
(
"${portal.csv-separator}"
)
private
char
separator
;
/**
* Gets the response for sending a csv file.
*
* @param path the path to the file to be sent
*/
public
ResponseEntity
<
Resource
>
getResponse
(
Path
path
)
throws
MalformedURLException
{
Resource
resource
=
new
UrlResource
(
"file:"
+
path
);
return
ResponseEntity
.
ok
()
.
contentType
(
MediaType
.
parseMediaType
(
"text/csv"
))
.
header
(
HttpHeaders
.
CONTENT_DISPOSITION
,
"attachment; filename=\""
+
path
+
"\""
)
.
body
(
resource
);
}
/**
* Reads data from a CSV file.
*
* @param path the path to the file
* @return the data from the CSV file
* @throws IOException if the file path is invalid or bad things happen during the read
* @throws CsvValidationException if a user defined validator fails
*/
public
List
<
String
[]>
readCSV
(
Path
path
)
throws
IOException
,
CsvValidationException
{
return
readCSV
(
path
,
true
,
0
);
}
/**
* Reads the data from a CSV file.
*
* @param path the path to the file which should be read
* @param ignoreQuotations whether to ignore quotation marks in the parsing
* @param skipLines the number of lines to skip
* @return the data from the CSV file
* @throws IOException if the file path is invalid or bad things happen during the read
* @throws CsvValidationException if a user defined validator fails
*/
public
List
<
String
[]>
readCSV
(
Path
path
,
boolean
ignoreQuotations
,
int
skipLines
)
throws
IOException
,
CsvValidationException
{
// initialize the parser of the csv
CSVParser
parser
=
new
CSVParserBuilder
()
.
withSeparator
(
separator
)
.
withIgnoreQuotations
(
ignoreQuotations
)
.
build
();
// initialize the reader of the csv
Reader
reader
=
Files
.
newBufferedReader
(
path
,
StandardCharsets
.
UTF_8
);
CSVReader
csvReader
=
new
CSVReaderBuilder
(
reader
)
.
withCSVParser
(
parser
)
.
withSkipLines
(
skipLines
)
.
build
();
// read the data from the csv
List
<
String
[]>
data
=
new
ArrayList
<>();
String
[]
line
;
while
((
line
=
csvReader
.
readNext
())
!=
null
)
{
data
.
add
(
line
);
}
// close the readers
reader
.
close
();
csvReader
.
close
();
return
data
;
}
/**
* Writes data to a csv file.
*
* @param data the data to be written
* @param directory the path to the file directory
* @param fileName the name of the file
* @throws IOException if the filename is wrong or an error occurs during closing of the writer
*/
public
void
writeToCSV
(
List
<
String
[]>
data
,
Path
directory
,
String
fileName
)
throws
IOException
{
if
(!
directory
.
toFile
().
exists
())
{
directory
.
toFile
().
mkdirs
();
}
CSVWriter
writer
=
new
CSVWriter
(
new
FileWriter
(
Path
.
of
(
directory
.
toString
(),
fileName
).
toFile
()),
separator
,
CSVWriter
.
NO_QUOTE_CHARACTER
,
CSVWriter
.
DEFAULT_ESCAPE_CHARACTER
,
CSVWriter
.
DEFAULT_LINE_END
);
writer
.
writeAll
(
data
);
writer
.
close
();
}
/**
* Converts a string into a valid filename.
*
* @param string the string to be converted
* @return a string which has the format of a valid filename
*/
public
String
convertToValidFileName
(
String
string
)
{
if
(
string
==
null
||
string
.
isBlank
())
{
return
"unknown"
;
}
StringBuilder
filename
=
new
StringBuilder
();
for
(
char
c
:
string
.
toCharArray
())
{
if
(
Character
.
isDigit
(
c
)
||
Character
.
isAlphabetic
(
c
))
{
filename
.
append
(
c
);
}
else
if
(
c
==
' '
)
{
filename
.
append
(
'_'
);
}
else
if
(
c
==
'/'
)
{
filename
.
append
(
'-'
);
}
}
return
filename
.
toString
();
}
}
Loading