Skip to content

Commit

Permalink
Merge pull request #176 from lamchenghou/lamchenghou/add-mapping-sort
Browse files Browse the repository at this point in the history
Add mapping sort
  • Loading branch information
lamchenghou authored Nov 2, 2023
2 parents 5d34e03 + 6dd057f commit a52f646
Show file tree
Hide file tree
Showing 15 changed files with 428 additions and 48 deletions.
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/logic/SeplendidLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ public interface SeplendidLogic {
ObservableList<PartnerCourse> getFilteredPartnerCourseCatalogue();

Path getPartnerCourseCatalogueFilePath();

ObservableList<PartnerCourse> getSortedPartnerCourseCatalogue();


//=========== UniversityCatalogue ============================================================================
ReadOnlyUniversityCatalogue getUniversityCatalogue();

ObservableList<University> getFilteredUniversityCatalogue();

Path getUniversityeCatalogueFilePath();

ObservableList<University> getSortedUniversityCatalogue();

//=========== NoteCatalogue ============================================================================
ObservableList<Note> getFilteredNoteCatalogue();

Expand All @@ -89,5 +94,7 @@ public interface SeplendidLogic {
ObservableList<Mapping> getFilteredMappingCatalogue();

Path getMappingCatalogueFilePath();

ObservableList<Mapping> getSortedMappingCatalogue();
}

4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/SeplendidLogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,8 @@ public ObservableList<Mapping> getFilteredMappingCatalogue() {
public Path getMappingCatalogueFilePath() {
return model.getMappingCatalogueFilePath();
}

public ObservableList<Mapping> getSortedMappingCatalogue() {
return model.getSortedMappingList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package seedu.address.logic.commands.mapping;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.SeplendidModel;
import seedu.address.model.mapping.MappingComparatorByAttribute;
import seedu.address.seplendidui.UiUtil;

/**
* Lists all mappings in the MappingCatalogue.
*/
public class MappingSortCommand extends MappingCommand {

public static final String MAPPING_SORT_MESSAGE_USAGE = COMMAND_WORD
+ " sort [localcode/localname/partnercode/partnername/university/information]: "
+ "Sorts all mappings based on the specified attribute.";
public static final String ACTION_WORD = "sort";

public static final String MESSAGE_SUCCESS = "Sorted mappings by %1$s.";

private final MappingComparatorByAttribute mappingComparator;

/**
* Creates a MappingSortCommand to add the specified {@code mappingComparator}
*
* @param mappingComparator Search mappingComparator.
*/
public MappingSortCommand(MappingComparatorByAttribute mappingComparator) {
super();
this.mappingComparator = mappingComparator;
}

/**
* TBD: This stub is to be removed after morphing is complete.
*
* @param model {@code Model} which the command should operate on.
* @return Nothing.
* @throws CommandException Always.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
throw new CommandException("TBD: this is a stub and should be removed after morph.");
}

@Override
public CommandResult execute(SeplendidModel seplendidModel) throws CommandException {
requireNonNull(seplendidModel);
// Inject model functions into mappingComparator
mappingComparator.initialiseGetLocalPartnercourse(seplendidModel::getLocalCourseIfExists,
seplendidModel::getPartnerCourseIfExists);
seplendidModel.updateSortedMappingList(mappingComparator);
return new CommandResult(
String.format(MESSAGE_SUCCESS, mappingComparator.getAttribute().toString().toLowerCase()),
UiUtil.ListViewModel.MAPPING_SORT);
}

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

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

MappingSortCommand otherMappingSortCommand = (MappingSortCommand) other;
return mappingComparator.equals(otherMappingSortCommand.mappingComparator);
}


@Override
public String toString() {
return new ToStringBuilder(this)
.add("mappingComparator", mappingComparator)
.toString();
}


}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/SeplendidParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import seedu.address.logic.commands.mapping.MappingDeleteCommand;
import seedu.address.logic.commands.mapping.MappingListCommand;
import seedu.address.logic.commands.mapping.MappingSearchCommand;
import seedu.address.logic.commands.mapping.MappingSortCommand;
import seedu.address.logic.commands.note.NoteAddCommand;
import seedu.address.logic.commands.note.NoteCommand;
import seedu.address.logic.commands.note.NoteDeleteCommand;
Expand All @@ -48,6 +49,7 @@
import seedu.address.logic.parser.mapping.MappingAddCommandParser;
import seedu.address.logic.parser.mapping.MappingDeleteCommandParser;
import seedu.address.logic.parser.mapping.MappingSearchCommandParser;
import seedu.address.logic.parser.mapping.MappingSortCommandParser;
import seedu.address.logic.parser.note.NoteAddCommandParser;
import seedu.address.logic.parser.note.NoteSearchCommandParser;
import seedu.address.logic.parser.partnercourse.PartnerCourseAddCommandParser;
Expand Down Expand Up @@ -220,6 +222,8 @@ private MappingCommand getMappingCommandWithArg(String userInput, String actionW
return new MappingDeleteCommandParser().parse(arguments);
case MappingSearchCommand.ACTION_WORD:
return new MappingSearchCommandParser().parse(arguments);
case MappingSortCommand.ACTION_WORD:
return new MappingSortCommandParser().parse(arguments);
default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package seedu.address.logic.parser.mapping;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PARAMETER_MAPPINGATTRIBUTE;
import static seedu.address.logic.parser.ParserUtil.areValuesEnclosedAndNonEmpty;
import static seedu.address.logic.parser.ParserUtil.parseMappingAttribute;

import seedu.address.logic.commands.mapping.MappingSortCommand;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.SeplendidArgumentMap;
import seedu.address.logic.parser.SeplendidArgumentTokenizer;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.mapping.MappingAttributeEnum;
import seedu.address.model.mapping.MappingComparatorByAttribute;

/**
* Parses input arguments and creates a new MappingSortCommandParser object.
*/
public class MappingSortCommandParser implements Parser<MappingSortCommand> {

/**
* Parses the given {@code String} of arguments in the context of the MappingSortCommandParser
* and returns a MappingSortCommandParser object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
public MappingSortCommand parse(String args) throws ParseException {
if (!areValuesEnclosedAndNonEmpty(args)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
MappingSortCommand.MAPPING_SORT_MESSAGE_USAGE));
}

SeplendidArgumentMap parameterToArgMap =
SeplendidArgumentTokenizer.tokenize(args, PARAMETER_MAPPINGATTRIBUTE);


if (!ParserUtil.areArgumentsPresent(parameterToArgMap, PARAMETER_MAPPINGATTRIBUTE)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
MappingSortCommand.MAPPING_SORT_MESSAGE_USAGE));
}

// All arguments should be a non-empty {@code Optional}
MappingAttributeEnum attributeEnumValue = parseMappingAttribute(parameterToArgMap.getValue(
PARAMETER_MAPPINGATTRIBUTE).get().toLowerCase());

MappingComparatorByAttribute mappingComparator = new MappingComparatorByAttribute(attributeEnumValue);

return new MappingSortCommand(mappingComparator);
}

}
17 changes: 17 additions & 0 deletions src/main/java/seedu/address/model/SeplendidModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void searchLocalCourses(LocalCourseAttribute attribute,

void searchPartnerCourses(PartnerCourseAttribute attribute,
PartnerCourseContainsKeywordsPredicate predicate);

ReadOnlyPartnerCourseCatalogue getPartnerCourseCatalogue();

/**
Expand Down Expand Up @@ -175,6 +176,7 @@ void searchPartnerCourses(PartnerCourseAttribute attribute,
void deletePartnerCourse(PartnerCourse partnerCourse) throws CommandException;

public ObservableList<PartnerCourse> getSortedPartnerCourseList();

public void updateSortedPartnerList(Comparator<PartnerCourse> partnerCourseComparator);

/**
Expand All @@ -195,6 +197,7 @@ void searchPartnerCourses(PartnerCourseAttribute attribute,
ObservableList<University> getFilteredUniversityList();

boolean hasUniversity(University university);

boolean hasUniversity(UniversityName universityName);

void getSearchUniversityIfExists(UniversityNameContainsKeywordsPredicate universityPredicate);
Expand All @@ -203,6 +206,7 @@ void searchPartnerCourses(PartnerCourseAttribute attribute,
void addUniversity(University university);

void setUniversity(University target, University editedUniversity);

ObservableList<University> getSortedUniversityList();

void updateSortedUniversityList(Comparator<University> universityComparator);
Expand Down Expand Up @@ -257,6 +261,7 @@ void searchPartnerCourses(PartnerCourseAttribute attribute,

/**
* Gets the notes with the specific tags
*
* @param notePredicate
*/
void getSearchNoteIfExists(NoteTagContainsKeywordsPredicate notePredicate);
Expand Down Expand Up @@ -339,6 +344,18 @@ void searchPartnerCourses(PartnerCourseAttribute attribute,
*/
void setMapping(Mapping mapping, Mapping editedMapping);

/**
* Returns the sorted mapping list.
*/
ObservableList<Mapping> getSortedMappingList();

/**
* Sorts the mapping list according to the given comparator.
*
* @param mappingComparator This is the given comparator.
*/
void updateSortedMappingList(Comparator<Mapping> mappingComparator);

//=========== FilteredMappingList Accessors =============================================================

/**
Expand Down
54 changes: 35 additions & 19 deletions src/main/java/seedu/address/model/SeplendidModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class SeplendidModelManager implements SeplendidModel {

private final MappingCatalogue mappingCatalogue;
private final FilteredList<Mapping> filteredMappingCatalogue;
private final SortedList<Mapping> sortedMappingCatalogue;


/**
Expand All @@ -73,17 +74,17 @@ public SeplendidModelManager(ReadOnlyUserPrefs userPrefs,
noteCatalogue, mappingCatalogue);

logger.fine(String.format("Initializing with user prefs: %s,\n"
+ "local course catalogue: %s, \n"
+ "partner course catalogue: %s,\n"
+ "university catalogue: %s,\n"
+ "mapping catalogue: %s,\n"
+ "and note catalogue: %s",
userPrefs,
localCourseCatalogue,
partnerCourseCatalogue,
universityCatalogue,
mappingCatalogue,
noteCatalogue));
+ "local course catalogue: %s, \n"
+ "partner course catalogue: %s,\n"
+ "university catalogue: %s,\n"
+ "mapping catalogue: %s,\n"
+ "and note catalogue: %s",
userPrefs,
localCourseCatalogue,
partnerCourseCatalogue,
universityCatalogue,
mappingCatalogue,
noteCatalogue));

this.localCourseCatalogue = new LocalCourseCatalogue(localCourseCatalogue);
this.userPrefs = new UserPrefs(userPrefs);
Expand All @@ -99,19 +100,20 @@ public SeplendidModelManager(ReadOnlyUserPrefs userPrefs,
filteredNoteCatalogue = new FilteredList<>(this.noteCatalogue.getNoteList());
this.mappingCatalogue = new MappingCatalogue(mappingCatalogue);
filteredMappingCatalogue = new FilteredList<>(this.mappingCatalogue.getMappingList());
sortedMappingCatalogue = new SortedList<>(this.mappingCatalogue.getMappingList());
}

/**
* Constructs Seplendid Model Manager.
*/
public SeplendidModelManager() {
this(
new UserPrefs(),
new LocalCourseCatalogue(),
new PartnerCourseCatalogue(),
new UniversityCatalogue(),
new MappingCatalogue(),
new NoteCatalogue());
new UserPrefs(),
new LocalCourseCatalogue(),
new PartnerCourseCatalogue(),
new UniversityCatalogue(),
new MappingCatalogue(),
new NoteCatalogue());
}

//=========== UserPrefs ==================================================================================
Expand Down Expand Up @@ -235,6 +237,7 @@ public void updateSortedLocalList(Comparator<LocalCourse> localCourseComparator)

/**
* Checks if the local course exists.
*
* @param attribute
* @param predicate
*/
Expand Down Expand Up @@ -262,8 +265,10 @@ public void updateFilteredLocalCourseList(Predicate<LocalCourse> predicate) {
}

//=========== PartnerCourseCatalogue ============================================================================

/**
* Checks if the local course exists.
*
* @param attribute
* @param predicate
*/
Expand Down Expand Up @@ -380,6 +385,7 @@ public boolean hasUniversity(University university) {

/**
* Check if there exist the same university in the catalogue.
*
* @param universityName
* @return
*/
Expand All @@ -388,12 +394,14 @@ public boolean hasUniversity(UniversityName universityName) {
Optional<University> maybeUniversity = getUniversityIfExists(universityName);
return maybeUniversity.isPresent();
}

@Override
public void getSearchUniversityIfExists(UniversityNameContainsKeywordsPredicate universityPredicate) {
requireNonNull(universityPredicate);
filteredUniversityCatalogue.setPredicate(universityPredicate);

}

@Override
public void addUniversity(University university) {
universityCatalogue.addUniversity(university);
Expand All @@ -419,8 +427,6 @@ public void updateSortedUniversityList(Comparator<University> universityComparat
}




//=========== FilteredUniversityList Accessors =============================================================

@Override
Expand Down Expand Up @@ -620,6 +626,16 @@ public void setMapping(Mapping mapping, Mapping editedMapping) {
mappingCatalogue.setMapping(mapping, editedMapping);
}

@Override
public ObservableList<Mapping> getSortedMappingList() {
return sortedMappingCatalogue;
}

@Override
public void updateSortedMappingList(Comparator<Mapping> mappingComparator) {
sortedMappingCatalogue.setComparator(mappingComparator);
}


//=========== FilteredNMappingList Accessors =============================================================

Expand Down
Loading

0 comments on commit a52f646

Please sign in to comment.