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.application;
20  
21  import java.util.Date;
22  
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.ApplicationActionEvent;
30  import com.hack23.cia.model.internal.application.system.impl.ApplicationSession;
31  import com.hack23.cia.model.internal.application.system.impl.ApplicationSessionType;
32  import com.hack23.cia.model.internal.application.system.impl.ApplicationSession_;
33  import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
34  import com.hack23.cia.service.api.action.application.CreateApplicationEventResponse;
35  import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
36  import com.hack23.cia.service.data.api.ApplicationSessionDAO;
37  import com.hack23.cia.service.impl.action.common.AbstractBusinessServiceImpl;
38  import com.hack23.cia.service.impl.action.common.BusinessService;
39  
40  /**
41   * The Class CreateApplicationEventService.
42   */
43  @Service
44  @Transactional(propagation = Propagation.REQUIRED)
45  public final class CreateApplicationEventService
46  		extends AbstractBusinessServiceImpl<CreateApplicationEventRequest, CreateApplicationEventResponse>
47  		implements BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> {
48  
49  	/** The application session dao. */
50  	@Autowired
51  	private ApplicationSessionDAO applicationSessionDAO;
52  
53  	/**
54  	 * Instantiates a new creates the application event service.
55  	 */
56  	public CreateApplicationEventService() {
57  		super(CreateApplicationEventRequest.class);
58  	}
59  
60  	@Secured({ "ROLE_ANONYMOUS","ROLE_USER","ROLE_ADMIN" })
61  	@Override
62  	public CreateApplicationEventResponse processService(final CreateApplicationEventRequest serviceRequest) {
63  		final ApplicationSession applicationSession = applicationSessionDAO.findFirstByProperty(ApplicationSession_.sessionId, serviceRequest.getSessionId());
64  
65  		if (applicationSession != null) {
66  			final ApplicationActionEvent applicationActionEvent = new ApplicationActionEvent();
67  
68  			applicationActionEvent.setEventGroup(serviceRequest.getEventGroup());
69  			applicationActionEvent.setCreatedDate(new Date());
70  			applicationActionEvent.setSessionId(serviceRequest.getSessionId());
71  
72  			applicationActionEvent.setPage(serviceRequest.getPage());
73  			applicationActionEvent.setPageMode(serviceRequest.getPageMode());
74  			applicationActionEvent.setElementId(serviceRequest.getElementId());
75  
76  			applicationActionEvent.setUserId(serviceRequest.getUserId());
77  			if (serviceRequest.getUserId() != null && ApplicationSessionType.ANONYMOUS == applicationSession.getSessionType()) {
78  				applicationSession.setSessionType(ApplicationSessionType.REGISTERED_USER);
79  				applicationSession.setUserId(serviceRequest.getUserId());
80  			}
81  
82  			applicationActionEvent.setApplicationOperation(serviceRequest.getApplicationOperation());
83  			applicationActionEvent.setActionName(serviceRequest.getActionName());
84  
85  			applicationActionEvent.setApplicationMessage(serviceRequest.getApplicationMessage());
86  			applicationActionEvent.setErrorMessage(serviceRequest.getErrorMessage());
87  
88  			applicationSession.getEvents().add(applicationActionEvent);
89  
90  			applicationSessionDAO.persist(applicationSession);
91  			return new CreateApplicationEventResponse(ServiceResult.SUCCESS);
92  		} else {
93  			return new CreateApplicationEventResponse(ServiceResult.FAILURE);
94  		}
95  	}
96  
97  }