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.mongo.entities.Hashfolder; 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.MongoPage; import org.ly.packages.mongo.impl.StandardCodedException; import org.ly.proxies.DBProxy; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; /** * @author angelo.geminiani */ public class HashfolderService extends AbstractMongoService { // -------------------------------------------------------------------- // c o n s t r u c t o r // -------------------------------------------------------------------- public HashfolderService() throws Exception { super((DB) DBProxy.get().getDBMain(), Hashfolder.COLLECTION, Smartly.getLanguages()); } // -------------------------------------------------------------------- // p u b l i c // -------------------------------------------------------------------- @Override public int upsert(final DBObject item) throws StandardCodedException { //-- regenerate keywords --// Hashfolder.regenerateKeywords(item); return super.upsert(item); } public DBObject moveTo(final String fileId, final String parentId) throws Exception { final DBObject resource = super.findById(fileId); Hashfolder.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(Hashfolder.HUB_ID, hubId); query.put(Hashfolder.PARENT_ID, StringUtils.hasText(parentId) ? parentId : hubId); return super.find(query, null, 0, 0, new String[] { Hashfolder.NAME }, null); } return new ArrayList<DBObject>(); } public MongoPage lookup(final String id, final int skip, final int limit) throws Exception { final DBObject item = super.findById(id); if (null != item) { if (Hashfolder.isDirectory(item)) { // files final FileService srvc = new FileService(); return srvc.lookup(Hashfolder.getHubId(item), skip, limit, Hashfolder.getKeywords(item)); } else if (Hashfolder.isAlbum(item)) { // files, but only images } else if (Hashfolder.isContactList(item)) { // contacts final ContactService srvc = new ContactService(); return srvc.lookup(Hashfolder.getHubId(item), skip, limit, Hashfolder.getKeywords(item)); } } return new MongoPage(); } public int countContent(final String id, final Collection<String> keywords) { try { final DBObject item = super.findById(id); if (null != item) { final Collection<String> keys = null != keywords ? keywords : Hashfolder.getKeywords(item); final String hubId = Hashfolder.getHubId(item); if (Hashfolder.isDirectory(item)) { // files final FileService srvc = new FileService(); return srvc.countByHubAndKeywords(hubId, keys); } else if (Hashfolder.isAlbum(item)) { // files, but only images } else if (Hashfolder.isContactList(item)) { // contacts final ContactService srvc = new ContactService(); return srvc.countByHubAndKeywords(hubId, keys); } } } catch (Throwable ignored) { } return 0; } // -------------------------------------------------------------------- // 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 HashfolderService srvc = new HashfolderService(); return srvc.findById(id); } catch (Throwable ignored) { } return null; } public static DBObject remove(final String userId, final String id) { try { final HashfolderService srvc = new HashfolderService(); return srvc.removeById(id); } catch (Throwable ignored) { } return null; } public static List<DBObject> getTypedEntities(final String id, final Collection<String> keys, final String[] fieldNames) { try { final DBObject item = get(id); if (null != item) { final Collection<String> keywords = null != keys ? keys : Hashfolder.getKeywords(item); if (Hashfolder.isDirectory(item)) { // files final FileService srvc = new FileService(); return srvc.getByHubAndKeywords(Hashfolder.getHubId(item), keywords, fieldNames); } else if (Hashfolder.isAlbum(item)) { // files, but only images } else if (Hashfolder.isContactList(item)) { // contacts final ContactService srvc = new ContactService(); return srvc.getByHubAndKeywords(Hashfolder.getHubId(item), keywords, fieldNames); } } } catch (Throwable ignored) { } return new ArrayList<DBObject>(); } public static List<String> getTypedEntitiesIds(final String id, final Collection<String> keys) { final List<String> result = new LinkedList<String>(); final List<DBObject> entities = getTypedEntities(id, keys, new String[] { Hashfolder.ID }); for (final DBObject item : entities) { result.add(Hashfolder.getId(item)); } return result; } public static int countByHub(final String hubId, final String type) { try { final DBObject query = new BasicDBObject(); query.put(Hashfolder.HUB_ID, hubId); if (StringUtils.hasText(type)) { query.put(Hashfolder.TYPE, type); } final HashfolderService srvc = new HashfolderService(); return srvc.count(query); } catch (Throwable ignored) { } return 0; } }