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

Java tutorial

Introduction

Here is the source code for com.eywa.impl.app.mongo.services.ShopTemplateService.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.ShopTemplate;
import com.eywa.impl.app.mongo.entities.User;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import org.ly.Smartly;
import org.ly.commons.util.StringUtils;
import org.ly.packages.mongo.impl.AbstractMongoService;
import org.ly.packages.mongo.impl.StandardCodedException;
import org.ly.proxies.DBProxy;

import java.util.ArrayList;
import java.util.List;

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

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

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

    // --------------------------------------------------------------------
    //               l o c a l i z a t i o n
    // --------------------------------------------------------------------

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

    @Override
    public int upsert(final DBObject item) throws StandardCodedException {
        return super.upsert(item);
    }

    public List<DBObject> lookup(final String parentId, final String type) {
        try {
            final DBObject query = new BasicDBObject();

            if (StringUtils.hasText(parentId)) {
                query.put(ShopTemplate.PARENT_ID, parentId);
            }
            if (StringUtils.hasText(type)) {
                query.put(ShopTemplate.TYPE, type);
            }

            return super.find(query);
        } catch (Throwable ignored) {
        }
        return new ArrayList<>();
    }

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

    // --------------------------------------------------------------------
    //               S T A T I C   p r i v a t e
    // --------------------------------------------------------------------

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

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

    public static List<DBObject> getTemplateElements(final String templateId) {
        try {
            if (StringUtils.hasText(templateId)) {
                final String id = StringUtils.isNULL(templateId) ? User.DEFAULT_SHOP_TEMPLATE_ID : templateId;
                final DBObject query = new BasicDBObject();
                query.put(ShopTemplate.ROOT_ID, id);
                final ShopTemplateService srvc = new ShopTemplateService();
                return srvc.find(query, null, new String[] { ShopTemplate.UID }, null);
            }
        } catch (Throwable ignored) {
        }
        return new ArrayList<>();
    }
}