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.controllers.central; import com.eywa.impl.app.IAppConstants; import com.mongodb.DBObject; import org.ly.commons.util.DateUtils; import org.ly.commons.util.StringUtils; import org.ly.packages.mongo.impl.MongoObject; import org.ly.packages.mongo.impl.util.MongoUtils; import java.util.List; /** * */ public class Activity extends MongoObject { // ------------------------------------------------------------------------ // Constants // ------------------------------------------------------------------------ //-- data attributes --// public static final String ATTR_MESSAGE = "message"; public static final String ATTR_ERROR = "error"; public static final String ATTR_CONTENT = "content"; public static final String ATTR_PASSWORD = "password"; public static final String ATTR_STATUS = "status"; public static final String ATTR_POST_ID = "post_id"; public static final String ATTR_USER_ID = "user_id"; public static final String ATTR_FILE_ID = "file_id"; public static final String ATTR_PARENT_ID = "parent_id"; public static final String ATTR_IS_IN_FOLDER = "is_in_folder"; public static final String ATTR_USER = "user"; public static final String ATTR_HUB = "hub"; public static final String ATTR_RECIPIENTS = "recipients"; public static final String ATTR_FILES = "files"; public static final String ATTR_HTML = "html"; public static final String ATTR_MAIL_TEMPLATE_ID = "mail_template_id"; //-- activity types --// public static final String TYPE_INSERT = "insert"; public static final String TYPE_UPDATE = "update"; public static final String TYPE_REMOVE = "remove"; public static final String TYPE_RESET = "reset"; public static final String TYPE_DOWNLOAD = "download"; public static final String TYPE_UPLOAD = "upload"; public static final String TYPE_VIEW = "view"; public static final String TYPE_MOVE = "move"; public static final String TYPE_MAIL = "mail"; // direct email public static final String TYPE_ERROR = "error"; // system error public static final String TYPE_FINISHED = "finished"; // ------------------------------------------------------------------------ // fields // ------------------------------------------------------------------------ public static final String ID = IAppConstants.ID; public static final String TYPE = IAppConstants.TYPE; public static final String ENTITY_COLLECTION = IAppConstants.ENTITY_COLLECTION; public static final String CREATION_DATE = IAppConstants.CREATION_DATE; // milliseconds public static final String ENTITY = IAppConstants.ENTITY; public static final String DATA = IAppConstants.DATA; // ------------------------------------------------------------------------ // c o n s t r u c t o r // ------------------------------------------------------------------------ public Activity() { this("", TYPE_INSERT, null, null); } public Activity(final String collection) { this(collection, TYPE_INSERT, null, null); } public Activity(final String collection, final DBObject entity) { this(collection, TYPE_INSERT, entity, null); } public Activity(final String collection, final String type, final DBObject entity, final DBObject data) { this.append(ID, MongoUtils.createUUID()); this.append(ENTITY_COLLECTION, collection); this.append(TYPE, type); this.put(CREATION_DATE, DateUtils.now().getTime()); if (null != entity) { setEntity(entity); } if (null != data) { setData(data); } } public String getId() { return getId(this); } public long getCreationDateTime() { return getCreationDateTime(this); } public String getType() { return getType(this); } public Activity setType(final String value) { setType(this, value); return this; } public String getCollection() { return getEntityCollection(this); } public Activity setCollection(final String value) { setEntityCollection(this, value); return this; } public DBObject getEntity() { return getEntity(this); } public Activity setEntity(final DBObject value) { setEntity(this, value); return this; } public DBObject getData() { return getData(this); } public Activity setData(final DBObject value) { setData(this, value); return this; } public Activity putData(final String key, final Object value) { final DBObject data = getData(); data.put(key, value); setData(data); return this; } public Object getData(final String key) { final DBObject data = getData(); if (null != data && data.containsField(key)) { return data.get(key); } return null; } public String getDataAsString(final String key) { final DBObject data = getData(); return MongoUtils.getString(data, key); } public int getDataAsInt(final String key) { final DBObject data = getData(); return MongoUtils.getInt(data, key); } public long getDataAsLong(final String key) { final DBObject data = getData(); return MongoUtils.getLong(data, key); } public boolean getDataAsBoolean(final String key) { final DBObject data = getData(); return MongoUtils.getBoolean(data, key); } public DBObject getDataAsDBObject(final String key) { final DBObject data = getData(); return MongoUtils.getDBObject(data, key); } public List getDataAsList(final String key) { final DBObject data = getData(); return MongoUtils.getList(data, key); } // ------------------------------------------------------------------------ // F A C T O R Y // ------------------------------------------------------------------------ public static Activity newInstance() { return new Activity(); } // ------------------------------------------------------------------------ // S T A T I C // ------------------------------------------------------------------------ public static String getId(final DBObject item) { return MongoUtils.getString(item, ID); } public static void setId(final DBObject item, final String value) { MongoUtils.put(item, ID, value); } public static String getEntityCollection(final DBObject item) { return MongoUtils.getString(item, ENTITY_COLLECTION); } public static void setEntityCollection(final DBObject item, final String value) { MongoUtils.put(item, ENTITY_COLLECTION, value); } public static long getCreationDateTime(final DBObject item) { return MongoUtils.getLong(item, CREATION_DATE); } public static String getType(final DBObject item) { return MongoUtils.getString(item, TYPE); } public static void setType(final DBObject item, final String value) { MongoUtils.put(item, TYPE, value); } public static String getEntityIdPrefix(final DBObject item) { final DBObject entity = getEntity(item); if (null != entity) { final Object id = MongoUtils.getId(entity); if (null != id) { return StringUtils.splitFirst(id.toString(), "_")[0]; } } return ""; } public static DBObject getEntity(final DBObject item) { return MongoUtils.getDBObject(item, ENTITY); } public static void setEntity(final DBObject item, final DBObject value) { MongoUtils.put(item, ENTITY, value); } public static DBObject getData(final DBObject item) { return MongoUtils.getDBObject(item, DATA); } public static void setData(final DBObject item, final DBObject value) { MongoUtils.put(item, DATA, value); } }