com.setronica.ucs.storage.MongoCategoryStorage.java Source code

Java tutorial

Introduction

Here is the source code for com.setronica.ucs.storage.MongoCategoryStorage.java

Source

/*
 * Copyright (C) 2014 SETRONICA - setronica.com
 * This source code is available under the terms of the GNU Lesser General Public License
 * as published by The Open Source Initiative (OSI), either version 3 of the License,
 * or (at your option) any later version.
 *
 * UCS 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 Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License.
 */

package com.setronica.ucs.storage;

import com.mongodb.*;
import com.setronica.ucs.category.impl.MemCategoryDAO;
import com.setronica.ucs.service.DataIntegrityException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MongoCategoryStorage extends MemCategoryStorage implements PersistentStorage {
    private String mongoDbName;
    private String mongoCollectionName;
    protected DBCollection collection;
    protected MongoClient mongoClient;

    public MongoCategoryStorage(MongoClient mongoClient, String mongoDbName, String mongoCollectionName) {
        this.mongoClient = mongoClient;
        this.mongoDbName = mongoDbName;
        this.mongoCollectionName = mongoCollectionName;
    }

    @Override
    public int createCategory(int treeId, MemCategoryDAO.MemPackedCategory category) {
        int index = super.createCategory(treeId, category);
        collection.save(writeObject(getTree(treeId), index, category));
        return index;
    }

    @Override
    public void updateCategory(int treeId, int categoryId, MemCategoryDAO.MemPackedCategory category) {
        super.updateCategory(treeId, categoryId, category);
        MemPackedTree tree = getTree(treeId);
        collection.findAndModify(writeKey(tree, categoryId), writeObject(tree, categoryId, category));
    }

    @Override
    public void removeCategory(int treeId, int categoryId) throws DataIntegrityException {
        super.removeCategory(treeId, categoryId);
        collection.remove(writeKey(getTree(treeId), categoryId));
    }

    @Override
    public void removeTree(int treeId) {
        MemPackedTree tree = getTree(treeId);
        super.removeTree(treeId);
        if (tree != null)
            collection.remove(writeKey(tree, null));
    }

    private BasicDBObject writeKey(MemPackedTree tree, Integer categoryId) {
        BasicDBObject object = new BasicDBObject();
        object.put("ownerId", tree.ownerId);
        object.put("treeId", tree.treeId);
        object.put("treeName", tree.treeName);
        if (categoryId != null)
            object.put("categoryId", categoryId);
        return object;
    }

    protected DBObject writeObject(MemPackedTree tree, int categoryId, MemCategoryDAO.MemPackedCategory category) {
        BasicDBObject object = writeKey(tree, categoryId);
        object.put("attributes", category.getAttributes());
        object.put("parentId", category.getParentId());
        if (category.getAssignedProducts() != null) {
            object.put("products", category.getAssignedProducts().toArray());
        }
        return object;
    }

    protected MemCategoryDAO.MemPackedCategory readObject(DBObject object) {
        int parentId = (Integer) object.get("parentId");

        Object attributes = object.get("attributes");
        int[] attributesTmp;
        if (attributes instanceof List) {
            attributesTmp = new int[((List) attributes).size()];
            for (int i = 0, length = attributesTmp.length; i < length; i++) {
                attributesTmp[i] = (Integer) ((List) attributes).get(i);
            }
        } else {
            attributesTmp = (int[]) attributes;
        }

        Object products = object.get("products");
        long[] assignmentsTmp;
        if (products instanceof List) {
            assignmentsTmp = new long[((List) products).size()];
            for (int i = 0, length = assignmentsTmp.length; i < length; i++) {
                assignmentsTmp[i] = (Long) ((List) products).get(i);
            }
        } else {
            assignmentsTmp = (long[]) products;
        }
        return new MemCategoryDAO.MemPackedCategory(attributesTmp, parentId, assignmentsTmp);
    }

    @Override
    public void open() throws IOException {
        DB db = mongoClient.getDB(mongoDbName);
        collection = db.getCollection(mongoCollectionName);
        if (collection == null) {
            collection = db.createCollection(mongoCollectionName, null);
            DBObject object = new BasicDBObject();
            object.put("ownerId", 1);
            object.put("treeId", 1);
            object.put("treeName", 1);
            object.put("categoryId", 1);
            collection.createIndex(object);
        }
        collection.setWriteConcern(WriteConcern.ACKNOWLEDGED);

        DBCursor cursor = collection.find();
        for (DBObject entry : cursor) {
            int treeId = (Integer) entry.get("treeId");
            MemPackedTree tree = treeStorage.get(treeId);
            if (tree == null) {
                tree = new MemPackedTree();
                tree.treeId = treeId;
                tree.ownerId = (Integer) entry.get("ownerId");
                tree.treeName = (String) entry.get("treeName");
                tree.categories = new ArrayList<MemCategoryDAO.MemPackedCategory>();
                treeStorage.put(treeId, tree);
            }

            List<MemCategoryDAO.MemPackedCategory> categories = tree.categories;
            int index = (Integer) entry.get("categoryId");
            while (index >= categories.size()) {
                categories.add(null);
            }
            categories.set(index, readObject(entry));
        }
        populateOwnerMap();
    }

    @Override
    public void close() throws IOException {
    }
}