PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/atlassian/uwc/ui/listeners/ExportWikiListener.java

https://bitbucket.org/atlassianlabs/universal-wiki-connector
Java | 346 lines | 229 code | 32 blank | 85 comment | 14 complexity | 61e3db95e148569dd1a7fcd003280afa MD5 | raw file
  1. package com.atlassian.uwc.ui.listeners;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.sql.SQLException;
  7. import java.util.Map;
  8. import javax.swing.JComboBox;
  9. import org.apache.log4j.Logger;
  10. import com.atlassian.uwc.exporters.Exporter;
  11. import com.atlassian.uwc.ui.FeedbackWindow;
  12. import com.atlassian.uwc.ui.State;
  13. import com.atlassian.uwc.ui.SwingWorker;
  14. import com.atlassian.uwc.ui.UWCGuiModel;
  15. import com.atlassian.uwc.ui.UWCUserSettings;
  16. import com.atlassian.uwc.util.PropertyFileManager;
  17. /**
  18. * object that listens for and initiates an export
  19. */
  20. public class ExportWikiListener extends ExportHandler implements ActionListener, FeedbackHandler, FeedbackCanceller {
  21. Logger log = Logger.getLogger(this.getClass());
  22. /**
  23. * represents feedback that might be communicated to the user
  24. */
  25. Feedback feedback = Feedback.NONE;
  26. /**
  27. * window that provides the user with feedback
  28. */
  29. private FeedbackWindow feedbackWindow;
  30. /**
  31. * represents the internal state used to represent feedback that will be communicated to the user
  32. */
  33. private State state;
  34. /**
  35. * thread used to run the export
  36. */
  37. private Worker exportThread;
  38. /**
  39. * object that handles the wiki specific details of the export
  40. */
  41. private Exporter exporter;
  42. /**
  43. * @param wikitypes the component that will be set to the desired wiki on export
  44. * @param model object representing the UWC model. Important for getting settings
  45. * @param dir the directory where export.xxx.properties files exist
  46. * @param feedbackWindow window where the user feedback will be displayed.
  47. */
  48. public ExportWikiListener(JComboBox wikitypes, UWCGuiModel model, String dir, FeedbackWindow feedbackWindow) {
  49. this.wikitypes = wikitypes;
  50. this.model = model;
  51. this.dir = dir;
  52. this.feedbackWindow = feedbackWindow;
  53. }
  54. /**
  55. * launches the feedback window, and starts the export
  56. * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  57. */
  58. public void actionPerformed(ActionEvent arg0) {
  59. //launch feedback window if required
  60. boolean feedbackOn = Boolean.parseBoolean(
  61. this.model.getSetting(UWCUserSettings.Setting.FEEDBACK_OPTION));
  62. if (feedbackOn) {
  63. this.feedbackWindow.launch();
  64. }
  65. this.feedbackWindow.updateFeedback(getInitialFeedback());
  66. //set up cancel handling in the feedback window
  67. this.feedbackWindow.setCurrent(this);
  68. this.feedbackWindow.cancelOn();
  69. //export
  70. this.exportThread = attemptExport();
  71. }
  72. /**
  73. * @return starts the export as a seperate thread
  74. */
  75. private Worker attemptExport() {
  76. Worker worker = new Worker(this);
  77. worker.start();
  78. return worker;
  79. }
  80. /**
  81. * sends the last feedback to the feedback window
  82. */
  83. void displayFinalFeedback() {
  84. displayFeedback(this.feedback);
  85. }
  86. /**
  87. * sends the given feedback to the feedback window
  88. * @param feedback
  89. */
  90. void displayFeedback(Feedback feedback) {
  91. String description = getFeedbackDescription(feedback);
  92. getState().updateNote(description);
  93. }
  94. /**
  95. * @param feedback
  96. * @return a String representing the given feedback
  97. */
  98. String getFeedbackDescription(Feedback feedback) {
  99. String description = "";
  100. description += "\nExport Status... ";
  101. if (feedback != Feedback.OK) {
  102. description += feedback.toString() + "\n";
  103. description += "Problem exporting wiki: ";
  104. switch(feedback) {
  105. case DB_FAILURE:
  106. description += "\nProblem logging into database. "
  107. + "Check that database is available, and "
  108. + "that exporter settings are correct: "
  109. + this.getExporterPropsPath(this.getCurrentWikitype());
  110. break;
  111. case NO_EXPORTER_FILE:
  112. description += "Could not find exporter settings at location: "
  113. + this.getExporterPropsPath(this.getCurrentWikitype());
  114. break;
  115. default:
  116. description += feedback.toString();
  117. }
  118. } else {
  119. description += "SUCCESS!";
  120. }
  121. return description + "\n";
  122. }
  123. /**
  124. * @return sends some initializing feedback to the feedback window
  125. */
  126. private String getInitialFeedback() {
  127. String description = "";
  128. description +=
  129. "\n***************\n\n" +
  130. "Exporting Wiki...\n";
  131. return description;
  132. }
  133. /**
  134. * sets up the feedback window, validates the chosen wiki type,
  135. * loads the export properties, and then runs the export.
  136. * heavily influenced by ChooseExporterForm.runExporter
  137. */
  138. protected void exportWiki() {
  139. //setup feedback window - we're going to use max = 100 for the progress bar
  140. State state = getState();
  141. //validate wiki type
  142. state.updateNote("Validating Wiki Type.");
  143. state.updateProgress(10);
  144. String wikitype = this.getCurrentWikitype();
  145. String exporterPropPath = getExporterPropsPath(wikitype);
  146. File exporterConfFile = new File(exporterPropPath);
  147. if (!exporterConfFile.exists()) {
  148. String message = "Could not find export properties file: " + exporterPropPath;
  149. handleLastError(message, Feedback.NO_EXPORTER_FILE);
  150. return;
  151. }
  152. //load export properties
  153. state.updateNote("Loading Export Properties");
  154. state.updateProgress(10);
  155. Map exporterProperties = null;
  156. try {
  157. exporterProperties = PropertyFileManager.loadPropertiesFile(exporterConfFile.getPath());
  158. } catch (IOException e) {
  159. String message = "Could not load exporter properties file: " + exporterPropPath;
  160. handleLastError(message, Feedback.BAD_SETTINGS_FILE);
  161. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  162. return;
  163. }
  164. assert exporterProperties != null;
  165. //load export class
  166. state.updateNote("Loading Export Class");
  167. state.updateProgress(10);
  168. String exporterClassName = (String) exporterProperties.get("exporter.class");
  169. Class exporterClass = null;
  170. try {
  171. exporterClass = Class.forName(exporterClassName);
  172. } catch (ClassNotFoundException e) {
  173. String message = "The exporter class was not found: "+exporterClassName;
  174. handleLastError(message, Feedback.BAD_EXPORTER_CLASS);
  175. return;
  176. }
  177. //exporter
  178. state.updateNote("Exporting... ");
  179. state.updateProgress(10);
  180. feedback = Feedback.OK;
  181. try {
  182. this.exporter = (Exporter) exporterClass.newInstance();
  183. this.exporter.export(exporterProperties);
  184. } catch (InstantiationException e) {
  185. feedback = Feedback.BAD_EXPORTER_CLASS;
  186. handleLastError("", feedback);
  187. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  188. exportcleanup();
  189. return;
  190. } catch (IllegalAccessException e) {
  191. feedback = Feedback.BAD_EXPORTER_CLASS;
  192. handleLastError("", feedback);
  193. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  194. exportcleanup();
  195. return;
  196. } catch (ClassNotFoundException e) {
  197. feedback = Feedback.DB_DRIVER_FAILURE;
  198. handleLastError("", feedback);
  199. e.printStackTrace();
  200. exportcleanup();
  201. return;
  202. } catch (SQLException e) {
  203. feedback = Feedback.DB_FAILURE;
  204. handleLastError("", feedback);
  205. e.printStackTrace();
  206. exportcleanup();
  207. return;
  208. } catch (Exception e) {
  209. feedback = Feedback.BAD_SETTING;
  210. handleLastError("", feedback);
  211. e.printStackTrace();
  212. exportcleanup();
  213. return;
  214. }
  215. state.updateProgress(state.getMax());
  216. exportcleanup();
  217. }
  218. /**
  219. * @return creates or gets the State object representing the feedback for this export
  220. */
  221. private State getState() {
  222. if (this.state == null ) {
  223. State state = new State("Initializing Exporter", 0, 100);
  224. this.state = state;
  225. this.feedbackWindow.setState(state);
  226. }
  227. return this.state;
  228. }
  229. /**
  230. * cleans up the feedback window and the export objects
  231. */
  232. private void exportcleanup() {
  233. this.feedbackWindow.end();
  234. this.feedbackWindow.cancelOff();
  235. this.feedbackWindow.setCurrent(null);
  236. this.exportThread = null;
  237. this.exporter = null;
  238. }
  239. /**
  240. * updates the feedback state with the given message and feedback,
  241. * and ends the progress bar.
  242. * @param message
  243. * @param feedback
  244. */
  245. private void handleLastError(String message, Feedback feedback) {
  246. this.feedback = feedback;
  247. log.error(message);
  248. this.state.updateNote(message);
  249. this.state.updateProgress(this.state.getMax());
  250. }
  251. /**
  252. * @param wikitype
  253. * @return the filepath for the given wikitype's properties file
  254. */
  255. private String getExporterPropsPath(String wikitype) {
  256. String exporterPropPath =
  257. this.dir +
  258. File.separator +
  259. "exporter." +
  260. wikitype +
  261. ".properties";
  262. return exporterPropPath;
  263. }
  264. /**
  265. * @return the current feedback
  266. */
  267. protected Feedback getFeedback() {
  268. return feedback;
  269. }
  270. /**
  271. * cancel the current export
  272. * @see com.atlassian.uwc.ui.listeners.FeedbackCanceller#cancel()
  273. */
  274. public void cancel() {
  275. log.info("Begin Cancelling Export");
  276. if (this.exportThread != null) {
  277. this.exportThread.cancel();
  278. handleLastError("Export Cancelled. ", Feedback.CANCELLED);
  279. exportcleanup();
  280. }
  281. }
  282. /**
  283. * thread that handles the export
  284. */
  285. public class Worker extends SwingWorker {
  286. private ExportWikiListener listener;
  287. public Worker(ExportWikiListener listener) {
  288. this.listener = listener;
  289. }
  290. @Override
  291. public Object construct() {
  292. try {
  293. if (listener.isExportable()) {
  294. exportWiki();
  295. }
  296. else {
  297. feedback = Feedback.NO_EXPORTER_FILE;
  298. String feedbackDescription = getFeedbackDescription(feedback);
  299. log.error("Cannot export this wikitype: " + listener.getCurrentWikitype() +
  300. feedbackDescription);
  301. }
  302. } finally {
  303. //give back final feedback
  304. displayFinalFeedback();
  305. }
  306. return null;
  307. }
  308. public void cancel() {
  309. if (exporter != null) {
  310. exporter.cancel();
  311. }
  312. }
  313. }
  314. }