1   package com.atlassian.plugins.codegen.modules.jira;
2   
3   import com.atlassian.plugins.codegen.PluginProjectChangeset;
4   import com.atlassian.plugins.codegen.annotations.JiraPluginModuleCreator;
5   import com.atlassian.plugins.codegen.modules.AbstractPluginModuleCreator;
6   import com.atlassian.plugins.codegen.modules.common.Resource;
7   
8   import org.codehaus.plexus.util.FileUtils;
9   
10  import static com.atlassian.plugins.codegen.ResourceFile.resourceFile;
11  import static com.atlassian.plugins.codegen.modules.Dependencies.MOCKITO_TEST;
12  
13  /**
14   * @since 3.6
15   */
16  @JiraPluginModuleCreator
17  public class ReportModuleCreator extends AbstractPluginModuleCreator<ReportProperties>
18  {
19  
20      public static final String MODULE_NAME = "Report";
21      private static final String TEMPLATE_PREFIX = "templates/jira/report/";
22  
23      //stub
24      private static final String CLASS_TEMPLATE = TEMPLATE_PREFIX + "Report.java.vtl";
25      private static final String UNIT_TEST_TEMPLATE = "templates/generic/GenericTest.java.vtl";
26      private static final String VIEW_TEMPLATE = "templates/common/actionview.vm.vtl";
27  
28      //examples
29      private static final String EXAMPLE_CLASS_TEMPLATE = TEMPLATE_PREFIX + "Example" + CLASS_TEMPLATE;
30  
31      private static final String PLUGIN_MODULE_TEMPLATE = TEMPLATE_PREFIX + "report-plugin.xml.vtl";
32  
33      @Override
34      public PluginProjectChangeset createModule(ReportProperties props) throws Exception
35      {
36          PluginProjectChangeset ret = new PluginProjectChangeset()
37              .with(MOCKITO_TEST)
38              .with(createModule(props, PLUGIN_MODULE_TEMPLATE));
39          
40          if (props.includeExamples())
41          {
42              return ret.with(createClass(props, EXAMPLE_CLASS_TEMPLATE));
43          }
44          else
45          {
46              ret = ret.with(createClassAndTests(props, CLASS_TEMPLATE, UNIT_TEST_TEMPLATE));
47              
48              //since we know resources are velocity templates, let's create them
49              for (Resource resource : props.getResources())
50              {
51                  if (resource.getType().equals("i18n"))
52                  {
53                      String path = FileUtils.getPath(resource.getLocation());
54                      String name = FileUtils.filename(resource.getLocation());
55                      ret = ret.with(resourceFile(path, name + ".properties", ""));
56                  }
57                  else
58                  {
59                      ret = ret.with(createTemplateResource(props, resource, VIEW_TEMPLATE));
60                  }
61              }
62              
63              return ret;
64          }
65      }
66  
67  
68      @Override
69      public String getModuleName
70              ()
71      {
72          return MODULE_NAME;
73      }
74  }