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