All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ebay.jetstream.configurationmanagement.MongoLogDAO Maven / Gradle / Ivy

/*******************************************************************************
 *  Copyright © 2012-2015 eBay Software Foundation
 *  This program is dual licensed under the MIT and Apache 2.0 licenses.
 *  Please see LICENSE for more information.
 *******************************************************************************/
package com.ebay.jetstream.configurationmanagement;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;

import com.ebay.jetstream.config.mongo.MongoConfigRuntimeException;
import com.ebay.jetstream.configurationmanagement.model.JetStreamBeanConfigurationLogDo;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

public class MongoLogDAO {

	public static List findConfigurationByAppNameAndVersion(
			BasicDBObject query, MongoLogConnection mongoConnection) {

		List beanConfigs = new ArrayList();
		List dbObjects = new ArrayList();
		DBCollection dbCol = mongoConnection.getDBCollection();

		if (dbCol == null) {
			throw new MongoConfigRuntimeException(
					"jetstreamconfiglog collection is unknown");
		}

		Exception e = null;
		DBCursor cur = null;
		try {
			cur = (query == null ? dbCol.find() : dbCol.find(query));
			while (cur.hasNext()) {
				dbObjects.add((BasicDBObject) cur.next());
			}

			for (BasicDBObject dbObject : dbObjects) {
				String jsonString = dbObject.toString();
				beanConfigs.add(unMarshalJSONResponse(jsonString));
			}
		} catch (Exception err) {
			e = err;
			throw new MongoConfigRuntimeException(err);
		} finally {
			if (cur != null) {
				cur.close();
			}
		}

		return beanConfigs;
	}

	public static List findConfigurationByQuery(
			BasicDBObject query, MongoLogConnection mongoLogConnection) {

		List beanConfigs = new ArrayList();
		List dbObjects = new ArrayList();
		DBCollection dbCol = mongoLogConnection.getDBCollection();

		if (dbCol == null) {
			throw new MongoConfigRuntimeException(
					"jetstreamconfigLog collection is unknown");
		}

		Exception e = null;

		DBCursor cur = null;
		try {
			cur = (query == null ? dbCol.find() : dbCol.find(query));
			while (cur.hasNext()) {
				dbObjects.add((BasicDBObject) cur.next());
			}

			for (BasicDBObject dbObject : dbObjects) {
				String jsonString = dbObject.toString();
				beanConfigs.add(unMarshalJSONResponse(jsonString));
				// beanConfig =
				// (JetStreamBeanConfigurationDo)fromJson(jsonString,
				// JetStreamBeanConfigurationDo.class);
			}
		} catch (Exception err) {
			e = err;
			throw new MongoConfigRuntimeException(err);
		} finally {
			if (cur != null) {
				cur.close();
			}
		}

		return beanConfigs;
	}

	private static JetStreamBeanConfigurationLogDo unMarshalJSONResponse(
			String jsonString) {
		try {
			JetStreamBeanConfigurationLogDo result = null;
			if (jsonString != null) {
				try {
					ObjectMapper mapper = new ObjectMapper();
					mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
					result = mapper.readValue(jsonString,
							JetStreamBeanConfigurationLogDo.class);
				} catch (Exception e) {
					throw new MongoConfigRuntimeException(e);
				}
			}

			return result;

		} catch (Throwable e) {
			throw new MongoConfigRuntimeException(e);
		}
	}

	/**
	 * UPLOAD TO DB
	 */
	public static void insertJetStreamConfiguration(BasicDBObject dbObject,
			MongoLogConnection mongoLogConnection) {
		JetStreamBeanConfigurationLogDo beanConfig = null;
		DBCollection dbCol = mongoLogConnection.getDBCollection();

		if (dbCol == null) {
			throw new MongoConfigRuntimeException(
					"jetstreamconfig collection is unknown");
		}

		WriteResult result = dbCol.insert(dbObject);
		if (result.getError() != null) {
			throw new MongoConfigRuntimeException(result.getError());
		}
	}

	public static boolean removeConfigurationByQuery(BasicDBObject query,
			MongoLogConnection mongoLogConnection) {

		DBCollection dbCol = mongoLogConnection.getDBCollection();

		if (dbCol == null) {
			throw new MongoConfigRuntimeException(
					"jetstreamconfig collection is unknown");
		}

		try {
			if (query == null) {
				return false;
			}

			WriteResult result = dbCol.remove(query, WriteConcern.SAFE);

			if (result.getLastError().ok()) {
				return true;
			}

		} catch (Exception err) {
			throw new MongoConfigRuntimeException(err);
		}

		return true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy