Skip to content
Snippets Groups Projects
Commit 35c74a45 authored by Jack MacLauchlan CS2015's avatar Jack MacLauchlan CS2015
Browse files

writes to individual files now

parent b05d13a8
No related branches found
No related tags found
1 merge request!1Issuedir
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
......@@ -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>
......
......@@ -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" +
......
......@@ -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;
......
......@@ -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
......
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();
}
});
}
......
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();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment