1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.impl.action.admin;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.security.access.annotation.Secured;
25 import org.springframework.stereotype.Service;
26 import org.springframework.transaction.annotation.Propagation;
27 import org.springframework.transaction.annotation.Transactional;
28
29 import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
30 import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
31 import com.hack23.cia.model.internal.application.user.impl.UserAccount;
32 import com.hack23.cia.service.api.action.admin.SendEmailRequest;
33 import com.hack23.cia.service.api.action.admin.SendEmailResponse;
34 import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
35 import com.hack23.cia.service.api.action.application.CreateApplicationEventResponse;
36 import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
37 import com.hack23.cia.service.impl.action.common.AbstractBusinessServiceImpl;
38 import com.hack23.cia.service.impl.action.common.BusinessService;
39 import com.hack23.cia.service.impl.email.EmailService;
40
41
42
43
44 @Service
45 @Transactional(propagation = Propagation.REQUIRED, timeout = 600)
46 public final class SendEmailService extends AbstractBusinessServiceImpl<SendEmailRequest, SendEmailResponse>
47 implements BusinessService<SendEmailRequest, SendEmailResponse> {
48
49
50 private static final Logger LOGGER = LoggerFactory.getLogger(SendEmailService.class);
51
52
53 @Autowired
54 private EmailService emailService;
55
56
57 @Autowired
58 private BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> createApplicationEventService;
59
60
61
62
63 public SendEmailService() {
64 super(SendEmailRequest.class);
65 }
66
67 @Override
68 @Secured({ "ROLE_ADMIN" })
69 public SendEmailResponse processService(final SendEmailRequest serviceRequest) {
70
71 final CreateApplicationEventRequest eventRequest = new CreateApplicationEventRequest();
72 eventRequest.setEventGroup(ApplicationEventGroup.ADMIN);
73 eventRequest.setApplicationOperation(ApplicationOperationType.CREATE);
74 eventRequest.setActionName(SendEmailRequest.class.getSimpleName());
75 eventRequest.setSessionId(serviceRequest.getSessionId());
76
77 final UserAccount userAccount = getUserAccountFromSecurityContext();
78
79 if (userAccount != null) {
80 LOGGER.info("{} started:{}", serviceRequest.getClass().getSimpleName(), userAccount.getEmail());
81 eventRequest.setUserId(userAccount.getUserId());
82 }
83
84 SendEmailResponse response;
85 emailService.sendEmail(serviceRequest.getEmail(), serviceRequest.getSubject(), serviceRequest.getContent());
86 response = new SendEmailResponse(ServiceResult.SUCCESS);
87
88 eventRequest.setApplicationMessage(response.getResult().toString());
89 createApplicationEventService.processService(eventRequest);
90
91 return response;
92 }
93
94 }