/* 
 * 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. 
 */
package org.ngrinder.common.service;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.ngrinder.infra.spring.SpringContext;
import org.ngrinder.model.BaseModel;
import org.ngrinder.model.User;
import org.ngrinder.user.service.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Model aspect.
 * 
 * @author Liu Zhifei
 * @author JunHo Yoon
 * @since 3.0
 */
@Aspect
@Service
public class ModelAspect {

	public static final String EXECUTION_SAVE = "execution(* org.ngrinder.**.*Service.save*(..))";

	@Autowired
	private UserContext userContext;

	@Autowired
	private SpringContext springContext;

	/**
	 * Save current user to modified date or created date. It's only workable when it's from servlet context.
	 * 
	 * @param joinPoint
	 *            joint point
	 */
	@Before(EXECUTION_SAVE)
	public void beforeSave(JoinPoint joinPoint) {
		for (Object object : joinPoint.getArgs()) {
			// If the object is base model and it's on request of servlet
			// It's not executed on Task scheduling.
			SpringContext springContext = getSpringContext();
			if (object instanceof BaseModel
					&& (springContext.isServletRequestContext() || springContext.isUnitTestContext())) {
				BaseModel<?> model = (BaseModel<?>) object;
				Date lastModifiedDate = new Date();
				model.setLastModifiedDate(lastModifiedDate);
				User currentUser = userContext.getCurrentUser();
				model.setLastModifiedUser(currentUser);

				if (!model.exist() || model.getCreatedUser() == null) {
					model.setCreatedDate(lastModifiedDate);
					model.setCreatedUser(currentUser.getFactualUser());
				}
			}
		}
	}

	public SpringContext getSpringContext() {
		return springContext;
	}

	public void setSpringContext(SpringContext springContext) {
		this.springContext = springContext;
	}

}

@@ -860,16 +860,68 @@
 l.User;%0A
+import org.ngrinder.user.repository.UserRepository;%0A
 import o
@@ -1396,16 +1396,70 @@
 ntext;%0A%0A
+%09@Autowired%0A%09private UserRepository userRepository;%0A%0A%0A
 %09/**%0A%09 *
@@ -1552,16 +1552,20 @@
  servlet
+%0A%09 *
  context
@@ -1944,16 +1944,18 @@
 el%0A%09%09%09%09%09
+%09%09
 && (spri
@@ -2247,24 +2247,47 @@
 odifiedUser(
+userRepository.findOne(
 currentUser)
@@ -2285,16 +2285,25 @@
 rentUser
+.getId())
 );%0A%0A%09%09%09%09
@@ -2412,45 +2412,107 @@
 %09%09%09%09
-model.setCreatedUser(currentUser.getF
+User factualUser = currentUser.getFactualUser();%0A%09%09%09%09%09model.setCreatedUser(userRepository.findOne(f
 actu
@@ -2517,17 +2517,24 @@
 tualUser
-(
+.getId()
 ));%0A%09%09%09%09

Fix the bug which makes the cached user used in modelaspect.