/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.weld.deployment;

import org.jboss.as.ejb3.component.EJBComponentDescription;
import org.jboss.as.ejb3.component.singleton.SingletonComponentDescription;
import org.jboss.as.ejb3.component.stateful.StatefulComponentDescription;
import org.jboss.as.ejb3.component.stateless.StatelessComponentDescription;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.msc.service.ServiceName;
import org.jboss.weld.ejb.spi.BusinessInterfaceDescriptor;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.resources.spi.ResourceLoader;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Implementation of EjbDescriptor
 * @author Stuart Douglas
 */
public class EjbDescriptorImpl<T> implements EjbDescriptor<T>{

    private final EJBComponentDescription componentDescription;
    private final BeanDeploymentArchiveImpl beanDeploymentArchive;
    private final Set<BusinessInterfaceDescriptor<?>> localInterfaces;
    private final ServiceName baseName;

    public EjbDescriptorImpl(EJBComponentDescription componentDescription, BeanDeploymentArchiveImpl beanDeploymentArchive,DeploymentUnit deploymentUnit) {
        this.componentDescription = componentDescription;
        this.beanDeploymentArchive = beanDeploymentArchive;
        final Set<BusinessInterfaceDescriptor<?>> localInterfaces = new HashSet<BusinessInterfaceDescriptor<?>>();
        for(String className : componentDescription.getViewClassNames()) {
            localInterfaces.add(new BusinessInterfaceDescriptorImpl<Object>(beanDeploymentArchive,className));
        }
        this.localInterfaces=localInterfaces;
        this.baseName =  deploymentUnit.getServiceName().append("component").append(componentDescription.getComponentName());
    }


    @Override
    public Class<T> getBeanClass() {
        return (Class<T>) beanDeploymentArchive.getServices().get(ResourceLoader.class).classForName(componentDescription.getEJBClassName());
    }

    @Override
    public Collection<BusinessInterfaceDescriptor<?>> getLocalBusinessInterfaces() {
        return localInterfaces;
    }

    @Override
    public Collection<BusinessInterfaceDescriptor<?>> getRemoteBusinessInterfaces() {
        return Collections.emptySet();
    }

    @Override
    public String getEjbName() {
        return componentDescription.getEJBName();
    }

    @Override
    public Collection<Method> getRemoveMethods() {
        //TODO: fix this
        return Collections.emptySet();
    }

    @Override
    public boolean isStateless() {
        return componentDescription instanceof StatelessComponentDescription;
    }

    @Override
    public boolean isSingleton() {
        return componentDescription instanceof SingletonComponentDescription;
    }

    @Override
    public boolean isStateful() {
        return componentDescription instanceof StatefulComponentDescription;
    }

    @Override
    public boolean isMessageDriven() {
        //TODO: message driven beans
        return false;
    }

    public EJBComponentDescription getComponentDescription() {
        return componentDescription;
    }

    public ServiceName getBaseName() {
        return baseName;
    }

    public ServiceName getCreateServiceName() {
        return baseName.append("CREATE");
    }

    public ServiceName getStartServiceName() {
        return baseName.append("START");
    }

}

@@ -1048,16 +1048,66 @@
 yment;%0A%0A
+import org.jboss.as.ee.component.ViewDescription;%0A
 import o
@@ -1817,16 +1817,19 @@
 criptor%0A
+ *%0A
  * @auth
@@ -1911,16 +1911,17 @@
 iptor%3CT%3E
+ 
 %7B%0A%0A    p
@@ -2281,16 +2281,17 @@
 Archive,
+ 
 Deployme
@@ -2560,75 +2560,198 @@
     
-for(String className : componentDescription.getViewClassNames()) %7B%0A
+if (componentDescription.getViews() != null) %7B%0A            for (ViewDescription view : componentDescription.getViews()) %7B%0A                String viewClassName = view.getViewClassName();%0A    
     
@@ -2844,17 +2844,22 @@
 Archive,
-c
+ viewC
 lassName
@@ -2858,24 +2858,38 @@
 lassName));%0A
+            %7D%0A
         %7D%0A  
@@ -2914,17 +2914,19 @@
 terfaces
-=
+ = 
 localInt
@@ -2957,17 +2957,16 @@
 seName =
- 
  deploym

Migrate EjbDescriptorImpl to new EE framework