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

Java tutorial

Introduction

Here is the source code for com.eywa.impl.app.mongo.services.ShopService.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.Shop;
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.LinkedList;
import java.util.List;

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

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

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

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

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

            if (!StringUtils.hasText(Shop.getUserId(item))) {
                // new shop
                Shop.setUserId(item, userId);
                Shop.setSellerId(item, userId);
                Shop.setUid(item, Shop.getId(item));
            }

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

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

            return super.upsert(item);
        }
        return -1;
    }

    public DBObject removeShop(final String id) throws Exception {
        return super.removeById(id);
    }

    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);
            result.addAll(super.find(query, null, 0, 0, new String[] { Shop.NAME }, null));

            // user collaborations
            final List<String> collaborations = User.getCollaborations(user);
            if (!CollectionUtils.isEmpty(collaborations)) {
                final DBObject query2 = MongoUtils.queryIn(Shop.ID,
                        collaborations.toArray(new Object[collaborations.size()]));
                result.addAll(super.find(query2, null, 0, 0, new String[] { Shop.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[] collaborations = User.getCollaborationsAsArray(user);
            if (!CollectionUtils.isEmpty(collaborations)) {
                // user filter
                final DBObject query_user = this.queryByUserId(userId);

                // collaboration filter
                final DBObject query_collaborations = MongoUtils.queryIn(Shop.ID, collaborations);

                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[] { Shop.NAME }, null);
            return result;
        }
        return new MongoPage();
    }

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

    // --------------------------------------------------------------------
    //               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[] { Shop.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(Shop.USER_ID, userId);

        return query;
    }

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

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

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

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

    public static DBObject get(final String id) {
        try {
            final ShopService srvc = new ShopService();
            return srvc.findById(id);
        } catch (Throwable ignored) {
        }
        return null;
    }

    public static DBObject remove(final String id) {
        try {
            final ShopService srvc = new ShopService();
            return srvc.removeShop(id);
        } catch (Throwable ignored) {
        }
        return null;
    }

    public static boolean isAuthorized(final String userId, final String shopId) {
        try {
            final ShopService srvc = new ShopService();
            final DBObject item = srvc.findById(shopId);
            if (null != item) {
                if (Shop.getUserId(item).equalsIgnoreCase(userId)
                        || Shop.getSellerId(item).equalsIgnoreCase(userId)) {
                    return true;
                }
                // check if is a collaborator
                final List<String> collaborators = Shop.getCollaborators(item);
                return collaborators.indexOf(userId) > -1;
            }
        } catch (final Throwable ignored) {
        }
        return false;
    }

    public static boolean isOwner(final String userId, final String shopId) {
        try {
            final ShopService srvc = new ShopService();
            final DBObject item = srvc.findById(shopId);
            if (null != item) {
                return Shop.getUserId(item).equalsIgnoreCase(userId);
            }
        } catch (final Throwable ignored) {
        }
        return false;
    }

    public static boolean isSeller(final String userId, final String shopId) {
        try {
            final ShopService srvc = new ShopService();
            final DBObject item = srvc.findById(shopId);
            if (null != item) {
                return Shop.getSellerId(item).equalsIgnoreCase(userId);
            }
        } catch (final Throwable ignored) {
        }
        return false;
    }

    public static boolean isCollaborator(final String userId, final String shopId) {
        try {
            final ShopService srvc = new ShopService();
            final DBObject item = srvc.findById(shopId);
            if (null != item) {
                // check if is a collaborator
                final List<String> collaborators = Shop.getCollaborators(item);
                return collaborators.indexOf(userId) > -1;
            }
        } catch (final Throwable ignored) {
        }
        return false;
    }

}