b2b19b986ec1c88d3a278ebf137d075d691f2344
[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;
18     UserQuestionnaireCheck(String url) {
19         this.url = url;
20     }
21     public void run() {
22         boolean prompt=false;
23         try {
24             if (url.indexOf("checkresponder.pl")==-1) {
25                 throw new Error("Invalid URL for the checkForQuestionnaire() method. The -questionnaire method is for Jalview user questionnaires only.");
26             }
27             // extract qid if there is one - else just get the latest
28             String rid=null,qid=null;
29             String args=null;
30             String baseurl=null,surl=null;
31             int t;
32             if ((t=url.indexOf("?"))>-1) {
33                 args=url.substring(t+1);
34                 surl=url.substring(0,t);
35             }
36             if ((t=url.lastIndexOf('/'))>-1) {
37                 baseurl=url.substring(0,t+1);
38             }
39             if (args!=null && (t=args.indexOf("qid="))>-1) {
40                 int e = args.indexOf("&",t+4);
41                 if (e<0)
42                     qid=args.substring(t+4);
43                 else
44                     qid=args.substring(t+4,e);
45                 if (qid!=null && qid.length()==0)
46                     qid=null;
47             }
48                     
49             if (qid==null) {
50                 // get a new questionnaire
51                 URL qurl = new URL(url);
52                 BufferedReader br = new BufferedReader(new InputStreamReader(qurl.openStream()));
53                 String qresp=null;
54                 while ((qresp=br.readLine())!=null) {
55                     // check if response is of form we expect.
56                     if (qresp.indexOf(':')>-1) {
57                         rid=null;
58                         qid=qresp.substring(0,qresp.indexOf(':'));
59                         if (qresp.indexOf(':')<(qresp.length()-1)) {
60                             rid = qresp.substring(qresp.indexOf(':')+1);
61                         }
62                     }
63                 }                
64             } else {
65                 // retrieve an id for an existing questionnaire
66                 // get a new questionnaire
67                 URL qurl = new URL(url);
68                 BufferedReader br = new BufferedReader(new InputStreamReader(qurl.openStream()));
69                 String qresp=null;
70                 while ((qresp=br.readLine())!=null) {
71                     // check if response is of form we expect.
72                     if (qresp.indexOf(':')>-1) {
73                         rid=null;
74                         qid=qresp.substring(0,qresp.indexOf(':'));
75                         if (qresp.indexOf(':')<(qresp.length()-1)) {
76                             rid = qresp.substring(qresp.indexOf(':')+1);
77                         }
78                     }
79                 }
80
81             }
82             // compare the questionnaire id against the id of the last questionnaire
83             // that the user was prompted with.
84             if (qid!=null) {
85                 String lastq=jalview.bin.Cache.getProperty("QUESTIONNAIRE");
86                 if (lastq==null || !lastq.startsWith(qid+":")) {
87                     prompt = true;
88                 } else { 
89                     String qrid = lastq.substring(lastq.indexOf(':')+1); // retrieve old rid
90                     if (qrid!=null && !qrid.equals("null"))
91                         rid = qrid;
92                     // see if we have already responsed to this questionnaire.
93                     URL qurl = new URL(url+"?qid="+qid+"&rid="+rid);
94                     BufferedReader br = new BufferedReader(new InputStreamReader(qurl.openStream()));
95                     String qresp;
96                     if (br.ready() && (qresp=br.readLine())!=null && qresp.indexOf("NOTYET")>-1) {
97                         prompt=true; // not yet responded under that ID
98                     }
99                 }
100             }
101             // Update our local property cache
102             jalview.bin.Cache.setProperty("QUESTIONNAIRE",qid+":"+rid);
103             if (prompt) {
104                 String qurl = null;
105                 if (baseurl!=null)
106                     qurl = baseurl+"questionnaire.pl?qid="+qid+"&rid="+rid;
107                 else
108                     qurl = url;
109                 jalview.bin.Cache.log.info("Prompting user for questionnaire at "+qurl);
110                 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
111                         "There is a new Questionnaire available." +
112                         "Would you like to complete it now ?\n",
113                         "Jalview User Survey",
114                         JOptionPane.YES_NO_OPTION,
115                         JOptionPane.QUESTION_MESSAGE);
116
117                 if (reply == JOptionPane.YES_OPTION)
118                 {
119                     jalview.bin.Cache.log.debug("Opening "+qurl);
120                     jalview.util.BrowserLauncher.openURL(qurl);
121                 }
122             }
123         } catch (Exception e) {
124             jalview.bin.Cache.log.warn("When trying to access questionnaire URL "+url,e);
125         }
126     }
127
128 }