1   package com.atlassian.plugins.codegen.modules.common.component;
2   
3   import com.atlassian.plugins.codegen.ComponentDeclaration;
4   import com.atlassian.plugins.codegen.PluginProjectChangeset;
5   import com.atlassian.plugins.codegen.annotations.BambooPluginModuleCreator;
6   import com.atlassian.plugins.codegen.annotations.ConfluencePluginModuleCreator;
7   import com.atlassian.plugins.codegen.annotations.CrowdPluginModuleCreator;
8   import com.atlassian.plugins.codegen.annotations.FeCruPluginModuleCreator;
9   import com.atlassian.plugins.codegen.annotations.JiraPluginModuleCreator;
10  import com.atlassian.plugins.codegen.annotations.RefAppPluginModuleCreator;
11  import com.atlassian.plugins.codegen.modules.AbstractPluginModuleCreator;
12  
13  import static com.atlassian.fugue.Option.option;
14  import static com.atlassian.fugue.Option.some;
15  import static com.atlassian.plugins.codegen.modules.Dependencies.MOCKITO_TEST;
16  
17  /**
18   * @since 3.6
19   */
20  @RefAppPluginModuleCreator
21  @JiraPluginModuleCreator
22  @ConfluencePluginModuleCreator
23  @BambooPluginModuleCreator
24  @FeCruPluginModuleCreator
25  @CrowdPluginModuleCreator
26  public class ComponentModuleCreator extends AbstractPluginModuleCreator<ComponentProperties>
27  {
28      public static final String MODULE_NAME = "Component";
29      private static final String TEMPLATE_PREFIX = "templates/common/component/";
30  
31      //stub
32      private static final String CLASS_TEMPLATE = TEMPLATE_PREFIX + "Component.java.vtl";
33      private static final String INTERFACE_TEMPLATE = TEMPLATE_PREFIX + "ComponentInterface.java.vtl";
34  
35      //examples
36      private static final String EXAMPLE_CLASS_TEMPLATE = TEMPLATE_PREFIX + "Example" + CLASS_TEMPLATE;
37  
38      @Override
39      public PluginProjectChangeset createModule(ComponentProperties props) throws Exception
40      {
41          ComponentDeclaration.Builder component = ComponentDeclaration.builder(props.getClassId(), props.getModuleKey())
42              .name(option(props.getModuleName()))
43              .nameI18nKey(option(props.getNameI18nKey()))
44              .description(option(props.getDescription()))
45              .descriptionI18nKey(option(props.getDescriptionI18nKey()));
46          if (props.generateInterface())
47          {
48              component.interfaceId(some(props.getInterfaceId()));
49          }
50          if (props.getServiceProps() != null)
51          {
52              component.serviceProperties(props.getServiceProps());
53          }
54  
55          PluginProjectChangeset ret = new PluginProjectChangeset()
56              .withDependencies(MOCKITO_TEST)
57              .withComponentDeclarations(component.build());
58          
59          if (props.includeExamples())
60          {
61              return ret.with(createClass(props, EXAMPLE_CLASS_TEMPLATE));
62          }
63          else
64          {
65              if (props.generateClass())
66              {
67                  ret = ret.with(createClassAndTests(props, CLASS_TEMPLATE, GENERIC_TEST_TEMPLATE, GENERIC_TEST_TEMPLATE));
68              }
69              if (props.generateInterface())
70              {
71                  ret = ret.with(createClass(props, props.getInterfaceId(), INTERFACE_TEMPLATE));
72              }
73              return ret;
74          }
75      }
76  
77      @Override
78      public String getModuleName()
79      {
80          return MODULE_NAME;
81      }
82  }