Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum DbType {
TEXT_NOT_NULL("TEXT NOT NULL"),
TEXT_DEFAULT_EMPTY("TEXT DEFAULT \"\""),
BLOB("BLOB"),
SERIALIZED("BLOB"),
SERIALIZED(""),
NO_TYPE(""); // just for default annotation value

private String sqlRepresentation;
Expand Down
1 change: 1 addition & 0 deletions LikeOrmExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ android {

dependencies {
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.code.gson:gson:2.2.4'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':LikeOrmLib')
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.github.nrudenko.orm.LikeOrmSQLiteOpenHelper;
import com.github.nrudenko.orm.example.model.Attach;
import com.github.nrudenko.orm.example.model.ExampleModel;
import com.github.nrudenko.orm.example.model.Message;

import java.util.List;

Expand All @@ -30,8 +32,8 @@ public DatabaseHelper(Context context) {

@Override
protected void appendSchemes(List<Class> classes) {
// classes.add(Message.class);
// classes.add(ExampleModel.class);
classes.add(Message.class);
classes.add(ExampleModel.class);
classes.add(Attach.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import com.github.nrudenko.orm.CursorUtil;
import com.github.nrudenko.orm.QueryBuilder;
import com.github.nrudenko.orm.example.model.Attach;
import com.github.nrudenko.orm.example.model.Audio;
import com.github.nrudenko.orm.example.model.ExampleModel;
import com.github.nrudenko.orm.example.model.Image;
import com.github.nrudenko.orm.example.model.Message;

import java.util.Date;

Expand All @@ -31,6 +34,24 @@ private void insert() {
new QueryBuilder<ExampleModel>(this, SimpleContentProvider.class)
.table(ExampleModel.class)
.insert(exampleModel);

Audio audio = new Audio();
audio.setDate(new Date());
audio.setPath("audio path");

Image image = new Image();
audio.setDate(new Date());
audio.setPath("image");

Message message = new Message();
message.setDate(new Date());
message.setAudio(audio);
message.setImage(image);
message.setText("lucky message");

new QueryBuilder<Message>(this, SimpleContentProvider.class)
.table(Message.class)
.insert(message);
}

private void update() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.nrudenko.orm.example.adapter;

import android.database.Cursor;

import com.github.nrudenko.orm.adapter.SerializeAdapter;
import com.github.nrudenko.orm.example.utils.GsonWrapper;

/**
* Serialize adapter for stores data in byte array
*/
public class StringSerializeAdapter implements SerializeAdapter<String> {
@Override
public String serialize(Object object) {
return GsonWrapper.getGson().toJson(object);
}

@Override
public Object deserialize(Cursor cursor, int columnIndex, Class modelClass) {
String objectString = cursor.getString(columnIndex);
return GsonWrapper.getGson().fromJson(objectString, modelClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.nrudenko.orm.example.model;

import java.io.Serializable;
import java.util.Date;

public class Audio implements Serializable {
String path;
Date date;

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.nrudenko.orm.example.model;

import java.io.Serializable;
import java.util.Date;

public class Image implements Serializable {
String path;
Date date;

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.github.nrudenko.orm.example.model;

import com.github.nrudenko.orm.adapter.BLOBAdapter;
import com.github.nrudenko.orm.annotation.DbColumn;
import com.github.nrudenko.orm.annotation.Table;
import com.github.nrudenko.orm.commons.DbType;
import com.github.nrudenko.orm.example.adapter.StringSerializeAdapter;

import java.util.Date;

Expand All @@ -9,6 +13,10 @@ public class Message {

String text;
Date date;
@DbColumn(type = DbType.SERIALIZED, adapter = BLOBAdapter.class)
Audio audio;
@DbColumn(type = DbType.SERIALIZED, adapter = StringSerializeAdapter.class)
Image image;

public String getText() {
return text;
Expand All @@ -26,4 +34,19 @@ public void setDate(Date date) {
this.date = date;
}

public Audio getAudio() {
return audio;
}

public void setAudio(Audio audio) {
this.audio = audio;
}

public Image getImage() {
return image;
}

public void setImage(Image image) {
this.image = image;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.nrudenko.orm.example.utils;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class DateUtils {
private static final String TAG = DateUtils.class.getSimpleName();

public static final String DATABASE_DATE_FORMAT = "yyyy-MM-dd";
public static final String SHORT_DATE_PATTERN = "yyyy-MM-dd";

private static ArrayList<DateFormat> dateFormatArrayList = new ArrayList<DateFormat>();

static {
dateFormatArrayList.add(new SimpleDateFormat(DATABASE_DATE_FORMAT, Locale.getDefault()));
dateFormatArrayList.add(new SimpleDateFormat(SHORT_DATE_PATTERN, Locale.getDefault()));
}

private DateUtils() {
// not called
}

public static Date stringToDate(String stringDate) {
Date result = null;

for (int i = 0; i < dateFormatArrayList.size(); i++) {
DateFormat dateFormat = dateFormatArrayList.get(i);
try {
result = dateFormat.parse(stringDate);
break;
} catch (ParseException e) {
Log.d(TAG, e.getMessage());
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.nrudenko.orm.example.utils;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class GsonWrapper {
private static Gson gson;

protected GsonWrapper() {
}

public static Gson getGson() {
if (gson == null) {
gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateAdapter())
.registerTypeAdapter(java.sql.Date.class, new DateAdapter())
.create();
}
return gson;
}

static class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
Date result = null;
if (jsonElement != null) {
String stringDate = jsonElement.getAsString();
if (stringDate.matches("[0-9]+")) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(jsonElement.getAsLong() * 1000));
result = calendar.getTime();
} else {
result = DateUtils.stringToDate(stringDate);
}
}
return result;
}

@Override
public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) {
JsonElement result = null;
if (date != null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.DATABASE_DATE_FORMAT, Locale.getDefault());
String stringDate = simpleDateFormat.format(date);
result = new JsonPrimitive(stringDate);
}
return result;
}
}
}
73 changes: 30 additions & 43 deletions LikeOrmLib/src/main/java/com/github/nrudenko/orm/CursorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
import android.text.TextUtils;
import android.util.Log;

import com.github.nrudenko.orm.adapter.BLOBAdapter;
import com.github.nrudenko.orm.adapter.SerializeAdapter;
import com.github.nrudenko.orm.annotation.DbColumn;
import com.github.nrudenko.orm.annotation.DbSkipField;
import com.github.nrudenko.orm.annotation.VirtualColumn;
import com.github.nrudenko.orm.commons.DbType;
import com.github.nrudenko.orm.commons.FieldType;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -109,28 +106,19 @@ private static <T> T buildModel(Class<T> modelClass, Cursor cursor) {
field.set(model, cursor.getBlob(columnIndex));
break;
case SERIALIZED:
final byte[] byteArray = cursor.getBlob(columnIndex);
if (byteArray == null) {
continue;
}
if (Serializable.class.isAssignableFrom(field.getType())) {
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(bis);
Object o = ois.readObject();
field.set(model, o);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
bis.close();
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
if (field.isAnnotationPresent(DbColumn.class)) {
Class serializeAdapterClass = field.getAnnotation(DbColumn.class).adapter();
if (serializeAdapterClass != null) {
try {
SerializeAdapter serializeAdapter = (SerializeAdapter) serializeAdapterClass.newInstance();
Object object = serializeAdapter.deserialize(cursor, columnIndex, modelClass);
if (object != null) {
field.set(model, object);
}
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
} else {
Expand Down Expand Up @@ -234,23 +222,22 @@ public static ContentValues objectToContentValues(Object model) {
break;
case SERIALIZED:
if (Serializable.class.isAssignableFrom(field.getType())) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(bos);
oos.writeObject(value);
oos.flush();
bos.close();
contentValues.put(fieldName, bos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
if (field.isAnnotationPresent(DbColumn.class)) {
Class serializeAdapterClass = field.getAnnotation(DbColumn.class).adapter();
if (serializeAdapterClass != null) {
try {
SerializeAdapter serializeAdapter = (SerializeAdapter) serializeAdapterClass.newInstance();
Object object = serializeAdapter.serialize(value);
if (object != null) {
if (serializeAdapter instanceof BLOBAdapter) {
contentValues.put(fieldName, (byte[]) object);
} else {
contentValues.put(fieldName, (String) object);
}
}
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
} else {
Expand Down
Loading