package com.izforge.izpack.util;

import com.izforge.izpack.installer.AutomatedInstallData;
import com.izforge.izpack.panels.ProcessingClient;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;

import java.util.Arrays;
import java.util.List;

public class MongoDBValidator implements com.izforge.izpack.installer.DataValidator 
{

    public Status validateData(AutomatedInstallData adata)
    {
        Status bReturn = Status.ERROR;
        try
        {
        
            String userName = adata.getVariable("mongodb.url.username");
            String passWord = adata.getVariable("mongodb.url.password");
            String hostName = adata.getVariable("mongodb.service.hostname");
            String hostPort = adata.getVariable("mongodb.service.port");
            String dbName = adata.getVariable("mongodb.service.dbname");
            
            if (dbName == null || dbName.trim().equals("")) 
            {
                dbName="test";
            }
            
            MongoClient mongoClient = new MongoClient( hostName , Integer.parseInt(hostPort) );
            
            DB db = mongoClient.getDB(dbName);

            if (userName!=null && !userName.trim().equals("")) 
            {
                if (!db.authenticate(userName, passWord.toCharArray())) 
                {
                    return Status.ERROR;
                }
            }
            db.getCollectionNames();
            
            bReturn = Status.OK; 

            // test if syracuse db already exists
            List<String> lstDb = mongoClient.getDatabaseNames();
            
            for (String dbb : lstDb)
            {
                if (dbb.equals("syracuse"))
                {
                    bReturn = Status.WARNING;
                }
            }
            

        }
        catch (Exception ex)
        {
            Debug.trace(ex.getMessage());
            bReturn = Status.ERROR; 
        }

        return bReturn;
    }

    public String getErrorMessageId()
    {
        // TODO Auto-generated method stub
        return "mongodbtesterror";
    }

    public String getWarningMessageId()
    {
        // TODO Auto-generated method stub
        return "mongodbtestwarn";
    }

    public boolean getDefaultAnswer()
    {
        // TODO Auto-generated method stub
        return false;
    }

}

@@ -1003,647 +1003,139 @@
     
-String dbName = adata.getVariable(%22mongodb.service.dbname%22);%0A            %0A            if (dbName == null %7C%7C dbName.trim().equals(%22%22)) %0A            %7B%0A                dbName=%22test%22;%0A            %7D%0A            %0A            MongoClient mongoClient = new MongoClient( hostName , Integer.parseInt(hostPort) );%0A            %0A            DB db = mongoClient.getDB(dbName);%0A%0A            if (userName!=null && !userName.trim().equals(%22%22)) %0A            %7B%0A                if (!db.authenticate(userName, passWord.toCharArray())) %0A                %7B%0A                    return Status.ERROR;%0A                %7D%0A            %7D%0A            db.getCollectionNames
+%0A            MongoClient mongoClient = new MongoClient( hostName , Integer.parseInt(hostPort) );%0A            mongoClient.getVersion
 ();%0A

fix databse checking where syracuse database always present