mirror of
https://github.com/sabazadam/Se302.git
synced 2025-12-31 12:21:22 +00:00
error fixlendi
This commit is contained in:
@@ -208,7 +208,9 @@ public class DataImportService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Import enrollment data from CSV file.
|
* Import enrollment data from CSV file.
|
||||||
* Format: Alternating lines - course code, then Python list of student IDs
|
* Format: Alternating lines - course code, then Python list of student IDs,
|
||||||
|
* then blank line
|
||||||
|
* NO HEADER LINE in this file format.
|
||||||
*/
|
*/
|
||||||
public ImportResult importEnrollments(File file) {
|
public ImportResult importEnrollments(File file) {
|
||||||
ImportResult result = new ImportResult();
|
ImportResult result = new ImportResult();
|
||||||
@@ -228,51 +230,78 @@ public class DataImportService {
|
|||||||
int courseCount = 0;
|
int courseCount = 0;
|
||||||
int enrollmentCount = 0;
|
int enrollmentCount = 0;
|
||||||
|
|
||||||
// Skip header, then process in pairs (course code, student list)
|
// Process lines - pattern is: CourseCode, StudentList, EmptyLine, repeat...
|
||||||
for (int i = 1; i < lines.size(); i++) {
|
// NO HEADER in this file format
|
||||||
String courseCode = lines.get(i).trim();
|
int i = 0;
|
||||||
|
while (i < lines.size()) {
|
||||||
|
String currentLine = lines.get(i).trim();
|
||||||
|
|
||||||
if (courseCode.isEmpty()) {
|
// Skip empty lines
|
||||||
continue; // Skip empty lines
|
if (currentLine.isEmpty()) {
|
||||||
}
|
i++;
|
||||||
|
|
||||||
// Next line should be the student list
|
|
||||||
if (i + 1 >= lines.size()) {
|
|
||||||
result.addError("Line " + (i + 1) + ": Missing student list for course " + courseCode);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
i++; // Move to student list line
|
|
||||||
String studentListStr = lines.get(i).trim();
|
|
||||||
|
|
||||||
// Verify course exists
|
|
||||||
Course course = dataManager.getCourse(courseCode);
|
|
||||||
if (course == null) {
|
|
||||||
result.addError("Line " + (i) + ": Course not found: " + courseCode);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse Python list format: ['Std_ID_001', 'Std_ID_002', ...]
|
// Check if this looks like a course code
|
||||||
List<String> studentIds = parseStudentList(studentListStr);
|
if (COURSE_CODE_PATTERN.matcher(currentLine).matches()) {
|
||||||
|
String courseCode = currentLine;
|
||||||
|
|
||||||
for (String studentId : studentIds) {
|
// Verify course exists
|
||||||
Student student = dataManager.getStudent(studentId);
|
Course course = dataManager.getCourse(courseCode);
|
||||||
if (student == null) {
|
if (course == null) {
|
||||||
result.addError("Line " + (i + 1) + ": Student not found: " + studentId);
|
result.addError("Line " + (i + 1) + ": Course not found: " + courseCode);
|
||||||
|
i++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
dataManager.addEnrollment(studentId, courseCode);
|
// Next line should be the student list
|
||||||
enrollmentCount++;
|
i++;
|
||||||
}
|
if (i >= lines.size()) {
|
||||||
|
result.addError("Line " + i + ": Missing student list for course " + courseCode);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
courseCount++;
|
String studentListStr = lines.get(i).trim();
|
||||||
|
|
||||||
|
// Check if this is a valid student list (starts with '[')
|
||||||
|
if (!studentListStr.startsWith("[")) {
|
||||||
|
result.addError("Line " + (i + 1) + ": Invalid student list format for course " + courseCode);
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse Python list format: ['Std_ID_001', 'Std_ID_002', ...]
|
||||||
|
List<String> studentIds = parseStudentList(studentListStr);
|
||||||
|
|
||||||
|
for (String studentId : studentIds) {
|
||||||
|
Student student = dataManager.getStudent(studentId);
|
||||||
|
if (student == null) {
|
||||||
|
result.addError("Line " + (i + 1) + ": Student not found: " + studentId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
dataManager.addEnrollment(studentId, courseCode);
|
||||||
|
enrollmentCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
courseCount++;
|
||||||
|
i++;
|
||||||
|
} else if (currentLine.startsWith("[")) {
|
||||||
|
// This is a student list without a course code before it - skip
|
||||||
|
result.addError("Line " + (i + 1) + ": Student list without course code: " +
|
||||||
|
(currentLine.length() > 50 ? currentLine.substring(0, 50) + "..." : currentLine));
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
// Unknown line format - skip
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result.setRecordCount(enrollmentCount);
|
result.setRecordCount(enrollmentCount);
|
||||||
if (!result.hasErrors()) {
|
if (!result.hasErrors()) {
|
||||||
result.setSuccess(true);
|
result.setSuccess(true);
|
||||||
result.setMessage("Successfully imported " + enrollmentCount + " enrollments for " + courseCount + " courses");
|
result.setMessage(
|
||||||
|
"Successfully imported " + enrollmentCount + " enrollments for " + courseCount + " courses");
|
||||||
} else {
|
} else {
|
||||||
result.setMessage("Imported " + enrollmentCount + " enrollments with errors");
|
result.setMessage("Imported " + enrollmentCount + " enrollments with errors");
|
||||||
}
|
}
|
||||||
@@ -299,7 +328,7 @@ public class DataImportService {
|
|||||||
String[] parts = content.split(",");
|
String[] parts = content.split(",");
|
||||||
for (String part : parts) {
|
for (String part : parts) {
|
||||||
String cleanId = part.trim()
|
String cleanId = part.trim()
|
||||||
.replace("'", "") // Remove single quotes
|
.replace("'", "") // Remove single quotes
|
||||||
.replace("\"", "") // Remove double quotes
|
.replace("\"", "") // Remove double quotes
|
||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user