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