[919] | 1 | /**
|
---|
| 2 | * GreenstoneActions.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[376] | 19 | package org.expeditee.actions;
|
---|
| 20 |
|
---|
| 21 | import java.lang.reflect.InvocationTargetException;
|
---|
| 22 | import java.lang.reflect.Method;
|
---|
| 23 | import java.util.Map;
|
---|
| 24 |
|
---|
| 25 | import org.expeditee.agents.SearchGreenstone;
|
---|
[1102] | 26 | import org.expeditee.core.Colour;
|
---|
[376] | 27 | import org.expeditee.greenstone.ResultDocument;
|
---|
| 28 | import org.expeditee.gui.AttributeValuePair;
|
---|
| 29 | import org.expeditee.gui.MessageBay;
|
---|
| 30 |
|
---|
| 31 | public class GreenstoneActions {
|
---|
| 32 | public static void givePositiveFeedback(String text) {
|
---|
| 33 | giveFeedback(1, text);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public static void giveNegativeFeedback(String text) {
|
---|
| 37 | giveFeedback(-1, text);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public static void clearGreenstoneSession() {
|
---|
| 41 | SearchGreenstone.clearSession();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public static void giveFeedback(double change, String text) {
|
---|
| 45 | Map<String, ResultDocument> sessionResults = SearchGreenstone
|
---|
| 46 | .getConnection().getSessionResults();
|
---|
| 47 |
|
---|
| 48 | if (sessionResults.size() == 0) {
|
---|
| 49 | MessageBay.errorMessage("The Greenstone Session is empty");
|
---|
| 50 | return;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | for (String line : text.split("\n")) {
|
---|
| 54 | AttributeValuePair avp = new AttributeValuePair(line, false);
|
---|
| 55 |
|
---|
| 56 | Method getMethod = null;
|
---|
| 57 | String targetValue = null;
|
---|
| 58 |
|
---|
| 59 | if (!avp.hasPair()) {
|
---|
| 60 | if (!avp.hasAttributeOrValue())
|
---|
| 61 | continue;
|
---|
| 62 | /*
|
---|
| 63 | * If only an attribute is supplied then search for it in the
|
---|
| 64 | * entire biliographic text.
|
---|
| 65 | *
|
---|
| 66 | */
|
---|
| 67 | try {
|
---|
| 68 | getMethod = ResultDocument.class.getMethod("toString",
|
---|
| 69 | new Class[] {});
|
---|
| 70 | } catch (SecurityException e) {
|
---|
| 71 | e.printStackTrace();
|
---|
| 72 | } catch (NoSuchMethodException e) {
|
---|
| 73 | e.printStackTrace();
|
---|
| 74 | }
|
---|
| 75 | targetValue = avp.getAttributeOrValue();
|
---|
| 76 | } else {
|
---|
| 77 | targetValue = avp.getValue().trim().toLowerCase();
|
---|
| 78 | String attribute = avp.getAttribute().trim().toLowerCase();
|
---|
| 79 |
|
---|
| 80 | String methodName = "get"
|
---|
| 81 | + Character.toUpperCase(attribute.charAt(0))
|
---|
| 82 | + attribute.substring(1);
|
---|
| 83 |
|
---|
| 84 | try {
|
---|
| 85 | getMethod = ResultDocument.class.getMethod(methodName,
|
---|
| 86 | new Class[] {});
|
---|
| 87 | } catch (SecurityException e) {
|
---|
| 88 | e.printStackTrace();
|
---|
| 89 | continue;
|
---|
| 90 | } catch (NoSuchMethodException e) {
|
---|
| 91 | // User provided an invalid attribute value pair
|
---|
| 92 | MessageBay.errorMessage("Invalid feedback characteristic: "
|
---|
| 93 | + attribute);
|
---|
| 94 | continue;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if(getMethod == null){
|
---|
| 98 | MessageBay.errorMessage("Document attribute does not exist: " + methodName);
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | for (ResultDocument srd : sessionResults.values()) {
|
---|
| 103 | double sessionScore = srd.getSessionScore();
|
---|
| 104 |
|
---|
| 105 | /*
|
---|
| 106 | * You will refine this part to make more sophisticated
|
---|
| 107 | * comparisons and more sensible score modifications
|
---|
| 108 | */
|
---|
| 109 |
|
---|
| 110 | Object value = null;
|
---|
| 111 | try {
|
---|
| 112 | if(getMethod.getReturnType() == null){
|
---|
| 113 | //Get methods should always return a value
|
---|
| 114 | assert(false);
|
---|
| 115 | }
|
---|
| 116 | value = getMethod.invoke(srd, new Object[] {}).toString();
|
---|
| 117 | } catch (IllegalArgumentException e) {
|
---|
| 118 | e.printStackTrace();
|
---|
| 119 | } catch (IllegalAccessException e) {
|
---|
| 120 | e.printStackTrace();
|
---|
| 121 | } catch (InvocationTargetException e) {
|
---|
| 122 | e.printStackTrace();
|
---|
| 123 | }catch (NullPointerException e) {
|
---|
| 124 | e.printStackTrace();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | if (value != null) {
|
---|
| 128 | if (value.toString().trim().toLowerCase().contains(targetValue)) {
|
---|
| 129 | sessionScore = sessionScore + change;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | srd.setSessionScore(sessionScore);
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[1102] | 136 | MessageBay.displayMessage("Feedback complete", null, Colour.GREEN
|
---|
[376] | 137 | .darker(), true, null);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|