|
1
|
|
|
package com.osomapps.pt.admin.user; |
|
2
|
|
|
|
|
3
|
|
|
import com.osomapps.pt.ResourceNotFoundException; |
|
4
|
|
|
import com.osomapps.pt.dictionary.DictionaryService; |
|
5
|
|
|
import com.osomapps.pt.token.InUser; |
|
6
|
|
|
import com.osomapps.pt.token.InUserRepository; |
|
7
|
|
|
import com.osomapps.pt.xlsx.XlsxProgramModifier; |
|
8
|
|
|
import com.osomapps.pt.xlsx.XlsxProgramParser; |
|
9
|
|
|
import java.io.IOException; |
|
10
|
|
|
import java.io.InputStream; |
|
11
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
12
|
|
|
import org.springframework.stereotype.Service; |
|
13
|
|
|
import org.springframework.util.FastByteArrayOutputStream; |
|
14
|
|
|
|
|
15
|
|
|
@Slf4j |
|
16
|
|
|
@Service |
|
17
|
|
|
class AdminUserProgramFileService { |
|
18
|
|
|
|
|
19
|
|
|
private final DictionaryService dictionaryService; |
|
20
|
|
|
private final InUserRepository inUserRepository; |
|
21
|
|
|
|
|
22
|
|
|
AdminUserProgramFileService( |
|
23
|
|
|
DictionaryService dictionaryService, InUserRepository inUserRepository) { |
|
24
|
|
|
this.dictionaryService = dictionaryService; |
|
25
|
|
|
this.inUserRepository = inUserRepository; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
ProgramResponseDTO createXlsx(Long id, FastByteArrayOutputStream outputStream) { |
|
29
|
|
|
final InUser inUser = inUserRepository.findById(id).orElse(null); |
|
30
|
|
|
if (inUser == null) { |
|
31
|
|
|
throw new ResourceNotFoundException("User with id " + id + " not found."); |
|
32
|
|
|
} |
|
33
|
|
|
try (InputStream inputStream = |
|
34
|
|
|
XlsxProgramParser.class.getResourceAsStream("program01.xlsx"); |
|
35
|
|
|
FastByteArrayOutputStream localOutputStream = new FastByteArrayOutputStream()) { |
|
36
|
|
|
final byte[] buffer = new byte[1024]; |
|
37
|
|
|
int length; |
|
38
|
|
|
while ((length = inputStream.read(buffer)) != -1) { |
|
39
|
|
|
localOutputStream.write(buffer, 0, length); |
|
40
|
|
|
} |
|
41
|
|
|
final XlsxProgramModifier xlsxProgramModifier = |
|
42
|
|
|
XlsxProgramModifier.of(localOutputStream.getInputStream(), dictionaryService); |
|
43
|
|
|
xlsxProgramModifier.updateCellData(outputStream, inUser); |
|
44
|
|
|
return new ProgramResponseDTO() |
|
45
|
|
|
.setFileName("program_for_user_" + id + ".xlsx") |
|
46
|
|
|
.setFileType( |
|
47
|
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
|
48
|
|
|
} catch (IOException ex) { |
|
49
|
|
|
log.error(ex.getMessage(), ex); |
|
50
|
|
|
throw new UnsupportedOperationException(ex); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|