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