1
|
|
|
package com.osomapps.pt.xlsx; |
2
|
|
|
|
3
|
|
|
import static org.apache.poi.ss.usermodel.CellType.NUMERIC; |
4
|
|
|
|
5
|
|
|
import com.osomapps.pt.dictionary.DictionaryName; |
6
|
|
|
import com.osomapps.pt.dictionary.DictionaryService; |
7
|
|
|
import com.osomapps.pt.programs.InProgram; |
8
|
|
|
import com.osomapps.pt.programs.InWarmupWorkoutItem; |
9
|
|
|
import com.osomapps.pt.programs.InWorkoutItem; |
10
|
|
|
import com.osomapps.pt.token.InUser; |
11
|
|
|
import com.osomapps.pt.token.InUserGoal; |
12
|
|
|
import java.io.IOException; |
13
|
|
|
import java.io.InputStream; |
14
|
|
|
import java.io.OutputStream; |
15
|
|
|
import java.util.Arrays; |
16
|
|
|
import java.util.Objects; |
17
|
|
|
import java.util.stream.Collectors; |
18
|
|
|
import lombok.extern.slf4j.Slf4j; |
19
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCell; |
20
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRow; |
21
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet; |
22
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
23
|
|
|
|
24
|
|
|
@Slf4j |
25
|
|
|
public class XlsxProgramModifier { |
26
|
|
|
private final InputStream inputStream; |
27
|
|
|
private final DictionaryService dictionaryService; |
28
|
|
|
|
29
|
|
|
private XlsxProgramModifier(InputStream inputStream, DictionaryService dictionaryService) { |
30
|
|
|
this.inputStream = inputStream; |
31
|
|
|
this.dictionaryService = dictionaryService; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static XlsxProgramModifier of( |
35
|
|
|
InputStream inputStream, DictionaryService dictionaryService) { |
36
|
|
|
return new XlsxProgramModifier(inputStream, dictionaryService); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public void updateCellData(OutputStream outputStream, InUser inUser) { |
40
|
|
|
try (final XSSFWorkbook book = new XSSFWorkbook(inputStream)) { |
41
|
|
|
final XSSFSheet inputSheet = book.getSheetAt(0); |
42
|
|
|
fillCell(inputSheet, 2, 1, inUser.getD_sex()); |
43
|
|
|
fillCell(inputSheet, 2, 2, emptyOrInteger(inUser.getAge())); |
44
|
|
|
fillCell(inputSheet, 2, 3, emptyOrInteger(inUser.getWeight())); |
45
|
|
|
fillCell(inputSheet, 2, 4, emptyOrInteger(inUser.getHeight())); |
46
|
|
|
fillCell(inputSheet, 2, 5, nullOrInteger(inUser.getD_level())); |
47
|
|
|
if (inUser.getInUserGoals().size() > 0) { |
|
|
|
|
48
|
|
|
fillCell(inputSheet, 2, 7, getGoalName(inUser.getInUserGoals().get(0))); |
49
|
|
|
} else { |
50
|
|
|
fillCell(inputSheet, 1, 7, ""); |
51
|
|
|
fillCell(inputSheet, 2, 7, ""); |
52
|
|
|
} |
53
|
|
|
if (inUser.getInUserGoals().size() > 1) { |
54
|
|
|
fillCell(inputSheet, 2, 8, getGoalName(inUser.getInUserGoals().get(1))); |
55
|
|
|
} else { |
56
|
|
|
fillCell(inputSheet, 1, 8, ""); |
57
|
|
|
fillCell(inputSheet, 2, 8, ""); |
58
|
|
|
} |
59
|
|
|
if (!inUser.getInPrograms().isEmpty()) { |
60
|
|
|
fillProgram( |
61
|
|
|
book.getSheetAt(2), |
62
|
|
|
inUser.getInPrograms().get(inUser.getInPrograms().size() - 1)); |
63
|
|
|
} |
64
|
|
|
book.write(outputStream); |
65
|
|
|
} catch (IOException ex) { |
66
|
|
|
log.error(ex.getMessage(), ex); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private void fillProgram(XSSFSheet programSheet, InProgram inProgram) { |
71
|
|
|
for (int index = 0; index < 16; index += 1) { |
72
|
|
|
fillCell(programSheet, 2 + index, 3, ""); |
73
|
|
|
fillCell(programSheet, 2 + index, 4, ""); |
74
|
|
|
fillCell(programSheet, 2 + index, 5, ""); |
75
|
|
|
} |
76
|
|
|
for (int index = 0; index < inProgram.getInWorkouts().size(); index += 1) { |
77
|
|
|
fillCell( |
78
|
|
|
programSheet, |
79
|
|
|
2 + index, |
80
|
|
|
3, |
81
|
|
|
"Goal " + (inProgram.getInWorkouts().get(index).getGoal_index() + 1)); |
82
|
|
|
fillCell( |
83
|
|
|
programSheet, |
84
|
|
|
2 + index, |
85
|
|
|
4, |
86
|
|
|
inProgram.getInWorkouts().get(index).getPart_name()); |
87
|
|
|
fillCell( |
88
|
|
|
programSheet, |
89
|
|
|
2 + index, |
90
|
|
|
5, |
91
|
|
|
"Workout " + (inProgram.getInWorkouts().get(index).getWorkout_index() + 1)); |
92
|
|
|
final InWarmupWorkoutItem inWarmupWorkoutItem = |
93
|
|
|
inProgram.getInWorkouts().get(index).getInWarmupWorkoutItems().get(0); |
94
|
|
|
fillCell(programSheet, 2 + index, 7, inWarmupWorkoutItem.getD_exercise_name()); |
95
|
|
|
fillCell(programSheet, 2 + index, 8, inWarmupWorkoutItem.getSpeed() + " km/h"); |
96
|
|
|
fillCell(programSheet, 2 + index, 9, inWarmupWorkoutItem.getIncline() + "%"); |
97
|
|
|
fillCell( |
98
|
|
|
programSheet, |
99
|
|
|
2 + index, |
100
|
|
|
10, |
101
|
|
|
(inWarmupWorkoutItem.getTime_in_sec() / 60) + " min"); |
102
|
|
|
for (int index2 = 0; |
103
|
|
|
index2 < inProgram.getInWorkouts().get(index).getInWorkoutItems().size(); |
104
|
|
|
index2 += 1) { |
105
|
|
|
final InWorkoutItem inWorkoutItem = |
106
|
|
|
inProgram.getInWorkouts().get(index).getInWorkoutItems().get(index2); |
107
|
|
|
fillCell( |
108
|
|
|
programSheet, |
109
|
|
|
2 + index, |
110
|
|
|
11 + 7 * index2, |
111
|
|
|
inWorkoutItem.getD_exercise_name()); |
112
|
|
|
fillCell( |
113
|
|
|
programSheet, |
114
|
|
|
2 + index, |
115
|
|
|
12 + 7 * index2, |
116
|
|
|
inWorkoutItem.getInWorkoutItemSets().size()); |
117
|
|
|
if (inWorkoutItem.getInWorkoutItemSets().get(0).getRepetitions() != null) { |
118
|
|
|
fillCell( |
119
|
|
|
programSheet, |
120
|
|
|
2 + index, |
121
|
|
|
13 + 7 * index2, |
122
|
|
|
inWorkoutItem.getInWorkoutItemSets().stream() |
123
|
|
|
.map(set -> "" + set.getRepetitions()) |
124
|
|
|
.collect(Collectors.joining(","))); |
125
|
|
|
} |
126
|
|
|
if (inWorkoutItem.getInWorkoutItemSets().get(0).getTime_in_sec() != null) { |
127
|
|
|
fillCell( |
128
|
|
|
programSheet, |
129
|
|
|
2 + index, |
130
|
|
|
13 + 7 * index2, |
131
|
|
|
inWorkoutItem.getInWorkoutItemSets().stream() |
132
|
|
|
.map(set -> round(set.getTime_in_sec() / 60F) + " min") |
133
|
|
|
.collect(Collectors.joining(","))); |
134
|
|
|
} |
135
|
|
|
if (inWorkoutItem.getInWorkoutItemSets().get(0).getWeight() != null) { |
136
|
|
|
fillCell( |
137
|
|
|
programSheet, |
138
|
|
|
2 + index, |
139
|
|
|
14 + 7 * index2, |
140
|
|
|
inWorkoutItem.getInWorkoutItemSets().stream() |
141
|
|
|
.map(set -> round(set.getWeight())) |
142
|
|
|
.collect(Collectors.joining(","))); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private String getGoalName(InUserGoal inUserGoal) { |
149
|
|
|
return Arrays.asList( |
150
|
|
|
dictionaryService.getEnValue( |
151
|
|
|
DictionaryName.goal_title, inUserGoal.getD_goal_title(), null), |
152
|
|
|
dictionaryService.getEnValue( |
153
|
|
|
DictionaryName.goal_title_2, inUserGoal.getD_goal_title_2(), null)) |
154
|
|
|
.stream() |
155
|
|
|
.filter(Objects::nonNull) |
156
|
|
|
.collect(Collectors.joining(", ")); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private Integer emptyOrInteger(Float value) { |
160
|
|
|
return value == null ? null : value.intValue(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private Integer nullOrInteger(String value) { |
164
|
|
|
return value == null ? null : Integer.parseInt(value); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
private void fillCell(XSSFSheet sheet, int columnNumber, int rowNumber, String value) { |
|
|
|
|
168
|
|
|
XSSFRow row = sheet.getRow(rowNumber); |
169
|
|
|
XSSFCell cell = row.getCell(columnNumber); |
170
|
|
|
if (cell == null) { |
171
|
|
|
cell = row.createCell(columnNumber); |
172
|
|
|
} |
173
|
|
|
cell.setCellValue(value == null ? "" : value); |
174
|
|
|
cell.setCellFormula(null); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
View Code Duplication |
private void fillCell(XSSFSheet sheet, int columnNumber, int rowNumber, Integer value) { |
|
|
|
|
178
|
|
|
XSSFRow row = sheet.getRow(rowNumber); |
179
|
|
|
XSSFCell cell = row.getCell(columnNumber); |
180
|
|
|
if (cell == null) { |
181
|
|
|
cell = row.createCell(columnNumber, NUMERIC); |
182
|
|
|
} |
183
|
|
|
if (value == null) { |
184
|
|
|
cell.setCellValue(""); |
185
|
|
|
} else { |
186
|
|
|
cell.setCellValue(value); |
187
|
|
|
} |
188
|
|
|
cell.setCellFormula(null); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private String round(Float value) { |
192
|
|
|
if (value.floatValue() == value.intValue()) { |
193
|
|
|
return Integer.toString(value.intValue()); |
194
|
|
|
} |
195
|
|
|
return "" + value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|