/*
 * Copyright 2005 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.guvnor.server.repository;


import org.drools.repository.*;
import org.drools.repository.events.CheckinEvent;
import org.drools.repository.events.StorageEventManager;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.jcr.LoginException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletContextEvent;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


/**
 * This startup class manages the JCR repository, sets it up if necessary.
 */
@ApplicationScoped
public class RepositoryStartupService {

    protected transient final Logger log = LoggerFactory.getLogger(getClass());

    @Inject
    protected GuvnorBootstrapConfiguration guvnorBootstrapConfiguration;

    private RulesRepositoryConfigurator configurator;

    protected Repository repository;
    protected Session sessionForSetup;

    public Repository getRepositoryInstance() {
        try {
            // Convert Map to Properties object
            Properties properties = new Properties();
            properties.putAll(guvnorBootstrapConfiguration.getProperties());
            configurator = RulesRepositoryConfigurator.getInstance(properties);
            repository = configurator.getJCRRepository();
        } catch (RepositoryException e) {
            throw new RulesRepositoryException(e);
        }
        return repository;
    }

    @PostConstruct
    public void create() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        repository = getRepositoryInstance();
        String adminUsername = guvnorBootstrapConfiguration.extractAdminUsername();
        String adminPassword = guvnorBootstrapConfiguration.extractAdminPassword();
        sessionForSetup = newSession(adminUsername, adminPassword);
        create(sessionForSetup);
    }

    void create(Session sessionForSetup) {

        RulesRepositoryAdministrator admin = new RulesRepositoryAdministrator(sessionForSetup);
        if (!admin.isRepositoryInitialized()) {
            try {
                configurator.setupRepository(sessionForSetup);
            } catch (RepositoryException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        //
        //Migrate v4 ruleflows to v5
        //This section checks if the repository contains drools v4
        //ruleflows that need to be migrated to drools v5
        //
        RulesRepository repo = new RulesRepository(sessionForSetup);
        try {
            if (MigrateRepository.needsRuleflowMigration(repo)) {
                MigrateRepository.migrateRuleflows(repo);
            }
        } catch (RepositoryException e) {
            e.printStackTrace();
            throw new RulesRepositoryException(e);
        }
    }

    @PreDestroy
    public void close() {
        sessionForSetup.logout();
        log.info( "Shutting down repository..." );
        configurator.shutdown();
    }

    /**
     * This will create a new Session, based on the current user.
     *
     * @return
     */
    public Session newSession(String userName) {
        try {
            return configurator.login(userName);
        } catch (LoginException e) {
            throw new RulesRepositoryException("Unable to login to JCR backend.", e);
        } catch (RepositoryException e) {
            throw new RulesRepositoryException(e);
        }
    }

    /**
     * This will create a new Session, based on the current user.
     *
     * @return
     */
    public Session newSession(String userName, String password) {
        try {
            return configurator.login(userName, password);
        } catch (LoginException e) {
            throw new RulesRepositoryException("UserName: [ " + userName + "] Unable to login to JCR backend.", e);
        } catch (RepositoryException e) {
            throw new RulesRepositoryException(e);
        }
    }

}

@@ -671,115 +671,9 @@
 .*;%0A
-import org.drools.repository.events.CheckinEvent;%0Aimport org.drools.repository.events.StorageEventManager;
 %0A
+
 impo
@@ -827,35 +827,9 @@
 ct;%0A
-import javax.inject.Named;
 %0A
+
 impo
@@ -886,76 +886,8 @@
 y;%0A%0A
-import javax.crypto.Cipher;%0Aimport javax.crypto.spec.SecretKeySpec;%0A
 impo
@@ -974,32 +974,32 @@
 itoryException;%0A
+
 import javax.jcr
@@ -1012,127 +1012,8 @@
 on;%0A
-import javax.servlet.ServletContextEvent;%0Aimport java.math.BigInteger;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A
 impo
@@ -2411,22 +2411,31 @@
         
-create
+setupRepository
 (session
@@ -2456,27 +2456,44 @@
  %7D%0A%0A    
-void create
+private void setupRepository
 (Session
@@ -2512,17 +2512,16 @@
 etup) %7B%0A
-%0A
         
@@ -2803,77 +2803,44 @@
     
-// TODO Auto-generated catch block%0A                e.printStackTrace(
+throw new RulesRepositoryException(e
 );%0A 

make RepositoryStartupService more readable