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

Java tutorial

Introduction

Here is the source code for com.eywa.impl.app.mongo.services.ProductTemplateListService.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.ProductCategory;
import com.eywa.impl.app.mongo.entities.ProductTemplate;
import com.eywa.impl.app.mongo.entities.ProductTemplateList;
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.StringUtils;
import org.ly.packages.mongo.impl.AbstractMongoService;
import org.ly.packages.mongo.impl.StandardCodedException;
import org.ly.packages.mongo.impl.util.MongoUtils;
import org.ly.proxies.DBProxy;

import java.util.List;

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

    private static final String[] LOCALIZATION = new String[] { ProductTemplateList.NAME,
            ProductTemplateList.DESCRIPTION };

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

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

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

    public DBObject localize(final DBObject item, final String lang) {
        if (null != item) {
            super.localize(item, lang, LOCALIZATION);
        }
        return item;
    }

    public List<DBObject> localize(final List<DBObject> items, final String lang) {
        if (null != items) {
            super.localize(items, lang, LOCALIZATION);
        }
        return items;
    }

    public void addLocalization(final String id, final String lang, final String value) {
        super.addLocalization(id, lang, ProductCategory.NAME, value);
    }

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

    @Override
    public int upsert(final DBObject item) throws StandardCodedException {

        return super.upsert(item);
    }

    public DBObject saveAs(final DBObject raw) throws Exception {
        final String product_id = ProductTemplateList.getProductId(raw);
        DBObject item;
        if (StringUtils.hasText(product_id)) {
            item = getByProductId(product_id);
            if (null == item) {
                item = new ProductTemplateList(raw);
            } else {
                // merge
                ProductTemplateList.setName(item, ProductTemplateList.getName(raw));
                ProductTemplateList.setDescription(item, ProductTemplateList.getDescription(raw));
                ProductTemplateList.setUid(item, ProductTemplateList.getUid(raw));
                ProductTemplateList.setI18n(item, ProductTemplateList.getI18n(raw));
                ProductTemplateList.setDataModel(item, ProductTemplateList.getDataModel(raw));
                ProductTemplateList.setGuiMarkup(item, ProductTemplateList.getGuiMarkup(raw));
                ProductTemplateList.setGuiLogic(item, ProductTemplateList.getGuiLogic(raw));
                ProductTemplateList.setCoreLogic(item, ProductTemplateList.getCoreLogic(raw));
            }
        } else {
            item = new ProductTemplateList(raw);
        }
        super.upsert(item);
        return super.findById(ProductTemplateList.getId(item));
    }

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

    private DBObject getByProductId(final String productId) {
        final DBObject query = new BasicDBObject();
        query.put(ProductTemplateList.PRODUCT_ID, productId);
        return super.findOne(query);
    }

    // --------------------------------------------------------------------
    //               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 ProductTemplateListService srvc = new ProductTemplateListService();
            return srvc.findById(id);
        } catch (Throwable ignored) {
        }
        return null;
    }

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

    public static List<DBObject> getList(final String lang, final String uid) {
        try {
            final ProductTemplateListService srvc = new ProductTemplateListService();
            final List<DBObject> result;
            if (StringUtils.hasText(uid)) {
                final BasicDBList or = new BasicDBList();
                or.add(new BasicDBObject(ProductTemplateList.UID, uid));
                or.add(new BasicDBObject(ProductTemplateList.UID, '*'));
                final DBObject query = new BasicDBObject();
                query.put(MongoUtils.OP_OR, or);
                result = srvc.find(query);
            } else {
                result = srvc.find();
            }
            srvc.localize(result, lang);
            return result;
        } catch (Throwable ignored) {
        }
        return null;
    }

}