simplified and refactored along with cgi-script.
[jalview.git] / src / jalview / gui / UserQuestionnaireCheck.java
1 package jalview.gui;
2
3 import java.io.BufferedReader;
4 import java.io.InputStreamReader;
5 import java.net.URL;
6
7 import javax.swing.JOptionPane;
8
9 public class UserQuestionnaireCheck implements Runnable {
10     /**
11      * Implements the client side machinery for detecting a new questionnaire, 
12      * checking if the user has responded to an existing one, 
13      * and prompting the user for responding to a questionnaire.
14      * This is intended to work with the perl CGI scripts checkresponder.pl and
15      * questionnaire.pl
16      */
17     String url=null;
18     UserQuestionnaireCheck(String url) {
19         if (url.indexOf("questionnaire.pl")==-1) {
20             jalview.bin.Cache.log.error("'"+url+"' is an Invalid URL for the checkForQuestionnaire() method.\n"
21                     +"This argument is only for questionnaires derived from jalview's questionnaire.pl cgi interface.");
22         } else {
23             this.url = url;
24         }
25     }
26     String qid=null,rid=null;
27     private boolean checkresponse(URL qurl) throws Exception {
28         boolean prompt=false;
29         // see if we have already responsed to this questionnaire or get a new qid/rid pair
30         BufferedReader br = new BufferedReader(new InputStreamReader(qurl.openStream()));
31         String qresp;
32         while ((qresp=br.readLine())!=null) {
33             if (qresp.indexOf("NOTYET:")==0) {
34                 prompt=true; // not yet responded under that ID
35             } else {
36                 if (qresp.indexOf("RESPONDED:")==-1) {
37                     // may be a qid:rid for the latest questionnaire.
38                     // check if response is of form we expect.
39                     if (qresp.indexOf(':')>-1) {
40                         rid=null;
41                         qid=qresp.substring(0,qresp.indexOf(':'));
42                         if (qresp.indexOf(':')<(qresp.length()-1)) {
43                             rid = qresp.substring(qresp.indexOf(':')+1);
44                             prompt=true; // this is a new qid/rid pair
45                         }
46                     }
47                 } else {
48                     prompt=false;
49                 }
50             }
51         }
52         return prompt;
53     }
54     public void run() {
55         if (url==null)
56             return;
57         boolean prompt=false;
58         try {
59             // First - check to see if wee have an old questionnaire/response id pair.
60             String lastq=jalview.bin.Cache.getProperty("QUESTIONNAIRE");
61             if (lastq==null) {
62                 prompt = checkresponse(new URL(url+(url.indexOf('?')>-1 ? "&" : "?") +"checkresponse=1"));
63             } else {
64                 String qurl=url+(url.indexOf('?')>-1 ? "&" : "?") +"checkresponse=1";
65                 // query the server with the old qid/id pair
66                 String qqid = lastq.indexOf(':')>-1 ? lastq.substring(0,lastq.indexOf(':')) : null;
67                 if (qqid!=null && qqid.length()>0) {
68                     qurl+="&qid="+qqid;
69                     qid=qqid;
70                 }
71                 String qrid = lastq.substring(lastq.indexOf(':')+1); // retrieve old rid
72                 if (qrid!=null && !qrid.equals("null")) {
73                     rid=qrid;
74                     qurl+="&rid="+qrid;
75                 }
76                 // see if we have already responsed to this questionnaire.
77                 prompt = checkresponse(new URL(qurl));
78             }
79             // Update our local property cache with latest qid and rid
80             jalview.bin.Cache.setProperty("QUESTIONNAIRE",qid+":"+rid);
81             if (prompt) {
82                 String qurl = url+(url.indexOf('?')>-1 ? "&" : "?") + "qid="+qid+"&rid="+rid;
83                 jalview.bin.Cache.log.info("Prompting user for questionnaire at "+qurl);
84                 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
85                         "There is a new Questionnaire available." +
86                         "Would you like to complete it now ?\n",
87                         "Jalview User Survey",
88                         JOptionPane.YES_NO_OPTION,
89                         JOptionPane.QUESTION_MESSAGE);
90
91                 if (reply == JOptionPane.YES_OPTION)
92                 {
93                     jalview.bin.Cache.log.debug("Opening "+qurl);
94                     jalview.util.BrowserLauncher.openURL(qurl);
95                 }
96             }
97         } catch (Exception e) {
98             jalview.bin.Cache.log.warn("When trying to access questionnaire URL "+url,e);
99         }
100     }
101
102 }