Skip to content

Commit

Permalink
Merge pull request #74 from H4mes/branch-AddOutlets
Browse files Browse the repository at this point in the history
Implement Outlet article attribute as class
  • Loading branch information
Howlong11 authored Mar 28, 2024
2 parents 1815d09 + 07221ae commit 5aee997
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import seedu.address.model.article.Article;
import seedu.address.model.article.Article.Status;
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.tag.Tag;

Expand Down Expand Up @@ -92,14 +93,15 @@ private static Article createEditedArticle(Article articleToEdit, EditArticleDes

String title = editArticleDescriptor.getTitle().orElse(articleToEdit.getTitle());
Set<Author> authors = editArticleDescriptor.getAuthors().orElse(articleToEdit.getAuthors());
LocalDateTime publicationDate = editArticleDescriptor.getPublicationDate()
.orElse(articleToEdit.getPublicationDate());
Set<Source> sources = editArticleDescriptor.getSources().orElse(articleToEdit.getSources());
Set<Tag> tags = editArticleDescriptor.getTags().orElse(articleToEdit.getTags());
Set<Outlet> outlets = editArticleDescriptor.getOutlets().orElse(articleToEdit.getOutlets());
LocalDateTime publicationDate = editArticleDescriptor.getPublicationDate()
.orElse(articleToEdit.getPublicationDate());
Status status = editArticleDescriptor.getStatus().orElse(articleToEdit.getStatus());

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

@Override
Expand Down Expand Up @@ -134,9 +136,10 @@ public static class EditArticleDescriptor {

private String title;
private Set<Author> authors;
private LocalDateTime publicationDate;
private Set<Source> sources;
private Set<Tag> tags;
private Set<Outlet> outlets;
private LocalDateTime publicationDate;
private Status status;

public EditArticleDescriptor() {}
Expand All @@ -147,17 +150,18 @@ public EditArticleDescriptor() {}
public EditArticleDescriptor(EditArticleDescriptor toCopy) {
setTitle(toCopy.title);
setAuthors(toCopy.authors);
setPublicationDate(toCopy.publicationDate);
setSources(toCopy.sources);
setTags(toCopy.tags);
setOutlets(toCopy.outlets);
setPublicationDate(toCopy.publicationDate);
setStatus(toCopy.status);
}

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

public void setTitle(String title) {
Expand Down Expand Up @@ -200,6 +204,13 @@ public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

public void setOutlets(Set<Outlet> outlets) {
this.outlets = (outlets != null) ? new HashSet<>(outlets) : null;
}

public Optional<Set<Outlet>> getOutlets() {
return (outlets != null) ? Optional.of(Collections.unmodifiableSet(outlets)) : Optional.empty();
}
public void setStatus(Status status) {
this.status = status;
}
Expand All @@ -224,8 +235,9 @@ public boolean equals(Object other) {
// Add more equality checks for article attributes below here.
return Objects.equals(title, otherEditArticleDescriptor.title)
&& Objects.equals(authors, otherEditArticleDescriptor.authors)
&& Objects.equals(publicationDate, otherEditArticleDescriptor.publicationDate)
&& Objects.equals(sources, otherEditArticleDescriptor.sources)
&& Objects.equals(outlets, otherEditArticleDescriptor.outlets)
&& Objects.equals(publicationDate, otherEditArticleDescriptor.publicationDate)
&& Objects.equals(tags, otherEditArticleDescriptor.tags)
&& Objects.equals(status, otherEditArticleDescriptor.status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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_OUTLET;
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;
Expand All @@ -16,6 +17,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.article.Article;
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.tag.Tag;

Expand All @@ -31,23 +33,24 @@ public class AddArticleCommandParser implements Parser<AddArticleCommand> {
*/
public AddArticleCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_AUTHOR, PREFIX_PUBLICATION_DATE, PREFIX_SOURCE,
PREFIX_ARTICLETAG, PREFIX_STATUS);
//Temporarily reinstate source requirement
ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_AUTHOR, PREFIX_SOURCE, PREFIX_ARTICLETAG,
PREFIX_OUTLET, PREFIX_PUBLICATION_DATE, PREFIX_STATUS);
//TODO: REMOVE PUBLICATION DATE REQUIREMENT
if (!arePrefixesPresent(argMultimap, PREFIX_TITLE, PREFIX_PUBLICATION_DATE, 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());
Set<Author> authorList = ParserUtil.parseAuthors(argMultimap.getAllValues(PREFIX_AUTHOR));
LocalDateTime publicationDate = ParserUtil.parsePublicationDate(argMultimap.getValue(PREFIX_PUBLICATION_DATE)
.get());
Set<Source> sourceList = ParserUtil.parseSources(argMultimap.getAllValues(PREFIX_SOURCE));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_ARTICLETAG));
Set<Outlet> outletList = ParserUtil.parseOutlets(argMultimap.getAllValues(PREFIX_OUTLET));
LocalDateTime publicationDate = ParserUtil.parsePublicationDate(argMultimap.getValue(PREFIX_PUBLICATION_DATE)
.get());
Article.Status status = (Article.Status) ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());

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

return new AddArticleCommand(article);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public class CliSyntax {

public static final Prefix PREFIX_TITLE = new Prefix("T/");
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_ARTICLETAG = new Prefix("TAG/");
public static final Prefix PREFIX_OUTLET = new Prefix("O/");
public static final Prefix PREFIX_PUBLICATION_DATE = new Prefix("D/");
public static final Prefix PREFIX_STATUS = new Prefix("S/");

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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_OUTLET;
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;
Expand All @@ -20,6 +21,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.article.Article;
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.tag.Tag;

Expand All @@ -36,8 +38,8 @@ public class EditArticleCommandParser {
public EditArticleCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_AUTHOR, PREFIX_PUBLICATION_DATE, PREFIX_SOURCE,
PREFIX_ARTICLETAG, PREFIX_STATUS);
ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_AUTHOR, PREFIX_SOURCE,
PREFIX_ARTICLETAG, PREFIX_OUTLET, PREFIX_PUBLICATION_DATE, PREFIX_STATUS);

Index index;

Expand Down Expand Up @@ -67,6 +69,7 @@ public EditArticleCommand parse(String args) throws ParseException {
parseAuthorsForEdit(argMultimap.getAllValues(PREFIX_AUTHOR)).ifPresent(editArticleDescriptor::setAuthors);
parseSourcesForEdit(argMultimap.getAllValues(PREFIX_SOURCE)).ifPresent(editArticleDescriptor::setSources);
parseTagsForEdit(argMultimap.getAllValues(PREFIX_ARTICLETAG)).ifPresent(editArticleDescriptor::setTags);
parseOutletsForEdit(argMultimap.getAllValues(PREFIX_OUTLET)).ifPresent(editArticleDescriptor::setOutlets);

if (!editArticleDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand Down Expand Up @@ -104,5 +107,14 @@ private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws Pars
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseTags(tagSet));
}
private Optional<Set<Outlet>> parseOutletsForEdit(Collection<String> outlets) throws ParseException {
assert outlets != null;

if (outlets.isEmpty()) {
return Optional.empty();
}
Collection<String> outletSet = outlets.size() == 1 && outlets.contains("") ? Collections.emptySet() : outlets;
return Optional.of(ParserUtil.parseOutlets(outletSet));
}

}
27 changes: 21 additions & 6 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.article.Article;
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
Expand Down Expand Up @@ -210,13 +211,27 @@ public static Set<Source> parseSources(Collection<String> sources) throws ParseE
}

/**
* Parses a {@code String category} into a {@code Category}.
* Parses a {@code String outlet} into a {@code Outlet}.
*/
public static String parseCategory(String category) throws ParseException {
requireNonNull(category);
String trimmedCategory = category.trim();
//removed the check for category validity
return trimmedCategory;
public static Outlet parseOutlet(String outlet) throws ParseException {
requireNonNull(outlet);
String trimmedOutlet = outlet.trim();
if (!Outlet.isValidOutletName(trimmedOutlet)) {
throw new ParseException(Outlet.MESSAGE_CONSTRAINTS);
}
return new Outlet(trimmedOutlet);
}

/**
* Parses a {@code List<String> outlets} into a {@code Set<Outlet>}.
*/
public static Set<Outlet> parseOutlets(Collection<String> outlets) throws ParseException {
requireNonNull(outlets);
final Set<Outlet> outletSet = new HashSet<>();
for (String outletName : outlets) {
outletSet.add(parseOutlet(outletName));
}
return outletSet;
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/seedu/address/model/article/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
*/
public class Article {
private final String title;
private final Set<Outlet> outlets = new HashSet<>();
private final Set<Author> authors = new HashSet<>();
private final LocalDateTime publicationDate;
private final Set<Source> sources = new HashSet<>();
private final Set<Tag> tags = new HashSet<>();
private final LocalDateTime publicationDate;

/**
* Enumeration of Status of an article.
Expand All @@ -41,14 +42,15 @@ public enum Status {
* @param tags the subject of the article.
* @param status the current status of the article.
*/
public Article(String title, Set<Author> authors, LocalDateTime publicationDate,
Set<Source> sources, Set<Tag> tags, Status status) {
requireAllNonNull(title, authors, publicationDate, sources, tags, status);
public Article(String title, Set<Author> authors, Set<Source> sources, Set<Tag> tags,
Set<Outlet> outlets, LocalDateTime publicationDate, Status status) {
requireAllNonNull(title, authors, sources, tags, outlets, publicationDate, status);
this.title = title;
this.authors.addAll(authors);
this.publicationDate = publicationDate;
this.sources.addAll(sources);
this.tags.addAll(tags);
this.outlets.addAll(outlets);
this.publicationDate = publicationDate;
this.status = status;
}

Expand All @@ -69,6 +71,10 @@ public String getPublicationDateAsString() {
return this.publicationDate.format(formatter);
}

public Set<Outlet> getOutlets() {
return Collections.unmodifiableSet(outlets);
}

public Set<Source> getSources() {
return Collections.unmodifiableSet(sources);
}
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/seedu/address/model/article/Outlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package seedu.address.model.article;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents the news outlet an Article is published by
*/
public class Outlet {
public static final String MESSAGE_CONSTRAINTS = "Outlet names should be alphanumeric";
public static final String VALIDATION_REGEX = "[\\p{Alnum} ]+";

public final String outletName;


/**
* Constructs a {@code outletName}.
*
* @param outletName A valid outlet name.
*/
public Outlet(String outletName) {
requireNonNull(outletName);
checkArgument(isValidOutletName(outletName), MESSAGE_CONSTRAINTS);
this.outletName = outletName;
}

/**
* Returns true if a given string is a valid outlet name.
*/
public static boolean isValidOutletName(String test) {
return test.matches(VALIDATION_REGEX);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof Outlet)) {
return false;
}

Outlet otherOutlet = (Outlet) other;
return outletName.equals(otherOutlet.outletName);
}

@Override
public int hashCode() {
return outletName.hashCode();
}

/**
* Format state as text for viewing.
*/
public String toString() {
return '[' + outletName + ']';
}
}
14 changes: 12 additions & 2 deletions src/main/java/seedu/address/model/util/SampleArticleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.model.article.Article;
import seedu.address.model.article.Article.Status;
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.tag.Tag;

Expand All @@ -21,8 +22,8 @@ public class SampleArticleDataUtil {
public static Article[] getSampleArticles() {
return new Article[]{
new Article("The epitome of pain and suffering by NUS CS students.", getAuthorSet("Alice", "Bob"),
LocalDateTime.now(), getSourceSet("NUS Computing Club"), getTagSet("Student Life"),
Status.PUBLISHED)
getSourceSet("NUS Computing Club"), getTagSet("Student Life"), getOutletSet("SOC News Bulletin"),
LocalDateTime.now(), Status.PUBLISHED)
};
}

Expand Down Expand Up @@ -60,4 +61,13 @@ public static Set<Tag> getTagSet(String... strings) {
.map(Tag::new)
.collect(Collectors.toSet());
}

/**
* Returns an outlet set containing the list of strings given.
*/
public static Set<Outlet> getOutletSet(String... strings) {
return Arrays.stream(strings)
.map(Outlet::new)
.collect(Collectors.toSet());
}
}
Loading

0 comments on commit 5aee997

Please sign in to comment.