Export csv data

This commit is contained in:
Filip Izydorczyk 2021-01-17 16:32:59 +01:00
parent c4ec88e691
commit d4514affcc
4 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,86 @@
package com.plannaplan.models;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Container to keep data to export
*/
public class ExportData {
private static final String GROUP_FIELD = "gr_nr";
private static final String USER_FIELD = "user_id";
private static final String CYKL_FIELD = "zaj_cykl_id";
private String userId;
private String zajCyklId;
private String grNr;
/**
* @param userId usosid
* @param zajCyklId course cycle
* @param grNr group number
*/
public ExportData(String userId, String zajCyklId, String grNr) {
this.setUserId(userId);
this.setZajCyklId(zajCyklId);
this.setGrNr(grNr);
}
/**
* @return group number
*/
public String getGrNr() {
return grNr;
}
/**
* @param grNr group number
*/
public void setGrNr(String grNr) {
this.grNr = grNr;
}
/**
* @return course cycle
*/
public String getZajCyklId() {
return zajCyklId;
}
/**
* @param zajCyklId course cycle
*/
public void setZajCyklId(String zajCyklId) {
this.zajCyklId = zajCyklId;
}
/**
* @return usosid
*/
public String getUserId() {
return userId;
}
/**
* @param userId usosid
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* @return csv fromated line
*/
public String convertToCSVRecord() {
return Stream.of(this.userId, this.zajCyklId, this.grNr).collect(Collectors.joining(","));
}
/**
* @return csv formated first line
*/
public static String getCSVHeader() {
return USER_FIELD + ", " + CYKL_FIELD + ", " + GROUP_FIELD;
}
}

View File

@ -1,10 +1,12 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.models.ExportData;
import com.plannaplan.repositories.AssignmentRepository; import com.plannaplan.repositories.AssignmentRepository;
import com.plannaplan.repositories.CommisionRepository; import com.plannaplan.repositories.CommisionRepository;
@ -22,6 +24,8 @@ public class CommisionService {
private CommisionRepository repo; private CommisionRepository repo;
@Autowired @Autowired
private AssignmentRepository aRepository; private AssignmentRepository aRepository;
@Autowired
private UserService userService;
public CommisionService() { public CommisionService() {
} }
@ -77,4 +81,21 @@ public class CommisionService {
public long getCommisionsAmmount() { public long getCommisionsAmmount() {
return this.repo.count(); return this.repo.count();
} }
/**
* @return list of ExportData inmstancces keeping data to exprt to file
*/
public List<ExportData> getExportData() {
final List<ExportData> response = new ArrayList<>();
this.userService.getAllStudents().forEach(student -> {
student.getStudentRegisteredGrups().forEach(group -> {
response.add(new ExportData(student.getUsosId(), Integer.toString(group.getZajCykId()),
Integer.toString(group.getGrNr())));
});
});
return response;
}
} }

View File

@ -62,6 +62,14 @@
<version>4.5.10</version> <version>4.5.10</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>

View File

@ -12,12 +12,15 @@ import io.swagger.annotations.ApiParam;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.servlet.http.HttpServletResponse;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException; import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.models.ExportData;
import com.plannaplan.responses.mappers.CommisionResponseMappers; import com.plannaplan.responses.mappers.CommisionResponseMappers;
import com.plannaplan.responses.models.CommisionResponse; import com.plannaplan.responses.models.CommisionResponse;
import com.plannaplan.services.AssignmentService; import com.plannaplan.services.AssignmentService;
@ -27,6 +30,8 @@ import com.plannaplan.services.GroupService;
import com.plannaplan.types.AppState; import com.plannaplan.types.AppState;
import com.plannaplan.types.UserRoles; import com.plannaplan.types.UserRoles;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -37,6 +42,8 @@ import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -236,4 +243,32 @@ public class CommisionController extends TokenBasedController {
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK); return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
} }
/**
* @param response spring response to set headers
*/
@GetMapping("/export/csv")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation(value = "Export acceptes assignmetns as csv file to import to usos. You need to provide ADMIN token in order to get access to this data.")
public void getFile(HttpServletResponse response) {
try {
String csvString = ExportData.getCSVHeader() + "\n";
final Iterator<ExportData> it = this.commisionService.getExportData().iterator();
while (it.hasNext()) {
final ExportData data = it.next();
csvString += (data.convertToCSVRecord() + "\n");
}
final InputStream is = IOUtils.toInputStream(csvString, "UTF-8");
IOUtils.copy(is, response.getOutputStream());
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"export.csv\"");
response.flushBuffer();
} catch (IOException ex) {
throw new RuntimeException("IOError writing file to output stream");
}
}
} }