/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * 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.auraframework.impl.instance;

import org.auraframework.adapter.ExceptionAdapter;
import org.auraframework.annotations.Annotations.ServiceComponent;
import org.auraframework.def.ActionDef;
import org.auraframework.def.ControllerDef;
import org.auraframework.def.DefDescriptor;
import org.auraframework.def.JavaControllerDef;
import org.auraframework.impl.java.controller.JavaAction;
import org.auraframework.impl.java.controller.JavaActionDef;
import org.auraframework.instance.Action;
import org.auraframework.instance.InstanceBuilder;
import org.auraframework.service.ContextService;
import org.auraframework.service.DefinitionService;
import org.auraframework.service.LoggingService;
import org.auraframework.system.AuraContext;
import org.auraframework.system.SubDefDescriptor;
import org.auraframework.throwable.quickfix.QuickFixException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.inject.Inject;
import java.util.Map;

/**
 * Provide an interface for an injectable builder of an instance.
 */
@ServiceComponent
public class JavaActionInstanceBuilder implements InstanceBuilder<Action, ActionDef>, ApplicationContextAware {
    @Inject
    private ContextService contextService;

    @Inject
    private DefinitionService definitionService;

    @Inject
    private ExceptionAdapter exceptionAdapter;

    @Inject
    private LoggingService loggingService;

    private ApplicationContext applicationContext;

    /**
     * Get the class that this builder knows how to instantiate.
     */
    @Override
    public Class<?> getDefinitionClass() {
        return JavaActionDef.class;
    }

    /**
     * Get an instance of the given def.
     */
    @Override
    public Action getInstance(ActionDef def, Map<String, Object> attributes) throws QuickFixException {
        AuraContext context = contextService.getCurrentContext();
        context.pushCallingDescriptor(def.getDescriptor());
        try {
            @SuppressWarnings("unchecked")
            SubDefDescriptor<ActionDef, ControllerDef> actionDesc = (SubDefDescriptor<ActionDef, ControllerDef>) def.getDescriptor();
            DefDescriptor<ControllerDef> controllerDesc = actionDesc.getParentDescriptor();

            JavaControllerDef controllerDef = (JavaControllerDef) definitionService.getDefinition(controllerDesc);

            Object controllerBean = applicationContext.getBean(controllerDef.getJavaType());

            return new JavaAction(controllerDesc, (JavaActionDef) def, controllerBean, attributes,
                    exceptionAdapter, loggingService);
        } finally {
            context.popCallingDescriptor();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

@@ -1390,32 +1390,89 @@
 bDefDescriptor;%0A
+import org.auraframework.throwable.AuraRuntimeException;%0A
 import org.auraf
@@ -3109,101 +3109,411 @@
 c);%0A
-%0A            Object controllerBean = applicationContext.getBean(controllerDef.getJavaType());
+            Class%3C?%3E controllerClass = controllerDef.getJavaType();%0A            Object controllerBean;%0A            try %7B%0A                controllerBean = applicationContext.getBean(controllerClass);%0A            %7D catch (Throwable t) %7B%0A                throw new AuraRuntimeException(%0A                        %22Failed to retreive controller instance for %22 + controllerDef.getDescriptor(), t);%0A            %7D
 %0A%0A  

add controller info to action instance contruction errors @rev goliver@