1 |
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.management.category.impl;
|
2 |
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
|
3 |
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
|
4 |
|
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Category;
|
5 |
|
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.ProjectInstance;
|
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.*;
|
|
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.interfaces.DatabaseObject;
|
6 |
7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
|
|
8 |
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.*;
|
|
9 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.AdditionalInformationDto;
|
7 |
10 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.CategoryDto;
|
8 |
11 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.GeneralResponseDto;
|
9 |
12 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.management.category.ICategories;
|
10 |
13 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.CategoryToDto;
|
11 |
14 |
import lombok.NonNull;
|
12 |
15 |
import lombok.RequiredArgsConstructor;
|
|
16 |
import org.slf4j.Logger;
|
|
17 |
import org.slf4j.LoggerFactory;
|
|
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
19 |
import org.springframework.data.util.Pair;
|
13 |
20 |
import org.springframework.http.HttpStatus;
|
14 |
21 |
import org.springframework.http.ResponseEntity;
|
15 |
22 |
import org.springframework.lang.Nullable;
|
... | ... | |
20 |
27 |
@Service
|
21 |
28 |
@RequiredArgsConstructor
|
22 |
29 |
public class CategoriesImpl implements ICategories {
|
23 |
|
|
|
30 |
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
|
24 |
31 |
private final ProjectService projectService;
|
25 |
|
private CategoryToDto categoryMapper = new CategoryToDto();
|
|
32 |
private final CategoryService categoryService;
|
|
33 |
private final WorkUnitService workUnitService;
|
|
34 |
private final IterationService iterationService;
|
|
35 |
private final ActivityService activityService;
|
|
36 |
private final PhaseService phaseService;
|
|
37 |
private final String GENERAL_ERROR_MESSAGE = "ERROR: Application error occurred";
|
|
38 |
private final String SUBMISSION_ERROR_MESSAGE = "Cannot proceed: Submission went wrong";
|
|
39 |
private final CategoryToDto categoryMapper = new CategoryToDto();
|
26 |
40 |
private final String PROJECT_NOT_FOUND_MESSAGE = "Project not found!";
|
27 |
41 |
private final String CATEGORIES_NOT_FOUND_MESSAGE = "Categories not found for the project!";
|
28 |
42 |
private final String INVALID_PROJECT_ID = "Provided project ID is in invalid format!";
|
... | ... | |
31 |
45 |
return projectService.getProjectById(id);
|
32 |
46 |
}
|
33 |
47 |
|
|
48 |
@Override
|
|
49 |
public ResponseEntity<GeneralResponseDto<CategoryDto, AdditionalInformationDto<String>>> handleCategoryChangeRequest(@Nullable List<CategoryDto> selectedCategories,
|
|
50 |
@Nullable Integer submitType,
|
|
51 |
@Nullable String submitId) {
|
|
52 |
List<Category> categories = new ArrayList<>();
|
|
53 |
|
|
54 |
if (selectedCategories != null && submitType != null) { // Selected submit
|
|
55 |
if (submitType <= 0 || submitType > Constants.SUBMIT_TYPES) { // ERROR: Unknown submitType
|
|
56 |
logger.warn("@PostMapping(\"/v2/management/changeCategory\") - Cannot proceed: Unknown submitType " + submitType);
|
|
57 |
return createResponseEntity(HttpStatus.BAD_REQUEST, GENERAL_ERROR_MESSAGE, null, null);
|
|
58 |
}
|
|
59 |
} else if (submitId != null) { //Inline submit
|
|
60 |
Pair<Integer, Long> resultPair = extractTypeAndId(submitId);
|
|
61 |
if (resultPair == null) { // Cannot be extracted
|
|
62 |
logger.warn("@PostMapping(\"/v2/management/changeCategory\") - Cannot proceed: Unknown submitId " + submitId);
|
|
63 |
return createResponseEntity(HttpStatus.BAD_REQUEST, GENERAL_ERROR_MESSAGE, null, null);
|
|
64 |
}
|
|
65 |
|
|
66 |
// Add one category into new list
|
|
67 |
submitType = resultPair.getFirst();
|
|
68 |
Long id = resultPair.getSecond();
|
|
69 |
categories.add(categoryService.getCategoryById(id));
|
|
70 |
|
|
71 |
} else { //Unspecific error
|
|
72 |
logger.warn("@PostMapping(\"/management/segment-category\") - Cannot proceed: Submission went wrong");
|
|
73 |
return createResponseEntity(HttpStatus.BAD_REQUEST, SUBMISSION_ERROR_MESSAGE, null, null);
|
|
74 |
}
|
|
75 |
|
|
76 |
// Process all selected categories
|
|
77 |
Pair<String, Boolean> result = processSelectedCategories(submitType, categories);
|
|
78 |
|
|
79 |
AdditionalInformationDto<String> additionalInformation = new AdditionalInformationDto<>();
|
|
80 |
if (!result.getSecond()) {
|
|
81 |
additionalInformation.addIntoCollection("successMessage", "All selected categories (" + categories.size() + ") were assign");
|
|
82 |
} else {
|
|
83 |
additionalInformation.addIntoCollection("informMessage", "Some categories cannot be assign, look at the log for more information");
|
|
84 |
}
|
|
85 |
additionalInformation.addIntoCollection("message", result.getFirst());
|
|
86 |
|
|
87 |
return createResponseEntity(HttpStatus.OK, null, categoryMapper.convert(categories), additionalInformation);
|
|
88 |
}
|
|
89 |
|
34 |
90 |
private List<CategoryDto> getCategories(Project project) {
|
35 |
91 |
|
36 |
92 |
if (project == null) {
|
... | ... | |
49 |
105 |
}
|
50 |
106 |
|
51 |
107 |
@Override
|
52 |
|
public ResponseEntity<GeneralResponseDto<CategoryDto>> getResponse(String projectId) {
|
|
108 |
public ResponseEntity<GeneralResponseDto<CategoryDto, AdditionalInformationDto<String>>> findCategoriesForProject(String projectId) {
|
53 |
109 |
// --- The value from the request body is String, so there must be a validation if the string is number or not.
|
54 |
110 |
final Long id = tryParse(projectId);
|
55 |
111 |
if (id == Long.MIN_VALUE)
|
56 |
|
return createResponseEntity(HttpStatus.BAD_REQUEST, INVALID_PROJECT_ID, null);
|
|
112 |
return createResponseEntity(HttpStatus.BAD_REQUEST, INVALID_PROJECT_ID, null, null);
|
57 |
113 |
|
58 |
114 |
// --- get project by ID
|
59 |
115 |
final Project project = getProjectById(id);
|
60 |
116 |
|
61 |
117 |
if (project == null)
|
62 |
|
return createResponseEntity(HttpStatus.NOT_FOUND, PROJECT_NOT_FOUND_MESSAGE, null);
|
|
118 |
return createResponseEntity(HttpStatus.NOT_FOUND, PROJECT_NOT_FOUND_MESSAGE, null, null);
|
63 |
119 |
|
64 |
120 |
// --- Get all categories for the specific project
|
65 |
121 |
final List<CategoryDto> categories = getCategories(project);
|
66 |
122 |
|
67 |
123 |
if (categories == null)
|
68 |
|
return createResponseEntity(HttpStatus.NOT_FOUND, CATEGORIES_NOT_FOUND_MESSAGE, null);
|
|
124 |
return createResponseEntity(HttpStatus.NOT_FOUND, CATEGORIES_NOT_FOUND_MESSAGE, null, null);
|
69 |
125 |
|
70 |
|
return createResponseEntity(HttpStatus.OK, null, categories);
|
|
126 |
return createResponseEntity(HttpStatus.OK, null, categories, null);
|
71 |
127 |
}
|
72 |
128 |
|
73 |
|
private ResponseEntity<GeneralResponseDto<CategoryDto>> createResponseEntity(@NonNull HttpStatus status,
|
74 |
|
@Nullable String message,
|
75 |
|
@Nullable List<CategoryDto> categories) {
|
|
129 |
private ResponseEntity<GeneralResponseDto<CategoryDto, AdditionalInformationDto<String>>> createResponseEntity(@NonNull HttpStatus status,
|
|
130 |
@Nullable String message,
|
|
131 |
@Nullable List<CategoryDto> categories,
|
|
132 |
@Nullable AdditionalInformationDto<String> additionalInfo) {
|
76 |
133 |
|
77 |
134 |
|
78 |
135 |
return new ResponseEntity<>(
|
79 |
136 |
GeneralResponseDto
|
80 |
|
.<CategoryDto>builder()
|
|
137 |
.<CategoryDto, AdditionalInformationDto<String>>builder()
|
81 |
138 |
.message(message)
|
82 |
139 |
.responseBody(categories)
|
|
140 |
.additionalInformation(additionalInfo)
|
83 |
141 |
.build(), status);
|
84 |
142 |
}
|
85 |
143 |
|
... | ... | |
91 |
149 |
}
|
92 |
150 |
}
|
93 |
151 |
|
|
152 |
private Pair<Integer, Long> extractTypeAndId(String submitId) {
|
|
153 |
String[] parts = submitId.split(Constants.HTML_DELIMITER);
|
|
154 |
try {
|
|
155 |
Integer type = Integer.parseInt(parts[0]);
|
|
156 |
Long id = Long.parseLong(parts[1]);
|
|
157 |
return Pair.of(type, id);
|
|
158 |
|
|
159 |
} catch (NumberFormatException e) {
|
|
160 |
return null;
|
|
161 |
}
|
|
162 |
|
|
163 |
}
|
|
164 |
|
|
165 |
private Pair<String, Boolean> processSelectedCategories(Integer submitType, List<Category> selectedCategories) {
|
|
166 |
|
|
167 |
boolean errorOccurred = false;
|
|
168 |
StringBuilder message = new StringBuilder();
|
|
169 |
for (Category category : selectedCategories) {
|
|
170 |
message.append("Category: ").append(category.getName()).append(System.getProperty("line.separator"));
|
|
171 |
|
|
172 |
if (category.getWorkUnits().size() == 0) { //Inform user that there aren't any WorkUnits
|
|
173 |
errorOccurred = true;
|
|
174 |
message.append(Constants.INDENT)
|
|
175 |
.append("cannot be assigned due to non-existent work unit")
|
|
176 |
.append(System.getProperty("line.separator"))
|
|
177 |
.append(System.getProperty("line.separator"));
|
|
178 |
continue;
|
|
179 |
}
|
|
180 |
|
|
181 |
DatabaseObject object = getNewObject(category.getWorkUnits(), submitType, category);
|
|
182 |
|
|
183 |
for (WorkUnit workUnit : category.getWorkUnits()) {
|
|
184 |
|
|
185 |
boolean hasAttr = true;
|
|
186 |
switch (submitType) {
|
|
187 |
case 1:
|
|
188 |
if (workUnit.getIteration() == null) {
|
|
189 |
hasAttr = false;
|
|
190 |
workUnit.setIteration((Iteration) object);
|
|
191 |
workUnitService.saveWorkUnit(workUnit);
|
|
192 |
}
|
|
193 |
break;
|
|
194 |
case 2:
|
|
195 |
if (workUnit.getPhase() == null) {
|
|
196 |
hasAttr = false;
|
|
197 |
workUnit.setPhase((Phase) object);
|
|
198 |
workUnitService.saveWorkUnit(workUnit);
|
|
199 |
}
|
|
200 |
break;
|
|
201 |
case 3:
|
|
202 |
if (workUnit.getActivity() == null) {
|
|
203 |
hasAttr = false;
|
|
204 |
workUnit.setActivity((Activity) object);
|
|
205 |
workUnitService.saveWorkUnit(workUnit);
|
|
206 |
}
|
|
207 |
break;
|
|
208 |
}
|
|
209 |
|
|
210 |
if (!hasAttr) { // Transformation of selected category was done
|
|
211 |
|
|
212 |
message.append(Constants.INDENT)
|
|
213 |
.append("successfully assigned to WU with id ").append(workUnit.getId())
|
|
214 |
.append(System.getProperty("line.separator"));
|
|
215 |
|
|
216 |
} else { // WorkUnit already has iteration, inform user
|
|
217 |
|
|
218 |
errorOccurred = true;
|
|
219 |
message.append(Constants.INDENT)
|
|
220 |
.append("cannot be assigned to WU with id ").append(workUnit.getId())
|
|
221 |
.append(" as it already exists with ").append(workUnit.getIteration().getName())
|
|
222 |
.append(System.getProperty("line.separator"));
|
|
223 |
}
|
|
224 |
}
|
|
225 |
message.append(System.getProperty("line.separator"));
|
|
226 |
}
|
|
227 |
return Pair.of(message.toString(), errorOccurred);
|
|
228 |
}
|
|
229 |
|
|
230 |
private DatabaseObject getNewObject(Set<WorkUnit> workUnits, Integer submitType, Category category) {
|
|
231 |
|
|
232 |
switch (submitType) {
|
|
233 |
case 1:
|
|
234 |
for (WorkUnit workUnit : workUnits) {
|
|
235 |
if (workUnit.getIteration() == null) {
|
|
236 |
Iteration iteration = new Iteration(category.getExternalId(),
|
|
237 |
category.getName(),
|
|
238 |
category.getDescription(),
|
|
239 |
category.getProjectInstance().getProjectId());
|
|
240 |
return iterationService.saveIteration(iteration);
|
|
241 |
}
|
|
242 |
}
|
|
243 |
break;
|
|
244 |
case 2:
|
|
245 |
for (WorkUnit workUnit : workUnits) {
|
|
246 |
if (workUnit.getPhase() == null) {
|
|
247 |
Phase phase = new Phase(category.getExternalId(),
|
|
248 |
category.getName(),
|
|
249 |
category.getDescription(),
|
|
250 |
category.getProjectInstance().getProjectId());
|
|
251 |
return phaseService.savePhase(phase);
|
|
252 |
}
|
|
253 |
}
|
|
254 |
break;
|
|
255 |
case 3:
|
|
256 |
for (WorkUnit workUnit : workUnits) {
|
|
257 |
if (workUnit.getActivity() == null) {
|
|
258 |
Activity activity = new Activity(category.getExternalId(),
|
|
259 |
category.getName(),
|
|
260 |
category.getDescription(),
|
|
261 |
category.getProjectInstance().getProjectId());
|
|
262 |
return activityService.saveActivity(activity);
|
|
263 |
}
|
|
264 |
}
|
|
265 |
break;
|
|
266 |
}
|
|
267 |
return null;
|
|
268 |
}
|
|
269 |
|
94 |
270 |
}
|
https://kivprogrammers.atlassian.net/browse/TSP2-58