Newer
Older
package de.unipotsdam.cs.toolup.model;
import de.unipotsdam.cs.toolup.database.DatabaseController;
import de.unipotsdam.cs.toolup.exceptions.InvalidIdException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class ApplicationLookup {
public static final String LIST_DELIMITER = ",";
private final List<String> features;
public ApplicationLookup(String features) {
this.features = Arrays.asList(features.split(LIST_DELIMITER));
}
public Set<Application> getApplications() throws IOException, SQLException, InvalidIdException {
throwExceptionIfNoFeaturesAreSpecified();
Set<String> intersectionOfApps = buildIntersectionOfRelatedApplications();
return loadApplicationsByIds(intersectionOfApps);
}
private void throwExceptionIfNoFeaturesAreSpecified() {
if (features.isEmpty()){
throw new UnsupportedOperationException("Feature ID must be specified.");
}
}
private Set<String> buildIntersectionOfRelatedApplications() throws SQLException, InvalidIdException, IOException {
//load the related applications of the first feature
String firstFeatureId = features.get(0).trim();
Set<String> intersectionOfApps = DatabaseController.getInstance()
.loadRelatedApplicationsForFeat(firstFeatureId);
//if there is more than one feature, make the intersection of the result sets
for (int i = 1; i < features.size(); i++) {
String id = features.get(i).trim();
Set<String> apps = DatabaseController.getInstance()
.loadRelatedApplicationsForFeat(id);
intersectionOfApps.retainAll(apps);
}
return intersectionOfApps;
}
private Set<Application> loadApplicationsByIds(Set<String> intersectionOfApps) throws SQLException, InvalidIdException, IOException {
Set<Application> resultSetOfApps = new HashSet<>();
for (String id: intersectionOfApps){
BusinessObject app = DatabaseController.getInstance().load(id);
if (!(app instanceof NullBusinessObject)) {
resultSetOfApps.add((Application) app);
}