/*******************************************************************************
 * Copyright 2013-2014 Gengyu Shi
 * 
 * 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 com.shigengyu.hyperion.cache;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.cache.CacheLoader;
import com.shigengyu.hyperion.core.WorkflowDefinition;
import com.shigengyu.hyperion.dao.WorkflowDefinitionDao;

@Service
public class WorkflowDefinitionCacheLoader extends CacheLoader<Class<? extends WorkflowDefinition>, WorkflowDefinition> {

	@Resource
	private WorkflowDefinitionDao workflowDefinitionDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public WorkflowDefinition load(final Class<? extends WorkflowDefinition> key) throws Exception {
		final WorkflowDefinition workflowDefinition = key.getConstructor().newInstance();
		workflowDefinitionDao.saveOrUpdate(workflowDefinition.toEntity());

		// Load all transitions in this definition to cache.
		WorkflowTransitionCache.getInstance().get(workflowDefinition);

		return workflowDefinition;
	}
}

@@ -837,24 +837,191 @@
 esource;%0D%0A%0D%0A
+import org.springframework.beans.BeansException;%0D%0Aimport org.springframework.context.ApplicationContext;%0D%0Aimport org.springframework.context.ApplicationContextAware;%0D%0A
 import org.s
@@ -1478,16 +1478,54 @@
 inition%3E
+%0D%0A%09%09implements ApplicationContextAware
  %7B%0D%0A%0D%0A%09@
@@ -1591,16 +1591,67 @@
 Dao;%0D%0A%0D%0A
+%09private ApplicationContext applicationContext;%0D%0A%0D%0A
 %09@Overri
@@ -1963,16 +1963,150 @@
 ());%0D%0A%0D%0A
+%09%09// Autowire workflow definition instance%0D%0A%09%09applicationContext.getAutowireCapableBeanFactory().autowireBean(workflowDefinition);%0D%0A%0D%0A
 %09%09// Loa
@@ -2254,12 +2254,178 @@
 on;%0D%0A%09%7D%0D
+%0A%0D%0A%09@Override%0D%0A%09public void setApplicationContext(ApplicationContext applicationContext) throws BeansException %7B%0D%0A%09%09this.applicationContext = applicationContext;%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Support workflow definition autowiring.