<commit_before>/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Arbalo AG
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.opentdc.events.mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.opentdc.mongo.AbstractMongodbServiceProvider;
import org.opentdc.events.EventModel;
import org.opentdc.events.InvitationState;
import org.opentdc.events.SalutationType;
import org.opentdc.events.ServiceProvider;
import org.opentdc.service.exception.DuplicateException;
import org.opentdc.service.exception.InternalServerErrorException;
import org.opentdc.service.exception.NotFoundException;
import org.opentdc.service.exception.ValidationException;
// import org.opentdc.util.EmailSender;
// import org.opentdc.util.FreeMarkerConfig;
import org.opentdc.util.PrettyPrinter;

// import freemarker.template.Template;

/**
 * A MongoDB-based implementation of the Events service.
 * @author <NAME>
 *
 */
public class MongodbServiceProvider 
	extends AbstractMongodbServiceProvider<EventModel> 
	implements ServiceProvider 
{
	private static final Logger logger = Logger.getLogger(MongodbServiceProvider.class.getName());
	/*
	private EmailSender emailSender = null;
	private static final String SUBJECT = "Einladung zum Arbalo Launch Event";
	*/

	/**
	 * Constructor.
	 * @param context the servlet context.
	 * @param prefix the simple class name of the service provider; this is also used as the collection name.
	 */
	public MongodbServiceProvider(
		ServletContext context, 
		String prefix) 
	{
		super(context);
		connect();
		collectionName = prefix;
		getCollection(collectionName);
		// new FreeMarkerConfig(context);
		// emailSender = new EmailSender(context);
		logger.info("MongodbServiceProvider(context, " + prefix + ") -> OK");
	}
	
	private Document convert(EventModel eventModel, boolean withId) 
	{
		Document _doc = new Document("firstName", eventModel.getFirstName())
			.append("lastName", eventModel.getLastName())
			.append("email", eventModel.getEmail())
			.append("comment", eventModel.getComment())
			.append("contact",  eventModel.getContact())
			.append("salutation", eventModel.getSalutation().toString())
			.append("invitationState", eventModel.getInvitationState().toString())
			.append("createdAt", eventModel.getCreatedAt())
			.append("createdBy", eventModel.getCreatedBy())
			.append("modifiedAt", eventModel.getModifiedAt())
			.append("modifiedBy", eventModel.getModifiedBy());
		if (withId == true) {
			_doc.append("_id", new ObjectId(eventModel.getId()));
		}
		return _doc;
	}
	
	private EventModel convert(Document doc)
	{
		if (doc == null) {
			return null;
		}
		EventModel _model = new EventModel();
		_model.setId(doc.getObjectId("_id").toString());
		_model.setFirstName(doc.getString("firstName"));
		_model.setLastName(doc.getString("lastName"));
		_model.setEmail(doc.getString("email"));
		_model.setComment(doc.getString("comment"));
		_model.setContact(doc.getString("contact"));
		_model.setSalutation(SalutationType.valueOf(doc.getString("salutation")));
		_model.setInvitationState(InvitationState.valueOf(doc.getString("invitationState")));
		_model.setCreatedAt(doc.getDate("createdAt"));
		_model.setCreatedBy(doc.getString("createdBy"));
		_model.setModifiedAt(doc.getDate("modifiedAt"));
		_model.setModifiedBy(doc.getString("modifiedBy"));
		return _model;
	}

	/* (non-Javadoc)
	 * @see org.opentdc.events.ServiceProvider#list(java.lang.String, java.lang.String, long, long)
	 */
	@Override
	public ArrayList<EventModel> list(
		String queryType,
		String query,
		int position,
		int size) {
		logger.warning("EventsService.list() should not be called; returning empty list");
		return new ArrayList<EventModel>();
		/*<commit_msg>consistent ordering of query and queryType<commit_after>/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Arbalo AG
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.opentdc.events.mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.opentdc.mongo.AbstractMongodbServiceProvider;
import org.opentdc.events.EventModel;
import org.opentdc.events.InvitationState;
import org.opentdc.events.SalutationType;
import org.opentdc.events.ServiceProvider;
import org.opentdc.service.exception.DuplicateException;
import org.opentdc.service.exception.InternalServerErrorException;
import org.opentdc.service.exception.NotFoundException;
import org.opentdc.service.exception.ValidationException;
// import org.opentdc.util.EmailSender;
// import org.opentdc.util.FreeMarkerConfig;
import org.opentdc.util.PrettyPrinter;

// import freemarker.template.Template;

/**
 * A MongoDB-based implementation of the Events service.
 * @author <NAME>
 *
 */
public class MongodbServiceProvider 
	extends AbstractMongodbServiceProvider<EventModel> 
	implements ServiceProvider 
{
	private static final Logger logger = Logger.getLogger(MongodbServiceProvider.class.getName());
	/*
	private EmailSender emailSender = null;
	private static final String SUBJECT = "Einladung zum Arbalo Launch Event";
	*/

	/**
	 * Constructor.
	 * @param context the servlet context.
	 * @param prefix the simple class name of the service provider; this is also used as the collection name.
	 */
	public MongodbServiceProvider(
		ServletContext context, 
		String prefix) 
	{
		super(context);
		connect();
		collectionName = prefix;
		getCollection(collectionName);
		// new FreeMarkerConfig(context);
		// emailSender = new EmailSender(context);
		logger.info("MongodbServiceProvider(context, " + prefix + ") -> OK");
	}
	
	private Document convert(EventModel eventModel, boolean withId) 
	{
		Document _doc = new Document("firstName", eventModel.getFirstName())
			.append("lastName", eventModel.getLastName())
			.append("email", eventModel.getEmail())
			.append("comment", eventModel.getComment())
			.append("contact",  eventModel.getContact())
			.append("salutation", eventModel.getSalutation().toString())
			.append("invitationState", eventModel.getInvitationState().toString())
			.append("createdAt", eventModel.getCreatedAt())
			.append("createdBy", eventModel.getCreatedBy())
			.append("modifiedAt", eventModel.getModifiedAt())
			.append("modifiedBy", eventModel.getModifiedBy());
		if (withId == true) {
			_doc.append("_id", new ObjectId(eventModel.getId()));
		}
		return _doc;
	}
	
	private EventModel convert(Document doc)
	{
		if (doc == null) {
			return null;
		}
		EventModel _model = new EventModel();
		_model.setId(doc.getObjectId("_id").toString());
		_model.setFirstName(doc.getString("firstName"));
		_model.setLastName(doc.getString("lastName"));
		_model.setEmail(doc.getString("email"));
		_model.setComment(doc.getString("comment"));
		_model.setContact(doc.getString("contact"));
		_model.setSalutation(SalutationType.valueOf(doc.getString("salutation")));
		_model.setInvitationState(InvitationState.valueOf(doc.getString("invitationState")));
		_model.setCreatedAt(doc.getDate("createdAt"));
		_model.setCreatedBy(doc.getString("createdBy"));
		_model.setModifiedAt(doc.getDate("modifiedAt"));
		_model.setModifiedBy(doc.getString("modifiedBy"));
		return _model;
	}

	/* (non-Javadoc)
	 * @see org.opentdc.events.ServiceProvider#list(java.lang.String, java.lang.String, long, long)
	 */
	@Override
	public ArrayList<EventModel> list(
		String query,
		String queryType,
		int position,
		int size) {
		logger.warning("EventsService.list() should not be called; returning empty list");
		return new ArrayList<EventModel>();
		/*
{'fraction_non_alphanumeric': 0.11255936142265333, 'fraction_numerical': 0.0008083257552793776, 'mean_word_length': 5.170822942643391, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3018363', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2800, 'n_words': 781}