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