Export csv data
This commit is contained in:
@ -12,12 +12,15 @@ import io.swagger.annotations.ApiParam;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.plannaplan.App;
|
||||
import com.plannaplan.entities.Assignment;
|
||||
import com.plannaplan.entities.Commision;
|
||||
import com.plannaplan.entities.Groups;
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.exceptions.UserNotFoundException;
|
||||
import com.plannaplan.models.ExportData;
|
||||
import com.plannaplan.responses.mappers.CommisionResponseMappers;
|
||||
import com.plannaplan.responses.models.CommisionResponse;
|
||||
import com.plannaplan.services.AssignmentService;
|
||||
@ -27,6 +30,8 @@ import com.plannaplan.services.GroupService;
|
||||
import com.plannaplan.types.AppState;
|
||||
import com.plannaplan.types.UserRoles;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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.RequestParam;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@ -236,4 +243,32 @@ public class CommisionController extends TokenBasedController {
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user