Formatting
[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         else
67         {
68           prompt = false;
69         }
70       }
71     }
72     return prompt;
73   }
74
75   public void run()
76   {
77     if (url == null)
78     {
79       return;
80     }
81     boolean prompt = false;
82     try
83     {
84       // First - check to see if wee have an old questionnaire/response id pair.
85       String lastq = jalview.bin.Cache.getProperty("QUESTIONNAIRE");
86       if (lastq == null)
87       {
88         prompt = checkresponse(new URL(url + (url.indexOf('?') > -1 ? "&" : "?") +
89                                        "checkresponse=1"));
90       }
91       else
92       {
93         String qurl = url + (url.indexOf('?') > -1 ? "&" : "?") +
94             "checkresponse=1";
95         // query the server with the old qid/id pair
96         String qqid = lastq.indexOf(':') > -1 ?
97             lastq.substring(0, lastq.indexOf(':')) : null;
98         if (qqid != null && qqid != "null" && qqid.length() > 0)
99         {
100           qurl += "&qid=" + qqid;
101           qid = qqid;
102           String qrid = lastq.substring(lastq.indexOf(':') + 1); // retrieve old rid
103           if (qrid != null && !qrid.equals("null"))
104           {
105             rid = qrid;
106             qurl += "&rid=" + qrid;
107           }
108         }
109         // see if we have already responsed to this questionnaire.
110         prompt = checkresponse(new URL(qurl));
111       }
112       if (qid != null && rid != null)
113       {
114         // Update our local property cache with latest qid and rid
115         jalview.bin.Cache.setProperty("QUESTIONNAIRE", qid + ":" + rid);
116       }
117       if (prompt)
118       {
119         String qurl = url + (url.indexOf('?') > -1 ? "&" : "?") + "qid=" + qid +
120             "&rid=" + rid;
121         jalview.bin.Cache.log.info("Prompting user for questionnaire at " +
122                                    qurl);
123         int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
124             "There is a new Questionnaire available." +
125             "Would you like to complete it now ?\n",
126             "Jalview User Survey",
127             JOptionPane.YES_NO_OPTION,
128             JOptionPane.QUESTION_MESSAGE);
129
130         if (reply == JOptionPane.YES_OPTION)
131         {
132           jalview.bin.Cache.log.debug("Opening " + qurl);
133           jalview.util.BrowserLauncher.openURL(qurl);
134         }
135       }
136     }
137     catch (Exception e)
138     {
139       jalview.bin.Cache.log.warn("When trying to access questionnaire URL " +
140                                  url, e);
141     }
142   }
143
144 }