Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Authors, Sources and Tags classes #60

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.address.logic;

import java.time.format.DateTimeFormatter;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -57,27 +56,16 @@
* Formats the {@code article} for display to the user.
*/
public static String format(Article article) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
final StringBuilder builder = new StringBuilder();
builder.append(article.getTitle())
.append("; Authors: ");
for (int i = 0; i < article.getAuthors().length; i++) {
builder.append(article.getAuthors()[i]);
if (i < article.getAuthors().length - 1) {
builder.append(", ");
}
}
builder.append("; Publication Date: ")
.append("; Authors: ")
.append(article.getAuthors())
.append("; Publication Date: ")

Check warning on line 63 in src/main/java/seedu/address/logic/Messages.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/Messages.java#L61-L63

Added lines #L61 - L63 were not covered by tests
.append(article.getPublicationDateAsString())
.append("; Source: ");
for (int i = 0; i < article.getSources().length; i++) {
builder.append(article.getSources()[i]);
if (i < article.getSources().length - 1) {
builder.append(", ");
}
}
builder.append("; Category: ")
.append(article.getCategory())
.append("; Sources: ")
.append(article.getSources())
.append("; Tags: ")
.append(article.getTags())

Check warning on line 68 in src/main/java/seedu/address/logic/Messages.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/Messages.java#L65-L68

Added lines #L65 - L68 were not covered by tests
.append("; Status: ")
.append(article.getStatus());
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ARTICLES;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
Expand All @@ -18,6 +20,9 @@
import seedu.address.model.Model;
import seedu.address.model.article.Article;
import seedu.address.model.article.Article.Status;
import seedu.address.model.article.Author;
import seedu.address.model.article.Source;
import seedu.address.model.tag.Tag;

/**
* Edits the details of an existing article in the article book.
Expand Down Expand Up @@ -86,15 +91,15 @@
assert articleToEdit != null;

String title = editArticleDescriptor.getTitle().orElse(articleToEdit.getTitle());
String[] authors = editArticleDescriptor.getAuthors().orElse(articleToEdit.getAuthors());
Set<Author> authors = editArticleDescriptor.getAuthors().orElse(articleToEdit.getAuthors());

Check warning on line 94 in src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java#L94

Added line #L94 was not covered by tests
LocalDateTime publicationDate = editArticleDescriptor.getPublicationDate()
.orElse(articleToEdit.getPublicationDate());
String[] sources = editArticleDescriptor.getSources().orElse(articleToEdit.getSources());
String category = editArticleDescriptor.getCategory().orElse(articleToEdit.getCategory());
Set<Source> sources = editArticleDescriptor.getSources().orElse(articleToEdit.getSources());
Set<Tag> tags = editArticleDescriptor.getTags().orElse(articleToEdit.getTags());

Check warning on line 98 in src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java#L97-L98

Added lines #L97 - L98 were not covered by tests
Status status = editArticleDescriptor.getStatus().orElse(articleToEdit.getStatus());

return new Article(title, authors, publicationDate,
sources, category, status); // Include all article attributes here.
sources, tags, status); // Include all article attributes here.
}

@Override
Expand Down Expand Up @@ -128,10 +133,10 @@
public static class EditArticleDescriptor {

private String title;
private String[] authors;
private Set<Author> authors;
private LocalDateTime publicationDate;
private String[] sources;
private String category;
private Set<Source> sources;
private Set<Tag> tags;
private Status status;

public EditArticleDescriptor() {}
Expand All @@ -143,16 +148,16 @@
setTitle(toCopy.title);
setAuthors(toCopy.authors);
setPublicationDate(toCopy.publicationDate);
setSource(toCopy.sources);
setCategory(toCopy.category);
setSources(toCopy.sources);
setTags(toCopy.tags);

Check warning on line 152 in src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java#L151-L152

Added lines #L151 - L152 were not covered by tests
setStatus(toCopy.status);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(title, authors, publicationDate, sources, category, status);
return CollectionUtil.isAnyNonNull(title, authors, publicationDate, sources, tags, status);

Check warning on line 160 in src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/articlecommands/EditArticleCommand.java#L160

Added line #L160 was not covered by tests
}

public void setTitle(String title) {
Expand All @@ -163,12 +168,12 @@
return Optional.ofNullable(title);
}

public void setAuthors(String[] authors) {
this.authors = authors;
public void setAuthors(Set<Author> authors) {
this.authors = (authors != null) ? new HashSet<>(authors) : null;
}

public Optional<String[]> getAuthors() {
return Optional.ofNullable(authors);
public Optional<Set<Author>> getAuthors() {
return (authors != null) ? Optional.of(Collections.unmodifiableSet(authors)) : Optional.empty();
}

public void setPublicationDate(LocalDateTime publicationDate) {
Expand All @@ -179,20 +184,20 @@
return Optional.ofNullable(publicationDate);
}

public void setSource(String[] sources) {
this.sources = sources;
public void setSources(Set<Source> sources) {
this.sources = (sources != null) ? new HashSet<>(sources) : null;
}

public Optional<String[]> getSources() {
return Optional.ofNullable(sources);
public Optional<Set<Source>> getSources() {
return (sources != null) ? Optional.of(Collections.unmodifiableSet(sources)) : Optional.empty();
}

public void setCategory(String category) {
this.category = category;
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}

public Optional<String> getCategory() {
return Optional.ofNullable(category);
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

public void setStatus(Status status) {
Expand All @@ -218,10 +223,10 @@

// Add more equality checks for article attributes below here.
return Objects.equals(title, otherEditArticleDescriptor.title)
&& Arrays.equals(authors, otherEditArticleDescriptor.authors)
&& Objects.equals(authors, otherEditArticleDescriptor.authors)
&& Objects.equals(publicationDate, otherEditArticleDescriptor.publicationDate)
&& Arrays.equals(sources, otherEditArticleDescriptor.sources)
&& Objects.equals(category, otherEditArticleDescriptor.category)
&& Objects.equals(sources, otherEditArticleDescriptor.sources)
&& Objects.equals(tags, otherEditArticleDescriptor.tags)
&& Objects.equals(status, otherEditArticleDescriptor.status);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ARTICLETAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_AUTHOR;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CATEGORY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PUBLICATION_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOURCE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TITLE;

import java.time.LocalDateTime;
import java.util.Set;
import java.util.stream.Stream;

import seedu.address.logic.commands.articlecommands.AddArticleCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.article.Article;
import seedu.address.model.article.Author;
import seedu.address.model.article.Source;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new AddArticleCommand object
Expand All @@ -28,22 +32,22 @@
public AddArticleCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_AUTHOR, PREFIX_PUBLICATION_DATE, PREFIX_SOURCE,
PREFIX_CATEGORY, PREFIX_STATUS);
PREFIX_ARTICLETAG, PREFIX_STATUS);
//Temporarily reinstate source requirement
if (!arePrefixesPresent(argMultimap, PREFIX_TITLE, PREFIX_AUTHOR, PREFIX_PUBLICATION_DATE, PREFIX_SOURCE,
PREFIX_CATEGORY, PREFIX_STATUS) || !argMultimap.getPreamble().isEmpty()) {
PREFIX_ARTICLETAG, PREFIX_STATUS) || !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddArticleCommand.MESSAGE_USAGE));
}

String title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get());
String[] authorList = ParserUtil.parseAuthors(argMultimap.getAllValues(PREFIX_AUTHOR));
Set<Author> authorList = ParserUtil.parseAuthors(argMultimap.getAllValues(PREFIX_AUTHOR));

Check warning on line 43 in src/main/java/seedu/address/logic/parser/AddArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/AddArticleCommandParser.java#L43

Added line #L43 was not covered by tests
LocalDateTime publicationDate = ParserUtil.parsePublicationDate(argMultimap.getValue(PREFIX_PUBLICATION_DATE)
.get());
String[] sourceList = ParserUtil.parseSources(argMultimap.getAllValues(PREFIX_SOURCE));
String category = ParserUtil.parseCategory(argMultimap.getValue(PREFIX_CATEGORY).get());
Set<Source> sourceList = ParserUtil.parseSources(argMultimap.getAllValues(PREFIX_SOURCE));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_ARTICLETAG));

Check warning on line 47 in src/main/java/seedu/address/logic/parser/AddArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/AddArticleCommandParser.java#L46-L47

Added lines #L46 - L47 were not covered by tests
Article.Status status = (Article.Status) ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());

Article article = new Article(title, authorList, publicationDate, sourceList, category, status);
Article article = new Article(title, authorList, publicationDate, sourceList, tagList, status);

Check warning on line 50 in src/main/java/seedu/address/logic/parser/AddArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/AddArticleCommandParser.java#L50

Added line #L50 was not covered by tests

return new AddArticleCommand(article);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CliSyntax {
public static final Prefix PREFIX_AUTHOR = new Prefix("A/");
public static final Prefix PREFIX_PUBLICATION_DATE = new Prefix("D/");
public static final Prefix PREFIX_SOURCE = new Prefix("SRC/");
public static final Prefix PREFIX_CATEGORY = new Prefix("C/");
public static final Prefix PREFIX_ARTICLETAG = new Prefix("TAG/");
public static final Prefix PREFIX_STATUS = new Prefix("S/");

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ARTICLETAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_AUTHOR;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CATEGORY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PUBLICATION_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOURCE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TITLE;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.articlecommands.EditArticleCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.article.Article;
import seedu.address.model.article.Author;
import seedu.address.model.article.Source;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new EditArticleCommand object
Expand All @@ -33,7 +38,7 @@
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_AUTHOR, PREFIX_PUBLICATION_DATE, PREFIX_SOURCE,
PREFIX_CATEGORY, PREFIX_STATUS);
PREFIX_ARTICLETAG, PREFIX_STATUS);

Index index;

Expand All @@ -44,7 +49,7 @@
pe);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TITLE, PREFIX_PUBLICATION_DATE, PREFIX_CATEGORY, PREFIX_STATUS);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TITLE, PREFIX_PUBLICATION_DATE, PREFIX_STATUS);

Check warning on line 52 in src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java#L52

Added line #L52 was not covered by tests

EditArticleCommand.EditArticleDescriptor editArticleDescriptor = new EditArticleCommand.EditArticleDescriptor();

Expand All @@ -55,16 +60,14 @@
editArticleDescriptor.setPublicationDate(ParserUtil.parsePublicationDate(argMultimap
.getValue(PREFIX_PUBLICATION_DATE).get()));
}
if (argMultimap.getValue(PREFIX_CATEGORY).isPresent()) {
editArticleDescriptor.setCategory(ParserUtil.parseCategory(argMultimap.getValue(PREFIX_CATEGORY).get()));
}
if (argMultimap.getValue(PREFIX_STATUS).isPresent()) {
editArticleDescriptor.setStatus((Article.Status) ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS)
.get()));
}

parseAuthorsForEdit(argMultimap.getAllValues(PREFIX_AUTHOR)).ifPresent(editArticleDescriptor::setAuthors);
parseSourcesForEdit(argMultimap.getAllValues(PREFIX_SOURCE)).ifPresent(editArticleDescriptor::setSource);
parseSourcesForEdit(argMultimap.getAllValues(PREFIX_SOURCE)).ifPresent(editArticleDescriptor::setSources);
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editArticleDescriptor::setTags);

Check warning on line 70 in src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java#L69-L70

Added lines #L69 - L70 were not covered by tests

if (!editArticleDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand All @@ -73,30 +76,34 @@
return new EditArticleCommand(index, editArticleDescriptor);
}

private Optional<String[]> parseAuthorsForEdit(List<String> authors) throws ParseException {
private Optional<Set<Author>> parseAuthorsForEdit(Collection<String> authors) throws ParseException {
assert authors != null;

for (String author : authors) {
if (author.isEmpty()) {
return Optional.empty();
}
if (authors.isEmpty()) {
return Optional.empty();

Check warning on line 83 in src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java#L83

Added line #L83 was not covered by tests
}
List<String> authorSet = authors;
authorSet = authorSet.size() == 1 && authorSet.contains("") ? Collections.emptyList() : authorSet;
Collection<String> authorSet = authors.size() == 1 && authors.contains("") ? Collections.emptySet() : authors;
return Optional.of(ParserUtil.parseAuthors(authorSet));
}

private Optional<String[]> parseSourcesForEdit(List<String> sources) throws ParseException {
private Optional<Set<Source>> parseSourcesForEdit(Collection<String> sources) throws ParseException {
assert sources != null;

for (String source : sources) {
if (source.isEmpty()) {
return Optional.empty();
}
if (sources.isEmpty()) {
return Optional.empty();

Check warning on line 93 in src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java#L93

Added line #L93 was not covered by tests
}
List<String> sourceSet = sources;
sourceSet = sourceSet.size() == 1 && sourceSet.contains("") ? Collections.emptyList() : sourceSet;
Collection<String> sourceSet = sources.size() == 1 && sources.contains("") ? Collections.emptySet() : sources;
return Optional.of(ParserUtil.parseSources(sourceSet));
}

private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws ParseException {
assert tags != null;

if (tags.isEmpty()) {
return Optional.empty();

Check warning on line 103 in src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java#L103

Added line #L103 was not covered by tests
}
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseTags(tagSet));

Check warning on line 106 in src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/EditArticleCommandParser.java#L106

Added line #L106 was not covered by tests
}

}
Loading
Loading