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