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

/src/main/java/org/sylfra/idea/plugins/revu/externalizing/impl/IssueConverter.java

http://idea-revu.googlecode.com/
Java | 209 lines | 178 code | 21 blank | 10 comment | 44 complexity | 799adc7218e012106f23fcca82f0533a MD5 | raw file
  1. package org.sylfra.idea.plugins.revu.externalizing.impl;
  2. import com.intellij.openapi.project.Project;
  3. import com.intellij.openapi.vfs.VirtualFile;
  4. import com.thoughtworks.xstream.converters.MarshallingContext;
  5. import com.thoughtworks.xstream.converters.UnmarshallingContext;
  6. import com.thoughtworks.xstream.io.HierarchicalStreamReader;
  7. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
  8. import org.sylfra.idea.plugins.revu.model.*;
  9. import org.sylfra.idea.plugins.revu.utils.RevuVfsUtils;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.SortedSet;
  13. import java.util.TreeSet;
  14. /**
  15. * @author <a href="mailto:syllant@gmail.com">Sylvain FRANCOIS</a>
  16. * @version $Id: IssueConverter.java 29 2010-04-21 20:51:59Z syllant $
  17. */
  18. class IssueConverter extends AbstractConverter
  19. {
  20. public boolean canConvert(Class type)
  21. {
  22. return Issue.class.equals(type);
  23. }
  24. public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context)
  25. {
  26. Issue issue = (Issue) source;
  27. Project project = getProject(context);
  28. if (issue.getFile() != null)
  29. {
  30. String filePath = RevuVfsUtils.buildRelativePath(project, issue.getFile());
  31. writer.addAttribute("filePath", filePath);
  32. }
  33. writer.addAttribute("summary", issue.getSummary());
  34. if (issue.getVcsRev() != null)
  35. {
  36. writer.addAttribute("vcsRev", issue.getVcsRev());
  37. }
  38. if (issue.getLocalRev() != null)
  39. {
  40. writer.addAttribute("localRev", issue.getLocalRev());
  41. }
  42. writer.addAttribute("lineStart", String.valueOf(issue.getLineStart()));
  43. writer.addAttribute("lineEnd", String.valueOf(issue.getLineEnd()));
  44. writer.addAttribute("hash", String.valueOf(issue.getHash()));
  45. List<IssueTag> tagList = issue.getTags();
  46. if ((tagList != null) && (!tagList.isEmpty()))
  47. {
  48. SortedSet<IssueTag> tags = new TreeSet<IssueTag>(tagList);
  49. writer.addAttribute("tags", ConverterUtils.toString(tags, ",", false));
  50. }
  51. if (issue.getPriority() != null)
  52. {
  53. writer.addAttribute("priority", issue.getPriority().getName());
  54. }
  55. writer.addAttribute("status", issue.getStatus().toString().toLowerCase());
  56. // Assignees
  57. List<User> assignees = issue.getAssignees();
  58. if ((assignees != null) && (!assignees.isEmpty()))
  59. {
  60. SortedSet<User> users = new TreeSet<User>(assignees);
  61. writer.addAttribute("assignees", ConverterUtils.toString(users, ",", false));
  62. }
  63. // History
  64. writer.startNode("history");
  65. context.convertAnother(issue.getHistory());
  66. writer.endNode();
  67. // Desc
  68. if ((issue.getDesc() != null) && (issue.getDesc().length() > 0))
  69. {
  70. writer.startNode("desc");
  71. writer.setValue(issue.getDesc());
  72. writer.endNode();
  73. }
  74. // Notes
  75. List<IssueNote> notes = issue.getNotes();
  76. if ((notes != null) && (!notes.isEmpty()))
  77. {
  78. writer.startNode("notes");
  79. for (IssueNote note : notes)
  80. {
  81. writer.startNode("note");
  82. context.convertAnother(note);
  83. writer.endNode();
  84. }
  85. writer.endNode();
  86. }
  87. }
  88. public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
  89. {
  90. String summary = reader.getAttribute("summary");
  91. String filePath = reader.getAttribute("filePath");
  92. String vcsRev = reader.getAttribute("vcsRev");
  93. String localRev = reader.getAttribute("localRev");
  94. String lineStart = reader.getAttribute("lineStart");
  95. String hash = reader.getAttribute("hash");
  96. String lineEnd = reader.getAttribute("lineEnd");
  97. String tags = reader.getAttribute("tags");
  98. String priority = reader.getAttribute("priority");
  99. String status = reader.getAttribute("status");
  100. String assignees = reader.getAttribute("assignees");
  101. Review review = getReview(context);
  102. Issue issue = new Issue();
  103. issue.setReview(review);
  104. Project project = getProject(context);
  105. if (filePath != null)
  106. {
  107. VirtualFile file = RevuVfsUtils.findVFileFromRelativeFile(project, filePath);
  108. issue.setFile(file);
  109. }
  110. issue.setVcsRev(vcsRev);
  111. issue.setLocalRev(localRev);
  112. issue.setLineStart(Integer.parseInt(lineStart));
  113. issue.setLineEnd(Integer.parseInt(lineEnd));
  114. if (hash != null)
  115. {
  116. issue.setHash(Integer.parseInt(hash));
  117. }
  118. if (tags != null)
  119. {
  120. String[] tagNames = tags.split(",");
  121. List<IssueTag> tagSet = new ArrayList<IssueTag>();
  122. for (String tagName : tagNames)
  123. {
  124. IssueTag issueTag = review.getDataReferential().getIssueTag(tagName);
  125. if (issueTag == null)
  126. {
  127. // @TODO report error to user
  128. logger.warn("Can't find tag in referential. Tag: '" + tagName + "', review: " + review.getFile());
  129. }
  130. else
  131. {
  132. tagSet.add(issueTag);
  133. }
  134. }
  135. issue.setTags(tagSet);
  136. }
  137. if (assignees != null)
  138. {
  139. String[] userLogins = assignees.split(",");
  140. List<User> userSet = new ArrayList<User>();
  141. for (String login : userLogins)
  142. {
  143. User user = review.getDataReferential().getUser(login, true);
  144. if (user == null)
  145. {
  146. // @TODO report error to user
  147. logger.warn("Can't find user in referential. Login:'" + login + "', review: " + review.getFile());
  148. }
  149. else
  150. {
  151. userSet.add(user);
  152. }
  153. }
  154. issue.setAssignees(userSet);
  155. }
  156. if (priority != null)
  157. {
  158. issue.setPriority(review.getDataReferential().getIssuePriority(priority));
  159. }
  160. issue.setStatus(IssueStatus.valueOf(status.toUpperCase()));
  161. issue.setSummary(summary);
  162. while (reader.hasMoreChildren())
  163. {
  164. reader.moveDown();
  165. if ("history".equals(reader.getNodeName()))
  166. {
  167. issue.setHistory((History) context.convertAnother(issue, History.class));
  168. }
  169. else if ("desc".equals(reader.getNodeName()))
  170. {
  171. issue.setDesc(reader.getValue());
  172. }
  173. else if ("notes".equals(reader.getNodeName()))
  174. {
  175. List<IssueNote> notes = new ArrayList<IssueNote>();
  176. while (reader.hasMoreChildren())
  177. {
  178. reader.moveDown();
  179. notes.add((IssueNote) context.convertAnother(notes, IssueNote.class));
  180. reader.moveUp();
  181. }
  182. issue.setNotes(notes);
  183. }
  184. reader.moveUp();
  185. }
  186. return issue;
  187. }
  188. }