com.eywa.impl.app.mongo.entities.Contact.java Source code

Java tutorial

Introduction

Here is the source code for com.eywa.impl.app.mongo.entities.Contact.java

Source

/*
 * EYWA.COM (Eywa Commerce)
 * This program is an integrated platform with E-Commerce and Configurator system.
 * Support: Please, contact the Author on http://www.smartfeeling.org.
 * Copyright (C) 2014  Gian Angelo Geminiani
 *
 * 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 com.eywa.impl.app.mongo.entities;

import com.eywa.impl.app.IAppConstants;
import com.eywa.impl.app.mongo.ICollectionNames;
import com.eywa.impl.app.server.utils.HtmlUtils;
import com.mongodb.BasicDBList;
import com.mongodb.DBObject;
import org.ly.commons.util.StringUtils;
import org.ly.packages.mongo.impl.MongoObject;
import org.ly.packages.mongo.impl.util.MongoUtils;

import java.util.*;

/**
 * }
 */
public class Contact extends MongoObject implements Comparable<Contact> {

    // ------------------------------------------------------------------------
    //                      Constants
    // ------------------------------------------------------------------------

    public static String COLLECTION = ICollectionNames.CONTACTS;

    //-- fields --//
    public static final String ID = IAppConstants.ID;
    public static final String DOMAIN_ID = IAppConstants.DOMAIN_ID;
    public static final String OWNER_ID = IAppConstants.OWNER_ID; // hub owner
    public static final String ACCOUNT_ID = IAppConstants.ACCOUNT_ID; // id od USERS collection
    public static final String LANG = IAppConstants.LANG;
    public static final String HUB_ID = IAppConstants.HUB_ID; // hub container
    public static final String PREV_HUB_ID = IAppConstants.PREV_HUB_ID;
    public static final String NAME = IAppConstants.NAME;
    public static final String SURNAME = IAppConstants.SURNAME;
    public static final String DESCRIPTION = IAppConstants.DESCRIPTION;
    public static final String DESCRIPTION_HTML = IAppConstants.DESCRIPTION_HTML;
    public static final String EMAIL = IAppConstants.EMAIL;
    public static final String IMAGE = IAppConstants.IMAGE;
    public static final String REALNAME = IAppConstants.REALNAME;
    public static final String PHONEMOBILE = IAppConstants.PHONEMOBILE;
    public static final String BIRTHDATE = IAppConstants.BIRTHDATE;

    public static final String PARALLEL_ARRAY_KEY = IAppConstants.PARALLEL_ARRAY_KEY; // keywords + recipients + files

    public static final String EMAILS = IAppConstants.EMAILS;
    public static final String KEYWORDS = IAppConstants.KEYWORDS;
    public static final String KEYWORDS_HTML = IAppConstants.KEYWORDS_HTML;
    public static final String COLLABORATION_HUBS = IAppConstants.COLLABORATIONS;

    public Contact(final DBObject item) {
        super(item);
    }

    public Contact(final String json) {
        super(json);
    }

    public Contact() {
        this.init();
    }

    @Override
    public int compareTo(final Contact user) {
        return 0;
    }

    // ------------------------------------------------------------------------
    //                      p r i v a t e
    // ------------------------------------------------------------------------

    private void init() {
        final String id = createUUID();
        this.append(ID, id);
        this.append(COLLABORATION_HUBS, new BasicDBList());
    }

    // --------------------------------------------------------------------
    //               S T A T I C
    // --------------------------------------------------------------------

    public static String createUUID() {
        return COLLECTION.concat("_").concat(MongoUtils.createUUID());
    }

    public static boolean is(final String id) {
        return StringUtils.hasText(id) && id.startsWith(COLLECTION);
    }

    public static String getId(final DBObject item) {
        return MongoUtils.getString(item, ID);
    }

    public static void setId(final DBObject item, final String value) {
        MongoUtils.put(item, ID, value);
    }

    public static String getDomainId(final DBObject item) {
        return MongoUtils.getString(item, DOMAIN_ID);
    }

    public static void setDomainId(final DBObject item, final String value) {
        MongoUtils.put(item, DOMAIN_ID, value);
    }

    public static String getAccountId(final DBObject item) {
        return MongoUtils.getString(item, ACCOUNT_ID);
    }

    public static void setAccountId(final DBObject item, final String value) {
        MongoUtils.put(item, ACCOUNT_ID, value);
    }

    public static String getName(final DBObject item) {
        return MongoUtils.getString(item, NAME);
    }

    public static void setName(final DBObject item, final String value) {
        MongoUtils.put(item, NAME, value);
    }

    public static String getSurname(final DBObject item) {
        return MongoUtils.getString(item, SURNAME);
    }

    public static void setSurname(final DBObject item, final String value) {
        MongoUtils.put(item, SURNAME, value);
    }

    public static String getLang(final DBObject item) {
        return MongoUtils.getString(item, LANG);
    }

    public static void setLang(final DBObject item, final String value) {
        MongoUtils.put(item, LANG, value);
    }

    public static String getDescription(final DBObject item) {
        return MongoUtils.getString(item, DESCRIPTION);
    }

    public static void setDescription(final DBObject item, final String value) {
        MongoUtils.put(item, DESCRIPTION, value);
        final HtmlUtils.HtmlText html = HtmlUtils.toHtml(value);
        setDescriptionHtml(item, html.getHtml());
        setKeywords(item, html.getTagsList());
    }

    public static String getDescriptionHtml(final DBObject item) {
        return MongoUtils.getString(item, DESCRIPTION_HTML);
    }

    public static void setDescriptionHtml(final DBObject item, final String value) {
        MongoUtils.put(item, DESCRIPTION_HTML, value);
    }

    public static String getEmail(final DBObject item) {
        return MongoUtils.getString(item, EMAIL);
    }

    public static void setEmail(final DBObject item, final String value) {
        MongoUtils.put(item, EMAIL, value);
    }

    public static void addEmail(final DBObject item, final String value) {
        if (StringUtils.hasText(value)) {
            final Set<String> emails = new HashSet<String>(MongoUtils.getList(item, EMAILS));
            if (!emails.contains(value)) {
                emails.add(value);
                setEmails(item, emails);
            }
        }
    }

    public static List<String> getEmails(final DBObject item) {
        final String email = getEmail(item);
        final List<String> result = MongoUtils.getList(item, EMAILS);
        if (StringUtils.hasText(email) && !result.contains(email)) {
            result.add(email);
        }
        return result;
    }

    public static void setEmails(final DBObject item, final Collection<String> value) {
        MongoUtils.put(item, EMAILS, value);
    }

    public static void setEmails(final DBObject item, final String value) {
        final String[] emails_array = StringUtils.splitUnique(value, new String[] { " ", ",", ";" });
        MongoUtils.put(item, EMAILS, Arrays.asList(emails_array));
    }

    public static String getImage(final DBObject item) {
        return MongoUtils.getString(item, IMAGE);
    }

    public static void setImage(final DBObject item, final String value) {
        MongoUtils.put(item, IMAGE, value);
    }

    public static String getRealName(final DBObject item) {
        return MongoUtils.getString(item, REALNAME);
    }

    public static void setRealname(final DBObject item, final String value) {
        MongoUtils.put(item, REALNAME, value);
    }

    public static String getBirthDate(final DBObject item) {
        return MongoUtils.getString(item, BIRTHDATE);
    }

    public static void setBirthDate(final DBObject item, final String value) {
        MongoUtils.put(item, BIRTHDATE, value);
    }

    public static String getPhoneMobile(final DBObject item) {
        return MongoUtils.getString(item, PHONEMOBILE);
    }

    public static void setPhoneMobile(final DBObject item, final String value) {
        MongoUtils.put(item, PHONEMOBILE, value);
    }

    public static String getOwnerId(final DBObject item) {
        return MongoUtils.getString(item, OWNER_ID);
    }

    public static void setOwnerId(final DBObject item, final String value) {
        MongoUtils.put(item, OWNER_ID, value);
    }

    public static String getHubId(final DBObject item) {
        return MongoUtils.getString(item, HUB_ID);
    }

    public static void setHubId(final DBObject item, final String value) {
        MongoUtils.put(item, HUB_ID, value);
    }

    public static String getPrevHubId(final DBObject item) {
        return MongoUtils.getString(item, PREV_HUB_ID);
    }

    public static void setPrevHubId(final DBObject item, final String value) {
        MongoUtils.put(item, PREV_HUB_ID, value);
    }

    public static List<String> getKeywords(final DBObject item) {
        return MongoUtils.getList(item, KEYWORDS);
    }

    public static void setKeywords(final DBObject item, final List<String> value) {
        MongoUtils.put(item, KEYWORDS, value);
    }

    public static String getKeywordsHtml(final DBObject item) {
        return MongoUtils.getString(item, KEYWORDS_HTML);
    }

    public static void setKeywordsHtml(final DBObject item, final String value) {
        MongoUtils.put(item, KEYWORDS_HTML, value);
    }

    public static List<String> getCollaborationHubs(final DBObject item) {
        return MongoUtils.getList(item, COLLABORATION_HUBS);
    }

    public static void setCollaborationHubs(final DBObject item, final List<String> value) {
        MongoUtils.put(item, COLLABORATION_HUBS, value);
    }

    public static void addCollaboration(final DBObject item, final String hubId) {
        final List<String> hubs = getCollaborationHubs(item);
        if (!hubs.contains(hubId)) {
            hubs.add(hubId);
        }
        MongoUtils.put(item, COLLABORATION_HUBS, hubs);
    }

    public static void removeCollaboration(final DBObject item, final String hubId) {
        final List<String> hubs = getCollaborationHubs(item);
        if (hubs.contains(hubId)) {
            hubs.remove(hubId);
        }
        MongoUtils.put(item, COLLABORATION_HUBS, hubs);
    }

    public static void regenerateDescription(final DBObject item) {
        final StringBuilder description = new StringBuilder(Contact.getDescription(item));

        final HtmlUtils.HtmlText html = HtmlUtils.toHtml(description.toString());
        Contact.setDescription(item, description.toString());
        Contact.setDescriptionHtml(item, html.getHtml());
        Contact.setKeywords(item, html.getTagsList());
        Contact.setKeywordsHtml(item, html.getHtmlTags());
    }

    public static void regenerateKeywords(final DBObject item) {
        final List<String> keywords = Contact.getKeywords(item);
        if (!keywords.isEmpty()) {
            final List<String> clean_keywords = new LinkedList<String>();
            // remove hash
            for (final String keyword : keywords) {
                clean_keywords.add(keyword.replaceAll("#", "").toLowerCase());
            }
            Contact.setKeywords(item, clean_keywords);
            Contact.setKeywordsHtml(item, HtmlUtils.htmlHashTags(clean_keywords));
        }
    }

    public static void regenerateParallelArrayKey(final DBObject item) {
        final Set<String> keys = new HashSet<String>();

        keys.addAll(getKeywords(item));
        keys.addAll(getEmails(item));
        keys.addAll(getCollaborationHubs(item));

        // add single fields
        keys.add(MongoUtils.getString(item, CREATIONDATE));
        if (StringUtils.hasText(Contact.getDomainId(item))) {
            keys.add(Contact.getDomainId(item));
        }

        MongoUtils.put(item, PARALLEL_ARRAY_KEY, keys);
    }

}