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;

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; 

        }
        catch (Exception ex)
        {
            Debug.trace(ex.getMessage());
        }

        return bReturn;
    }

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

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

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

}

@@ -443,16 +443,39 @@
 .Arrays;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -1642,16 +1642,29 @@
 ames();%0A
+            %0A
         
@@ -1690,16 +1690,349 @@
 s.OK; %0A%0A
+            // test if syracuse db already exists%0A            List%3CString%3E lstDb = mongoClient.getDatabaseNames();%0A            %0A            for (String dbb : lstDb)%0A            %7B%0A                if (dbb.equals(%22syracuse%22))%0A                %7B%0A                    bReturn = Status.WARNING;%0A                %7D%0A            %7D%0A            %0A%0A
         
@@ -2114,16 +2114,53 @@
 age());%0A
+            bReturn = Status.ERROR; %0A
         
@@ -2430,37 +2430,36 @@
 urn %22mongodbtest
-error
+warn
 %22;%0A    %7D%0A%0A    pu

check wether the MongoDB INstance already has a syracuse database