PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/deblauwe/jira/plugin/databasevalues/config/DatabaseValuesCFParametersLoader.java

https://bitbucket.org/wimdeblauwe/jdvp
Java | 247 lines | 211 code | 21 blank | 15 comment | 44 complexity | 4fb155d88d9997722dda3187ebb72cf4 MD5 | raw file
  1. package org.deblauwe.jira.plugin.databasevalues.config;
  2. import com.atlassian.core.util.ClassLoaderUtils;
  3. import com.atlassian.jira.issue.fields.CustomField;
  4. import org.apache.log4j.Logger;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.net.URL;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.Properties;
  13. /**
  14. * Created by Wim Deblauwe
  15. */
  16. public class DatabaseValuesCFParametersLoader
  17. {
  18. private static final Logger logger = Logger.getLogger( DatabaseValuesCFParametersLoader.class );
  19. /**
  20. * Cache is refreshed every 15 minutes by default
  21. */
  22. static final long DEFAULT_CACHE_TIMEOUT = 15 * 60 * 1000;
  23. private static final int DEFAULT_NUMBER_OF_PROJECTS_IN_CACHE = 1;
  24. private static final int DEFAULT_EDITPATTERN_WIDTH = 50;
  25. private static final int DEFAULT_SEARCHPATTERN_WIDTH = 50;
  26. private DatabaseValuesCFParametersLoader()
  27. {
  28. }
  29. /**
  30. * Loads the parameters for the {@link org.deblauwe.jira.plugin.databasevalues.DatabaseValuesCFType}
  31. * from a properties file from disk.
  32. *
  33. * @param customField the customfield to load the properties of
  34. * @return a {@link org.deblauwe.jira.plugin.databasevalues.config.DatabaseValuesCFParameters} object.
  35. * Null if the properties could not be loaded.
  36. */
  37. public static DatabaseValuesCFParameters loadParameters( CustomField customField )
  38. {
  39. DatabaseValuesCFParameters result;
  40. try
  41. {
  42. Properties properties = new Properties();
  43. InputStream stream = ClassLoaderUtils.getResourceAsStream( "jira-database-values-plugin-" + customField.getIdAsLong() +
  44. ".properties", DatabaseValuesCFParametersLoader.class );
  45. if( stream == null )
  46. {
  47. URL resource = ClassLoaderUtils.getResource( "jira-database-values-plugin-" + customField.getIdAsLong() + ".properties", DatabaseValuesCFParametersLoader.class );
  48. if( resource != null )
  49. {
  50. File propFile = new File( resource.getFile() );
  51. stream = new FileInputStream( propFile );
  52. }
  53. }
  54. if (stream != null)
  55. {
  56. properties.load( stream );
  57. result = new DatabaseValuesCFParameters();
  58. if (properties.containsKey("database.driver")
  59. && properties.containsKey("database.user")
  60. && properties.containsKey("database.password")
  61. && properties.containsKey("database.connection.url"))
  62. {
  63. result.setDatabaseDriver(properties.getProperty("database.driver").trim());
  64. result.setDatabaseUser(properties.getProperty("database.user").trim());
  65. result.setDatabasePassword(properties.getProperty("database.password").trim());
  66. result.setDatabaseConnectionUrl(properties.getProperty("database.connection.url").trim());
  67. }
  68. else
  69. {
  70. result.setUseInternalJiraDatabase(true);
  71. }
  72. result.setSqlQuery( properties.getProperty( "sql.query" ).trim() );
  73. if( properties.getProperty( "sql.query.search" ) != null )
  74. {
  75. result.setSqlQuerySearch( properties.getProperty( "sql.query.search" ) );
  76. }
  77. else
  78. {
  79. result.setSqlQuerySearch( null );
  80. }
  81. result.setPrimaryKeyColumnNumber( Integer.parseInt( properties.getProperty( "primarykey.column.number" ).trim() ) );
  82. result.setRenderingViewPattern( properties.getProperty( "rendering.viewpattern" ).trim() );
  83. result.setRenderingEditPattern( properties.getProperty( "rendering.editpattern" ).trim() );
  84. if (properties.getProperty( "rendering.editwidth" ) != null)
  85. {
  86. result.setRenderingEditWidth( Integer.parseInt( properties.getProperty( "rendering.editwidth" ).trim() ) );
  87. }
  88. else
  89. {
  90. result.setRenderingEditWidth( DEFAULT_EDITPATTERN_WIDTH );
  91. }
  92. if (properties.getProperty( "rendering.searchwidth" ) != null)
  93. {
  94. result.setRenderingSearchWidth( Integer.parseInt( properties.getProperty( "rendering.searchwidth" ).trim() ) );
  95. }
  96. else
  97. {
  98. result.setRenderingSearchWidth( DEFAULT_SEARCHPATTERN_WIDTH );
  99. }
  100. result.setRenderingSearchPattern( properties.getProperty( "rendering.searchpattern" ).trim() );
  101. String changeLogViewPattern = properties.getProperty("rendering.changelog.viewpattern");
  102. if( changeLogViewPattern != null )
  103. {
  104. result.setChangeLogViewPattern( changeLogViewPattern.trim() );
  105. }
  106. else
  107. {
  108. result.setChangeLogViewPattern( result.getRenderingViewPattern() );
  109. }
  110. String statisticsViewPattern = properties.getProperty("rendering.statistics.viewpattern");
  111. if( statisticsViewPattern != null )
  112. {
  113. result.setStatisticsViewPattern( statisticsViewPattern.trim() );
  114. }
  115. else
  116. {
  117. result.setStatisticsViewPattern( result.getRenderingViewPattern() );
  118. }
  119. if (properties.getProperty( "cache.timeout" ) != null)
  120. {
  121. result.setCacheTimeout( Long.parseLong( properties.getProperty( "cache.timeout" ).trim() ) );
  122. }
  123. else
  124. {
  125. result.setCacheTimeout( DEFAULT_CACHE_TIMEOUT );
  126. }
  127. if (properties.getProperty( "cache.maximum.projects" ) != null)
  128. {
  129. result.setNumberOfProjectsInCache( Integer.parseInt( properties.getProperty( "cache.maximum.projects" ).trim() ) );
  130. }
  131. else
  132. {
  133. result.setNumberOfProjectsInCache( DEFAULT_NUMBER_OF_PROJECTS_IN_CACHE );
  134. }
  135. // This property is needed for sorting when you depend on the project key
  136. if (properties.getProperty( "primarykey.column.name" ) != null)
  137. {
  138. result.setPrimaryKeyColumnName( properties.getProperty( "primarykey.column.name" ).trim() );
  139. }
  140. if (properties.getProperty( "edit.type" ) != null)
  141. {
  142. if (Integer.parseInt( properties.getProperty( "edit.type" ).trim() ) == DatabaseValuesCFParameters.EDIT_TYPE_AJAX_INPUT)
  143. {
  144. result.setEditType( DatabaseValuesCFParameters.EDIT_TYPE_AJAX_INPUT );
  145. }
  146. else if (Integer.parseInt( properties.getProperty( "edit.type" ).trim() ) == DatabaseValuesCFParameters.EDIT_TYPE_CASCADING_SELECT)
  147. {
  148. result.setEditType( DatabaseValuesCFParameters.EDIT_TYPE_CASCADING_SELECT );
  149. result.setGroupingColumnNumber( Integer.parseInt( properties.getProperty( "rendering.editpattern.group.column.number" ).trim() ) );
  150. }
  151. else
  152. {
  153. result.setEditType( DatabaseValuesCFParameters.EDIT_TYPE_COMBOBOX );
  154. }
  155. }
  156. else
  157. {
  158. result.setEditType( DatabaseValuesCFParameters.EDIT_TYPE_COMBOBOX );
  159. }
  160. if( properties.getProperty( "search.type") != null )
  161. {
  162. if( Integer.parseInt( properties.getProperty( "search.type" ).trim() ) == DatabaseValuesCFParameters.SEARCH_TYPE_AJAX_INPUT)
  163. {
  164. result.setSearchType( DatabaseValuesCFParameters.SEARCH_TYPE_AJAX_INPUT );
  165. }
  166. else
  167. {
  168. result.setSearchType( DatabaseValuesCFParameters.SEARCH_TYPE_LIST );
  169. }
  170. }
  171. else
  172. {
  173. result.setSearchType( DatabaseValuesCFParameters.SEARCH_TYPE_LIST );
  174. }
  175. if (properties.getProperty( "rendering.sortpattern" ) != null)
  176. {
  177. result.setSortingViewPattern( properties.getProperty( "rendering.sortpattern" ).trim() );
  178. }
  179. else
  180. {
  181. result.setSortingViewPattern( result.getRenderingViewPattern() );
  182. }
  183. if( properties.getProperty( "sql.linkedfields") != null )
  184. {
  185. logger.warn( "Linked fields are no longer supported!" );
  186. }
  187. result.setJqlQueries( loadJqlQueries(properties) );
  188. }
  189. else
  190. {
  191. logger.error("Could not load file 'jira-database-values-plugin-" + customField.getIdAsLong() + ".properties!");
  192. result = null;
  193. }
  194. }
  195. catch (IOException e)
  196. {
  197. logger.error( e.getMessage(), e );
  198. result = null;
  199. }
  200. return result;
  201. }
  202. private static Map<String, String> loadJqlQueries(Properties properties)
  203. {
  204. int i = 1;
  205. Map<String, String> jqlQueries = new HashMap<String, String>();
  206. String queryReferencePropertyKey = "jql." + i + ".query.reference";
  207. String queryReferenceProperty = properties.getProperty(queryReferencePropertyKey);
  208. while( queryReferenceProperty != null )
  209. {
  210. String queryPropertyKey = "jql." + i + ".query";
  211. String queryProperty = properties.getProperty(queryPropertyKey);
  212. if( queryProperty != null )
  213. {
  214. jqlQueries.put( queryReferenceProperty, queryProperty );
  215. }
  216. else
  217. {
  218. logger.warn( "Found '" + queryReferencePropertyKey + "' but no matching query (Was looking for '" + queryPropertyKey +"')" );
  219. }
  220. i++;
  221. queryReferencePropertyKey = "jql." + i + ".query.reference";
  222. queryReferenceProperty = properties.getProperty(queryReferencePropertyKey);
  223. }
  224. return jqlQueries;
  225. }
  226. }