PageRenderTime 131ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/groovy/com/onresolve/jira/groovy/customfield/CustomFieldConfiguration.groovy

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 203 lines | 128 code | 43 blank | 32 comment | 7 complexity | a61cbc7ac35cd4836c0fd4d7ac539a43 MD5 | raw file
  1. package com.onresolve.jira.groovy.customfield;
  2. import com.atlassian.core.util.ClassLoaderUtils
  3. import com.atlassian.jira.util.json.JSONObject
  4. import groovy.util.slurpersupport.GPathResult
  5. public class CustomFieldConfiguration {
  6. // todo: what the fuck are all these things
  7. private Long id;
  8. /**
  9. * The inline groovy script
  10. */
  11. private String script;
  12. /**
  13. * The groovy script file - use this OR the script. Use the file by preference
  14. */
  15. private String scriptFile;
  16. private String template;
  17. /**
  18. * The entity to take templates from, eg float, datetime etc
  19. */
  20. private String modelTemplate;
  21. /**
  22. * A custom template if ever implemented, needs inline as well as file impls
  23. */
  24. private String customTemplate;
  25. /**
  26. * The name of the custom field associated with this configuration
  27. */
  28. private String name;
  29. /**
  30. * The field config scheme associated with this configuration
  31. */
  32. private String fieldConfigSchemeName;
  33. /**
  34. * The velocity template which corresponds with this field's model template
  35. */
  36. private String viewTemplate;
  37. private Long cfId;
  38. public String getFieldConfigSchemeName() {
  39. return fieldConfigSchemeName;
  40. }
  41. public String getViewTemplate() {
  42. if (modelTemplate) {
  43. if (viewTemplate) {
  44. return viewTemplate
  45. }
  46. if (modelTemplate == "html") {
  47. viewTemplate = "templates/customfield/view-scriptedfield-html.vm"
  48. }
  49. else {
  50. GPathResult gpath = new XmlSlurper().parse(ClassLoaderUtils.getResourceAsStream("system-customfieldtypes-plugin.xml", getClass()))
  51. viewTemplate = gpath."customfield-type".find {cft ->
  52. cft."@key".text() == modelTemplate
  53. }."resource".find {
  54. it."@name" == "view"
  55. }."@location".text()
  56. }
  57. return viewTemplate
  58. }
  59. return null
  60. }
  61. public void setFieldConfigSchemeName(String fieldConfigSchemeName) {
  62. this.fieldConfigSchemeName = fieldConfigSchemeName;
  63. }
  64. public static String NAME = "name";
  65. public static String FCS_NAME = "fcsName";
  66. public static String SCRIPT = "script";
  67. public static String SCRIPT_FILE = "scriptFile";
  68. public static String TEMPLATE = "template";
  69. public static String MODEL_TEMPLATE = "modelTemplate";
  70. public static String CUSTOM_TEMPLATE = "customTemplate";
  71. public CustomFieldConfiguration() {
  72. }
  73. public CustomFieldConfiguration(String jsonString) {
  74. JSONObject jsonObject = new JSONObject(jsonString)
  75. this.setName(jsonObject.get(NAME) as String)
  76. this.setScript(jsonObject.get(SCRIPT) as String)
  77. this.setScriptFile(jsonObject.has(SCRIPT_FILE) ? jsonObject.get(SCRIPT_FILE) as String : null)
  78. this.setTemplate(jsonObject.get(TEMPLATE) as String)
  79. this.setModelTemplate(jsonObject.get(MODEL_TEMPLATE) as String)
  80. this.setCustomTemplate(jsonObject.get(CUSTOM_TEMPLATE) as String)
  81. }
  82. public CustomFieldConfiguration(String name, String fieldConfigSchemeName, Long cfId) {
  83. this.name = name;
  84. this.fieldConfigSchemeName = fieldConfigSchemeName;
  85. this.cfId = cfId;
  86. }
  87. /*
  88. // todo: unused
  89. public CustomFieldConfiguration(Long id, String script, String template, String modelTemplate, String customTemplate) {
  90. this.id = id;
  91. this.script = script;
  92. this.template = template;
  93. this.modelTemplate = modelTemplate;
  94. this.customTemplate = customTemplate;
  95. }
  96. */
  97. @SuppressWarnings("unchecked")
  98. public JSONObject asJsonString() {
  99. return new JSONObject([(getId().toString()): asMap()]);
  100. }
  101. public Map asMap() {
  102. return [
  103. (SCRIPT): getScript() ?: JSONObject.NULL,
  104. (TEMPLATE): getTemplate() ?: JSONObject.NULL,
  105. (SCRIPT_FILE): getScriptFile() ?: JSONObject.NULL,
  106. (MODEL_TEMPLATE): getModelTemplate() ?: JSONObject.NULL,
  107. (CUSTOM_TEMPLATE): getCustomTemplate() ?: JSONObject.NULL,
  108. (NAME): getName(),
  109. (FCS_NAME): getFieldConfigSchemeName()
  110. ];
  111. }
  112. public Long getId() {
  113. return id;
  114. }
  115. public void setId(Long id) {
  116. this.id = id;
  117. }
  118. public String getScript() {
  119. return script;
  120. }
  121. public void setScript(String script) {
  122. this.script = script;
  123. }
  124. public String getScriptFile() {
  125. return scriptFile;
  126. }
  127. public void setScriptFile(String scriptFile) {
  128. this.scriptFile = scriptFile;
  129. }
  130. public String getTemplate() {
  131. return template;
  132. }
  133. public void setTemplate(String template) {
  134. this.template = template;
  135. }
  136. public String getModelTemplate() {
  137. return modelTemplate;
  138. }
  139. public void setModelTemplate(String modelTemplate) {
  140. this.modelTemplate = modelTemplate;
  141. }
  142. public String getCustomTemplate() {
  143. return customTemplate;
  144. }
  145. public void setCustomTemplate(String customTemplate) {
  146. this.customTemplate = customTemplate;
  147. }
  148. public String getName() {
  149. return name;
  150. }
  151. public void setName(String name) {
  152. this.name = name;
  153. }
  154. public Long getCfId() {
  155. return cfId
  156. }
  157. public void setCfId(Long cfId) {
  158. this.cfId = cfId
  159. }
  160. }