1   package com.atlassian.plugins.codegen.modules.jira;
2   
3   import java.io.File;
4   
5   import com.atlassian.plugins.codegen.annotations.Dependencies;
6   import com.atlassian.plugins.codegen.annotations.Dependency;
7   import com.atlassian.plugins.codegen.annotations.JiraPluginModuleCreator;
8   import com.atlassian.plugins.codegen.modules.AbstractPluginModuleCreator;
9   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
10  import com.atlassian.plugins.codegen.modules.common.Resource;
11  
12  import org.apache.commons.io.FilenameUtils;
13  import org.apache.commons.lang.StringUtils;
14  
15  /**
16   * @since 3.6
17   */
18  @JiraPluginModuleCreator
19  @Dependencies({
20          @Dependency(groupId = "org.mockito", artifactId = "mockito-all", version = "1.8.5", scope = "test")
21  })
22  public class ReportModuleCreator extends AbstractPluginModuleCreator<ReportProperties>
23  {
24  
25      public static final String MODULE_NAME = "Report";
26      private static final String TEMPLATE_PREFIX = "templates/jira/report/";
27  
28      //stub
29      private static final String CLASS_TEMPLATE = TEMPLATE_PREFIX + "Report.java.vtl";
30      private static final String UNIT_TEST_TEMPLATE = "templates/generic/GenericTest.java.vtl";
31      private static final String FUNC_TEST_TEMPLATE = TEMPLATE_PREFIX + "ReportFuncTest.java.vtl";
32      private static final String VIEW_TEMPLATE = "templates/common/actionview.vm.vtl";
33  
34      //examples
35      private static final String EXAMPLE_CLASS_TEMPLATE = TEMPLATE_PREFIX + "Example" + CLASS_TEMPLATE;
36  
37      private static final String PLUGIN_MODULE_TEMPLATE = TEMPLATE_PREFIX + "report-plugin.xml.vtl";
38  
39      @Override
40      public void createModule(PluginModuleLocation location, ReportProperties props) throws Exception
41      {
42          String packageName = props.getPackage();
43  
44          String classname = props.getClassname();
45  
46          if (props.includeExamples())
47          {
48              templateHelper.writeJavaClassFromTemplate(EXAMPLE_CLASS_TEMPLATE, classname, location.getSourceDirectory(), packageName, props);
49          } else
50          {
51              //main class
52              templateHelper.writeJavaClassFromTemplate(CLASS_TEMPLATE, classname, location.getSourceDirectory(), packageName, props);
53  
54              //unit test
55              templateHelper.writeJavaClassFromTemplate(UNIT_TEST_TEMPLATE, testClassname(classname), location.getTestDirectory(), packageName, props);
56  
57              //func test
58              //templateHelper.writeJavaClassFromTemplate(FUNC_TEST_TEMPLATE, funcTestClassname(classname), location.getTestDirectory(), funcTestPackageName(packageName), props);
59          }
60  
61          //since we know resources are velocity templates, let's create them
62          for (Resource resource : props.getResources())
63          {
64              if (resource.getType()
65                      .equals("i18n"))
66              {
67                  File resourceFile = new File(location.getResourcesDir(), resource.getLocation() + ".properties");
68                  if (!resourceFile.exists())
69                  {
70                      resourceFile.createNewFile();
71                  }
72              } else
73              {
74                  String resourcePath = FilenameUtils.separatorsToSystem(resource.getLocation());
75  
76                  if (resourcePath.startsWith("templates" + File.separator) || resourcePath.startsWith(File.separator + "templates" + File.separator))
77                  {
78                      resourcePath = StringUtils.substringAfter(resourcePath, "templates" + File.separator);
79                  }
80  
81                  File resourceFolder = new File(location.getTemplateDirectory(), FilenameUtils.getPath(resourcePath));
82                  String resourceFile = FilenameUtils.getName(resourcePath);
83  
84                  props.setProperty("CURRENT_VIEW", resourceFile);
85  
86                  templateHelper.writeFileFromTemplate(VIEW_TEMPLATE, FilenameUtils.getName(resourcePath), resourceFolder, props);
87              }
88          }
89  
90          addModuleToPluginXml(PLUGIN_MODULE_TEMPLATE, location, props);
91      }
92  
93  
94      @Override
95      public String getModuleName
96              ()
97      {
98          return MODULE_NAME;
99      }
100 }