/**
 * Copyright (c) 2012-2013 Axelor. All Rights Reserved.
 *
 * The contents of this file are subject to the Common Public
 * Attribution License Version 1.0 (the “License”); you may not use
 * this file except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://license.axelor.com/.
 *
 * The License is based on the Mozilla Public License Version 1.1 but
 * Sections 14 and 15 have been added to cover use of software over a
 * computer network and provide for limited attribution for the
 * Original Developer. In addition, Exhibit A has been modified to be
 * consistent with Exhibit B.
 *
 * Software distributed under the License is distributed on an “AS IS”
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is part of "Axelor Business Suite", developed by
 * Axelor exclusively.
 *
 * The Original Developer is the Initial Developer. The Initial Developer of
 * the Original Code is Axelor.
 *
 * All portions of the code written by Axelor are
 * Copyright (c) 2012-2013 Axelor. All Rights Reserved.
 */
package com.axelor.web;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.axelor.meta.MetaLoader;

@Singleton
public class InitServlet extends HttpServlet {
	
	private static final long serialVersionUID = -2493577642638670615L;
	
	private static final Logger LOG = LoggerFactory.getLogger(InitServlet.class);
	
	@Inject
	private AppSettings settings;
	
	@Inject
	private MetaLoader metaLoader;

	@Override
	public void init() throws ServletException {
		LOG.info("Initializing...");
		try {
			String output = settings.get("temp.dir");
			metaLoader.load(output);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.init();
	}
}

@@ -1406,16 +1406,143 @@
 ctory;%0A%0A
+import com.axelor.auth.AuthService;%0Aimport com.axelor.auth.db.Group;%0Aimport com.axelor.auth.db.User;%0Aimport com.axelor.db.JPA;%0A
 import c
@@ -1568,16 +1568,16 @@
 Loader;%0A
-
 %0A@Single
@@ -1627,18 +1627,17 @@
 rvlet %7B%0A
-%09
 %0A
+
 %09private
@@ -1697,18 +1697,17 @@
 70615L;%0A
-%09
 %0A
+
 %09private
@@ -1777,18 +1777,17 @@
 class);%0A
-%09
 %0A
+
 %09@Inject
@@ -1822,10 +1822,9 @@
 gs;%0A
-%09
 %0A
+
 %09@In
@@ -1861,16 +1861,60 @@
 oader;%0A%0A
+%09@Inject%0A%09private AuthService authService;%0A%0A
 %09@Overri
@@ -1993,16 +1993,132 @@
 g...%22);%0A
+%0A%09%09JPA.runInTransaction(new Runnable() %7B%0A%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09createInitialUsers();%0A%09%09%09%7D%0A%09%09%7D);%0A%0A
 %09%09try %7B%0A
@@ -2244,16 +2244,16 @@
 ();%0A%09%09%7D%0A
-
 %09%09super.
@@ -2262,11 +2262,611 @@
 it();%0A%09%7D
+%0A%0A%09private void createInitialUsers() %7B%0A%0A%09%09if (User.all().count() %3E 0) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09Group g1 = Group.findByCode(%22admins%22);%0A%09%09Group g2 = Group.findByCode(%22users%22);%0A%0A%09%09if (g1 == null) %7B%0A%09%09%09g1 = new Group(%22admins%22, %22Administrators%22);%0A%09%09%09g1.save();%0A%09%09%7D%0A%0A%09%09if (g2 == null) %7B%0A%09%09%09g2 = new Group(%22users%22, %22Users%22);%0A%09%09%09g2.save();%0A%09%09%7D%0A%0A%09%09User u1 = new User(%22admin%22, %22Administrator%22);%0A%09%09User u2 = new User(%22demo%22, %22Demo User%22);%0A%0A%09%09u1.setGroup(g1);%0A%09%09u2.setGroup(g2);%0A%0A%09%09u1.setPassword(%22admin%22);%0A%09%09u2.setPassword(%22demo%22);%0A%0A%09%09authService.encrypt(u1);%0A%09%09authService.encrypt(u2);%0A%0A%09%09u1.save();%0A%09%09u2.save();%0A%09%7D
 %0A%7D%0A

create default users (RM-1645)