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