InitializeSubscriber JAVA » History » Version 1
Tomek Dziemidowicz, 2019-07-04 09:01 PM
| 1 | 1 | Tomek Dziemidowicz | h1. InitializeSubscriber JAVA |
|---|---|---|---|
| 2 | |||
| 3 | <pre><code class="java"> |
||
| 4 | /** |
||
| 5 | * Recreate database schema from remote server for specific subscriber |
||
| 6 | * @param subscriberId id of subscriber |
||
| 7 | * @throws Exception |
||
| 8 | */ |
||
| 9 | public void initializeSubscriber(String subscriberId) throws Exception { |
||
| 10 | HttpURLConnection connection = null; |
||
| 11 | InputStream resultStream = null; |
||
| 12 | String resultString = null; |
||
| 13 | Map<String, String> schema = null; |
||
| 14 | |||
| 15 | String requestUrl = String.format("%s/InitializeSubscriber/%s", _serverURL, subscriberId); |
||
| 16 | |||
| 17 | try { |
||
| 18 | connection = (HttpURLConnection) new URL(requestUrl).openConnection(); |
||
| 19 | |||
| 20 | int status = connection.getResponseCode(); |
||
| 21 | |||
| 22 | switch (status){ |
||
| 23 | case HttpURLConnection.HTTP_OK: |
||
| 24 | resultStream = connection.getInputStream(); |
||
| 25 | resultString = IOUtils.toString(resultStream, "UTF-8"); |
||
| 26 | schema = new Gson().fromJson(resultString, new TypeToken<Map<String, String>>(){}.getType()); |
||
| 27 | break; |
||
| 28 | default: |
||
| 29 | resultStream = connection.getErrorStream(); |
||
| 30 | resultString = IOUtils.toString(resultStream, "UTF-8"); |
||
| 31 | throw new Exception(resultString); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | finally { |
||
| 35 | if (resultStream != null) { |
||
| 36 | try { |
||
| 37 | resultStream.close(); |
||
| 38 | } catch (IOException e) { |
||
| 39 | } |
||
| 40 | } |
||
| 41 | if (connection != null) { |
||
| 42 | connection.disconnect(); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | List<String> keys = new ArrayList<String>(); |
||
| 47 | Set<String> keySet = schema.keySet(); |
||
| 48 | for(String key : keySet){ |
||
| 49 | keys.add(key); |
||
| 50 | } |
||
| 51 | Collections.sort(keys); |
||
| 52 | |||
| 53 | SQLiteDatabase db = openOrCreateDatabase(_dbFileName, null); |
||
| 54 | try{ |
||
| 55 | db.beginTransaction(); |
||
| 56 | for (String key : keys) { |
||
| 57 | if(!key.startsWith("00000")){ |
||
| 58 | db.execSQL(schema.get(key)); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | db.setTransactionSuccessful(); |
||
| 62 | } |
||
| 63 | finally { |
||
| 64 | db.endTransaction(); |
||
| 65 | db.close(); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | </code></pre> |