package com.basic.security.model; import android.text.TextUtils; import android.util.Base64; import com.basic.security.manager.impl.sqlite.SlBaseManager; import com.basic.security.utils.Base64Util; import com.basic.security.utils.Constants; import com.couchbase.lite.Blob; import com.couchbase.lite.MutableDocument; import com.nostra13.universalimageloader.utils.IoUtils; 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 sqliteModel; public ModelAdapter() { if (Constants.useCouchbase) { couchbaseModel = new MutableDocument(); } else { sqliteModel = new HashMap<>(); } } public ModelAdapter(MutableDocument mutableDocument) { if (Constants.useCouchbase) { this.couchbaseModel = mutableDocument; } else { this.sqliteModel = new HashMap<>(); } } public ModelAdapter(String id) { if (Constants.useCouchbase) { this.couchbaseModel = new MutableDocument(id); } else { this.sqliteModel = new HashMap<>(); this.sqliteModel.put("id", 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)sqliteModel.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 { sqliteModel.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)sqliteModel.get("id"); if (id == null) { id = UUID.randomUUID().toString(); sqliteModel.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)sqliteModel.get("table"); if (table == null) { table = ""; } if ("camera_image_feature".equals(key)) { return Base64Util.featureBase642Bytes((String)this.sqliteModel.get(key)); } else { 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 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 "/sdcard/security_attachment/"+table+"_" + key+"_"+id ; } 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)sqliteModel.get("table"); if (table == null) { table = ""; } if ("camera_image_feature".equals(key)) { this.sqliteModel.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("/sdcard/security_attachment"); if (!security_attachment_dir.exists()) { security_attachment_dir.mkdirs(); } IOUtils.write(content, new FileOutputStream(filePath)); this.sqliteModel.put(key, filePath); } } catch (IOException e) { e.printStackTrace(); } } } } public Map toMap() { if (Constants.useCouchbase) { return couchbaseModel.toMap(); } else { return sqliteModel; } } }