Untitled
unknown
java
3 years ago
6.9 kB
11
Indexable
public UploadedStudents read(InputStream stream) {
XlsxReader reader = XlsxReader.create(stream);
XSSFWorkbook workbook = reader.getXssfWorkbook();
XSSFSheet sheet = reader.getXssfSheet();
errorTemplates = new HashMap<>();
int rowCount = sheet.getPhysicalNumberOfRows();
List<ExtendedStudent> succeedStudents = new ArrayList<>(rowCount + 1);
List<ExtendedStudent> failedStudents = new ArrayList<>(rowCount + 1);
for (int i = 1; i < rowCount; i++) {
StudentErrorTemplate errorTemplate = new StudentErrorTemplate();
ExtendedStudent student = new ExtendedStudent();
boolean isFailed = false;
String no = reader.getCellValueAsString(i, 0);
if (!isStudentIdentityOk(no)) {
isFailed = true;
errorTemplate.setId(false);
}
String name = reader.getCellValueAsString(i, 1);
if (!isStudentNameOk(name)) {
isFailed = true;
errorTemplate.setName(false);
}
String surname = reader.getCellValueAsString(i, 2);
if (!isStudentSurnameOk(surname)) {
isFailed = true;
errorTemplate.setSurname(false);
}
String schoolId = reader.getCellValueAsString(i, 3);
if (!isSchoolIdOk(schoolId)) {
isFailed = true;
errorTemplate.setSchoolId(false);
}
String parentName = reader.getCellValueAsString(i, 4);
if (!isParentNameOk(parentName)) {
isFailed = true;
errorTemplate.setParentName(false);
}
String parentSurname = reader.getCellValueAsString(i, 5);
if (!isParentSurnameOk(parentSurname)) {
isFailed = true;
errorTemplate.setParentSurname(false);
}
String gsm = reader.getCellValueAsString(i, 7);
if (!isParentGsmOk(gsm)) {
isFailed = true;
errorTemplate.setParentGsm(false);
}
String parentEmail = reader.getCellValueAsString(i, 6);
if (parentEmail == null || parentEmail.isEmpty()) {
parentEmail = gsm + "@terrayazilim.com.tr";
}
if (!isParentEmailOk(parentEmail)) {
isFailed = true;
errorTemplate.setParentEmail(false);
}
boolean addressExists;
boolean coordinateExists = false;
String coordinate = null;
String address = null;
Set<Position> positions = null;
Set<Position> secondaryPositions = null;
boolean secondaryAddressExists;
boolean secondaryCoordinateExists = false;
String secondaryCoordinate = null;
String secondaryAddress;
if (!ignoreAddresses) {
address = reader.getCellValueAsString(i, 9);
addressExists = address != null && !address.isEmpty();
coordinate = reader.getCellValueAsString(i, 8);
coordinateExists = coordinate != null && !coordinate.isEmpty();
if (addressExists && !coordinateExists) {
positions = isAddressOk(address);
if (positions == null || positions.size() < 1) {
isFailed = true;
errorTemplate.setAddress(false);
}
}
if (coordinateExists && !addressExists) {
if (!isCoordinateOk(coordinate)) {
isFailed = true;
errorTemplate.setCoordinate(false);
}
}
if (addressExists && coordinateExists) {
if (!isCoordinateOk(coordinate)) {
isFailed = true;
errorTemplate.setCoordinate(false);
}
}
if (!addressExists && !coordinateExists) {
isFailed = true;
errorTemplate.setCoordinate(false);
errorTemplate.setAddress(false);
}
secondaryPositions = null;
secondaryAddress = reader.getCellValueAsString(i, 11);
secondaryAddressExists = secondaryAddress != null && !secondaryAddress.isEmpty();
secondaryCoordinate = reader.getCellValueAsString(i, 10);
secondaryCoordinateExists = secondaryCoordinate != null && !secondaryCoordinate.isEmpty();
if (secondaryAddressExists && !secondaryCoordinateExists) {
secondaryPositions = isAddressOk(secondaryAddress);
if (secondaryPositions == null || secondaryPositions.size() < 1) {
isFailed = true;
errorTemplate.setSecondaryAddress(false);
}
}
if (!secondaryAddressExists && secondaryCoordinateExists) {
if (!isCoordinateOk(secondaryCoordinate)) {
isFailed = true;
errorTemplate.setSecondaryCoordinate(false);
}
}
if (secondaryAddressExists && secondaryCoordinateExists) {
if (!isCoordinateOk(secondaryCoordinate)) {
isFailed = true;
errorTemplate.setSecondaryCoordinate(false);
}
}
}
student.setIdentity(no);
student.setName(name);
student.setSurname(surname);
student.setSchoolId(schoolId);
student.setParentName(parentName);
student.setParentSurname(parentSurname);
student.setParentEmail(parentEmail);
student.setParentGsm(gsm);
student.setAddress(address);
if (!ignoreAddresses) {
if (coordinateExists) {
student.setCoordinate(coordinate);
} else {
if (positions != null) {
Position pos = null;
// export first position.
for (Position position : positions) {
pos = position;
break;
}
if (pos != null) {
double lat = pos.getOrdinate(CoordinateSystemAxes.GEODETIC_LATITUDE.getDirection().getOrder());
double lng = pos.getOrdinate(CoordinateSystemAxes.GEODETIC_LONGITUDE.getDirection().getOrder());
student.setCoordinate(lat + "," + lng);
}
}
}
if (secondaryCoordinateExists) {
student.setSecondaryCoordinate(secondaryCoordinate);
} else {
if (secondaryPositions != null) {
Position pos = null;
// export first position.
for (Position position : secondaryPositions) {
pos = position;
break;
}
if (pos != null) {
double lat = pos.getOrdinate(CoordinateSystemAxes.GEODETIC_LATITUDE.getDirection().getOrder());
double lng = pos.getOrdinate(CoordinateSystemAxes.GEODETIC_LONGITUDE.getDirection().getOrder());
student.setSecondaryCoordinate(lat + "," + lng);
}
}
}
}
errorTemplate.setStudentId(no);
errorTemplates.put(errorTemplate.getStudentId(), errorTemplate);
if (isFailed) {
failedStudents.add(student);
} else {
succeedStudents.add(student);
}
}
return new UploadedStudents(succeedStudents, failedStudents);
}Editor is loading...