applied copyright 2008
[jalview.git] / src / jalview / gui / UserQuestionnaireCheck.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.io.*;
22 import java.net.*;
23
24 import javax.swing.*;
25
26 public class UserQuestionnaireCheck
27     implements Runnable
28 {
29   /**
30    * Implements the client side machinery for detecting a new questionnaire,
31    * checking if the user has responded to an existing one,
32    * and prompting the user for responding to a questionnaire.
33    * This is intended to work with the perl CGI scripts checkresponder.pl and
34    * questionnaire.pl
35    */
36   String url = null;
37   UserQuestionnaireCheck(String url)
38   {
39     if (url.indexOf("questionnaire.pl") == -1)
40     {
41       jalview.bin.Cache.log.error("'" + url +
42           "' is an Invalid URL for the checkForQuestionnaire() method.\n"
43                                   + "This argument is only for questionnaires derived from jalview's questionnaire.pl cgi interface.");
44     }
45     else
46     {
47       this.url = url;
48     }
49   }
50
51   String qid = null, rid = null;
52   private boolean checkresponse(URL qurl)
53       throws Exception
54   {
55     jalview.bin.Cache.log.debug("Checking Response for : " + qurl);
56     boolean prompt = false;
57     // see if we have already responsed to this questionnaire or get a new qid/rid pair
58     BufferedReader br = new BufferedReader(new InputStreamReader(qurl.
59         openStream()));
60     String qresp;
61     while ( (qresp = br.readLine()) != null)
62     {
63       if (qresp.indexOf("NOTYET:") == 0)
64       {
65         prompt = true; // not yet responded under that ID
66       }
67       else
68       {
69         if (qresp.indexOf("QUESTIONNAIRE:") == 0)
70         {
71           // QUESTIONNAIRE:qid:rid for the latest questionnaire.
72           int p = qresp.indexOf(':', 14);
73           if (p > -1)
74           {
75             rid = null;
76             qid = qresp.substring(14, p);
77             if (p < (qresp.length() - 1))
78             {
79               rid = qresp.substring(p + 1);
80               prompt = true; // this is a new qid/rid pair
81             }
82           }
83         }
84       }
85     }
86     return prompt;
87   }
88
89   public void run()
90   {
91     if (url == null)
92     {
93       return;
94     }
95     boolean prompt = false;
96     try
97     {
98       // First - check to see if wee have an old questionnaire/response id pair.
99       String lastq = jalview.bin.Cache.getProperty("QUESTIONNAIRE");
100       if (lastq == null)
101       {
102         prompt = checkresponse(new URL(url + (url.indexOf('?') > -1 ? "&" : "?") +
103                                        "checkresponse=1"));
104       }
105       else
106       {
107         String qurl = url + (url.indexOf('?') > -1 ? "&" : "?") +
108             "checkresponse=1";
109         // query the server with the old qid/id pair
110         String qqid = lastq.indexOf(':') > -1 ?
111             lastq.substring(0, lastq.indexOf(':')) : null;
112         if (qqid != null && qqid != "null" && qqid.length() > 0)
113         {
114           qurl += "&qid=" + qqid;
115           qid = qqid;
116           String qrid = lastq.substring(lastq.indexOf(':') + 1); // retrieve old rid
117           if (qrid != null && !qrid.equals("null"))
118           {
119             rid = qrid;
120             qurl += "&rid=" + qrid;
121           }
122         }
123         // see if we have already responsed to this questionnaire.
124         prompt = checkresponse(new URL(qurl));
125       }
126       if (qid != null && rid != null)
127       {
128         // Update our local property cache with latest qid and rid
129         jalview.bin.Cache.setProperty("QUESTIONNAIRE", qid + ":" + rid);
130       }
131       if (prompt)
132       {
133         String qurl = url + (url.indexOf('?') > -1 ? "&" : "?") + "qid=" + qid +
134             "&rid=" + rid;
135         jalview.bin.Cache.log.info("Prompting user for questionnaire at " +
136                                    qurl);
137         int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
138             "There is a new Questionnaire available." +
139             "Would you like to complete it now ?\n",
140             "Jalview User Survey",
141             JOptionPane.YES_NO_OPTION,
142             JOptionPane.QUESTION_MESSAGE);
143
144         if (reply == JOptionPane.YES_OPTION)
145         {
146           jalview.bin.Cache.log.debug("Opening " + qurl);
147           jalview.util.BrowserLauncher.openURL(qurl);
148         }
149       }
150     }
151     catch (Exception e)
152     {
153       jalview.bin.Cache.log.warn("When trying to access questionnaire URL " +
154                                  url, e);
155     }
156   }
157
158 }