// CloudCoder - a web-based pedagogical programming environment
// Copyright (C) 2011-2015, Jaime Spacco <jspacco@knox.edu>
// Copyright (C) 2011-2015, David H. Hovemeyer <david.hovemeyer@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package org.cloudcoder.app.server.persist.txn;

import java.sql.Connection;
import java.sql.SQLException;

import org.cloudcoder.app.server.persist.util.AbstractDatabaseRunnableNoAuthException;
import org.cloudcoder.app.server.persist.util.DBUtil;
import org.cloudcoder.app.shared.model.CourseRegistration;
import org.cloudcoder.app.shared.model.CourseRegistrationSpec;
import org.cloudcoder.app.shared.model.OperationResult;
import org.cloudcoder.app.shared.model.User;

public class RegisterExistingUser extends AbstractDatabaseRunnableNoAuthException<OperationResult> {

	private CourseRegistrationSpec spec;

	public RegisterExistingUser(CourseRegistrationSpec spec) {
		this.spec = spec;
	}

	@Override
	public String getDescription() {
		return " register a single existing user in a course";
	}

	@Override
	public OperationResult run(Connection conn) throws SQLException {
		// First, find the user
		User user = Queries.getUser(conn, spec.getUsername(), this);
		if (user == null) {
			return new OperationResult(false, "Unknown user " + spec.getUsername());
		}
		
		// Create the CourseRegistration
		CourseRegistration reg = new CourseRegistration();
		reg.setUserId(user.getId());
		reg.setCourseId(spec.getCourseId());
		reg.setRegistrationType(spec.getRegistrationType());
		reg.setSection(spec.getSection());
		
		// Insert the CourseRegistration
		DBUtil.storeModelObject(conn, reg);
		
		return new OperationResult(true, "Added user " + spec.getUsername() + " to course");
	}
}

@@ -966,24 +966,90 @@
 Exception;%0A%0A
+import org.cloudcoder.app.server.persist.NoSuchUniqueIdException;%0A
 import org.c
@@ -1115,32 +1115,32 @@
 oAuthException;%0A
-
 import org.cloud
@@ -1177,16 +1177,63 @@
 DBUtil;%0A
+import org.cloudcoder.app.shared.model.Course;%0A
 import o
@@ -1446,16 +1446,73 @@
 el.User;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -1606,17 +1606,109 @@
 sult%3E %7B%0A
+%09private static final Logger logger = LoggerFactory.getLogger(RegisterExistingUser.class);%0A%09
 %0A
-
 %09private
@@ -2529,16 +2529,249 @@
 eg);%0A%09%09%0A
+%09%09Course course = null;%0A%09%09try %7B%0A%09%09%09course = DBUtil.loadModelObjectForId(conn, Course.SCHEMA, spec.getCourseId());%0A%09%09%7D catch (NoSuchUniqueIdException e) %7B%0A%09%09%09logger.error(%22Could not find course for id=%7B%7D%22, spec.getCourseId());%0A%09%09%7D%0A%09%09%0A
 %09%09return
@@ -2846,16 +2846,77 @@
  course%22
+ +%0A%09%09%09%09(course != null ? %22 %22 + course.getNameAndTitle() : %22%22)
 );%0A%09%7D%0A%7D%0A

include name of course in success message