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.entities.items.jobs; import com.eywa.impl.app.IAppConstants; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import org.ly.commons.util.StringUtils; import org.ly.packages.mongo.impl.util.MongoUtils; import java.util.Set; /** * Item for Scheduler Message System. * Contains information about Notification Message. */ public class ItemJobMessage extends BasicDBObject { private static final String DEFAULT_TEMPLATE = "default"; // ------------------------------------------------------------------------ // f i e l d s // ------------------------------------------------------------------------ public static final String MESSAGE_TEMPLATES = IAppConstants.MESSAGE_TEMPLATES; // map of key-value pairs (template_name, template_text) public static final String MESSAGE_RECIPIENTS = IAppConstants.MESSAGE_RECIPIENTS; // map of key-value pairs (user_id, template_name) // ------------------------------------------------------------------------ // t e m p l a t e f i e l d s // ------------------------------------------------------------------------ public static final String TITLE = IAppConstants.TITLE; public static final String CONTENT = IAppConstants.CONTENT; // ------------------------------------------------------------------------ // c o n s t r u c t o r // ------------------------------------------------------------------------ public ItemJobMessage() { } // ------------------------------------------------------------------------ // p r i v a t e // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // S T A T I C // ------------------------------------------------------------------------ public static DBObject getRecipients(final DBObject item) { return MongoUtils.getDBObject(item, MESSAGE_RECIPIENTS); } public static void setRecipients(final DBObject item, final DBObject value) { MongoUtils.put(item, MESSAGE_RECIPIENTS, value); } public static DBObject getRecipientsWithTemplates(final DBObject item) { final DBObject result = new BasicDBObject(); final DBObject recipients = getRecipients(item); final Set<String> users = recipients.keySet(); for (final String userId : users) { final String templateName = MongoUtils.getString(recipients, userId); final DBObject template = getTemplate(item, StringUtils.hasText(templateName) ? templateName : DEFAULT_TEMPLATE); if (null != template) { result.put(userId, template); } } return result; } public static void addRecipient(final DBObject item, final String userId) { addRecipient(item, userId, DEFAULT_TEMPLATE); } public static void addRecipient(final DBObject item, final String userId, final String templateName) { final DBObject recipients = getRecipients(item); recipients.put(userId, templateName); setRecipients(item, recipients); } public static DBObject getTemplates(final DBObject item) { return MongoUtils.getDBObject(item, MESSAGE_TEMPLATES); } public static void setTemplates(final DBObject item, final DBObject value) { MongoUtils.put(item, MESSAGE_TEMPLATES, value); } public static DBObject getTemplate(final DBObject item) { return getTemplate(item, DEFAULT_TEMPLATE); } public static DBObject getTemplate(final DBObject item, final String name) { final DBObject map = getTemplates(item); return MongoUtils.getDBObject(map, name); } public static void addTemplate(final DBObject item, final String title, final String content) { addTemplate(item, DEFAULT_TEMPLATE, title, content); } public static void addTemplate(final DBObject item, final String name, final String title, final String content) { final DBObject template = new BasicDBObject(); template.put(TITLE, title); template.put(CONTENT, content); addTemplate(item, name, template); } public static void addTemplate(final DBObject item, final String name, final DBObject template) { final DBObject map = getTemplates(item); map.put(name, template); setTemplates(item, map); } }