/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.usergrid.tools;


import org.apache.usergrid.management.UserInfo;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import static org.apache.usergrid.utils.JsonUtils.mapToFormattedJsonString;


/** @author zznate */
public class UserManager extends ToolBase {

    @Override
    public Options createOptions() {
        Options options = super.createOptions();
        options.addOption( "u", "username", true, "The username to lookup" );
        options.addOption( "p", "password", true, "The password to set for the user" );
        return options;
    }


    @Override
    public void runTool( CommandLine line ) throws Exception {
        startSpring();
        String userName = line.getOptionValue( "u" );

        UserInfo userInfo = managementService.findAdminUser( userName );
        if ( userInfo == null ) {
            logger.info( "user {} not found", userName );
            return;
        }

        logger.info( mapToFormattedJsonString( userInfo ) );


        if ( line.hasOption( "p" ) ) {
            String password = line.getOptionValue( "p" );
            managementService.setAdminUserPassword( userInfo.getUuid(), password );
            logger.info( "new password match?: " + managementService
                    .verifyAdminUserPassword( userInfo.getUuid(), password ) );
        }
    }
}

@@ -833,16 +833,56 @@
 ools;%0A%0A%0A
+import com.google.common.collect.BiMap;%0A
 import o
@@ -921,16 +921,16 @@
 erInfo;%0A
-
 %0Aimport 
@@ -1003,16 +1003,285 @@
 Options;
+%0Aimport org.apache.usergrid.persistence.EntityManager;%0Aimport org.apache.usergrid.persistence.cassandra.CassandraService;%0Aimport org.apache.usergrid.persistence.entities.User;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.UUID;
 %0A%0Aimport
@@ -2068,16 +2068,62 @@
      %7D%0A%0A
+        logger.info(%22--- User information:%22);%0A
         
@@ -2176,16 +2176,723 @@
 o ) );%0A%0A
+        logger.info(%22--- User organizations:%22);%0A        final BiMap%3CUUID, String%3E orgs = managementService.getOrganizationsForAdminUser( userInfo.getUuid() );%0A        logger.info( mapToFormattedJsonString( orgs ) );%0A%0A        logger.info(%22--- User dictionaries:%22);%0A        EntityManager em = emf.getEntityManager( CassandraService.MANAGEMENT_APPLICATION_ID );%0A        User user = em.get( userInfo.getUuid(), User.class );%0A        Set%3CString%3E dictionaries = em.getDictionaries( user );%0A        for (String dictionary : dictionaries) %7B%0A            Map%3CObject, Object%3E dict = em.getDictionaryAsMap( user, dictionary );%0A            logger.info( dictionary + %22 : %22 + mapToFormattedJsonString( dict ) );%0A        %7D%0A
 %0A       

Print out additional user information: orgs and dictionaries.