org.netbeans.modules.mongodb.native_tools.MongoRestoreExecAction.java Source code

Java tutorial

Introduction

Here is the source code for org.netbeans.modules.mongodb.native_tools.MongoRestoreExecAction.java

Source

/* 
 * Copyright (C) 2015 Yann D'Isanto
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.netbeans.modules.mongodb.native_tools;

import com.mongodb.MongoClientURI;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.netbeans.api.extexecution.ExecutionDescriptor;
import org.netbeans.modules.mongodb.CollectionInfo;
import org.netbeans.modules.mongodb.api.connections.ConnectionInfo;
import org.netbeans.modules.mongodb.DbInfo;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

/**
 *
 * @author Yann D'Isanto
 */
@NbBundle.Messages({ "ACTION_MongoRestore=mongorestore" })
public final class MongoRestoreExecAction extends NativeToolExecAction {

    public MongoRestoreExecAction(Lookup lookup) {
        super(Bundle.ACTION_MongoRestore(), lookup, MongoNativeTool.MONGO_RESTORE);
    }

    @Override
    protected ExecutionDescriptor getExecutionDescriptor() {
        return super.getExecutionDescriptor().frontWindow(true);
    }

    @Override
    protected Map<String, String> getOptionsFromContext() {
        final Map<String, String> options = new HashMap<>();
        final ConnectionInfo connectionInfo = getLookup().lookup(ConnectionInfo.class);
        if (connectionInfo != null) {
            final MongoClientURI uri = connectionInfo.getMongoURI();
            parseOptionsFromURI(uri, options);
        }
        final DbInfo dbInfo = getLookup().lookup(DbInfo.class);
        if (dbInfo != null) {
            options.put(MongoRestoreOptions.DB, dbInfo.getDbName());
        }
        final CollectionInfo collection = getLookup().lookup(CollectionInfo.class);
        if (collection != null) {
            options.put(MongoRestoreOptions.COLLECTION, collection.getName());
        }
        return options;
    }

    private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) {
        if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) {
            options.put(MongoRestoreOptions.USERNAME, uri.getUsername());
        }
        if (uri.getPassword() != null && uri.getPassword().length > 0) {
            options.put(MongoRestoreOptions.PASSWORD, new String(uri.getPassword()));
        }
        if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) {
            final String hostWithPort = uri.getHosts().get(0);
            final Pattern p = Pattern.compile("(.*)(:(\\d+))?");
            final Matcher m = p.matcher(hostWithPort);
            if (m.matches()) {
                final String host = m.group(1);
                final String port = m.group(3);
                if (host.isEmpty() == false) {
                    options.put(MongoRestoreOptions.HOST, host);
                    if (port != null) {
                        options.put(MongoRestoreOptions.PORT, port);
                    }
                }
            }
        }
        if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) {
            options.put(MongoRestoreOptions.DB, uri.getDatabase());
        }
        if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) {
            options.put(MongoRestoreOptions.COLLECTION, uri.getCollection());
        }
    }
}