Skip to content

Commit

Permalink
Merge pull request #101 from CMPUT301W21T03/search-refinement
Browse files Browse the repository at this point in the history
Search refinement
  • Loading branch information
NickNissen authored Mar 19, 2021
2 parents a3ba480 + 00bb5dd commit 3264f19
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public ExperimentManager()
/**
* Adds the given experiment that the user class/caller gives to this class
* @param id
* id corresponding to the experiment
* Id corresponding to the experiment
* @param experiment
* experiment to add to the manager
* Experiment to add to the manager
* @throws IllegalArgumentException
* the id is already associated to an experiment
* The id is already associated to an experiment
*/
public void add(UUID id, Experiment experiment) throws IllegalArgumentException {
if(experiments.containsKey(id))
Expand All @@ -50,14 +50,14 @@ public void add(UUID id, Experiment experiment) throws IllegalArgumentException
/**
* Deletes a given experiment from the currently maintained list
* @param id
* experiment ID to delete
* Experiment ID to delete
*/
public void delete(UUID id) { experiments.remove(id); }

/**
* gets the size of the currently maintained list
* Gets the size of the currently maintained list
* @return
* the size of the current list
* The size of the current list
*/
public int getSize() { return experiments.size(); }

Expand Down Expand Up @@ -89,12 +89,19 @@ public ArrayList<Experiment> getOwnedExperiments(UUID ownerId) {
public ArrayList<Experiment> queryExperiments(Collection<UUID> experimentIds) {
ArrayList<Experiment> experimentsList = new ArrayList<>();
for (UUID id : experimentIds) {
experimentsList.add(experiments.get(id));
if (experiments.containsKey(id)) {
experimentsList.add(experiments.get(id));
}
}
return experimentsList;
}

public ArrayList<Experiment> queryPublishedExperiments() {
/**
* Get all published experiments
* @return
* An arraylist of all published experiments
*/
public ArrayList<Experiment> getPublishedExperiments() {
ArrayList<Experiment> experimentsList = new ArrayList<>();
for (Map.Entry<UUID, Experiment> entry : experiments.entrySet()) {
Experiment experiment = experiments.get(entry.getKey());
Expand All @@ -118,40 +125,129 @@ public Experiment getAtUUIDDescription(UUID experimentUUID)
}

/**
* gives back a list of experiments that match a search term given by the
* Gives back a list of experiments that match a search term given by the
* user.
* @param query
* The query that is to be matched
* @return
* A list of experiments that match the given query.
*/
public ArrayList<Experiment> queryExperiments(String query) {
ArrayList<Experiment> experimentsList = new ArrayList<>();
for (Map.Entry<UUID, Experiment> entry : experiments.entrySet()) {
try {
Experiment experiment = experiments.get(entry.getKey());
//Log.d("SEARCHING", "Experiment:\t" + experiment.getDescription());
if (queryMatch(query, experiment.getDescription())) {
//Log.d("QUERY", "Found Match");
experimentsList.add(experiment);
}
} catch (NullPointerException e) {}
}
return experimentsList;
}

/**
* Queries experiments from group that have a description that matches the query
* @param query
* Description to match
* @param experimentIds
* Group of experiments to search through
* @return
* The experiments from the group that match the query
*/
public ArrayList<Experiment> queryExperiments(String query, Collection<UUID> experimentIds) {
ArrayList<Experiment> experimentsList = new ArrayList<>();
for (UUID id : experimentIds) {
try {
Experiment experiment = experiments.get(id);
if (queryMatch(query, experiment.getDescription())) {
experimentsList.add(experiments.get(id));
}
} catch (NullPointerException e) {}
}
return experimentsList;
}

/**
* Get all owned experiments based on a query
* @param query
* The description you are querying for
* @param ownerId
* The owner of the experiment
* @return
* All experiments that match the query and are had by that owner
*/
public ArrayList<Experiment> queryOwnedExperiments(String query, UUID ownerId) {
ArrayList<Experiment> experimentsList = new ArrayList<>();
for (Map.Entry<UUID, Experiment> entry : experiments.entrySet()) {
Experiment experiment = experiments.get(entry.getKey());
//Log.d("SEARCHING", "Experiment:\t" + experiment.getDescription());
if (queryMatch(query, experiment.getDescription())) {
//Log.d("QUERY", "Found Match");
if (experiment.getOwnerId().equals(ownerId) && queryMatch(query,
experiment.getDescription())) {
experimentsList.add(experiment);
}
}
return experimentsList;
}

/**
* Get all published experiments based on a query
* @param query
* The description you are querying for
* @return
* All experiments that match the query and are published
*/
public ArrayList<Experiment> queryPublishedExperiments(String query) {
ArrayList<Experiment> experimentsList = new ArrayList<>();
for (Map.Entry<UUID, Experiment> entry : experiments.entrySet()) {
Experiment experiment = experiments.get(entry.getKey());
if (experiment.isPublished() && queryMatch(query, experiment.getDescription())) {
experimentsList.add(experiment);
}
}
return experimentsList;
}

/**
* Will query a specific experiment based on it's UUID
* @param experimentId
* UUID of the experiment you want
* @return
* The requested experiment
*/
public Experiment query(UUID experimentId) {
return experiments.get(experimentId);
}

/**
* Function to help query specific experiments when searching based on description
* @param query
* The query you are searching for
* @param source
* The object you are comparing against
* @return
* A boolean for if the source has a partial match with the query
*/
private boolean queryMatch(String query, String source) {
String[] queryTokens = query.toLowerCase().split("\\W");
for (int j = 0; j < queryTokens.length; j++) {
// Log.d("SEARCHING", "query token:\t" + queryTokens[j]);
if (source.toLowerCase().indexOf(queryTokens[j]) >= 0) return true;
}
return false;
}

/**
* Get the current experiment being held in memory by the manager
* @return
* The current experiment
*/
public Experiment getCurrentExperiment() { return currentExperiment; }

/**
* Set the current experiment being held in memory by the manager
* @param experiment
* The experiment that you want to be set as the current experiment
*/
public void setCurrentExperiment(Experiment experiment) { currentExperiment = experiment; }

/**
Expand All @@ -163,4 +259,5 @@ public ArrayList<Experiment> getAllExperiments()
{
return new ArrayList(experiments.values());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
*
* Known Issue:
*
* 1. None
* 1. When searching for an item, the label at the top stays as whatever the current
* screen is. Should update to 'published' while still in search fragment
* 2. Search must be cleared each time or a bug appears where 2 searches are performed.
*/

public class NavigationActivity extends AppCompatActivity implements
Expand All @@ -76,6 +78,11 @@ public class NavigationActivity extends AppCompatActivity implements
public User loggedUser;
public Experiment currentExperiment;

/**
* Method called when creating NavigationActivity
* @param savedInstanceState
* A bundle with stored information if required
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -97,6 +104,11 @@ protected void onCreate(Bundle savedInstanceState) {
NavigationUI.setupWithNavController(navigationView, navController);
FloatingActionButton addExperimentButton = findViewById(R.id.add_experiment_button);
addExperimentButton.setOnClickListener(new View.OnClickListener() {
/**
* Deal with the FAB when clicked
* @param view
* The current view being used
*/
@Override
public void onClick(View view) {
switch (currentScreen) {
Expand Down Expand Up @@ -170,6 +182,13 @@ public void onClick(View view) {
});
}

/**
* Prepare search bar functionality
* @param menu
* A menu to help create the object
* @return
* A boolean based on success of creation
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand Down Expand Up @@ -215,13 +234,23 @@ public boolean onQueryTextChange(String newText) {
return true;
}

/**
* Deals with when a user navigates up in the application
* @return
* A boolean based on the success of the operation
*/
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}

/**
* Tells the application how to respond when OK is pressed in experiment creation
* @param experiment
* Experiment to be added to the experiment manager
*/
@Override
public void onOkPressed(Experiment experiment) {

Expand All @@ -241,6 +270,19 @@ public void onOkPressed(Experiment experiment) {
}
}

/**
* Edits an existing experiment with the information given
* @param experimentDescription
* New description of the experiment
* @param experimentTrials
* New minimum trials for this experiment
* @param experimentLocation
* Boolean for if this experiment requires a location
* @param experimentNewResults
* Boolean for if this experiment accepts new results
* @param currentExperiment
* The current experiment being modified
*/
@Override
// todo: this functionality should be moved into something else in the future
public void onOKPressedEdit(String experimentDescription, int experimentTrials,
Expand All @@ -253,20 +295,39 @@ public void onOKPressedEdit(String experimentDescription, int experimentTrials,
((NavExperimentDetailsFragment) currentFragment).updateScreen();
}


/**
* Sets the current screen variable
* @param currentScreen
* The screen to set as currentScreen
*/
public void setCurrentScreen(Screen currentScreen) {
this.currentScreen = currentScreen;
}

/**
* Sets the current fragment variable
* @param currentFragment
* The fragment to set as currentFragment
*/
public void setCurrentFragment(Fragment currentFragment) {
this.currentFragment = currentFragment;
}

/**
* Gives the singleton Experiment Manager used throughout
* @return
*/
public ExperimentManager getExperimentManager() {
return experimentManager;
}


/**
* Creates a question from QuestionDialog and adds it to QuestionManager
* @param question
* The message of the question
* @param experimentId
* The ID of the experiment it's related to.
*/
@Override
public void onOkPressedQuestion(String question, UUID experimentId) {
Question newQuestion = new Question(question, loggedUser.getUserId(), experimentId);
Expand All @@ -278,6 +339,13 @@ public void onOkPressedQuestion(String question, UUID experimentId) {
Log.d("current screen", currentScreen + "");
}

/**
* Creates a reply from the QuestionDialog and adds it to QuestionManager
* @param reply
* The message of the reply
* @param questionId
* The ID of the associated Question
*/
@Override
public void onOkPressedReply(String reply, UUID questionId) {
Reply newReply = new Reply(reply, questionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ public void populateList() {
break;
case "published":
experimentsArrayList.addAll(parentActivity.experimentManager
.queryPublishedExperiments());
.getPublishedExperiments());
break;
case "subscribed":
experimentsArrayList.addAll(parentActivity.experimentManager
.queryExperiments(parentActivity.loggedUser.getSubscriptions()));
break;
case "search":
String query = getArguments().getString("query");
experimentsArrayList.addAll(parentActivity.experimentManager
.queryExperiments(getArguments().getString("query")));
.queryPublishedExperiments(query));
break;
default:
throw new IllegalArgumentException();
Expand Down
Loading

0 comments on commit 3264f19

Please sign in to comment.