diff --git a/plugins/DVHMA-Storage/plugin.xml b/plugins/DVHMA-Storage/plugin.xml new file mode 100644 index 0000000..f9c7723 --- /dev/null +++ b/plugins/DVHMA-Storage/plugin.xml @@ -0,0 +1,20 @@ + + +DVHMA-Storage +DVHMA Storage Backend + + + + + + + + + + + + + + + diff --git a/plugins/DVHMA-Storage/src/android/DVHMAStorage.java b/plugins/DVHMA-Storage/src/android/DVHMAStorage.java new file mode 100644 index 0000000..4b488cd --- /dev/null +++ b/plugins/DVHMA-Storage/src/android/DVHMAStorage.java @@ -0,0 +1,140 @@ +/* Copyright 2015 SAP SE + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.zertapps.dvhma.plugins.storage; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaArgs; +import org.apache.cordova.CordovaInterface; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaWebView; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +public class DVHMAStorage extends CordovaPlugin { + + private DVHMAStorageDbHelper mDbHelper; + + @Override + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); + + mDbHelper = new DVHMAStorageDbHelper(webView.getContext()); + } + + @Override + public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) { + if ("create".equals(action)) { + create(args, callbackContext); + return true; + } else if ("delete".equals(action)) { + delete(args, callbackContext); + return true; + } else if ("get".equals(action)) { + get(args, callbackContext); + return true; + } else if ("edit".equals(action)) { + edit(args, callbackContext); + return true; + } else { + callbackContext.error("Unknown action!"); + return false; + } + } + + private void edit(CordovaArgs args, CallbackContext callbackContext) { + int index; + String newTitle; + String newContent; + try { + index = args.getInt(0); + newTitle = args.getJSONObject(1).getString("title"); + newContent = args.getJSONObject(1).getString("content"); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + Cursor c = db.rawQuery("SELECT * FROM " + DVHMAStorageDbHelper.TABLE_NAME + ";", null); + c.moveToPosition(index); + db.execSQL("UPDATE " + DVHMAStorageDbHelper.TABLE_NAME + " SET title='" + newTitle + "',content='" + newContent + "' WHERE id=" + c.getInt(c.getColumnIndex("id")) + ";"); + db.close(); + + get(null, callbackContext); + } + + private void get(CordovaArgs args, CallbackContext callbackContext) { + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + JSONArray array = new JSONArray(); + Cursor c = db.rawQuery("SELECT * FROM " + DVHMAStorageDbHelper.TABLE_NAME + ";", null); + while (c.moveToNext()) { + String title = c.getString(c.getColumnIndex("title")); + String content = c.getString(c.getColumnIndex("content")); + try { + JSONObject obj = new JSONObject(); + obj.put("title", title); + obj.put("content", content); + array.put(obj); + } catch (JSONException e) { + e.printStackTrace(); + } + } + c.close(); + db.close(); + callbackContext.success(array); + } + + private void delete(CordovaArgs args, CallbackContext callbackContext) { + int index; + try { + index = args.getInt(0); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + Cursor c = db.rawQuery("SELECT * FROM " + DVHMAStorageDbHelper.TABLE_NAME + ";", null); + c.moveToPosition(index); + db.execSQL("DELETE FROM " + DVHMAStorageDbHelper.TABLE_NAME + " WHERE id=" + c.getInt(c.getColumnIndex("id"))); + c.close(); + db.close(); + + get(null, callbackContext); + } + + private void create(CordovaArgs args, CallbackContext callbackContext) { + String newTitle; + String newContent; + try { + newTitle = args.getJSONObject(0).getString("title"); + newContent = args.getJSONObject(0).getString("content"); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + db.execSQL("INSERT INTO " + DVHMAStorageDbHelper.TABLE_NAME + " (title,content) VALUES('" + newTitle + "','" + newContent + "');"); + db.close(); + + get(null, callbackContext); + } +} diff --git a/plugins/DVHMA-Storage/src/android/DVHMAStorageDbHelper.java b/plugins/DVHMA-Storage/src/android/DVHMAStorageDbHelper.java new file mode 100644 index 0000000..5cf1e6c --- /dev/null +++ b/plugins/DVHMA-Storage/src/android/DVHMAStorageDbHelper.java @@ -0,0 +1,48 @@ +/* Copyright 2015 SAP SE + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.zertapps.dvhma.plugins.storage; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +public class DVHMAStorageDbHelper extends SQLiteOpenHelper { + public static final int DATABASE_VERSION = 4; + public static final String DATABASE_NAME = "Todos.db"; + + public static final String TABLE_NAME = "todos"; + + private static final String SQL_CREATE_ENTRIES = + "CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT)"; + + private static final String SQL_DELETE_ENTRIES = + "DROP TABLE IF EXISTS " + TABLE_NAME; + + public DVHMAStorageDbHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(SQL_CREATE_ENTRIES); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + db.execSQL(SQL_DELETE_ENTRIES); + db.execSQL(SQL_CREATE_ENTRIES); + } +} diff --git a/plugins/DVHMA-Storage/www/DVHMA-Storage.js b/plugins/DVHMA-Storage/www/DVHMA-Storage.js new file mode 100644 index 0000000..dbd5c39 --- /dev/null +++ b/plugins/DVHMA-Storage/www/DVHMA-Storage.js @@ -0,0 +1,57 @@ +/* Copyright 2015 SAP SE + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +(function(cordova){ + var DVHMAStorage = function() { + + }; + + DVHMAStorage.prototype.create = function(params, success, fail) { + return cordova.exec(function(args) { + success(args); + }, function(args) { + fail(args); + }, 'DVHMAStorage', 'create', params); + } + + DVHMAStorage.prototype.get = function(success, fail) { + return cordova.exec(function(args) { + success(args); + }, function(args) { + fail(args); + }, 'DVHMAStorage', 'get', []); + } + DVHMAStorage.prototype.delete = function(params, success, fail) { + return cordova.exec(function(args) { + success(args); + }, function(args) { + fail(args); + }, 'DVHMAStorage', 'delete', params); + } + + DVHMAStorage.prototype.edit = function(params, success, fail) { + return cordova.exec(function(args) { + success(args); + }, function(args) { + fail(args); + }, 'DVHMAStorage', 'edit', params); + } + + window.todo = new DVHMAStorage(); + + // backwards compatibility + window.plugins = window.plugins || {}; + window.plugins.webintent = window.webintent; +})(window.PhoneGap || window.Cordova || window.cordova);