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.session.files; import com.eywa.impl.app.IAppConstants; import com.eywa.impl.app.mongo.entities.items.ItemOptions; 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.List; /** * Item for Session DATA field. * Contains information about a file, a folder or a document split for viewer */ public class ItemSessionDataFiles extends BasicDBObject { private static final String TYPE_FILE = "data-file"; public static final String TYPE = IAppConstants.TYPE; // data type public static final String FILE_ID = IAppConstants.FILE_ID; public static final String FILE_PATH = IAppConstants.FILE_PATH; public static final String FILE_TEMP_ROOT = IAppConstants.FILE_TEMP_ROOT; public static final String FILE_TEMP_PATH = IAppConstants.FILE_TEMP_PATH; public static final String FILE_TYPE = IAppConstants.FILE_TYPE; // file or folder public static final String NAME = IAppConstants.NAME; public static final String EXTENSION = IAppConstants.EXTENSION; public static final String FORMAT = IAppConstants.FORMAT; // image, pdf. .... public static final String FILES_COUNT = IAppConstants.FILES_COUNT; //-- objects --// public static final String FILES = IAppConstants.FILES; public static final String OPTIONS = IAppConstants.OPTIONS; public ItemSessionDataFiles(final DBObject raw) { this.init(raw); } public ItemSessionDataFiles() { this.init(null); } // ------------------------------------------------------------------------ // p r i v a t e // ------------------------------------------------------------------------ private void init(final DBObject raw) { this.put(TYPE, TYPE_FILE); if (null != raw) { this.put(FILE_ID, getFileId(raw)); this.put(FILE_PATH, getFilePath(raw)); this.put(FILE_TEMP_ROOT, getFileTempRoot(raw)); this.put(FILE_TEMP_PATH, getFileTempPath(raw)); this.put(FILE_TYPE, getFileType(raw)); this.put(NAME, getName(raw)); this.put(EXTENSION, getExtension(raw)); this.put(FORMAT, getFormat(raw)); this.put(FILES, getFiles(raw)); this.put(FILES_COUNT, getFilesCount(raw)); this.put(OPTIONS, getOptions(raw)); } } public String getFileId() { return getFileId(this); } public void setFileId(final String value) { setFileId(this, value); } public String getFilePath() { return getFilePath(this); } public void setFilePath(final String value) { setFilePath(this, value); } public String getFileTempRoot() { return getFileTempRoot(this); } public void setFileTempRoot(final String value) { setFileTempRoot(this, value); } public String getFileTempPath() { return getFileTempPath(this); } public void setFileTempPath(final String value) { setFileTempPath(this, value); } public String getFileType() { return getFileType(this); } public void setFileType(final String value) { setFileType(this, value); } public String getName() { return getName(this); } public void setName(final String value) { setName(this, value); } public String getExtension() { return getExtension(this); } public void setExtension(final String value) { setExtension(this, value); } public String getFormat() { return getFormat(this); } public void setFormat(final String value) { setFormat(this, value); } public int getFilesCount() { return getFilesCount(this); } public void setFilesCount(final int value) { setFilesCount(this, value); } public List<DBObject> getFiles() { return getFiles(this); } public void setFiles(final List<DBObject> value) { setFiles(this, value); } public List<DBObject> addFile(final DBObject file) { final List<DBObject> files = getFiles(this); files.add(file); setFiles(files); return files; } public ItemOptions getOptions() { return getOptions(this); } public void setOptions(final DBObject value) { setOptions(this, value); } // ------------------------------------------------------------------------ // S T A T I C // ------------------------------------------------------------------------ public static boolean is(final DBObject item) { return is(getType(item)); } public static boolean is(final String type) { return StringUtils.hasText(type) && type.equalsIgnoreCase(TYPE_FILE); } public static String getType(final DBObject item) { return MongoUtils.getString(item, TYPE); } public static String getFileId(final DBObject item) { return MongoUtils.getString(item, FILE_ID); } public static void setFileId(final DBObject item, final String value) { MongoUtils.put(item, FILE_ID, value); } public static String getFilePath(final DBObject item) { return MongoUtils.getString(item, FILE_PATH); } public static void setFilePath(final DBObject item, final String value) { MongoUtils.put(item, FILE_PATH, value); } public static String getFileTempRoot(final DBObject item) { return MongoUtils.getString(item, FILE_TEMP_ROOT); } public static void setFileTempRoot(final DBObject item, final String value) { MongoUtils.put(item, FILE_TEMP_ROOT, value); } public static String getFileTempPath(final DBObject item) { return MongoUtils.getString(item, FILE_TEMP_PATH); } public static void setFileTempPath(final DBObject item, final String value) { MongoUtils.put(item, FILE_TEMP_PATH, value); } public static String getFileType(final DBObject item) { return MongoUtils.getString(item, FILE_TYPE); } public static void setFileType(final DBObject item, final String value) { MongoUtils.put(item, FILE_TYPE, value); } public static String getName(final DBObject item) { return MongoUtils.getString(item, NAME); } public static void setName(final DBObject item, final String value) { MongoUtils.put(item, NAME, value); } public static String getExtension(final DBObject item) { return MongoUtils.getString(item, EXTENSION); } public static void setExtension(final DBObject item, final String value) { MongoUtils.put(item, EXTENSION, value); } public static String getFormat(final DBObject item) { return MongoUtils.getString(item, FORMAT); } public static void setFormat(final DBObject item, final String value) { MongoUtils.put(item, FORMAT, value); } public static int getFilesCount(final DBObject item) { return MongoUtils.getInt(item, FILES_COUNT); } public static void setFilesCount(final DBObject item, final int value) { MongoUtils.put(item, FILES_COUNT, value); } public static List<DBObject> getFiles(final DBObject item) { return MongoUtils.getList(item, FILES); } public static void setFiles(final DBObject item, final List<DBObject> value) { MongoUtils.put(item, FILES, value); setFilesCount(item, getFiles(item).size()); } public static ItemOptions getOptions(final DBObject item) { return new ItemOptions(MongoUtils.getDBObject(item, OPTIONS)); } public static void setOptions(final DBObject item, final DBObject value) { MongoUtils.put(item, OPTIONS, value); } }