Search in sources :

Example 16 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class NoSQLRepositoryContextHandler method matchContextForAttribues.

@Override
protected void matchContextForAttribues(Connection conn, IConnParamName param, String noSqlVariableName) {
    NoSQLConnection noSqlConn = (NoSQLConnection) conn;
    EHadoopParamName noSqlParam = (EHadoopParamName) param;
    switch(noSqlParam) {
        case Server:
            noSqlConn.getAttributes().put(INoSQLCommonAttributes.HOST, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case ConnectionString:
            noSqlConn.getAttributes().put(IMongoDBAttributes.CONN_STRING, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Port:
            noSqlConn.getAttributes().put(INoSQLCommonAttributes.PORT, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Database:
            noSqlConn.getAttributes().put(INoSQLCommonAttributes.DATABASE, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Keyspace:
            noSqlConn.getAttributes().put(INoSQLCommonAttributes.DATABASE, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Databasepath:
            noSqlConn.getAttributes().put(INeo4jAttributes.DATABASE_PATH, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case ServerUrl:
            noSqlConn.getAttributes().put(INeo4jAttributes.SERVER_URL, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case UserName:
            noSqlConn.getAttributes().put(INoSQLCommonAttributes.USERNAME, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Password:
            noSqlConn.getAttributes().put(INoSQLCommonAttributes.PASSWORD, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Keystore:
            noSqlConn.getAttributes().put(IMongoDBAttributes.X509_CERT, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case KeystorePass:
            noSqlConn.getAttributes().put(IMongoDBAttributes.X509_CERT_KEYSTORE_PASSWORD, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Truststore:
            noSqlConn.getAttributes().put(IMongoDBAttributes.X509_CERT_AUTH, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case TruststorePass:
            noSqlConn.getAttributes().put(IMongoDBAttributes.X509_CERT_AUTH_TRUSTSTORE_PASSWORD, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case AuthenticationDatabase:
            noSqlConn.getAttributes().put(IMongoDBAttributes.AUTHENTICATION_DATABASE, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case UserPrincipal:
            noSqlConn.getAttributes().put(IMongoDBAttributes.KRB_USER_PRINCIPAL, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case Realm:
            noSqlConn.getAttributes().put(IMongoDBAttributes.KRB_REALM, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case KDCServer:
            noSqlConn.getAttributes().put(IMongoDBAttributes.KRB_KDC, ContextParameterUtils.getNewScriptCode(noSqlVariableName, LANGUAGE));
            break;
        case ReplicaSets:
            String replicaSets = noSqlConn.getAttributes().get(IMongoDBAttributes.REPLICA_SET);
            try {
                JSONArray jsa = new JSONArray(replicaSets);
                for (int i = 0; i < jsa.length(); i++) {
                    JSONObject jso = jsa.getJSONObject(i);
                    int paramNum = i + 1;
                    String hostParamName = noSqlVariableName + ExtendedNodeConnectionContextUtils.getReplicaParamName(EHadoopParamName.ReplicaHost, paramNum);
                    String portParamName = noSqlVariableName + ExtendedNodeConnectionContextUtils.getReplicaParamName(EHadoopParamName.ReplicaPort, paramNum);
                    jso.put(IMongoConstants.REPLICA_HOST_KEY, ContextParameterUtils.getNewScriptCode(hostParamName, LANGUAGE));
                    jso.put(IMongoConstants.REPLICA_PORT_KEY, ContextParameterUtils.getNewScriptCode(portParamName, LANGUAGE));
                }
                noSqlConn.getAttributes().put(IMongoDBAttributes.REPLICA_SET, jsa.toString());
            } catch (JSONException e) {
                ExceptionHandler.process(e);
            }
            break;
        default:
    }
}
Also used : JSONObject(org.talend.utils.json.JSONObject) EHadoopParamName(org.talend.metadata.managment.ui.utils.ExtendedNodeConnectionContextUtils.EHadoopParamName) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) NoSQLConnection(org.talend.repository.model.nosql.NoSQLConnection)

Example 17 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class NoSQLRepositoryContextHandler method setPropertiesForExistContextMode.

@Override
public void setPropertiesForExistContextMode(Connection connection, Set<IConnParamName> paramSet, Map<ContextItem, List<ConectionAdaptContextVariableModel>> adaptMap) {
    if (connection == null) {
        return;
    }
    if (connection instanceof NoSQLConnection) {
        NoSQLConnection noSqlConn = (NoSQLConnection) connection;
        ContextItem currentContext = null;
        for (IConnParamName param : paramSet) {
            if (param instanceof EHadoopParamName) {
                String noSqlVariableName = null;
                EHadoopParamName noSqlParam = (EHadoopParamName) param;
                if (noSqlParam == EHadoopParamName.ReplicaSets) {
                    String replicaSets = noSqlConn.getAttributes().get(IMongoDBAttributes.REPLICA_SET);
                    try {
                        JSONArray jsa = new JSONArray(replicaSets);
                        for (int i = 0; i < jsa.length(); i++) {
                            JSONObject jso = jsa.getJSONObject(i);
                            int paramNum = i + 1;
                            String hostParamName = ExtendedNodeConnectionContextUtils.getReplicaParamName(EHadoopParamName.ReplicaHost, paramNum);
                            String portParamName = ExtendedNodeConnectionContextUtils.getReplicaParamName(EHadoopParamName.ReplicaPort, paramNum);
                            String hostVariableName = null;
                            String portVariableName = null;
                            if (adaptMap != null && adaptMap.size() > 0) {
                                for (Map.Entry<ContextItem, List<ConectionAdaptContextVariableModel>> entry : adaptMap.entrySet()) {
                                    currentContext = entry.getKey();
                                    List<ConectionAdaptContextVariableModel> modelList = entry.getValue();
                                    for (ConectionAdaptContextVariableModel model : modelList) {
                                        if (model.getValue().equals(hostParamName)) {
                                            hostVariableName = model.getName();
                                        } else if (model.getValue().equals(portParamName)) {
                                            portVariableName = model.getName();
                                        }
                                        if (hostVariableName != null && portVariableName != null) {
                                            break;
                                        }
                                    }
                                }
                            }
                            if (hostVariableName != null && portVariableName != null) {
                                hostVariableName = getCorrectVariableName(currentContext, hostVariableName, hostParamName);
                                portVariableName = getCorrectVariableName(currentContext, portVariableName, portParamName);
                                jso.put(IMongoConstants.REPLICA_HOST_KEY, ContextParameterUtils.getNewScriptCode(hostVariableName, LANGUAGE));
                                jso.put(IMongoConstants.REPLICA_PORT_KEY, ContextParameterUtils.getNewScriptCode(portVariableName, LANGUAGE));
                            }
                        }
                        noSqlConn.getAttributes().put(IMongoDBAttributes.REPLICA_SET, jsa.toString());
                    } catch (JSONException e) {
                        ExceptionHandler.process(e);
                    }
                } else {
                    if (adaptMap != null && adaptMap.size() > 0) {
                        for (Map.Entry<ContextItem, List<ConectionAdaptContextVariableModel>> entry : adaptMap.entrySet()) {
                            currentContext = entry.getKey();
                            List<ConectionAdaptContextVariableModel> modelList = entry.getValue();
                            for (ConectionAdaptContextVariableModel model : modelList) {
                                if (model.getValue().equals(noSqlParam.name())) {
                                    noSqlVariableName = model.getName();
                                    break;
                                }
                            }
                        }
                    }
                    if (noSqlVariableName != null) {
                        noSqlVariableName = getCorrectVariableName(currentContext, noSqlVariableName, noSqlParam);
                        matchContextForAttribues(noSqlConn, noSqlParam, noSqlVariableName);
                    }
                }
            }
        }
    }
}
Also used : ContextItem(org.talend.core.model.properties.ContextItem) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) IConnParamName(org.talend.metadata.managment.ui.model.IConnParamName) ConectionAdaptContextVariableModel(org.talend.core.ui.context.model.table.ConectionAdaptContextVariableModel) JSONObject(org.talend.utils.json.JSONObject) EHadoopParamName(org.talend.metadata.managment.ui.utils.ExtendedNodeConnectionContextUtils.EHadoopParamName) ArrayList(java.util.ArrayList) List(java.util.List) NoSQLConnection(org.talend.repository.model.nosql.NoSQLConnection) Map(java.util.Map)

Example 18 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class NoSqlContextUpdateService method updateContextParameter.

@Override
public boolean updateContextParameter(Connection conn, String oldValue, String newValue) {
    boolean isModified = false;
    if (conn.isContextMode()) {
        if (conn instanceof NoSQLConnection) {
            NoSQLConnection connection = (NoSQLConnection) conn;
            EMap<String, String> connAttributes = (EMap<String, String>) connection.getAttributes();
            if (connAttributes == null) {
                return isModified;
            }
            for (Map.Entry<String, String> attr : connection.getAttributes()) {
                if (attr.equals(IMongoDBAttributes.REPLICA_SET)) {
                    String replicaSets = connAttributes.get(IMongoDBAttributes.REPLICA_SET);
                    try {
                        JSONArray jsa = new JSONArray(replicaSets);
                        for (int i = 0; i < jsa.length(); i++) {
                            JSONObject jso = jsa.getJSONObject(i);
                            String hostValue = jso.getString(IMongoConstants.REPLICA_HOST_KEY);
                            if (hostValue != null && hostValue.equals(oldValue)) {
                                jso.put(IMongoConstants.REPLICA_HOST_KEY, newValue);
                                connAttributes.put(IMongoDBAttributes.REPLICA_SET, jsa.toString());
                                isModified = true;
                            }
                            String portValue = jso.getString(IMongoConstants.REPLICA_PORT_KEY);
                            if (portValue != null && portValue.equals(oldValue)) {
                                jso.put(IMongoConstants.REPLICA_PORT_KEY, newValue);
                                connAttributes.put(IMongoDBAttributes.REPLICA_SET, jsa.toString());
                                isModified = true;
                            }
                        }
                    } catch (JSONException e) {
                        ExceptionHandler.process(e);
                    }
                } else if (attr.getValue().equals(oldValue)) {
                    attr.setValue(newValue);
                    isModified = true;
                }
            }
        }
    }
    return isModified;
}
Also used : JSONObject(org.talend.utils.json.JSONObject) EMap(org.eclipse.emf.common.util.EMap) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) NoSQLConnection(org.talend.repository.model.nosql.NoSQLConnection) Map(java.util.Map) EMap(org.eclipse.emf.common.util.EMap)

Example 19 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class NoSQLConnectionContextManager method revertPropertiesForContextMode.

/**
 * DOC PLV Comment method "revertPropertiesForContextMode".
 *
 * @param connectionItem
 * @param contextType
 * @param attributes
 */
public void revertPropertiesForContextMode(ContextType contextType) {
    EMap<String, String> connAttributes = connection.getAttributes();
    if (connAttributes == null) {
        return;
    }
    for (String attributeName : attributes) {
        if (attributeName.equals(IMongoDBAttributes.REPLICA_SET)) {
            String replicaSets = connAttributes.get(IMongoDBAttributes.REPLICA_SET);
            try {
                JSONArray jsa = new JSONArray(replicaSets);
                for (int i = 0; i < jsa.length(); i++) {
                    JSONObject jso = jsa.getJSONObject(i);
                    String hostValue = jso.getString(IMongoConstants.REPLICA_HOST_KEY);
                    String portValue = jso.getString(IMongoConstants.REPLICA_PORT_KEY);
                    String originalHostValue = ContextParameterUtils.getOriginalValue(contextType, hostValue);
                    String originalPortValue = ContextParameterUtils.getOriginalValue(contextType, portValue);
                    jso.put(IMongoConstants.REPLICA_HOST_KEY, originalHostValue);
                    jso.put(IMongoConstants.REPLICA_PORT_KEY, originalPortValue);
                }
                connAttributes.put(IMongoDBAttributes.REPLICA_SET, jsa.toString());
            } catch (JSONException e) {
                ExceptionHandler.process(e);
            }
        } else {
            String originalValue = ContextParameterUtils.getOriginalValue(contextType, connAttributes.get(attributeName));
            connAttributes.put(attributeName, originalValue);
        }
    }
    // set connection for context mode
    connection.setContextMode(false);
    connection.setContextId(ConnectionContextHelper.EMPTY);
}
Also used : JSONObject(org.talend.utils.json.JSONObject) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException)

Example 20 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class NoSQLRepositoryContextHandlerTest method validateResult.

private void validateResult() throws Exception {
    String dbName = noSqlConnection.getAttributes().get(INoSQLCommonAttributes.DATABASE);
    assertEquals("context.Mongo_Database", dbName);
    String replicaSet = noSqlConnection.getAttributes().get(IMongoDBAttributes.REPLICA_SET);
    assertNotNull(replicaSet);
    JSONArray jsa = new JSONArray(replicaSet);
    assertTrue(jsa.length() > 0);
    JSONObject jso = jsa.getJSONObject(0);
    String contextHostName = jso.getString(IMongoConstants.REPLICA_HOST_KEY);
    String contextPortName = jso.getString(IMongoConstants.REPLICA_PORT_KEY);
    assertEquals("context.Mongo_ReplicaHost_1", contextHostName);
    assertEquals("context.Mongo_ReplicaPort_1", contextPortName);
}
Also used : JSONObject(org.talend.utils.json.JSONObject) JSONArray(org.talend.utils.json.JSONArray)

Aggregations

JSONObject (org.talend.utils.json.JSONObject)36 JSONException (org.talend.utils.json.JSONException)21 JSONArray (org.talend.utils.json.JSONArray)19 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 NoSQLConnection (org.talend.repository.model.nosql.NoSQLConnection)4 Iterator (java.util.Iterator)3 List (java.util.List)3 Map (java.util.Map)3 PersistenceException (org.talend.commons.exception.PersistenceException)3 EHadoopParamName (org.talend.metadata.managment.ui.utils.ExtendedNodeConnectionContextUtils.EHadoopParamName)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SystemException (org.talend.commons.exception.SystemException)2 Context (org.talend.core.context.Context)2 RepositoryContext (org.talend.core.context.RepositoryContext)2 ConnectionBean (org.talend.core.model.general.ConnectionBean)2 Project (org.talend.core.model.general.Project)2 PreferenceManipulator (org.talend.core.prefs.PreferenceManipulator)2