/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.addon.javaee.jpa.ui;

import static org.jboss.forge.roaster.model.Visibility.PRIVATE;

import java.util.Collection;

import javax.inject.Inject;

import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.text.Inflector;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.input.UISelectMany;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.furnace.util.Iterators;
import org.jboss.forge.roaster.model.source.JavaClassSource;

/**
 * @author <a href="mailto:antonio.goncalves@gmail.com">Antonio Goncalves</a>
 */
public class JPANewEntityListenerCommand extends AbstractJPACommand<JavaClassSource>
{
   @Inject
   @WithAttributes(label = "Lifecyle", required = true)
   private UISelectMany<LifecycleType> lifecycles;

   @Inject
   private Inflector inflector;

   @Override
   public Metadata getMetadata(UIContext context)
   {
      return Metadata.from(super.getMetadata(context), getClass())
               .name("JPA: New Entity Listener")
               .description("Create a new JPA Entity Listener");
   }

   @Override
   protected String getType()
   {
      return "JPA Entity Listener";
   }

   @Override
   protected Class<JavaClassSource> getSourceType()
   {
      return JavaClassSource.class;
   }

   @Override
   public void initializeUI(UIBuilder builder) throws Exception
   {
      super.initializeUI(builder);
      builder.add(lifecycles);
   }

   public JavaClassSource decorateSource(UIExecutionContext context, Project project, JavaClassSource source)
            throws Exception
   {
      Collection<LifecycleType> lifecycles = Iterators.asList(this.lifecycles.getValue());
      for (LifecycleType lifecyle : lifecycles)
      {
         String methodName = inflector.lowerCamelCase(lifecyle.getAnnotation().getSimpleName());
         source.addMethod().setName(methodName).setParameters("Object object").setReturnTypeVoid()
                  .setVisibility(PRIVATE).setBody("").addAnnotation(lifecyle.getAnnotation());
      }
      return source;
   }
}

@@ -758,55 +758,8 @@
 ta;%0A
-import org.jboss.forge.furnace.util.Iterators;%0A
 impo
@@ -1034,16 +1034,17 @@
  %22Lifecy
+c
 le%22, req
@@ -1918,19 +1918,13 @@
     
-Collection%3C
+for (
 Life
@@ -1936,102 +1936,17 @@
 Type
-%3E
  lifecy
-c
 le
-s = Iterators.asList(this.lifecycles.getValue());%0A      for (LifecycleType lifecyle
  : l
@@ -1954,16 +1954,27 @@
 fecycles
+.getValue()
 )%0A      

Update JPANewEntityListenerCommand.java