Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / search / SearchHelper.java
1 /* SearchHelper.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.search;
17
18 import android.util.Log;
19
20 import com.loopj.android.http.AsyncHttpResponseHandler;
21
22 import org.jsoup.Jsoup;
23 import org.jsoup.nodes.Document;
24
25 import java.io.UnsupportedEncodingException;
26 import java.util.List;
27
28 import cz.msebera.android.httpclient.Header;
29 import de.dhbwloe.campusapp.fragments.AppSearchListItem;
30
31 /**
32  * Created by pk910 on 25.01.2016.
33  */
34 public abstract class SearchHelper {
35
36     protected boolean bRequestRunning = false;
37     protected boolean bHtmlParser;
38     protected SearchResultListener oRequestListener = null;
39
40     public SearchHelper(boolean htmlparser) {
41         bHtmlParser = htmlparser;
42     }
43
44     protected void performWebRequest(String keywords) {
45         AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {
46             @Override
47             public void onStart() {
48             }
49             @Override
50             public void onSuccess(int statusCode, Header[] headers, byte[] response) {
51                 boolean parsedHttp = false;
52                 if(bHtmlParser) {
53                     try {
54                         Document document = Jsoup.parse(new String(response));
55
56                         parsedHttp = true;
57                         bRequestRunning = false;
58                         try {
59                             List<AppSearchListItem> results = onHtmlDocumentReceived(document);
60                             oRequestListener.onSearchResultsReceived(results);
61                         } catch(Exception e) {
62                             oRequestListener.onSearchFailed(e.getMessage());
63                         }
64                     } catch(Exception e) {
65                     }
66                 }
67                 if(!parsedHttp) {
68                     try {
69                         List<AppSearchListItem> results = onPlainTextReceived(response);
70                         oRequestListener.onSearchResultsReceived(results);
71                     } catch(Exception e) {
72                         oRequestListener.onSearchFailed(e.getMessage());
73                     }
74                 }
75                 bRequestRunning = false;
76             }
77             @Override
78             public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
79                 String error = null;
80                 try {
81                     error = new String(errorResponse, "US-ASCII");
82                 } catch (Exception e1) {
83                 }
84                 Log.i("HTTPClient", "  Error: " + statusCode + " - " + error);
85                 bRequestRunning = false;
86                 oRequestListener.onSearchFailed(statusCode + ": " + error);
87             }
88             @Override
89             public void onRetry(int retryNo) {
90             }
91         };
92         processWebRequest(handler, keywords);
93     }
94
95     public void search(String keywords, SearchResultListener resultlistener) {
96         if(bRequestRunning) {
97             resultlistener.onSearchFailed("another request is already running!");
98             return;
99         }
100         oRequestListener = resultlistener;
101         bRequestRunning = true;
102
103         performWebRequest(keywords);
104     }
105
106     protected abstract void processWebRequest(AsyncHttpResponseHandler handler, String keywords);
107     protected abstract List<AppSearchListItem> onHtmlDocumentReceived(Document document);
108     protected abstract List<AppSearchListItem> onPlainTextReceived(byte[] data);
109 }