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 org.apache.commons.io.IOUtils;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.Serializable;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.UUID;
|
|
public class ModelAdapter implements Serializable {
|
static int __id = 0;
|
public Map<String, Object> model = new HashMap<>();
|
|
// public static Map<String, String> modelAdapterCreateMap = new HashMap<>();
|
|
public ModelAdapter() {
|
setString(Model.__id, __id + "");
|
// modelAdapterCreateMap.put(__id+"", FrameUtil.getFrames()+"");
|
__id++;
|
}
|
|
public ModelAdapter(String id) {
|
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);
|
System1.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 = (String) model.get(key);
|
if (value == null) {
|
value = "";
|
}
|
return value;
|
}
|
|
public ModelAdapter setString(String key, String value) {
|
model.put(key, value);
|
if (Person.id_card_number.equals(key)) {
|
if (Constants.TRUE.equals(getString(Model.debug))) {
|
// System.out.println("FaceDetailFragment.savePerson 3 key=" + key + " value=" + value + " __id=" + getString(Model.__id) + " debug=" + Constants.TRUE.equals(getString(Model.debug)) + " " + FrameUtil.getFrames());
|
}
|
}
|
return this;
|
}
|
|
public ModelAdapter setIdWithUuid() {
|
model.put("id", UUID.randomUUID().toString());
|
return this;
|
}
|
|
public String getId() {
|
return getString("id");
|
}
|
|
public byte[] getBlob(String key) {
|
try {
|
String table = (String) model.get("table");
|
if (table == null) {
|
table = "";
|
}
|
if ("camera_image_feature".equals(key) || PersonA.faceFeature.equals(key)) {
|
return Base64Util.featureBase642Bytes((String) this.model.get(key));
|
} else {
|
File attachFile = new File(getAttachmentPath(getId(), key, table));
|
if (attachFile.exists()) {
|
return IOUtils.toByteArray(new FileInputStream(attachFile));
|
}
|
}
|
} catch (IOException e) {
|
System1.out.println("ModelAdapter:getBlob exception=" + e.getLocalizedMessage());
|
}
|
return null;
|
}
|
|
public boolean getBool(String key) {
|
return Constants.TRUE.equals(getString(key));
|
}
|
|
public int getInt(String key) {
|
return Integer.parseInt(getString(key));
|
}
|
|
public ModelAdapter setTrue(String key) {
|
model.put(key, Constants.TRUE);
|
return this;
|
}
|
|
public ModelAdapter setFalse(String key) {
|
model.put(key, Constants.FALSE);
|
return this;
|
}
|
|
public void setBlob(String key, byte[] content) {
|
if (content != null) {
|
try {
|
String table = (String) model.get("table");
|
if (table == null) {
|
table = "";
|
}
|
if ("camera_image_feature".equals(key)) {
|
this.model.put(key, Base64Util.featureBytes2Base64(content));
|
} 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 void setBool(String key, boolean value) {
|
model.put(key, value ? Constants.TRUE : Constants.FALSE);
|
}
|
|
public String toString() {
|
List<String> items = new ArrayList<>();
|
for (Map.Entry<String, Object> entry : model.entrySet()) {
|
items.add(entry.getKey() + ":" + entry.getValue());
|
}
|
return TextUtils.join(", ", items);
|
}
|
|
public ModelAdapter cloneModelAdapter() {
|
ModelAdapter modelAdapter = new ModelAdapter();
|
modelAdapter.model = new HashMap<>();
|
modelAdapter.model.putAll(model);
|
return modelAdapter;
|
}
|
|
public void setObject(String key, Object value) {
|
model.put(key, value);
|
}
|
|
public Object getObject(String key) {
|
return model.get(key);
|
}
|
|
public void setTrueOne(String key, boolean value) {
|
model.put(key, value ? "1" : "0");
|
}
|
|
public boolean getOneTrue(String key) {
|
return "1".equals(model.get(key));
|
}
|
|
public void deleteAttachment(String camera_image_path) {
|
try {
|
String attachmentPath = ModelAdapter.getAttachmentPath(getId(), camera_image_path, Person.tableName);
|
if (!TextUtils.isEmpty(attachmentPath)) {
|
File attachmentPath_file = new File(attachmentPath);
|
if (attachmentPath_file.exists()) {
|
attachmentPath_file.delete();
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|