From 5ff4665a07cb4b77f43597ce56919fd9fad6c3c1 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Thu, 3 Nov 2011 02:39:36 -0500 Subject: [PATCH] Starting work on extended attributes. * `Issue` implementation is tentatively complete. * Started ona direction for implementation with `FileIssue`. * Revisiting the code for `XmlIssue` to polish it up. The XML code was never really finished, but it should be for 3.0.x --- libpit/src/com/jdbernard/pit/Issue.groovy | 20 +++--- .../com/jdbernard/pit/file/FileIssue.groovy | 61 ++++++++++++++++--- .../src/com/jdbernard/pit/xml/XmlIssue.groovy | 15 ++++- 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/libpit/src/com/jdbernard/pit/Issue.groovy b/libpit/src/com/jdbernard/pit/Issue.groovy index ca3ce71..2d4fbf8 100755 --- a/libpit/src/com/jdbernard/pit/Issue.groovy +++ b/libpit/src/com/jdbernard/pit/Issue.groovy @@ -9,8 +9,9 @@ public abstract class Issue { protected Status status protected int priority protected String text - protected Date deliveryDate - protected Date creationDate + protected String title + + protected Map extendedPropeties = [:] Issue(String id, Category c = Category.TASK, Status s = Status.NEW, int p = 9) { @@ -18,8 +19,6 @@ public abstract class Issue { this.category = c this.status = s this.priority = p - this.creationDate = new Date() - this.deliveryDate = null } public String getId() { return id; } @@ -48,19 +47,18 @@ public abstract class Issue { priority = Math.min(9, Math.max(0, p)) } - public String getTitle() { return text.readLines()[0] } + public String getTitle() { return title } + + public String setTitle(String t) throws IOException { title = t } public String getText() { return text } public void setText(String t) throws IOException { text = t } - public boolean hasDelivery() { return deliveryDate == null } + public def propertyMissing(String name) { extendedProperties[name] } - public Date getCreationDate() { return creationDate } - - public Date getDeliveryDate() { return deliveryDate } - - public void setDeliveryDate(Date dd) { deliveryDate = dd } + public def propertyMissing(String name, def value) { + extendedProperties[name] = value } @Override public String toString() { diff --git a/libpit/src/com/jdbernard/pit/file/FileIssue.groovy b/libpit/src/com/jdbernard/pit/file/FileIssue.groovy index 8e385ed..6598cfe 100755 --- a/libpit/src/com/jdbernard/pit/file/FileIssue.groovy +++ b/libpit/src/com/jdbernard/pit/file/FileIssue.groovy @@ -25,6 +25,7 @@ public class FileIssue extends Issue { this.source = file super.@text = file.text + super.@title = super.@text.readLines()[0] } public void setCategory(Category c) throws IOException { @@ -73,15 +74,14 @@ public class FileIssue extends Issue { return makeFilename(id, category, status, priority) } - public void setText(String text) throws IOException { - try { source.write(text) } - catch (IOException ioe) { - throw new IOException("I could not save the new text for this " - + "issue. I can not write to the file for this issue. I do not" - + " know why, I am sorry (maybe the file can not be reached).") - } + public void setTitle(String title) throws IOException { + super.setTitle(title) + writeFile() + } + public void setText(String text) throws IOException { super.setText(text) + writeFile() } boolean deleteFile() { return source.deleteDir() } @@ -107,4 +107,51 @@ public class FileIssue extends Issue { return id + category.symbol + status.symbol + priority + ".rst"; } + public static String formatIssue(Issue issue) { + def result = new StringBuilder() + result.append(title) + result.append("\n") + result.append("=".multiply(title.length())) + result.append("\n") + result.append(text) + result.append("\n----\n") + + // If there are any extended properties, let's write those. + if (super.@extendedProperties.size() > 0) { + def extOutput = [:] + def maxKeyLen = 0 + def maxValLen = 0 + + // Find the longest key and value, convert all to strings. + super.@extendedProperties.each { key, val -> + def ks = key.toString(), vs = val.toString() + extOutput[ks] = vs + if (ks.length() > maxKeyLen) { maxKeyLen = ks.length() } + if (vs.length() > maxKeyLen) { maxValLen = vs.length() } } + + result.append("=".multiply(maxKeyLen + 1)) + result.append(" ") + result.append("=".multiply(maxValLen)) + result.append("\n") + + extOutput.sort().each { key, val -> + result.append(key.padRight(maxKeyLen)) + result.append(": ") + result.append(val.padRight(maxValLen)) + result.append("\n") } + + result.append("=".multiply(maxKeyLen + 1)) + result.append(" ") + result.append("=".multiply(maxValLen)) + result.append("\n") }} + + protected void writeFile() { + try { source.write(formatIssue(this) } + catch (IOException ioe) { + throw new IOException("I could not save the new text for this " + + "issue. I can not write to the file for this issue. I do not" + + " know why, I am sorry (maybe the file can not be reached).") + } + } + } diff --git a/libpit/src/com/jdbernard/pit/xml/XmlIssue.groovy b/libpit/src/com/jdbernard/pit/xml/XmlIssue.groovy index 8d0898c..049e7cf 100755 --- a/libpit/src/com/jdbernard/pit/xml/XmlIssue.groovy +++ b/libpit/src/com/jdbernard/pit/xml/XmlIssue.groovy @@ -18,7 +18,8 @@ public class XmlIssue extends Issue { } XmlIssue(String id, Category c = Category.TASK, Status s = Status.NEW, - int p = 9, String text, XmlRepository repository, XmlProject project) { + int p = 9, String title, String text, XmlRepository repository, + XmlProject project) { super(id, c, s, p) this.project = project @@ -26,9 +27,10 @@ public class XmlIssue extends Issue { // Node constructor adds the node to the parent node issueNode = new Node(project.projectNode, "Issue", - [id: id, category: c, status: s, priority: p]) + [id: id, category: c, status: s, priority: p, title: title]) - this.text = text + super.@title = title + super.@text = text issueNode.value = text repository.persist() @@ -62,4 +64,11 @@ public class XmlIssue extends Issue { repository.persist() } + public void setTitle(String t) { + super.setTitle(t) + + issueNode.@title = t + repository.persist() + } + }