zhangzengfei
2020-03-31 0ce893695d32ab686f9e2309509e80c6feb0d380
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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;
        }
    }
}