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