com.eywa.impl.app.mongo.services.HubService.java Source code

Java tutorial

Introduction

Here is the source code for com.eywa.impl.app.mongo.services.HubService.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.services;

import com.eywa.impl.app.mongo.entities.Hub;
import com.eywa.impl.app.mongo.entities.User;
import com.eywa.impl.app.server.utils.HtmlUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import org.ly.Smartly;
import org.ly.commons.util.CollectionUtils;
import org.ly.commons.util.StringUtils;
import org.ly.packages.mongo.impl.AbstractMongoService;
import org.ly.packages.mongo.impl.IMongoConstants;
import org.ly.packages.mongo.impl.MongoPage;
import org.ly.packages.mongo.impl.util.MongoUtils;
import org.ly.proxies.DBProxy;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * @author angelo.geminiani
 */
public class HubService extends AbstractMongoService {

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

    public HubService() throws Exception {
        super((DB) DBProxy.get().getDBMain(), Hub.COLLECTION, Smartly.getLanguages());
    }

    // --------------------------------------------------------------------
    //               p u b l i c
    // --------------------------------------------------------------------

    public int upsert(final String userId, final DBObject item) throws Exception {
        final DBObject user = UserService.getEnabled(userId);
        if (null != user) {

            if (!StringUtils.hasText(Hub.getUserId(item))) {
                // new hub
                Hub.setDomainId(item, User.getDomainId(user));
                Hub.setUserId(item, userId);
                Hub.setUid(item, Hub.getId(item));
            }

            // update keywords
            final String description = Hub.getDescription(item);
            if (StringUtils.hasText(description)) {
                final HtmlUtils.HtmlText html = HtmlUtils.toHtml(description);
                Hub.setDescriptionHtml(item, html.getHtml());
                Hub.setKeywords(item, html.getTagsList());
            }

            // regenerates parallel keys
            Hub.regenerateParallelArrayKey(item);

            final int result = super.upsert(item);

            //-- creates Contact of hub owner --//
            try {
                final ContactService contactSrvc = new ContactService();
                contactSrvc.createHubOwner(item);
            } catch (final Throwable ignored) {
            }

            return result;
        }
        return -1;
    }

    public DBObject removeHub(final String userId, final String hubId) throws Exception {
        final DBObject result = super.removeById(hubId);

        //-- remove files --//
        FileService.removeByHubId(userId, hubId);

        //-- remove contacts --//
        ContactService.removeByHubId(userId, hubId);

        // TODO: log user removed this hub

        return result;
    }

    public DBObject getHubHome(final String userId) throws Exception {
        final DBObject user = UserService.get(userId);
        if (null != user) {
            final DBObject query = MongoUtils.queryEquals(Hub.USER_ID, userId);
            query.put(Hub.UID, Hub.HUB_HOME);
            DBObject hub = super.findOne(query);
            if (null == hub) {
                hub = new Hub();
                Hub.setUid(hub, Hub.HUB_HOME);
                Hub.setUserId(hub, userId);
                Hub.setName(hub, Hub.HUB_HOME);
                Hub.setDescription(hub, Hub.HUB_HOME);
                super.insert(hub);
            }
            return hub;
        }
        return null;
    }

    public DBObject getHubRecycleBin(final String userId) throws Exception {
        final DBObject user = UserService.getEnabled(userId);
        if (null != user) {
            final DBObject query = MongoUtils.queryEquals(Hub.USER_ID, userId);
            query.put(Hub.UID, Hub.HUB_RECYCLE_BIN);
            DBObject hub = super.findOne(query);
            if (null == hub) {
                hub = new Hub();
                Hub.setId(hub, Hub.createUUID(Hub.HUB_RECYCLE_BIN));
                Hub.setUid(hub, Hub.HUB_RECYCLE_BIN);
                Hub.setUserId(hub, userId);
                Hub.setName(hub, Hub.HUB_RECYCLE_BIN);
                Hub.setDescription(hub, Hub.HUB_RECYCLE_BIN);
                super.insert(hub);
            }
            return hub;
        }
        return null;
    }

    public List<DBObject> getByUserId(final String userId) {
        final List<DBObject> result = new LinkedList<DBObject>();
        final DBObject user = UserService.getEnabled(userId);
        if (null != user) {
            final DBObject query = this.queryByUserId(userId);
            // user hubs (limited)
            result.addAll(super.find(query, null, 0, 0, new String[] { Hub.NAME }, null));

            // user collaborations
            final List<String> hubCollaborations = User.getCollaborations(user);
            if (!CollectionUtils.isEmpty(hubCollaborations)) {
                final DBObject query2 = MongoUtils.queryIn(Hub.ID,
                        hubCollaborations.toArray(new Object[hubCollaborations.size()]));
                result.addAll(super.find(query2, null, 0, 0, new String[] { Hub.NAME }, null));
            }
        }
        return result;
    }

    public MongoPage pagedByUserId(final String userId, final String searchText, final int skip, final int limit) {
        final DBObject user = UserService.getEnabled(userId);
        if (null != user) {
            final DBObject query;

            // user collaborations
            final String[] hubCollaborations = User.getCollaborationsAsArray(user);
            if (!CollectionUtils.isEmpty(hubCollaborations)) {
                // user filter
                final DBObject query_user = this.queryByUserId(userId);

                // collaboration filter
                //final Object[] array = hubCollaborations.toArray(new Object[hubCollaborations.size()]);
                final DBObject query_collaborations = MongoUtils.queryIn(Hub.ID, hubCollaborations);

                final BasicDBList or_conditions = new BasicDBList();
                or_conditions.add(query_user);
                or_conditions.add(query_collaborations);

                query = StringUtils.hasText(searchText) ? queryLookup(searchText) : new BasicDBObject();
                query.put(IMongoConstants.OP_OR, or_conditions);
            } else {
                if (StringUtils.hasText(searchText)) {
                    // user filter
                    final BasicDBList and_conditions = new BasicDBList();
                    final DBObject query_user = this.queryByUserId(userId);
                    final DBObject query_search = queryLookup(searchText);
                    and_conditions.add(query_user);
                    and_conditions.add(query_search);

                    query = new BasicDBObject();
                    query.put(IMongoConstants.OP_AND, and_conditions);
                } else {
                    query = this.queryByUserId(userId);
                }
            }
            final MongoPage result = super.paged(query, null, skip, limit, new String[] { Hub.NAME }, null);
            return result;
        }
        return new MongoPage();
    }

    public int countHubs(final String userId) {
        return this.countAllByUserId(userId);
    }

    public int remainingHubs(final String userId) {
        final DBObject user = UserService.getEnabled(userId);
        if (null != user) {
            final int remain = this.countAllByUserId(userId);
            if (remain > 0) {
                return remain;
            }
        }
        return 0;
    }

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

    private List<DBObject> getAllByUserId(final String userId) {
        return super.find(this.queryByUserId(userId), null, 0, 0, new String[] { Hub.NAME }, null);
    }

    private int countAllByUserId(final String userId) {
        return super.count(this.queryByUserId(userId));
    }

    private DBObject queryByUserId(final String userId) {
        final DBObject query = MongoUtils.queryEquals(Hub.USER_ID, userId);

        // exclude home e recycle_bin
        final BasicDBList and_conditions = new BasicDBList();
        and_conditions.add(MongoUtils.queryNotEquals(Hub.UID, Hub.HUB_HOME));
        and_conditions.add(MongoUtils.queryNotEquals(Hub.UID, Hub.HUB_RECYCLE_BIN));
        query.put(IMongoConstants.OP_AND, and_conditions);

        return query;
    }

    private static DBObject queryLookup(final String searchText) {
        if (StringUtils.hasText(searchText)) {

            final BasicDBList conditions = new BasicDBList();
            conditions.add(MongoUtils.queryContains(Hub.NAME, searchText, MongoUtils.CASE_INSENSITIVE));
            conditions.add(MongoUtils.queryContains(Hub.DESCRIPTION, searchText, MongoUtils.CASE_INSENSITIVE));

            return new BasicDBObject(MongoUtils.OP_OR, conditions);
        }
        return null;
    }

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

    public static DBObject getHub(final String hubId) {
        try {
            final HubService srvc = new HubService();
            return srvc.findById(hubId);
        } catch (Throwable ignored) {
        }
        return null;
    }

    public static DBObject remove(final String userId, final String hubId) {
        try {
            final HubService srvc = new HubService();
            return srvc.removeHub(userId, hubId);
        } catch (Throwable ignored) {
        }
        return null;
    }

    public static DBObject getHome(final String userId) {
        try {
            final HubService srvc = new HubService();
            return srvc.getHubHome(userId);
        } catch (Throwable ignored) {
        }
        return null;
    }

    public static String getHomeId(final String userId) {
        try {
            final DBObject home = getHome(userId);
            return null != home ? Hub.getId(home) : "";
        } catch (Throwable ignored) {
        }
        return "";
    }

    public static boolean isHome(final String userId, final String hubId) {
        try {
            final String homeId = getHomeId(userId);
            return homeId.equalsIgnoreCase(hubId);
        } catch (Throwable ignored) {
        }
        return false;
    }

    public static DBObject getRecycleBin(final String userId) {
        try {
            final HubService srvc = new HubService();
            return srvc.getHubRecycleBin(userId);
        } catch (Throwable ignored) {
        }
        return null;
    }

    public static Collection<String> getHubIds(final String userId) {
        final Collection<String> result = new LinkedList<String>();
        try {
            final HubService srvc = new HubService();
            final DBObject query = srvc.queryByUserId(userId);
            final List<DBObject> items = srvc.find(query, new String[] { Hub.ID }, null, null);
            for (final DBObject item : items) {
                result.add(Hub.getId(item));
            }
        } catch (Throwable ignored) {
        }
        return result;
    }

    public static boolean isOwnerOrCollaborator(final String userId, final String hubId) {
        try {
            final HubService srvc = new HubService();
            final DBObject hub = srvc.findById(hubId);
            if (null != hub) {
                if (Hub.getUserId(hub).equalsIgnoreCase(userId)) {
                    return true;
                }
                // check if is a collaborator
                final List<String> collaborators = Hub.getCollaborators(hub);
                return collaborators.indexOf(userId) > -1;
            }
        } catch (final Throwable ignored) {
        }
        return false;
    }

}