a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
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();
        }
    }
}