Skip to content
Snippets Groups Projects
Commit 8f160708 authored by t.belmega@gmx.de's avatar t.belmega@gmx.de
Browse files

Implement new version of text search

parent b70c4fe6
No related branches found
No related tags found
No related merge requests found
......@@ -345,5 +345,4 @@ public class DatabaseController {
prepQuery.setString(1, id);
prepQuery.executeUpdate();
}
}
package de.unipotsdam.cs.toolup.model;
import de.unipotsdam.cs.toolup.database.DatabaseController;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Created by tbelm on 26.06.2016.
* <p>
* Returns a set of applications that do match the search string.
* <p>
* An Application matches, if the search string is contained either in its title or description,
* or in the title or description of one of its features.
* The comparison is NOT case sensitive (all lowercase).
*/
public class ApplicationStringSearch {
private final String searchString;
private Set<Application> result = new HashSet<>();
private Collection<BusinessObject> allFeatures;
private Map<String, BusinessObject> allApps;
public ApplicationStringSearch(String searchString) throws IOException, SQLException {
this.searchString = searchString.toLowerCase();
init();
search();
}
public Set<Application> getResult() {
return this.result;
}
private void search() throws IOException, SQLException {
for (BusinessObject feature : allFeatures) {
if (matchesSearchString(feature)) addRelatedApplications((Feature) feature);
}
for (BusinessObject application : allApps.values()) {
if (matchesSearchString(application)) result.add((Application) application);
}
}
private boolean matchesSearchString(BusinessObject bo) {
return bo.getTitle().toLowerCase().contains(searchString) ||
bo.getDescription().toLowerCase().contains(searchString);
}
private void addRelatedApplications(Feature f) {
for (String appId : f.getRelatedApplications()) {
if (allApps.containsKey(appId)) {
result.add((Application) allApps.get(appId));
//Remove from the set of apps that still need to be searched for a match,
//because it is already added to the result set
allApps.remove(appId);
}
}
}
/**
* Load all features and applications from the database.
*
* @throws SQLException
* @throws IOException
*/
private void init() throws SQLException, IOException {
allFeatures = DatabaseController.getInstance().
loadAllFrom(DatabaseController.TABLE_NAME_FEATURE).values();
allApps = DatabaseController.getInstance().
loadAllFrom(DatabaseController.TABLE_NAME_APPLICATION);
}
}
......@@ -3,6 +3,7 @@ package de.unipotsdam.cs.toolup.ws.beans;
import de.unipotsdam.cs.toolup.database.DatabaseController;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import de.unipotsdam.cs.toolup.model.Application;
import de.unipotsdam.cs.toolup.model.ApplicationStringSearch;
import de.unipotsdam.cs.toolup.model.BusinessObject;
import java.io.IOException;
......@@ -17,15 +18,12 @@ public class ApplicationBean extends BusinessObjectBean {
private Collection<String> categories;
private Collection<String> features;
private String shortDescription;
private String contact;
private String provider;
private Application wrappedApp;
private ApplicationBean(Application app) throws InvalidIdException, SQLException, IOException {
super(app);
this.shortDescription = app.getShortDescription();
this.contact = app.getContact();
this.provider = app.getProvider();
this.wrappedApp = app;
this.categories = getJSONRepresentations(app.getRelatedCategories());
this.features = getJSONRepresentations(app.getRelatedFeatures());
}
......@@ -59,37 +57,25 @@ public class ApplicationBean extends BusinessObjectBean {
public static Collection<ApplicationBean> searchFor(String searchString) throws IOException, SQLException, InvalidIdException {
Collection<ApplicationBean> result = new HashSet<>();
Map<String, BusinessObject> allApps = DatabaseController.getInstance().doFullTextSearch(searchString);
Set<Application> searchResult = new ApplicationStringSearch(searchString).getResult();
for (BusinessObject app : allApps.values()) {
result.add(new ApplicationBean((Application) app));
for (Application app : searchResult) {
result.add(new ApplicationBean(app));
}
return result;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
return wrappedApp.getShortDescription();
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
return wrappedApp.getContact();
}
public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
return wrappedApp.getProvider();
}
public Collection<String> getCategories() {
......
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