package com.basic.security.model;
|
|
import android.text.TextUtils;
|
import android.util.Base64;
|
|
import com.basic.security.utils.Base64Util;
|
import com.basic.security.utils.Constants;
|
import com.couchbase.lite.Blob;
|
import com.couchbase.lite.MutableDocument;
|
|
import org.apache.commons.io.IOUtils;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.UUID;
|
|
public class ModelAdapter {
|
|
public MutableDocument couchbaseModel;
|
public Map<String, Object> model;
|
|
public ModelAdapter() {
|
if (Constants.useCouchbase) {
|
couchbaseModel = new MutableDocument();
|
} else {
|
model = new HashMap<>();
|
}
|
}
|
|
public ModelAdapter(MutableDocument mutableDocument) {
|
if (Constants.useCouchbase) {
|
this.couchbaseModel = mutableDocument;
|
} else {
|
this.model = new HashMap<>();
|
}
|
}
|
|
public ModelAdapter(String id) {
|
if (Constants.useCouchbase) {
|
this.couchbaseModel = new MutableDocument(id);
|
} else {
|
this.model = new HashMap<>();
|
this.model.put("id", id);
|
}
|
}
|
|
public static void main(String[] args) {
|
byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6};
|
String str = Base64.encodeToString(bytes, Base64.NO_WRAP);
|
byte[] bytes1 = Base64.decode(str, Base64.NO_WRAP);
|
System.out.println("hello");
|
}
|
|
public static String getAttachmentPath(String id, String key, String table) {
|
return Constants.attachmentPath + table + "_" + key + "_" + id;
|
}
|
|
public String getString(String key) {
|
String value = "";
|
if (Constants.useCouchbase) {
|
try {
|
value = couchbaseModel.getString(key);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
} else {
|
try {
|
value = (String) model.get(key);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
if (value == null) {
|
value = "";
|
}
|
return value;
|
}
|
|
public ModelAdapter setString(String key, String value) {
|
if (Constants.useCouchbase) {
|
try {
|
couchbaseModel.setString(key, value);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
} else {
|
try {
|
model.put(key, value);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return this;
|
}
|
|
public String getId() {
|
if (Constants.useCouchbase) {
|
try {
|
return couchbaseModel.getId();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
} else {
|
try {
|
String id = (String) model.get("id");
|
if (id == null) {
|
id = UUID.randomUUID().toString();
|
model.put("id", id);
|
}
|
return id;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return "";
|
}
|
|
public byte[] getBlob(String key) {
|
if (Constants.useCouchbase) {
|
try {
|
Blob blob = couchbaseModel.getBlob(key);
|
if (blob != null) {
|
return blob.getContent();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
} else {
|
try {
|
String table = (String) model.get("table");
|
if (table == null) {
|
table = "";
|
}
|
if ("camera_image_feature".equals(key)) {
|
return Base64Util.featureBase642Bytes((String) this.model.get(key));
|
} else {
|
String value = (String) model.get(key);
|
if (!TextUtils.isEmpty(value)) {
|
File attachFile = new File(getAttachmentPath(getId(), key, table));
|
if (attachFile.exists()) {
|
return IOUtils.toByteArray(new FileInputStream(attachFile));
|
}
|
}
|
}
|
} catch (IOException e) {
|
System.out.println("ModelAdapter:getBlob exception=" + e.getLocalizedMessage());
|
}
|
}
|
return null;
|
}
|
|
public void setBlob(String key, byte[] content) {
|
if (content != null) {
|
if (Constants.useCouchbase) {
|
Blob blob = new Blob("image/jpeg", content);
|
couchbaseModel.setBlob(key, blob);
|
} else {
|
try {
|
String table = (String) model.get("table");
|
if (table == null) {
|
table = "";
|
}
|
if ("camera_image_feature".equals(key)) {
|
this.model.put(key, Base64Util.featureBytes2Base64(content));
|
// String key2 = key + "_path";
|
// String filePath= getAttachmentPath(getId(), key2, table);
|
// IOUtils.write(content, new FileOutputStream(filePath));
|
// this.sqliteModel.put(key2, filePath);
|
} else {
|
String filePath = getAttachmentPath(getId(), key, table);
|
File security_attachment_dir = new File(Constants.attachmentPath);
|
if (!security_attachment_dir.exists()) {
|
security_attachment_dir.mkdirs();
|
}
|
IOUtils.write(content, new FileOutputStream(filePath));
|
this.model.put(key, filePath);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
public Map<String, Object> toMap() {
|
if (Constants.useCouchbase) {
|
return couchbaseModel.toMap();
|
} else {
|
return model;
|
}
|
}
|
}
|