Java tutorial
/* * 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.controllers.central.Activity; import com.eywa.impl.app.controllers.central.CentralControl; import com.eywa.impl.app.mongo.entities.*; import com.eywa.impl.app.mongo.entities.items.ItemBillingUser; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBObject; import org.ly.Smartly; import org.ly.commons.logging.Level; 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.MongoPage; import org.ly.packages.mongo.impl.StandardCodedException; import org.ly.packages.mongo.impl.util.MongoUtils; import org.ly.proxies.DBProxy; import java.util.*; /** * Before create a Product call "exists" method to check if UID already exists. * Packages, Folders and Files must have a unique UID into its parent node (namespace). * * @author angelo.geminiani */ public class ProductService extends AbstractMongoService { private static final String[] LOCALIZATION = new String[] { Product.NAME, Product.DESCRIPTION, Product.SHEET, Product.SEO_TITLE, Product.SEO_KEYWORDS, Product.SEO_DESCRIPTION }; // -------------------------------------------------------------------- // c o n s t r u c t o r // -------------------------------------------------------------------- public ProductService() throws Exception { super((DB) DBProxy.get().getDBMain(), Product.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 { Product.regenerateKeywords(item); //-- update file if any --// if (StringUtils.hasText(Product.getFileId(item))) { final String uid = Product.getUid(item); if (StringUtils.hasText(uid)) { final DBObject existing = super.findById(Product.getId(item), new String[] { Product.UID }); if (null != existing && !uid.equalsIgnoreCase(Product.getUid(existing))) { FileService.updateName(Product.getFileId(item), uid); } } } //-- normalize numbers --// Product.setPrice(item, Product.getPrice(item)); Product.setQty(item, Product.getQty(item)); return super.upsert(item); } public DBObject getByUid(final String parentId, final String uid) { final DBObject query = new BasicDBObject(); query.put(Product.PARENT_ID, parentId); query.put(Product.UID, uid); return super.findOne(query); } public boolean exists(final String parentId, final String uid) { return null != getByUid(parentId, uid); } public DBObject createPackage(final String userId, final String hubId, final String parentId, final String uid) { final DBObject user = UserService.getEnabled(userId); final DBObject hub = HubService.getHub(hubId); return create(user, hub, parentId, uid, Product.TYPE_PACKAGE); } public DBObject createDirectory(final String userId, final String hubId, final String parentId, final String uid) { final DBObject user = UserService.getEnabled(userId); final DBObject hub = HubService.getHub(hubId); return create(user, hub, parentId, uid, Product.TYPE_DIRECTORY); } public DBObject create(final DBObject user, final DBObject hub, final String parentId, final String uid, final String type) { if (null != user && null != hub) { try { if (!exists(parentId, uid)) { final String userId = User.getId(user); final String hubId = Hub.getId(hub); final DBObject item = new Product(); Product.setUserId(item, userId); Product.setHubId(item, hubId); Product.setParentId(item, parentId); Product.setType(item, type); Product.setUid(item, uid); Product.setCurrencyCode(item, ItemBillingUser.getCurrencyCode(User.getBilling(user))); //-- create directory in files if is a Package --// if (type.equalsIgnoreCase(Product.TYPE_PACKAGE)) { final DBObject dir = FileService.getOrCreateProductDirectory(userId, hubId, Product.getId(item), uid); Product.setFileId(item, File.getId(dir)); } super.upsert(item); return item; } else { return getByUid(parentId, uid); } } catch (Throwable t) { super.getLogger().log(Level.SEVERE, null, t); } } return null; // duplicate name or server error } public DBObject moveTo(final String fileId, final String parentId) throws Exception { final DBObject resource = super.findById(fileId); Product.setParentId(resource, parentId); super.upsert(resource); return resource; } public List<DBObject> list(final String userId, final String hubId, final String parentId) { final DBObject user = UserService.getEnabled(userId); if (null != user) { final DBObject query = new BasicDBObject(Product.HUB_ID, hubId); query.put(File.PARENT_ID, parentId); return super.find(query, null, 0, 0, new String[] { Product.TYPE, Product.UID }, null); } return new ArrayList<DBObject>(); } public List<DBObject> list(final String userId, final String parentId, final boolean includeFolders) { final List<DBObject> result = new LinkedList<>(); final DBObject query = new BasicDBObject(); query.put(Product.USER_ID, userId); if (StringUtils.hasText(parentId)) { query.put(Product.PARENT_ID, parentId); } final List<DBObject> list = super.find(query); for (final DBObject item : list) { if (Product.isDirectory(item) && !includeFolders) { result.addAll(this.list(userId, Product.getId(item), false)); } else { result.add(item); } } return result; } public MongoPage lookup(final String hubId, final int skip, final int limit, final Collection<String> keywords) { final DBObject query = new BasicDBObject(); query.put(Product.HUB_ID, hubId); MongoUtils.queryIn(query, Product.KEYWORDS, keywords.toArray(new String[keywords.size()])); final String[] fieldNames = null; final String[] sortAsc = new String[] { Product.UID }; final String[] sortDes = new String[] {}; return super.paged(query, fieldNames, skip, limit, sortAsc, sortDes); } public MongoPage lookup(final String hubId, final int skip, final int limit, final String searchText) { DBObject query1 = null; DBObject query2 = null; if (!StringUtils.isNULL(hubId)) { // lookup in current hub query1 = new BasicDBObject(); MongoUtils.queryEquals(query1, Product.HUB_ID, hubId, MongoUtils.CASE_INSENSITIVE); } if (!StringUtils.isNULL(searchText)) { if (searchText.startsWith("#")) { query2 = new BasicDBObject(); query2.put(Product.KEYWORDS, searchText.substring(1)); } else { query2 = new BasicDBObject(); final BasicDBList conditions = new BasicDBList(); conditions.add(MongoUtils.queryEquals(Product.ID, searchText, MongoUtils.CASE_INSENSITIVE)); conditions.add(MongoUtils.queryContains(Product.UID, searchText, MongoUtils.CASE_INSENSITIVE)); conditions.add(MongoUtils.queryEquals(Product.KEYWORDS, searchText, MongoUtils.CASE_INSENSITIVE)); query2.put(MongoUtils.OP_OR, conditions); } } DBObject query = null; if (null != query1 && null != query2) { query = new BasicDBObject(); final BasicDBList conditions = new BasicDBList(); conditions.add(query1); conditions.add(query2); query.put(MongoUtils.OP_AND, conditions); } else if (null == query2) { query = query1; } else { query = query2; } final String[] fieldNames = null; final String[] sortAsc = new String[] { File.NAME }; final String[] sortDes = new String[] {}; return super.paged(query, fieldNames, skip, limit, sortAsc, sortDes); } // -------------------------------------------------------------------- // 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 ProductService srvc = new ProductService(); return srvc.findById(id); } catch (Throwable ignored) { } return null; } public static DBObject get(final String lang, final String id) { try { final ProductService srvc = new ProductService(); return srvc.localize(srvc.findById(id), lang); } catch (Throwable ignored) { } return null; } public static String getFileId(final String id) { try { final ProductService srvc = new ProductService(); final DBObject query = new BasicDBObject(); query.put(Product.ID, id); final DBObject product = srvc.findOne(query, new String[] { Product.FILE_ID }); return Product.getFileId(product); } catch (Throwable ignored) { } return ""; } public static List<DBObject> remove(final String userId, final String id) throws Exception { final DBObject resource = get(id); return remove(userId, resource); } public static List<DBObject> remove(final String userId, final DBObject resource) throws Exception { final List<DBObject> result = new LinkedList<DBObject>(); if (null != resource) { final ProductService srvc = new ProductService(); final String id = Product.getId(resource); srvc.removeById(id); result.add(resource); //-- dispatch activity --// CentralControl.dispatch(Activity.newInstance().setType(Activity.TYPE_REMOVE) .setCollection(Product.COLLECTION).setEntity(resource).putData(Activity.ATTR_USER_ID, userId)); //-- remove children in sub folders --// final List<DBObject> children = srvc.find(new BasicDBObject(Product.PARENT_ID, id)); if (!CollectionUtils.isEmpty(children)) { for (final DBObject child : children) { result.addAll(remove(userId, child)); } } } return result; } /** * Return list of parent folders * * @param itemId Package or Folder ID * @param addCurrent if start item is a directory * @param asc Ascendant. Default is false=Descendant (from start item to root). * Ascendant list is ordered from root to start item. * @return List of directories */ public static List<DBObject> getParents(final String itemId, final boolean addCurrent, final boolean asc) { final List<DBObject> result = new LinkedList<DBObject>(); final DBObject item = get(itemId); if (null != item) { if (addCurrent && Product.isDirectory(item)) { result.add(item); } final String parentId = Product.getParentId(item); if (StringUtils.hasText(parentId)) { final DBObject parent = get(parentId); if (null != parent) { result.add(parent); // recursive result.addAll(getParents(parentId, false, false)); } } } if (asc) { Collections.reverse(result); } return result; } }