Search in sources :

Example 6 with CacheContact

use of com.fanap.podchat.cachemodel.CacheContact in project pod-chat-android-sdk by FanapSoft.

the class MessageDatabaseHelper method saveBlockedContacts.

public void saveBlockedContacts(@NonNull List<BlockedContact> contacts, int expireAmount) {
    worker(() -> {
        List<CacheBlockedContact> cacheBlockedContacts = new ArrayList<>();
        for (BlockedContact blockedContact : contacts) {
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", Locale.getDefault());
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.SECOND, expireAmount);
            String expireDate = format.format(c.getTime());
            @Nullable Contact contact = blockedContact.getContactVO();
            // BlockedContact to CacheBlockedContact
            CacheBlockedContact cacheBlockedContact;
            if (contact != null) {
                CacheContact cacheContact = getCacheContact(expireDate, contact, true, null);
                cacheBlockedContact = getCacheBlockedContact(blockedContact, expireDate, cacheContact);
                saveContactVoInBlockedContact(cacheContact);
            } else {
                cacheBlockedContact = getCacheBlockedContact(blockedContact, expireDate, null);
            }
            cacheBlockedContacts.add(cacheBlockedContact);
        }
        if (cacheBlockedContacts.size() > 0)
            messageDao.insertBlockedContacts(cacheBlockedContacts);
    });
}
Also used : CacheBlockedContact(com.fanap.podchat.cachemodel.CacheBlockedContact) BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) CacheBlockedContact(com.fanap.podchat.cachemodel.CacheBlockedContact) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Nullable(android.support.annotation.Nullable) CacheContact(com.fanap.podchat.cachemodel.CacheContact) ResultContact(com.fanap.podchat.model.ResultContact) BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) RequestSearchContact(com.fanap.podchat.mainmodel.RequestSearchContact) Contact(com.fanap.podchat.mainmodel.Contact) CacheContact(com.fanap.podchat.cachemodel.CacheContact) CacheBlockedContact(com.fanap.podchat.cachemodel.CacheBlockedContact)

Example 7 with CacheContact

use of com.fanap.podchat.cachemodel.CacheContact in project pod-chat-android-sdk by FanapSoft.

the class MessageDatabaseHelper method getContactsByLast.

@NonNull
public List<Contact> getContactsByLast(String lastName) {
    List<Contact> contacts = new ArrayList<>();
    List<CacheContact> cacheContacts = messageDao.getContactsByLast(lastName);
    if (cacheContacts != null) {
        for (CacheContact cacheContact : cacheContacts) {
            Contact contact = new Contact(cacheContact.getId(), cacheContact.getFirstName(), cacheContact.getUserId(), cacheContact.getLastName(), cacheContact.getBlocked(), cacheContact.getCreationDate(), cacheContact.getLinkedUser(), cacheContact.getCellphoneNumber(), cacheContact.getEmail(), cacheContact.getUniqueId(), cacheContact.getNotSeenDuration(), cacheContact.isHasUser());
            contacts.add(contact);
        }
    }
    return contacts;
}
Also used : ArrayList(java.util.ArrayList) CacheContact(com.fanap.podchat.cachemodel.CacheContact) ResultContact(com.fanap.podchat.model.ResultContact) BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) RequestSearchContact(com.fanap.podchat.mainmodel.RequestSearchContact) Contact(com.fanap.podchat.mainmodel.Contact) CacheContact(com.fanap.podchat.cachemodel.CacheContact) CacheBlockedContact(com.fanap.podchat.cachemodel.CacheBlockedContact) NonNull(android.support.annotation.NonNull)

Example 8 with CacheContact

use of com.fanap.podchat.cachemodel.CacheContact in project pod-chat-android-sdk by FanapSoft.

the class MessageDatabaseHelper method getContacts.

@NonNull
public List<Contact> getContacts(Integer count, Long offset) throws RoomIntegrityException {
    if (!canUseDatabase())
        throw new RoomIntegrityException();
    List<Contact> contacts = new ArrayList<>();
    List<CacheContact> cacheContacts = messageDao.getContacts(count, offset);
    if (cacheContacts != null && cacheContacts.size() > 0) {
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", Locale.getDefault());
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowDate = c.getTime();
        for (CacheContact cacheContact : cacheContacts) {
            try {
                Date expireDate = format.parse(cacheContact.getExpireDate());
                if (expireDate != null && expireDate.compareTo(nowDate) < 0) {
                    deleteContact(cacheContact);
                } else {
                    Contact contact = new Contact(cacheContact.getId(), cacheContact.getFirstName(), cacheContact.getUserId(), cacheContact.getLastName(), cacheContact.getBlocked(), cacheContact.getCreationDate(), cacheContact.getLinkedUser(), cacheContact.getCellphoneNumber(), cacheContact.getEmail(), cacheContact.getUniqueId(), cacheContact.getNotSeenDuration(), cacheContact.isHasUser());
                    contact.setCache(true);
                    contacts.add(contact);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    return contacts;
}
Also used : Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) CacheContact(com.fanap.podchat.cachemodel.CacheContact) Date(java.util.Date) ResultContact(com.fanap.podchat.model.ResultContact) BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) RequestSearchContact(com.fanap.podchat.mainmodel.RequestSearchContact) Contact(com.fanap.podchat.mainmodel.Contact) CacheContact(com.fanap.podchat.cachemodel.CacheContact) CacheBlockedContact(com.fanap.podchat.cachemodel.CacheBlockedContact) NonNull(android.support.annotation.NonNull)

Example 9 with CacheContact

use of com.fanap.podchat.cachemodel.CacheContact in project pod-chat-android-sdk by FanapSoft.

the class MessageDatabaseHelper method cacheContactToContactMapper.

private Contact cacheContactToContactMapper(CacheContact cacheContact) {
    Contact contact = new Contact(cacheContact.getId(), cacheContact.getFirstName(), cacheContact.getUserId(), cacheContact.getLastName(), cacheContact.getBlocked(), cacheContact.getCreationDate(), cacheContact.getLinkedUser(), cacheContact.getCellphoneNumber(), cacheContact.getEmail(), cacheContact.getUniqueId(), cacheContact.getNotSeenDuration(), cacheContact.isHasUser());
    contact.setCache(true);
    return contact;
}
Also used : ResultContact(com.fanap.podchat.model.ResultContact) BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) RequestSearchContact(com.fanap.podchat.mainmodel.RequestSearchContact) Contact(com.fanap.podchat.mainmodel.Contact) CacheContact(com.fanap.podchat.cachemodel.CacheContact) CacheBlockedContact(com.fanap.podchat.cachemodel.CacheBlockedContact)

Example 10 with CacheContact

use of com.fanap.podchat.cachemodel.CacheContact in project pod-chat-android-sdk by FanapSoft.

the class MessageDatabaseHelper method searchContacts.

@NonNull
public ChatResponse<ResultContact> searchContacts(RequestSearchContact requestSearchContact, String size, String offset) {
    List<Contact> contacts = new ArrayList<>();
    ChatResponse<ResultContact> chatResponse = new ChatResponse<>();
    chatResponse.setCache(true);
    ResultContact resultContact = new ResultContact();
    resultContact.setContacts(new ArrayList<>(contacts));
    chatResponse.setHasError(false);
    long nextOffset = Long.parseLong(offset) + Long.parseLong(size);
    resultContact.setHasNext(false);
    resultContact.setNextOffset(nextOffset);
    if (requestSearchContact.getId() != null) {
        try {
            CacheContact cacheContact = messageDao.getContactById(Long.parseLong(requestSearchContact.getId()));
            contacts.add(cacheContactToContactMapper(cacheContact));
            resultContact.setContacts(new ArrayList<>(contacts));
            resultContact.setContentCount(1);
        } catch (NumberFormatException e) {
            Log.e(TAG, "Invalid Id");
            chatResponse.setHasError(true);
            chatResponse.setErrorMessage("Invalid Id");
            chatResponse.setErrorCode(ChatConstant.ERROR_CODE_INVALID_CONTACT_ID);
            resultContact.setContentCount(0);
        }
        chatResponse.setResult(resultContact);
        return chatResponse;
    }
    String order = Util.isNullOrEmpty(requestSearchContact.getOrder()) ? "desc" : requestSearchContact.getOrder();
    String orderBy = " order by hasUser " + order + ", lastName is null or lastName='', lastName, firstName is null or firstName='', firstName";
    String query = "select * from CacheContact where";
    if (!Util.isNullOrEmpty(requestSearchContact.getQuery())) {
        query += " (firstName LIKE '%" + requestSearchContact.getQuery() + "%' OR lastName LIKE '%" + requestSearchContact.getQuery() + "%') AND";
    } else if (!Util.isNullOrEmpty(requestSearchContact.getFirstName()) && !Util.isNullOrEmpty(requestSearchContact.getLastName()))
        query += " (firstName LIKE '%" + requestSearchContact.getFirstName() + "%' AND lastName LIKE '%" + requestSearchContact.getLastName() + "%') AND";
    else if (!Util.isNullOrEmpty(requestSearchContact.getFirstName()))
        query += " firstName LIKE '%" + requestSearchContact.getFirstName() + "%' AND";
    else if (!Util.isNullOrEmpty(requestSearchContact.getLastName()))
        query += " lastName LIKE '%" + requestSearchContact.getLastName() + "%' AND";
    if (!Util.isNullOrEmpty(requestSearchContact.getEmail()))
        query += " email LIKE '%" + requestSearchContact.getEmail() + "%' AND";
    if (!Util.isNullOrEmpty(requestSearchContact.getCellphoneNumber()))
        query += " cellphoneNumber LIKE '%" + requestSearchContact.getCellphoneNumber() + "%'";
    if (query.endsWith("AND")) {
        query = query.substring(0, query.lastIndexOf("AND") - 1);
    }
    long contentCount = messageDao.getRawContactsCount(new SimpleSQLiteQuery(query.replaceFirst("select \\* ", "select count(id) ")));
    query += orderBy + " LIMIT " + size + " OFFSET " + offset;
    List<CacheContact> cachedContacts = messageDao.getRawContacts(new SimpleSQLiteQuery(query));
    if (!Util.isNullOrEmpty(cachedContacts)) {
        for (CacheContact cachedContact : cachedContacts) {
            contacts.add(cacheContactToContactMapper(cachedContact));
        }
    }
    resultContact.setContacts(new ArrayList<>(contacts));
    resultContact.setHasNext(Long.parseLong(offset) + contacts.size() < contentCount);
    resultContact.setNextOffset(Long.parseLong(offset) + contacts.size());
    resultContact.setContentCount(contentCount);
    chatResponse.setResult(resultContact);
    return chatResponse;
}
Also used : ArrayList(java.util.ArrayList) ResultContact(com.fanap.podchat.model.ResultContact) BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) RequestSearchContact(com.fanap.podchat.mainmodel.RequestSearchContact) Contact(com.fanap.podchat.mainmodel.Contact) CacheContact(com.fanap.podchat.cachemodel.CacheContact) CacheBlockedContact(com.fanap.podchat.cachemodel.CacheBlockedContact) ResultContact(com.fanap.podchat.model.ResultContact) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) ChatResponse(com.fanap.podchat.model.ChatResponse) CacheContact(com.fanap.podchat.cachemodel.CacheContact) NonNull(android.support.annotation.NonNull)

Aggregations

CacheContact (com.fanap.podchat.cachemodel.CacheContact)14 CacheBlockedContact (com.fanap.podchat.cachemodel.CacheBlockedContact)13 BlockedContact (com.fanap.podchat.mainmodel.BlockedContact)13 Contact (com.fanap.podchat.mainmodel.Contact)13 RequestSearchContact (com.fanap.podchat.mainmodel.RequestSearchContact)13 ResultContact (com.fanap.podchat.model.ResultContact)13 ArrayList (java.util.ArrayList)11 NonNull (android.support.annotation.NonNull)9 SimpleDateFormat (java.text.SimpleDateFormat)7 Calendar (java.util.Calendar)7 Date (java.util.Date)7 ParseException (java.text.ParseException)4 IOException (java.io.IOException)2 SimpleSQLiteQuery (android.arch.persistence.db.SimpleSQLiteQuery)1 Nullable (android.support.annotation.Nullable)1 ChatResponse (com.fanap.podchat.model.ChatResponse)1