package uk.ac.edukapp.servlets;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.edukapp.model.Accountinfo;
import uk.ac.edukapp.model.Useraccount;
import uk.ac.edukapp.util.MD5Util;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private Log log;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public RegisterServlet() {
    super();
    log = LogFactory.getLog(RegisterServlet.class);
    // TODO Auto-generated constructor stub
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // get parameters
    String username = null;
    String email = null;
    String password = null;
    String realname = null;

    username = request.getParameter("username");
    email = request.getParameter("email");
    password = request.getParameter("password");
    realname = request.getParameter("name");

    // TO-DO add salt
    // need to store it in db -> need to alter table/entity
    // String salt = null;

    EntityManagerFactory factory = Persistence
        .createEntityManagerFactory("edukapp");
    EntityManager em = factory.createEntityManager();

    /*-----------*/
    em.getTransaction().begin();

    Useraccount ua = new Useraccount();
    ua.setUsername(username);
    ua.setEmail(email);

    UUID token = UUID.randomUUID();
    String salt = token.toString();
    String hashedPassword = MD5Util.md5Hex(salt + password);
    ua.setPassword(hashedPassword);
    ua.setSalt(salt);
    em.persist(ua);

    try {
      log.info("ua id:"+ua.getId());
      Accountinfo ai = new Accountinfo();
      ai.setId(ua.getId());
      ai.setRealname(realname); 
      java.util.Date date = new java.util.Date();
      Timestamp now = new Timestamp(date.getTime());
      ai.setJoined(now);
      em.persist(ai);
    } catch (Exception e) {
      log.info("got an exception");      
      e.printStackTrace();
    }

    em.getTransaction().commit();
    /*----------*/

    em.close();
    factory.close();

    doForward(request, response, "/index.jsp");

  }

  private void doForward(HttpServletRequest request,
      HttpServletResponse response, String jsp) throws ServletException,
      IOException {
    RequestDispatcher dispatcher = getServletContext()
        .getRequestDispatcher(jsp);
    dispatcher.forward(request, response);
  }

}

@@ -587,16 +587,57 @@
 tinfo;%0D%0A
+import uk.ac.edukapp.model.LtiProvider;%0D%0A
 import u
@@ -2967,24 +2967,176 @@
 e();%0D%0A    %7D%0D
+%0A    %0D%0A    //%0D%0A    // Create an LTI Provider for this account%0D%0A    //%0D%0A    LtiProvider ltiProvider = new LtiProvider(ua);%0D%0A    em.persist(ltiProvider);%0D
 %0A%0D%0A    em.ge

Create an LTIProvider when registering a new user