diff --git a/pom.xml b/pom.xml index c618b267a478f8dba20faae8e007c5b235e7adef..a102fe7cc9145a3e3d41c377a8fa496d13a2bbbb 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 4e6ee41f5d04c8dc7642e5ca4ec88e989dc80991..4b1092a9a93c568d8ed5004bb837fd4800133f61 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 9a41529132c0600e5c9a84b1f2b1d338d602469b..ab18626587ff626f903c689c4aa62864135c37ee 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 7f9a6677d6c6c9f208db82ed999279d3066277bc..161e57d5c7227c7a5d5763bbbe8af1d7be75549e 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 cb2da22e10bc27a4fb65e52baba28e8adce5d20a..84fe889b38aa2c2e64449b92d6300320f1219e82 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 0000000000000000000000000000000000000000..02d9b3a9c8fb98b6e4797f130327c0f2c4f5c0bc --- /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(); + } + + } + + +}