eu.codeplumbers.cosi.db.models.Contact.java Source code

Java tutorial

Introduction

Here is the source code for eu.codeplumbers.cosi.db.models.Contact.java

Source

/*
 *     Cos android client for Cozy Cloud
 *
 *     Copyright (C)  2016 Hamza Abdelkebir
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.codeplumbers.cosi.db.models;

import android.util.Base64;
import android.util.Log;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.List;

import eu.codeplumbers.cosi.utils.DateUtils;

/**
 * Created by thor on 10/7/16.
 */
@Table(name = "Contacts")
public class Contact extends Model {
    @Column(name = "remoteId")
    String remoteId;

    @Column(name = "fn")
    String fn;

    @Column(name = "systemId")
    String systemId;

    @Column(name = "n")
    String n;

    @Column(name = "anniversary")
    String anniversary;

    @Column(name = "revision")
    String revision;

    @Column(name = "tags")
    String tags;

    @Column(name = "photoBase64")
    String photoBase64;

    @Column(name = "_attachments")
    String attachments;

    @Column(name = "deviceId")
    String deviceId;

    public Contact() {

    }

    public Contact(JSONObject contactJson) throws JSONException {
        if (contactJson.has("n"))
            setN(contactJson.getString("n"));
        else
            setN("");

        if (contactJson.has("fn"))
            setFn(contactJson.getString("fn"));
        else
            setFn("");

        if (contactJson.has("revision")) {
            setRevision(contactJson.getString("revision"));
        } else {
            setRevision(DateUtils.formatDate(new Date().getTime()));
        }

        if (contactJson.has("tags") && !contactJson.getString("tags").equalsIgnoreCase("")) {
            setTags(contactJson.getJSONArray("tags").toString());
        } else {
            setTags(new JSONArray().toString());
        }

        setPhotoBase64("");
        setAnniversary("");

        if (contactJson.has("deviceId")) {
            setDeviceId(contactJson.getString("deviceId"));
        }

        if (contactJson.has("systemId")) {
            setSystemId(contactJson.getString("systemId"));
        }

        setRemoteId(contactJson.getString("_id"));

        if (contactJson.has("_attachments")) {
            JSONObject attachment = contactJson.getJSONObject("_attachments");

            setAttachments(attachment.toString());

            if (attachment.has("picture")) {
                JSONObject picture = attachment.getJSONObject("picture");
                String attachmentName = new String(
                        Base64.decode(picture.getString("digest").replace("md5-", ""), Base64.DEFAULT));
                Log.d("Contact", attachmentName);
            }
        }

        save();

        if (contactJson.has("datapoints")) {
            JSONArray datapoints = contactJson.getJSONArray("datapoints");

            for (int i = 0; i < datapoints.length(); i++) {
                JSONObject datapoint = datapoints.getJSONObject(i);
                String value = datapoint.getString("value");
                ContactDataPoint contactDataPoint = ContactDataPoint.getByValue(this, value);

                if (contactDataPoint == null && !value.isEmpty()) {
                    contactDataPoint = new ContactDataPoint();
                    contactDataPoint.setPref(false);
                    contactDataPoint.setType(datapoint.getString("type"));
                    contactDataPoint.setValue(value);
                    contactDataPoint.setName(datapoint.getString("name"));
                    contactDataPoint.setContact(this);
                    contactDataPoint.save();
                }
            }
        }

    }

    public JSONObject toJsonObject() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", getId());
        jsonObject.put("remoteId", remoteId);
        jsonObject.put("systemId", systemId);
        if (attachments != null && !attachments.equalsIgnoreCase("")) {
            jsonObject.put("_attachments", new JSONObject(attachments));
        }

        jsonObject.put("n", n);
        jsonObject.put("fn", fn);
        //jsonObject.put("photoBase64", photoBase64);
        //jsonObject.put("anniversary", anniversary);

        if (tags != null && !tags.equalsIgnoreCase("")) {
            jsonObject.put("tags", new JSONArray(tags));
        }

        JSONArray dpJsonArray = getDataPointsJsonArray();
        if (dpJsonArray != null)
            jsonObject.put("datapoints", dpJsonArray);
        jsonObject.put("deviceId", deviceId);
        jsonObject.put("docType", "Contact");

        return jsonObject;
    }

    private JSONArray getDataPointsJsonArray() {
        JSONArray datapointArray = new JSONArray();

        for (ContactDataPoint contactDataPoint : dataPoints()) {
            try {
                datapointArray.put(contactDataPoint.toJsonObject());
            } catch (JSONException e) {
                continue;
            }
        }

        return datapointArray;
    }

    public List<ContactDataPoint> dataPoints() {
        return getMany(ContactDataPoint.class, "contact");
    }

    public String getRemoteId() {
        return remoteId;
    }

    public void setRemoteId(String remoteId) {
        this.remoteId = remoteId;
    }

    public String getFn() {
        return fn;
    }

    public void setFn(String fn) {
        this.fn = fn;
    }

    public String getN() {
        return n;
    }

    public void setN(String n) {
        this.n = n;
    }

    public String getRevision() {
        return revision;
    }

    public void setRevision(String revision) {
        this.revision = revision;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getPhotoBase64() {
        return photoBase64;
    }

    public void setPhotoBase64(String photoBase64) {
        this.photoBase64 = photoBase64;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getAnniversary() {
        return anniversary;
    }

    public void setAnniversary(String anniversary) {
        this.anniversary = anniversary;
    }

    public String getSystemId() {
        return systemId;
    }

    public void setSystemId(String systemId) {
        this.systemId = systemId;
    }

    public String getAttachments() {
        return attachments;
    }

    public void setAttachments(String attachments) {
        this.attachments = attachments;
    }

    public static List<Contact> getAll() {
        return new Select().from(Contact.class).execute();
    }

    public static Contact getByName(String contactName, String id) {
        return new Select().from(Contact.class).where("fn = ?", contactName).and("systemId = ?", contactName)
                .executeSingle();
    }

    public static Contact getByName(String contactName) {
        return new Select().from(Contact.class).where("fn = ?", contactName).executeSingle();
    }

    public static void setAllUnsynced() {
        List<Contact> allContacts = getAll();

        for (Contact contact : allContacts) {
            contact.setRemoteId("");
            contact.save();
        }
    }

    public static Contact getByRemoteId(String remoteId) {
        return new Select().from(Contact.class).where("remoteId = ?", remoteId).executeSingle();
    }

    /**
     * Returns all unsynced contacts (empty remoteId)
     *
     * @return
     */
    public static List<Contact> getAllUnsynced() {
        return new Select().from(Contact.class).where("remoteId = ?", "").execute();
    }
}