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