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