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

Java tutorial

Introduction

Here is the source code for com.eywa.impl.app.mongo.entities.Hub.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.DBObject;
import org.ly.commons.util.DateUtils;
import org.ly.commons.util.StringUtils;
import org.ly.packages.mongo.impl.MongoObject;
import org.ly.packages.mongo.impl.util.MongoUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 */
public class Hub extends MongoObject {

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

    public static String COLLECTION = ICollectionNames.HUBS;

    public static String HUB_ALL = "all"; // jolly for queries.
    public static String HUB_HOME = "home"; // main user hub.
    public static String HUB_RECYCLE_BIN = "recycle_bin"; // main user recycle bin hub.

    // ------------------------------------------------------------------------
    //                      fields
    // ------------------------------------------------------------------------

    public static final String ID = IAppConstants.ID;
    public static final String DOMAIN_ID = IAppConstants.DOMAIN_ID;
    public static final String UID = IAppConstants.UID;
    public static final String USER_ID = IAppConstants.USER_ID;
    public static final String NAME = IAppConstants.NAME;
    public static final String DESCRIPTION = IAppConstants.DESCRIPTION;
    public static final String DESCRIPTION_HTML = IAppConstants.DESCRIPTION_HTML;
    public static final String CREATION_DATE = IAppConstants.CREATION_DATE;

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

    public static final String KEYWORDS = IAppConstants.KEYWORDS;
    public static final String COLLABORATORS = IAppConstants.COLLABORATORS;

    // ------------------------------------------------------------------------
    //                      c o n s t r u c t o r
    // ------------------------------------------------------------------------

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

    public Hub(final String json) {
        super(json);
        this.init();
    }

    private void init() {
        if (!StringUtils.hasText(Hub.getId(this))) {
            final String id = COLLECTION.concat("_").concat(MongoUtils.createUUID());
            this.append(ID, id);
            this.append(CREATION_DATE, DateUtils.now().getTime());
        }
    }

    public static void init(final DBObject hub) {
        if (!StringUtils.hasText(Hub.getId(hub))) {
            final String id = createUUID("");
            Hub.setId(hub, id);
            hub.put(CREATION_DATE, DateUtils.now().getTime());
        }
    }

    public static String createUUID(final String id_prefix) {
        return StringUtils.hasText(id_prefix)
                ? COLLECTION.concat("_").concat(id_prefix).concat("_").concat(MongoUtils.createUUID())
                : COLLECTION.concat("_").concat(MongoUtils.createUUID());
    }

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

    public static boolean isHome(final DBObject item) {
        return HUB_HOME.equalsIgnoreCase(MongoUtils.getString(item, UID));
    }

    public static boolean isRecycleBin(final DBObject item) {
        return HUB_RECYCLE_BIN.equalsIgnoreCase(MongoUtils.getString(item, UID));
    }

    public static boolean isRecycleBinId(final String itemId) {
        final String recycleBinPrefix = COLLECTION.concat("_").concat(HUB_RECYCLE_BIN).concat("_");
        return itemId.startsWith(recycleBinPrefix);
    }

    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 getUserId(final DBObject item) {
        return MongoUtils.getString(item, USER_ID);
    }

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

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

    public static void setUid(final DBObject item, final String value) {
        MongoUtils.put(item, UID, 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 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 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 List<String> getCollaborators(final DBObject item) {
        return MongoUtils.getList(item, COLLABORATORS);
    }

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

    public static void addCollaborator(final DBObject item, final String userId) {
        final List<String> collaborators = getCollaborators(item);
        if (!collaborators.contains(userId)) {
            collaborators.add(userId);
        }
        MongoUtils.put(item, COLLABORATORS, collaborators);
    }

    public static void removeCollaborator(final DBObject item, final String userId) {
        final List<String> collaborators = getCollaborators(item);
        if (collaborators.contains(userId)) {
            collaborators.remove(userId);
        }
        MongoUtils.put(item, COLLABORATORS, collaborators);
    }

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

        keys.addAll(getKeywords(item));
        keys.addAll(getCollaborators(item));

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

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