From 35c74a45d1161c0dfd084f1bc6ffa07031526f35 Mon Sep 17 00:00:00 2001 From: Jack MacLauchlan <jack.maclauchlan.2016@uni.strath.ac.uk> Date: Sat, 19 Jan 2019 21:05:51 +0000 Subject: [PATCH] writes to individual files now --- pom.xml | 5 ++ src/main/java/command/CommandShow.java | 2 +- src/main/java/issueData/Comment.java | 23 ++++++- src/main/java/issueData/Issue.java | 49 +++++++------- src/main/java/issueData/IssueRepository.java | 67 +++++++++++++++----- src/main/java/issueData/IssueWriter.java | 50 +++++++++++++++ 6 files changed, 152 insertions(+), 44 deletions(-) create mode 100644 src/main/java/issueData/IssueWriter.java diff --git a/pom.xml b/pom.xml index c618b26..a102fe7 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,11 @@ <version>1.10</version> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.6.3</version> + </dependency> </dependencies> diff --git a/src/main/java/command/CommandShow.java b/src/main/java/command/CommandShow.java index 4e6ee41..4b1092a 100644 --- a/src/main/java/command/CommandShow.java +++ b/src/main/java/command/CommandShow.java @@ -17,7 +17,7 @@ public class CommandShow implements Command { repository.getIssues().forEach(issue -> { if (issue.getOriginalHash().equals(id)) { - String status = issue.isOpen() ? "Open" : "Closed"; + String status = issue.isopen() ? "Open" : "Closed"; System.out.println("Issue title: " + issue.getTitle() + "\n" + "Issue Description: " + issue.getDescription() + "\n" + diff --git a/src/main/java/issueData/Comment.java b/src/main/java/issueData/Comment.java index 9a41529..ab18626 100644 --- a/src/main/java/issueData/Comment.java +++ b/src/main/java/issueData/Comment.java @@ -2,12 +2,10 @@ package issueData; import org.apache.commons.codec.digest.DigestUtils; -import java.io.Serializable; import java.util.Objects; -public class Comment implements Serializable { +public class Comment { - private static final long serialVersionUID = 1L; private String hash; private String author; private String message; @@ -20,22 +18,41 @@ public class Comment implements Serializable { this.hash = DigestUtils.sha256Hex(author + message + creationTime); } + public Comment() { + } + public String getHash() { return hash; } + public void setHash(String hash) { + this.hash = hash; + } + public String getAuthor() { return author; } + public void setAuthor(String author) { + this.author = author; + } + public String getMessage() { return message; } + public void setMessage(String message) { + this.message = message; + } + public long getCreationTime() { return creationTime; } + public void setCreationTime(long creationTime) { + this.creationTime = creationTime; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/issueData/Issue.java b/src/main/java/issueData/Issue.java index 7f9a667..161e57d 100644 --- a/src/main/java/issueData/Issue.java +++ b/src/main/java/issueData/Issue.java @@ -2,30 +2,25 @@ package issueData; import org.apache.commons.codec.digest.DigestUtils; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Objects; -public class Issue implements Serializable { +public class Issue { - private static final long serialVersionUID = 1L; private String title; private String description; private String creator; private String originalHash; private List<Comment> comments; - private boolean open; + private boolean status; private long creationTime; private long edited; public Issue(String title, String description) { this.title = title; this.description = description; - this.open = true; + this.status = true; long time = System.currentTimeMillis(); this.creationTime = time + 1; //Lamport timestamps this.edited = time + 1; @@ -33,6 +28,10 @@ public class Issue implements Serializable { this.comments = new ArrayList<>(); } + public Issue() { + comments = new ArrayList<>(); + } + private void edited() { edited = System.currentTimeMillis(); } @@ -41,19 +40,26 @@ public class Issue implements Serializable { return creationTime; } + public void setCreationTime(long creationTime) { + this.creationTime = creationTime; + } + public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; - edited(); } public String getOriginalHash() { return originalHash; } + public void setOriginalHash(String originalHash) { + this.originalHash = originalHash; + } + public void addComment(Comment comment) { comments.add(comment); } @@ -66,13 +72,16 @@ public class Issue implements Serializable { return edited; } + public void setEdited(long edited) { + this.edited = edited; + } + public String getDescription() { return description; } public void setDescription(String description) { this.description = description; - edited(); } public String getCreator() { @@ -83,25 +92,17 @@ public class Issue implements Serializable { this.creator = creator; } - public boolean isOpen() { - return open; + public boolean isopen() { + return status; } - public void setOpen(boolean open) { - this.open = open; + public void setStatus(boolean status) { + this.status = status; } public void write() { - FileOutputStream fileOutputStream; - try { - fileOutputStream = new FileOutputStream("/home/jack/Documents/testrepo/.issues" + "/" + originalHash); - ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); - objectOutputStream.writeObject(this); - objectOutputStream.flush(); - objectOutputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } + IssueWriter writer = new IssueWriter(this); + writer.write(); } @Override diff --git a/src/main/java/issueData/IssueRepository.java b/src/main/java/issueData/IssueRepository.java index cb2da22..84fe889 100644 --- a/src/main/java/issueData/IssueRepository.java +++ b/src/main/java/issueData/IssueRepository.java @@ -1,11 +1,15 @@ package issueData; -import java.io.*; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.File; +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Objects; -public class IssueRepository implements Serializable { +public class IssueRepository { private List<Issue> issues; public IssueRepository() { @@ -18,22 +22,53 @@ public class IssueRepository implements Serializable { } private void readAllIssues() { - File dir = new File("/home/jack/Documents/testrepo/.issues"); - - for (File file : Objects.requireNonNull(dir.listFiles())) { - if (!file.getName().startsWith(".")) { - try { - FileInputStream fileInputStream = new FileInputStream(file.getPath()); - ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); - Issue issue = (Issue) objectInputStream.readObject(); - objectInputStream.close(); - issues.add(issue); - } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); + ObjectMapper mapper = new ObjectMapper(); + File[] directories = new File("/home/jack/Documents/testrepo/.issues/").listFiles(File::isDirectory); + + Arrays.stream(Objects.requireNonNull(directories)).forEach(issueDir -> { + if (issueDir.getName().equals(".git")) return; + + try { + String originalHash = issueDir.getName(); + String title = mapper.readValue(new File(issueDir.getAbsolutePath() + "/title"), String.class); + String description = mapper.readValue(new File(issueDir.getAbsolutePath() + "/description"), String.class); + String creator = mapper.readValue(new File(issueDir.getAbsolutePath() + "/creator"), String.class); + boolean status = mapper.readValue(new File(issueDir.getAbsolutePath() + "/status"), boolean.class); + long creationTime = mapper.readValue(new File(issueDir.getAbsolutePath() + "/creationTime"), long.class); + long edited = mapper.readValue(new File(issueDir.getAbsolutePath() + "/editedTime"), long.class); + + Issue issue = new Issue(); + issue.setTitle(title); + issue.setDescription(description); + issue.setCreator(creator); + issue.setOriginalHash(originalHash); + issue.setStatus(status); + issue.setCreationTime(creationTime); + issue.setEdited(edited); + + String commentDirPath = "/home/jack/Documents/testrepo/.issues/" + issueDir.getName() + "/comments/"; + File[] commentDirs = new File(commentDirPath).listFiles((File::isDirectory)); + + for (File commentDir : Objects.requireNonNull(commentDirs)) { + Comment comment = new Comment(); + String commentHash = commentDir.getName(); + String author = mapper.readValue(new File(commentDirPath + commentDir.getName() + "/author"), String.class); + String message = mapper.readValue(new File(commentDirPath + commentDir.getName() + "/message"), String.class); + long commentTime = mapper.readValue(new File(commentDirPath + commentDir.getName() + "/creationTime"), long.class); + + comment.setHash(commentHash); + comment.setAuthor(author); + comment.setMessage(message); + comment.setCreationTime(commentTime); + + issue.addComment(comment); } - } - } + issues.add(issue); + } catch (IOException e) { + e.printStackTrace(); + } + }); } diff --git a/src/main/java/issueData/IssueWriter.java b/src/main/java/issueData/IssueWriter.java new file mode 100644 index 0000000..02d9b3a --- /dev/null +++ b/src/main/java/issueData/IssueWriter.java @@ -0,0 +1,50 @@ +package issueData; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class IssueWriter { + + private Issue issue; + + public IssueWriter(Issue issue) { + this.issue = issue; + } + + + public void write() { + String path = "/home/jack/Documents/testrepo/.issues/" + issue.getOriginalHash() + "/"; + Path commentDir = Paths.get(path + "comments/"); + + try { + Files.createDirectories(commentDir); + + ObjectMapper mapper = new ObjectMapper(); + + mapper.writeValue(new File(path + "title"), issue.getTitle()); + mapper.writeValue(new File(path + "description"), issue.getDescription()); + mapper.writeValue(new File(path + "creator"), issue.getCreator()); + mapper.writeValue(new File(path + "status"), issue.isopen()); + mapper.writeValue(new File(path + "creationTime"), issue.getCreationTime()); + mapper.writeValue(new File(path + "editedTime"), issue.getEdited()); + + for (Comment comment : issue.getComments()) { + String commentPath = commentDir + "/" + comment.getHash() + "/"; + Files.createDirectories(Paths.get(commentPath)); + mapper.writeValue(new File(commentPath + "author"), comment.getAuthor()); + mapper.writeValue(new File(commentPath + "message"), comment.getMessage()); + mapper.writeValue(new File(commentPath + "creationTime"), comment.getCreationTime()); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + +} -- GitLab