Simple storage plugin for DVHMA.

This commit is contained in:
Achim D. Brucker 2015-05-13 20:43:45 +02:00
parent 0d7b584a5a
commit 96e911d395
4 changed files with 265 additions and 0 deletions

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0" id="de.zertapps.dvhma.plugins.storage"
version="1.0.0">
<name>DVHMA-Storage</name>
<description>DVHMA Storage Backend</description>
<js-module src="www/DVHMA-Storage.js" name="DVHMA-Storage">
<clobbers target="DVHMA-Storage" />
</js-module>
<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="DVHMAStorage" >
<param name="android-package" value="de.zertapps.dvhma.plugins.storage.DVHMAStorage"/>
</feature>
</config-file>
<source-file src="src/android/DVHMAStorage.java" target-dir="src/de/zertapps/dvhma/plugins/storage/" />
<source-file src="src/android/DVHMAStorageDbHelper.java" target-dir="src/de/zertapps/dvhma/plugins/storage/" />
</platform>
</plugin>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);