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.Session; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBObject; import org.ly.Smartly; import org.ly.commons.util.DateUtils; import org.ly.commons.util.RandomUtils; import org.ly.commons.util.StringUtils; import org.ly.packages.mongo.impl.AbstractMongoService; import org.ly.packages.mongo.impl.util.MongoUtils; import org.ly.proxies.DBProxy; import java.util.List; /** * @author angelo.geminiani */ public final class SessionService extends AbstractMongoService { // -------------------------------------------------------------------- // c o n s t r u c t o r // -------------------------------------------------------------------- public SessionService() throws Exception { super((DB) DBProxy.get().getDBMain(), Session.COLLECTION, Smartly.getLanguages()); } // -------------------------------------------------------------------- // p u b l i c // -------------------------------------------------------------------- public List<DBObject> removeExpired() throws Exception { final DBObject query = MongoUtils.queryLowerThan(Session.EXPIRATION_DATE, DateUtils.now().getTime(), true); final List<DBObject> expired = super.find(query); if (!expired.isEmpty()) { super.remove(query); } return expired; } // -------------------------------------------------------------------- // 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 SessionService srvc = new SessionService(); return srvc.findById(id); } catch (Throwable ignored) { } return null; } public static DBObject getByType(final String type, final String uid) { try { final SessionService srvc = new SessionService(); final DBObject query = new BasicDBObject(); query.put(Session.TYPE, type); query.put(Session.UID, uid); return srvc.findOne(query); } catch (Throwable ignored) { } return null; } public static DBObject create(final String userId, final String type, final String uid) { try { final SessionService srvc = new SessionService(); final DBObject item = new Session(userId, type); if (StringUtils.hasText(uid)) { Session.setUid(item, uid); } else { Session.setUid(item, RandomUtils.random(8, RandomUtils.CHARS_LOW_NUMBERS)); } srvc.upsert(item); return item; } catch (Throwable ignored) { } return null; } public static DBObject sign(final String id, final String userId) { try { final SessionService srvc = new SessionService(); final DBObject item = srvc.findById(id); Session.ping(item); Session.setUserId(item, userId); srvc.upsert(item); return item; } catch (Throwable ignored) { } return null; } public static DBObject ping(final String id) { try { final SessionService srvc = new SessionService(); final DBObject item = srvc.findById(id); Session.ping(item); srvc.upsert(item); return item; } catch (Throwable ignored) { } return null; } /** * Restore old session after removed. * * @param json JSON string * @return Session */ public static DBObject restore(final String json) { try { return restore(MongoUtils.parseObject(json)); } catch (Throwable ignored) { } return null; } public static DBObject restore(final DBObject item) { try { if (null != item) { final String id = Session.getId(item); final SessionService srvc = new SessionService(); if (null == srvc.findById(id)) { Session.ping(item); srvc.upsert(item); return item; } } } catch (Throwable ignored) { } return null; } public static DBObject setData(final String id, final String data) { try { final SessionService srvc = new SessionService(); final DBObject item = srvc.findById(id); Session.ping(item); final DBObject dbData = MongoUtils.parseObject(data); Session.setData(item, dbData); srvc.upsert(item); return item; } catch (Throwable ignored) { } return null; } /** * Remove all expired sessions and returns removed items * * @return Removed items */ public static List<DBObject> clean() { try { final SessionService srvc = new SessionService(); return srvc.removeExpired(); } catch (Throwable ignored) { } return null; } }