1 | package org.expeditee.actions;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.lang.reflect.InvocationTargetException;
|
---|
5 | import java.lang.reflect.Method;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import org.expeditee.agents.SearchGreenstone;
|
---|
9 | import org.expeditee.greenstone.ResultDocument;
|
---|
10 | import org.expeditee.gui.AttributeValuePair;
|
---|
11 | import org.expeditee.gui.MessageBay;
|
---|
12 |
|
---|
13 | public class GreenstoneActions {
|
---|
14 | public static void givePositiveFeedback(String text) {
|
---|
15 | giveFeedback(1, text);
|
---|
16 | }
|
---|
17 |
|
---|
18 | public static void giveNegativeFeedback(String text) {
|
---|
19 | giveFeedback(-1, text);
|
---|
20 | }
|
---|
21 |
|
---|
22 | public static void clearGreenstoneSession() {
|
---|
23 | SearchGreenstone.clearSession();
|
---|
24 | }
|
---|
25 |
|
---|
26 | public static void giveFeedback(double change, String text) {
|
---|
27 | Map<String, ResultDocument> sessionResults = SearchGreenstone
|
---|
28 | .getConnection().getSessionResults();
|
---|
29 |
|
---|
30 | if (sessionResults.size() == 0) {
|
---|
31 | MessageBay.errorMessage("The Greenstone Session is empty");
|
---|
32 | return;
|
---|
33 | }
|
---|
34 |
|
---|
35 | for (String line : text.split("\n")) {
|
---|
36 | AttributeValuePair avp = new AttributeValuePair(line, false);
|
---|
37 |
|
---|
38 | Method getMethod = null;
|
---|
39 | String targetValue = null;
|
---|
40 |
|
---|
41 | if (!avp.hasPair()) {
|
---|
42 | if (!avp.hasAttributeOrValue())
|
---|
43 | continue;
|
---|
44 | /*
|
---|
45 | * If only an attribute is supplied then search for it in the
|
---|
46 | * entire biliographic text.
|
---|
47 | *
|
---|
48 | */
|
---|
49 | try {
|
---|
50 | getMethod = ResultDocument.class.getMethod("toString",
|
---|
51 | new Class[] {});
|
---|
52 | } catch (SecurityException e) {
|
---|
53 | e.printStackTrace();
|
---|
54 | } catch (NoSuchMethodException e) {
|
---|
55 | e.printStackTrace();
|
---|
56 | }
|
---|
57 | targetValue = avp.getAttributeOrValue();
|
---|
58 | } else {
|
---|
59 | targetValue = avp.getValue().trim().toLowerCase();
|
---|
60 | String attribute = avp.getAttribute().trim().toLowerCase();
|
---|
61 |
|
---|
62 | String methodName = "get"
|
---|
63 | + Character.toUpperCase(attribute.charAt(0))
|
---|
64 | + attribute.substring(1);
|
---|
65 |
|
---|
66 | try {
|
---|
67 | getMethod = ResultDocument.class.getMethod(methodName,
|
---|
68 | new Class[] {});
|
---|
69 | } catch (SecurityException e) {
|
---|
70 | e.printStackTrace();
|
---|
71 | continue;
|
---|
72 | } catch (NoSuchMethodException e) {
|
---|
73 | // User provided an invalid attribute value pair
|
---|
74 | MessageBay.errorMessage("Invalid feedback characteristic: "
|
---|
75 | + attribute);
|
---|
76 | continue;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if(getMethod == null){
|
---|
80 | MessageBay.errorMessage("Document attribute does not exist: " + methodName);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | for (ResultDocument srd : sessionResults.values()) {
|
---|
85 | double sessionScore = srd.getSessionScore();
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * You will refine this part to make more sophisticated
|
---|
89 | * comparisons and more sensible score modifications
|
---|
90 | */
|
---|
91 |
|
---|
92 | Object value = null;
|
---|
93 | try {
|
---|
94 | if(getMethod.getReturnType() == null){
|
---|
95 | //Get methods should always return a value
|
---|
96 | assert(false);
|
---|
97 | }
|
---|
98 | value = getMethod.invoke(srd, new Object[] {}).toString();
|
---|
99 | } catch (IllegalArgumentException e) {
|
---|
100 | e.printStackTrace();
|
---|
101 | } catch (IllegalAccessException e) {
|
---|
102 | e.printStackTrace();
|
---|
103 | } catch (InvocationTargetException e) {
|
---|
104 | e.printStackTrace();
|
---|
105 | }catch (NullPointerException e) {
|
---|
106 | e.printStackTrace();
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (value != null) {
|
---|
110 | if (value.toString().trim().toLowerCase().contains(targetValue)) {
|
---|
111 | sessionScore = sessionScore + change;
|
---|
112 | }
|
---|
113 |
|
---|
114 | srd.setSessionScore(sessionScore);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | MessageBay.displayMessage("Feedback complete", null, Color.green
|
---|
119 | .darker(), true, null);
|
---|
120 | }
|
---|
121 | }
|
---|