org.opentestsystem.delivery.testadmin.scheduling.DbDumpImporter.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testadmin.scheduling.DbDumpImporter.java

Source

/*
Educational Online Test Delivery System Copyright (c) 2013 American Institutes for Research
    
Distributed under the AIR Open Source License, Version 1.0 See accompanying file AIR-License-1_0.txt or at
http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 */

package org.opentestsystem.delivery.testadmin.scheduling;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.bson.BSON;
import org.springframework.data.mongodb.core.MongoTemplate;

import com.google.common.collect.Lists;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class DbDumpImporter {

    private static final List<String> DB_DUMP_COLLECTIONS = Lists.newArrayList("accessibilityEquipment",
            "assessment", "cacheMap", "clientEntity", "districtEntity", "eligibleStudent", "facility",
            "facilityAvailability", "institutionEntity", "proctor", "proctorRole", "stateEntity", "student",
            "subject", "testPlatform", "user");

    private static final int LENGTH_BYTES = 4;

    public static void loadDefaultBsonDumps(final MongoTemplate mongoTemplate) {

        InputStream is = null;
        byte[] buf = new byte[32768];
        byte[] lengthBuf = new byte[4];
        int len;
        int readLen = 0;

        for (String collection : DB_DUMP_COLLECTIONS) {

            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            is = DbDumpImporter.class.getResourceAsStream("/db-dump/" + collection + ".bson");

            List<DBObject> bsonList = new ArrayList<DBObject>();

            try {

                while ((len = is.read(lengthBuf, 0, LENGTH_BYTES)) > 0) {
                    ArrayUtils.reverse(lengthBuf);

                    readLen = new BigInteger(lengthBuf).intValue();

                    ArrayUtils.reverse(lengthBuf);

                    System.arraycopy(lengthBuf, 0, buf, 0, 4);

                    len = is.read(buf, 4, readLen - LENGTH_BYTES);

                    if (len > 0) {
                        bsonList.add(new BasicDBObject(BSON.decode(buf).toMap()));
                    }

                    Arrays.fill(buf, (byte) 0);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            DBCollection dbCollection = mongoTemplate.getCollection(collection);
            dbCollection.insert(bsonList);

        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}