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 CustomFieldSearcherModuleCreator extends AbstractPluginModuleCreator<CustomFieldSearcherProperties>
23  {
24  
25      public static final String MODULE_NAME = "Custom Field Searcher";
26      private static final String TEMPLATE_PREFIX = "templates/jira/customfield/searcher/";
27  
28      //stub
29      private static final String CLASS_TEMPLATE = TEMPLATE_PREFIX + "CustomFieldSearcher.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 + "CustomFieldSearcherFuncTest.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 + "customfield-searcher-plugin.xml.vtl";
38  
39      @Override
40      public void createModule(PluginModuleLocation location, CustomFieldSearcherProperties props) throws Exception
41      {
42          String packageName = props.getPackage();
43          String classname = props.getClassname();
44  
45          if (props.includeExamples())
46          {
47              templateHelper.writeJavaClassFromTemplate(EXAMPLE_CLASS_TEMPLATE, classname, location.getSourceDirectory(), packageName, props);
48          } else
49          {
50              if (props.generateClass())
51              {
52                  //main class
53                  templateHelper.writeJavaClassFromTemplate(CLASS_TEMPLATE, classname, location.getSourceDirectory(), packageName, props);
54  
55                  //unit test
56                  templateHelper.writeJavaClassFromTemplate(UNIT_TEST_TEMPLATE, testClassname(classname), location.getTestDirectory(), packageName, props);
57  
58                  //func test
59                  //templateHelper.writeJavaClassFromTemplate(FUNC_TEST_TEMPLATE, funcTestClassname(classname), location.getTestDirectory(), funcTestPackageName(packageName), props);
60              }
61          }
62  
63          //since we know resources are velocity templates, let's create them
64          for (Resource resource : props.getResources())
65          {
66              String resourcePath = FilenameUtils.separatorsToSystem(resource.getLocation());
67  
68              if (resourcePath.startsWith("templates" + File.separator) || resourcePath.startsWith(File.separator + "templates" + File.separator))
69              {
70                  resourcePath = StringUtils.substringAfter(resourcePath, "templates" + File.separator);
71              }
72  
73              File resourceFolder = new File(location.getTemplateDirectory(), FilenameUtils.getPath(resourcePath));
74              String resourceFile = FilenameUtils.getName(resourcePath);
75  
76              props.setProperty("CURRENT_VIEW", resourceFile);
77  
78              templateHelper.writeFileFromTemplate(VIEW_TEMPLATE, FilenameUtils.getName(resourcePath), resourceFolder, props);
79          }
80  
81          addModuleToPluginXml(PLUGIN_MODULE_TEMPLATE, location, props);
82      }
83  
84  
85      @Override
86      public String getModuleName()
87      {
88          return MODULE_NAME;
89      }
90  }