View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
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   * The Class SendEmailService.
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  	/** The Constant LOGGER. */
50  	private static final Logger LOGGER = LoggerFactory.getLogger(SendEmailService.class);
51  
52  	/** The email service. */
53  	@Autowired
54  	private EmailService emailService;
55  
56  	/** The create application event service. */
57  	@Autowired
58  	private BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> createApplicationEventService;
59  
60  	/**
61  	 * Instantiates a new send email service.
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  }