Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Register
Sign in
Toggle navigation
Menu
UPSMF
examsAndAdmissions
Merge requests
!15
An error occurred while fetching the assigned milestone of the selected merge_request.
generic method to read from csv and test it using controller
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
shishir suman
requested to merge
github/fork/Radheshhathwar/development
into
development
1 year ago
Overview
0
Commits
1
Pipelines
0
Changes
2
Created by: Radheshhathwar
0
0
Compare
development
development (base)
and
latest version
latest version
8eab03e3
1 commit,
1 year ago
2 files
+
102
−
0
Expand all files
Preferences
Preferences
File browser
List view
Tree view
Compare changes
Inline
Side-by-side
Show whitespace changes
Show one file at a time
Search (e.g. *.vue) (Ctrl+P)
src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/CsvImportController.java
0 → 100644
+
32
−
0
Options
View file @ 8eab03e3
package
com.tarento.upsmf.examsAndAdmissions.controller
;
import
com.tarento.upsmf.examsAndAdmissions.model.ExamCycle
;
import
com.tarento.upsmf.examsAndAdmissions.service.CsvDataImporterService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RequestMapping
(
"/api/v1/admin"
)
public
class
CsvImportController
{
@Autowired
private
CsvDataImporterService
csvDataImporterService
;
@Autowired
private
ExamCycle
examCycle
;
@GetMapping
(
"/importCsv"
)
public
String
importCsvData
(
Model
model
)
{
try
{
csvDataImporterService
.
importCsvData
(
examCycle
,
"sample.csv"
);
model
.
addAttribute
(
"message"
,
"CSV data imported successfully."
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
model
.
addAttribute
(
"message"
,
"Failed to import CSV data."
);
}
return
"import-result"
;
}
}
src/main/java/com/tarento/upsmf/examsAndAdmissions/service/CsvDataImporterService.java
0 → 100644
+
70
−
0
Options
View file @ 8eab03e3
package
com.tarento.upsmf.examsAndAdmissions.service
;
import
com.tarento.upsmf.examsAndAdmissions.model.ExamCycle
;
import
com.tarento.upsmf.examsAndAdmissions.repository.ExamCycleRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.util.ResourceUtils
;
import
javax.persistence.EntityManager
;
import
javax.persistence.PersistenceContext
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.FileReader
;
import
java.lang.reflect.Field
;
@Service
public
class
CsvDataImporterService
{
@PersistenceContext
private
EntityManager
entityManager
;
@Autowired
private
ExamCycleRepository
examCycleRepository
;
// ...
@Transactional
public
void
importCsvData
(
ExamCycle
examCycle
,
String
fileName
)
throws
Exception
{
File
csvFile
=
ResourceUtils
.
getFile
(
"classpath:"
+
fileName
);
try
(
BufferedReader
br
=
new
BufferedReader
(
new
FileReader
(
csvFile
)))
{
String
headerLine
=
br
.
readLine
();
// Read the header line
String
[]
headers
=
headerLine
.
split
(
","
);
// Split the headers
String
line
;
while
((
line
=
br
.
readLine
())
!=
null
)
{
String
[]
fields
=
line
.
split
(
","
);
// Create a new entity
ExamCycle
entity
=
new
ExamCycle
();
// Iterate over the headers and set entity properties dynamically
for
(
int
i
=
0
;
i
<
headers
.
length
;
i
++)
{
String
header
=
headers
[
i
];
String
fieldValue
=
fields
[
i
];
setEntityProperty
(
entity
,
header
,
fieldValue
);
}
// Save the entity to the database with the specified table name
entityManager
.
persist
(
entity
);
}
}
}
private
void
setEntityProperty
(
ExamCycle
examCycle
,
String
header
,
String
fieldValue
)
throws
Exception
{
Field
field
=
ExamCycle
.
class
.
getDeclaredField
(
header
);
field
.
setAccessible
(
true
);
// Determine the data type of the field and set its value accordingly
if
(
field
.
getType
().
equals
(
String
.
class
))
{
field
.
set
(
examCycle
,
fieldValue
);
}
else
if
(
field
.
getType
().
equals
(
Integer
.
class
))
{
field
.
set
(
examCycle
,
Integer
.
parseInt
(
fieldValue
));
}
else
if
(
field
.
getType
().
equals
(
Double
.
class
))
{
field
.
set
(
examCycle
,
Double
.
parseDouble
(
fieldValue
));
}
field
.
setAccessible
(
false
);
}
}
Menu
Explore
Projects
Groups
Topics
Snippets