Skip to content

Commit e44c4d1

Browse files
committed
alignment for audit controller
1 parent cb098b3 commit e44c4d1

File tree

1 file changed

+70
-61
lines changed

1 file changed

+70
-61
lines changed

src/main/java/main/controllers/AuditController.java

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -47,134 +47,141 @@ public AuditController(UserDto user) {
4747
public List<AuditDto> get(AuditDto searchTemplate) throws AqualityException {
4848
List<AuditDto> audits = auditDao.searchAll(searchTemplate);
4949
Integer projectId;
50-
try{
50+
try {
5151
projectId = searchTemplate.getId() == null
52-
? searchTemplate.getProject_id()
53-
: audits.get(0).getProject_id();
54-
}catch (IndexOutOfBoundsException e){
52+
? searchTemplate.getProject_id()
53+
: audits.get(0).getProject_id();
54+
} catch (IndexOutOfBoundsException e) {
5555
throw new AqualityException("The Audit you trying to access is not present!");
5656
}
57-
if(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()){
57+
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()) {
5858
return completeAudits(audits);
59-
}else{
59+
} else {
6060
throw new AqualityPermissionsException("Account is not allowed to view Audits.", baseUser);
6161
}
6262
}
63+
6364
public List<ServiceDto> get(ServiceDto template) throws AqualityException {
6465
return serviceDao.searchAll(template);
6566
}
67+
6668
public List<AuditCommentDto> get(AuditCommentDto template) throws AqualityException {
67-
if(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(template.getProject_id()).isViewer()){
69+
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(template.getProject_id()).isViewer()) {
6870
return completeComments(auditCommentsDao.searchAll(template));
69-
}else{
71+
} else {
7072
throw new AqualityPermissionsException("Account is not allowed to view Audit Comments.", baseUser);
7173
}
7274
}
75+
7376
public List<AuditStatisticDto> get(AuditStatisticDto template) throws AqualityException {
74-
if(baseUser.isFromGlobalManagement()){
77+
if (baseUser.isFromGlobalManagement()) {
7578
return completeAuditStatistic(auditStatisticDao.searchAll(template));
76-
}else{
79+
} else {
7780
throw new AqualityPermissionsException("Account is not allowed to view Audits.", baseUser);
7881
}
7982
}
83+
8084
public List<AuditAttachmentDto> get(AuditAttachmentDto searchTemplate) throws AqualityException {
8185
AuditDto audit = new AuditDto();
8286
audit.setId(searchTemplate.getAudit_id());
8387
Integer projectId;
84-
try{
88+
try {
8589
projectId = auditDao.searchAll(audit).get(0).getProject_id();
86-
}catch (IndexOutOfBoundsException e){
90+
} catch (IndexOutOfBoundsException e) {
8791
throw new AqualityException("The Audit you trying to access is not present!");
8892
}
89-
if(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()){
93+
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()) {
9094
return auditAttachmentsDao.searchAll(searchTemplate);
91-
}else{
95+
} else {
9296
throw new AqualityPermissionsException("Account is not allowed to view Audit Attachments.", baseUser);
9397
}
9498
}
9599

96100
@Override
97101
public AuditDto create(AuditDto createTemplate) throws AqualityException {
98-
if(baseUser.isAuditAdmin() || isAssignedAuditor(createTemplate.getId())){
102+
if (baseUser.isAuditAdmin() || isAssignedAuditor(createTemplate.getId())) {
99103
AuditDto createdAudit = auditDao.create(createTemplate);
100-
if(createdAudit.getAuditors() != null){
104+
if (createdAudit.getAuditors() != null) {
101105
AuditorDto template = new AuditorDto();
102106
template.setAudit_id(createdAudit.getId());
103107
updateAuditors(createdAudit.getAuditors());
104108
}
105109

106110
return createdAudit;
107-
}else{
111+
} else {
108112
throw new AqualityPermissionsException("Account is not allowed to create Audits.", baseUser);
109113
}
110114
}
115+
111116
public AuditCommentDto create(AuditCommentDto template) throws AqualityException {
112117
AuditDto audit = new AuditDto();
113118
audit.setId(template.getAudit_id());
114119
audit = get(audit).get(0);
115-
if(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(audit.getProject_id()).isEditor()){
120+
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(audit.getProject_id()).isEditor()) {
116121
template.setUser_id(baseUser.getId());
117122
return auditCommentsDao.create(template);
118-
}else{
123+
} else {
119124
throw new AqualityPermissionsException("Account is not allowed to create Audit Comments.", baseUser);
120125
}
121126
}
127+
122128
public String create(boolean all, boolean xls) throws AqualityException {
123129
ExcelUtils excelUtils = new ExcelUtils();
124130
AuditDto auditDto = new AuditDto();
125131
AuditStatusDto auditStatusDto = new AuditStatusDto();
126132
auditStatusDto.setId(4);
127133
auditDto.setStatus(auditStatusDto);
128134
List<AuditDto> audits = get(auditDto);
129-
if(!all){
135+
if (!all) {
130136
audits = getLatestAudits(audits);
131137
}
132138

133139
JSONArray resArray = new JSONArray();
134140
List<Pair<String, String>> fields = processExportArrayCreation(resArray, audits);
135-
try{
136-
if(xls){
137-
return excelUtils.writeXLSFile(resArray,fields, MessageFormat.format("Aquality_Tracking_{0}_Submitted_Audits_{1}", all ? "All" : "Last", formatter.format(new Date())), MessageFormat.format("{0} Submitted Audits", all ? "All" : "Last"));
138-
}else{
139-
return excelUtils.writeXLSXFile(resArray,fields, MessageFormat.format("Aquality_Tracking_{0}_Submitted_Audits_{1}", all ? "All" : "Last",formatter.format(new Date())), MessageFormat.format("{0} Submitted Audits", all ? "All" : "Last"));
141+
try {
142+
if (xls) {
143+
return excelUtils.writeXLSFile(resArray, fields, MessageFormat.format("Aquality_Tracking_{0}_Submitted_Audits_{1}", all ? "All" : "Last", formatter.format(new Date())), MessageFormat.format("{0} Submitted Audits", all ? "All" : "Last"));
144+
} else {
145+
return excelUtils.writeXLSXFile(resArray, fields, MessageFormat.format("Aquality_Tracking_{0}_Submitted_Audits_{1}", all ? "All" : "Last", formatter.format(new Date())), MessageFormat.format("{0} Submitted Audits", all ? "All" : "Last"));
140146
}
141-
} catch (Exception e){
147+
} catch (Exception e) {
142148
throw new AqualityException("Cannot create Export");
143149
}
144150
}
145151

146152
public void createMultiply(List<AuditAttachmentDto> listOfAttachments) throws AqualityException {
147-
if((baseUser.isAuditAdmin() || isAssignedAuditor(listOfAttachments.get(0).getAudit_id())) && isAuditOpened(listOfAttachments.get(0).getAudit_id())){
153+
if ((baseUser.isAuditAdmin() || isAssignedAuditor(listOfAttachments.get(0).getAudit_id())) && isAuditOpened(listOfAttachments.get(0).getAudit_id())) {
148154
auditAttachmentsDao.createMultiply(listOfAttachments);
149-
}else{
155+
} else {
150156
throw new AqualityPermissionsException("Account is not allowed to create Audit Attachments.", baseUser);
151157
}
152158
}
153159

154160
@Override
155161
public boolean delete(AuditDto template) throws AqualityException {
156-
if(baseUser.isAuditAdmin()){
162+
if (baseUser.isAuditAdmin()) {
157163
return auditDao.delete(template);
158-
}else{
164+
} else {
159165
throw new AqualityPermissionsException("Account is not allowed to delete Audits.", baseUser);
160166
}
161167
}
168+
162169
public boolean delete(AuditAttachmentDto template) throws AqualityException {
163-
if(baseUser.isAuditor() ||baseUser.isAuditAdmin()){
170+
if (baseUser.isAuditor() || baseUser.isAuditAdmin()) {
164171
FileUtils fileUtils = new FileUtils();
165172
List<AuditAttachmentDto> attachments = get(template);
166173
List<String> pathes = new ArrayList<>();
167174
pathes.add(attachments.get(0).getPath());
168175
fileUtils.removeFiles(pathes);
169176
return auditAttachmentsDao.delete(template);
170-
}else{
177+
} else {
171178
throw new AqualityPermissionsException("Account is not allowed to delete Audit Attachments.", baseUser);
172179
}
173180
}
174181

175182
public void updateAuditors(List<AuditorDto> newAuditors) throws AqualityException {
176-
if(baseUser.isAuditAdmin()){
177-
if(newAuditors.size() == 0){
183+
if (baseUser.isAuditAdmin()) {
184+
if (newAuditors.size() == 0) {
178185
throw new AqualityException("no new auditors");
179186
}
180187
AuditorDto auditorDtoTemplate = new AuditorDto();
@@ -192,12 +199,12 @@ public void updateAuditors(List<AuditorDto> newAuditors) throws AqualityExceptio
192199
}
193200
}
194201

195-
if(oldAuditors.size() > 0 ){
202+
if (oldAuditors.size() > 0) {
196203
for (AuditorDto oldAuditor : oldAuditors) {
197204
auditorsDao.delete(oldAuditor);
198205
}
199206
}
200-
}else{
207+
} else {
201208
throw new AqualityPermissionsException("Account is not allowed to update Audit Auditors.", baseUser);
202209
}
203210
}
@@ -214,7 +221,7 @@ private List<AuditDto> completeAudits(List<AuditDto> audits) throws AqualityExce
214221
List<AuditStatusDto> statuses = auditStatusDao.getAll();
215222
List<ServiceDto> services = serviceDao.getAll();
216223

217-
for(AuditDto audit: audits){
224+
for (AuditDto audit : audits) {
218225
filledAudits.add(completeAuditDto(audit, statuses, services));
219226
}
220227

@@ -224,18 +231,18 @@ private List<AuditDto> completeAudits(List<AuditDto> audits) throws AqualityExce
224231
private AuditDto completeAuditDto(AuditDto audit, List<AuditStatusDto> statuses, List<ServiceDto> services) throws AqualityException {
225232
audit.setProject(searchForProject(audit));
226233
audit.setAuditors(searchForAuditors(audit));
227-
audit.setStatus(searchForStatus(audit,statuses));
228-
audit.setService(searchForService(audit,services));
234+
audit.setStatus(searchForStatus(audit, statuses));
235+
audit.setService(searchForService(audit, services));
229236
audit.setComments(getAuditComments(audit));
230237

231238
return audit;
232239
}
233240

234-
private AuditStatusDto searchForStatus(AuditDto audit, List<AuditStatusDto> statuses){
241+
private AuditStatusDto searchForStatus(AuditDto audit, List<AuditStatusDto> statuses) {
235242
return statuses.stream().filter(x -> x.getId().equals(audit.getStatus_id())).findAny().orElse(null);
236243
}
237244

238-
private ServiceDto searchForService(AuditDto audit, List<ServiceDto> services){
245+
private ServiceDto searchForService(AuditDto audit, List<ServiceDto> services) {
239246
return services.stream().filter(x -> x.getId().equals(audit.getService_type_id())).findAny().orElse(null);
240247
}
241248

@@ -255,7 +262,7 @@ private List<AuditorDto> searchForAuditors(AuditDto audit) throws AqualityExcept
255262
}
256263

257264
private boolean isAssignedAuditor(Integer auditId) throws AqualityException {
258-
if(auditId != null){
265+
if (auditId != null) {
259266
AuditDto searchAudit = new AuditDto();
260267
searchAudit.setId(auditId);
261268
List<AuditorDto> auditors = searchForAuditors(searchAudit);
@@ -265,7 +272,7 @@ private boolean isAssignedAuditor(Integer auditId) throws AqualityException {
265272
}
266273

267274
private boolean isAuditOpened(Integer auditId) throws AqualityException {
268-
if(auditId != null){
275+
if (auditId != null) {
269276
AuditDto searchAudit = new AuditDto();
270277
searchAudit.setId(auditId);
271278
return get(searchAudit).get(0).getStatus_id() < 4;
@@ -276,11 +283,11 @@ private boolean isAuditOpened(Integer auditId) throws AqualityException {
276283
private List<AuditCommentDto> completeComments(List<AuditCommentDto> comments) throws AqualityUserException {
277284
UserDto userTemplate = new UserDto();
278285

279-
for (AuditCommentDto comment: comments){
286+
for (AuditCommentDto comment : comments) {
280287

281-
if(comment.getUser_id() != null){
288+
if (comment.getUser_id() != null) {
282289
userTemplate.setId(comment.getUser_id());
283-
}else{
290+
} else {
284291
userTemplate.setId(1);
285292
}
286293
comment.setAuthor(getUser(userTemplate));
@@ -300,20 +307,20 @@ private List<AuditStatisticDto> completeAuditStatistic(List<AuditStatisticDto> a
300307
List<ServiceDto> services = serviceDao.getAll();
301308
List<ProjectDto> projects = projectDao.getAll();
302309

303-
for (AuditStatisticDto auditStatistic: auditStatistics) {
304-
if(auditStatistic.getLast_submitted_id() != null){
310+
for (AuditStatisticDto auditStatistic : auditStatistics) {
311+
if (auditStatistic.getLast_submitted_id() != null) {
305312
AuditorDto auditorDtoTemplate = new AuditorDto();
306313
auditorDtoTemplate.setAudit_id(auditStatistic.getLast_submitted_id());
307314
List<AuditorDto> auditors = auditorsDao.searchAll(auditorDtoTemplate);
308315
auditStatistic.setAuditors_last(auditors);
309316
}
310-
if(auditStatistic.getLast_created_id() != null && auditStatistic.getStatus_id() != 4){
317+
if (auditStatistic.getLast_created_id() != null && auditStatistic.getStatus_id() != 4) {
311318
AuditorDto auditorDtoTemplate = new AuditorDto();
312319
auditorDtoTemplate.setAudit_id(auditStatistic.getLast_created_id());
313320
List<AuditorDto> auditors = auditorsDao.searchAll(auditorDtoTemplate);
314321
auditStatistic.setAuditors_next(auditors);
315322
}
316-
if(auditStatistic.getService_type_id() != null){
323+
if (auditStatistic.getService_type_id() != null) {
317324
Integer serviceTypeId = auditStatistic.getService_type_id();
318325
auditStatistic.setService(services.stream().filter(x -> Objects.equals(x.getId(), serviceTypeId)).findFirst().orElse(null));
319326
}
@@ -327,7 +334,7 @@ private List<AuditDto> getLatestAudits(List<AuditDto> audits) throws AqualityExc
327334
List<AuditDto> latest = new ArrayList<>();
328335
ProjectDao projectDao = new ProjectDao();
329336
List<ProjectDto> projects = projectDao.getAll();
330-
audits = audits.stream().filter(x-> x.getSubmitted() != null).collect(Collectors.toList());
337+
audits = audits.stream().filter(x -> x.getSubmitted() != null).collect(Collectors.toList());
331338
for (ProjectDto project : projects) {
332339
Integer id = project.getId();
333340
List<AuditDto> auditDtoList = audits.stream().filter(x -> Objects.equals(x.getProject().getId(), id)).collect(Collectors.toList());
@@ -340,33 +347,35 @@ private List<AuditDto> getLatestAudits(List<AuditDto> audits) throws AqualityExc
340347
return latest;
341348
}
342349

343-
private List<Pair<String,String>> processExportArrayCreation(JSONArray resArray, List<AuditDto> audits) throws AqualityException {
344-
try{
350+
private List<Pair<String, String>> processExportArrayCreation(JSONArray resArray, List<AuditDto> audits) throws AqualityException {
351+
try {
345352
for (AuditDto auditDto : audits) {
346353
JSONObject jsonObject = new JSONObject();
347354
jsonObject.put("id", auditDto.getId());
348355
jsonObject.put("project_name", auditDto.getProject().getName());
349-
if(auditDto.getCreated() != null) jsonObject.put("created", formatter.format(auditDto.getCreated()));
350-
if(auditDto.getStarted()!= null) jsonObject.put("started", formatter.format(auditDto.getStarted()));
351-
if(auditDto.getProgress_finished() != null) jsonObject.put("finished", formatter.format(auditDto.getProgress_finished()));
352-
if(auditDto.getSubmitted() != null) jsonObject.put("submitted", formatter.format(auditDto.getSubmitted()));
356+
if (auditDto.getCreated() != null) jsonObject.put("created", formatter.format(auditDto.getCreated()));
357+
if (auditDto.getStarted() != null) jsonObject.put("started", formatter.format(auditDto.getStarted()));
358+
if (auditDto.getProgress_finished() != null)
359+
jsonObject.put("finished", formatter.format(auditDto.getProgress_finished()));
360+
if (auditDto.getSubmitted() != null)
361+
jsonObject.put("submitted", formatter.format(auditDto.getSubmitted()));
353362
jsonObject.put("result", auditDto.getResult());
354363
String auditorsString = "";
355364
List<AuditorDto> auditors = auditDto.getAuditors();
356365
for (int j = 0; j < auditors.size(); j++) {
357366
if (j > 0) {
358367
auditorsString += ", ";
359368
}
360-
try{
369+
try {
361370
auditorsString += auditors.get(j).getFirst_name() + " " + auditors.get(j).getSecond_name();
362-
}catch (Exception e){
371+
} catch (Exception e) {
363372
//error on getting user info
364373
}
365374
}
366375
jsonObject.put("auditors", auditorsString);
367376
resArray.put(jsonObject);
368377
}
369-
List<Pair<String,String>> fields = new ArrayList<>();
378+
List<Pair<String, String>> fields = new ArrayList<>();
370379
fields.add(new Pair<>("Audit Id", "id"));
371380
fields.add(new Pair<>("Project", "project_name"));
372381
fields.add(new Pair<>("Date Created", "created"));
@@ -377,7 +386,7 @@ private List<Pair<String,String>> processExportArrayCreation(JSONArray resArray,
377386
fields.add(new Pair<>("Auditors", "auditors"));
378387

379388
return fields;
380-
} catch (Exception e){
389+
} catch (Exception e) {
381390
throw new AqualityException("Cannot process export creation");
382391
}
383392
}

0 commit comments

Comments
 (0)