/*
 * Copyright 2014 James Pether Sörling
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *	$Id$
 *  $HeadURL$
*/
package com.hack23.cia.web.impl.ui.application.action;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;

import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
import com.hack23.cia.model.internal.application.user.impl.UserAccount;
import com.hack23.cia.service.api.ApplicationManager;
import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
import com.hack23.cia.service.api.action.application.CreateApplicationEventResponse;

@Service
public class PageActionEventHelperImpl implements PageActionEventHelper {

	/** The application manager. */
	@Autowired
	private ApplicationManager applicationManager;

	/* (non-Javadoc)
	 * @see com.hack23.cia.web.impl.ui.application.action.PageActionEventHelper#createPageEvent(com.hack23.cia.web.impl.ui.application.action.ViewAction, com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void createPageEvent(final ViewAction viewAction,final ApplicationEventGroup applicationEventGroup,final String page, final String pageMode, final String elementId) {
		final CreateApplicationEventRequest serviceRequest = new CreateApplicationEventRequest();
		serviceRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());

		serviceRequest.setEventGroup(applicationEventGroup);
		serviceRequest.setApplicationOperation(ApplicationOperationType.READ);

		serviceRequest.setPage(StringUtils.defaultString(page));
		serviceRequest.setPageMode(StringUtils.defaultString(pageMode));
		serviceRequest.setElementId(StringUtils.defaultString(elementId));

		serviceRequest.setActionName(viewAction.toString());

		serviceRequest.setUserId(getUserIdFromSecurityContext());

		serviceRequest.setApplicationMessage(viewAction.toString());

		final CreateApplicationEventResponse response = (CreateApplicationEventResponse) applicationManager
				.service(serviceRequest);
	}

	private static String getUserIdFromSecurityContext() {

		String result=null;

		final SecurityContext context = SecurityContextHolder.getContext();
		if (context != null) {
			final Authentication authentication = context.getAuthentication();
			if (authentication != null) {
				final Object principal = authentication.getPrincipal();

				if (principal instanceof UserAccount) {
					final UserAccount userAccount = (UserAccount) principal;
					result = userAccount.getUserId();
				}
			}
		}

		return result;
	}
}

@@ -2230,16 +2230,383 @@
 ntId) %7B%0A
+%0A%0A%0A%09%09String pageModeToUse;%0A%0A%09%09if ((pageMode == null %7C%7C pageMode.equals(%22%22)) && ApplicationEventGroup.USER.equals(applicationEventGroup)) %7B%0A%09%09%09pageModeToUse=%22Overview%22;%0A%09%09%7D else if (pageMode != null && elementId != null && pageMode.contains(elementId)) %7B%0A%09%09%09pageModeToUse= pageMode.replace(elementId, %22%22).replace(%22/%22, %22%22);%0A%09%09%7D else %7B%0A%09%09%09pageModeToUse = pageMode;%0A%09%09%7D%0A%0A
 %09%09final 
@@ -3036,16 +3036,21 @@
 pageMode
+ToUse
 ));%0A%09%09se

fix pageMode data.