3 import java.io.BufferedReader;
4 import java.io.InputStreamReader;
7 import javax.swing.JOptionPane;
9 public class UserQuestionnaireCheck implements Runnable {
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
18 UserQuestionnaireCheck(String url) {
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.");
27 // extract qid if there is one - else just get the latest
28 String rid=null,qid=null;
30 String baseurl=null,surl=null;
32 if ((t=url.indexOf("?"))>-1) {
33 args=url.substring(t+1);
34 surl=url.substring(0,t);
36 if ((t=url.lastIndexOf('/'))>-1) {
37 baseurl=url.substring(0,t+1);
39 if (args!=null && (t=args.indexOf("qid="))>-1) {
40 int e = args.indexOf("&",t+4);
42 qid=args.substring(t+4);
44 qid=args.substring(t+4,e);
45 if (qid!=null && qid.length()==0)
50 // get a new questionnaire
51 URL qurl = new URL(url);
52 BufferedReader br = new BufferedReader(new InputStreamReader(qurl.openStream()));
54 while ((qresp=br.readLine())!=null) {
55 // check if response is of form we expect.
56 if (qresp.indexOf(':')>-1) {
58 qid=qresp.substring(0,qresp.indexOf(':'));
59 if (qresp.indexOf(':')<(qresp.length()-1)) {
60 rid = qresp.substring(qresp.indexOf(':')+1);
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()));
70 while ((qresp=br.readLine())!=null) {
71 // check if response is of form we expect.
72 if (qresp.indexOf(':')>-1) {
74 qid=qresp.substring(0,qresp.indexOf(':'));
75 if (qresp.indexOf(':')<(qresp.length()-1)) {
76 rid = qresp.substring(qresp.indexOf(':')+1);
82 // compare the questionnaire id against the id of the last questionnaire
83 // that the user was prompted with.
85 String lastq=jalview.bin.Cache.getProperty("QUESTIONNAIRE");
86 if (lastq==null || !lastq.startsWith(qid+":")) {
89 String qrid = lastq.substring(lastq.indexOf(':')+1); // retrieve old rid
90 if (qrid!=null && !qrid.equals("null"))
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()));
96 if (br.ready() && (qresp=br.readLine())!=null && qresp.indexOf("NOTYET")>-1) {
97 prompt=true; // not yet responded under that ID
101 // Update our local property cache
102 jalview.bin.Cache.setProperty("QUESTIONNAIRE",qid+":"+rid);
106 qurl = baseurl+"questionnaire.pl?qid="+qid+"&rid="+rid;
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);
117 if (reply == JOptionPane.YES_OPTION)
119 jalview.bin.Cache.log.debug("Opening "+qurl);
120 jalview.util.BrowserLauncher.openURL(qurl);
123 } catch (Exception e) {
124 jalview.bin.Cache.log.warn("When trying to access questionnaire URL "+url,e);