Search in sources :

Example 11 with JSONArray

use of org.talend.utils.json.JSONArray 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 12 with JSONArray

use of org.talend.utils.json.JSONArray in project tdi-studio-se by Talend.

the class JsonTableHandler method getDataRowList.

private List<List<Object>> getDataRowList(JSONObject rootObj, AtomicInteger columnsCount) throws JSONException {
    List<List<Object>> rows = new ArrayList<>();
    AtomicInteger count = columnsCount;
    if (count == null) {
        count = new AtomicInteger();
    }
    JSONArray jsonArr = rootObj.getJSONArray(DATA_KEY);
    for (int i = 0; i < jsonArr.length(); i++) {
        List<Object> row = new ArrayList<>();
        JSONArray rowArray = jsonArr.getJSONArray(i);
        for (int j = 0; j < rowArray.length(); j++) {
            row.add(rowArray.get(j));
        }
        if (row.size() > count.get()) {
            count.set(row.size());
        }
        rows.add(row);
    }
    return rows;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) JSONArray(org.talend.utils.json.JSONArray) List(java.util.List) ArrayList(java.util.ArrayList) JSONObject(org.talend.utils.json.JSONObject)

Example 13 with JSONArray

use of org.talend.utils.json.JSONArray in project tdi-studio-se by Talend.

the class FunctionManagerExt method getFunctionFromColumn.

public Function getFunctionFromColumn(MetadataColumnExt column) {
    Function function = null;
    String functionInfo = column.getFunctionInfo();
    if (functionInfo != null) {
        try {
            JSONObject functionObj = new JSONObject(functionInfo);
            String functionName = functionObj.getString(Function.NAME);
            int functionSize = 0;
            JSONArray parametersArray = functionObj.getJSONArray(Function.PARAMETERS);
            if (parametersArray != null) {
                functionSize = parametersArray.length();
            }
            List<Function> funcs = getFunctionsByType(column.getTalendType());
            for (Function func : funcs) {
                if (func.getName().equals(functionName) && func.getParameters().size() == functionSize) {
                    function = func;
                    break;
                }
            }
            if (function != null) {
                function = function.clone(parametersArray);
            }
        } catch (JSONException e) {
            ExceptionHandler.process(e);
        }
    }
    return function;
}
Also used : JSONObject(org.talend.utils.json.JSONObject) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException)

Example 14 with JSONArray

use of org.talend.utils.json.JSONArray in project tdi-studio-se by Talend.

the class ColumnListController method reUsedColumnFunctionArrayCheck.

private static void reUsedColumnFunctionArrayCheck(Map<String, Object> newLine, IElement element, String[] codes) {
    if (element instanceof INode && ((INode) element).getMetadataList().size() > 0 && ((INode) element).getComponent().getName().equals("tRowGenerator")) {
        IMetadataTable table = ((INode) element).getMetadataList().get(0);
        //$NON-NLS-1$
        String lineName = (String) newLine.get("SCHEMA_COLUMN");
        for (IMetadataColumn column : table.getListColumns()) {
            if (lineName != null && lineName.equals(column.getLabel()) && "".equals(newLine.get("ARRAY"))) {
                Map<String, String> columnFunction = column.getAdditionalField();
                String functionInfo = columnFunction.get(FUNCTION_INFO);
                if (functionInfo != null) {
                    try {
                        JSONObject functionInfoObj = new JSONObject(functionInfo);
                        String functionExpression = "";
                        String functionName = functionInfoObj.getString(Function.NAME);
                        if (!functionName.equals("...")) {
                            String className = functionInfoObj.getString(Function.PARAMETER_CLASS_NAME);
                            String parmValueApend = "";
                            functionExpression = className + '.' + functionName + "(";
                            JSONArray parametersArray = functionInfoObj.getJSONArray(Function.PARAMETERS);
                            if (parametersArray != null) {
                                for (int i = 0; i < parametersArray.length(); i++) {
                                    JSONObject parameterObj = parametersArray.getJSONObject(i);
                                    String paramValue = parameterObj.getString(Function.PARAMETER_VALUE);
                                    parmValueApend = parmValueApend + paramValue.trim() + ',';
                                }
                            }
                            if (parmValueApend.endsWith(",")) {
                                parmValueApend = parmValueApend.substring(0, parmValueApend.lastIndexOf(","));
                            }
                            functionExpression = functionExpression + parmValueApend + ')';
                            newLine.put("ARRAY", functionExpression);
                        }
                    } catch (JSONException e) {
                        ExceptionHandler.process(e);
                    }
                }
            }
        }
    }
}
Also used : IMetadataTable(org.talend.core.model.metadata.IMetadataTable) INode(org.talend.core.model.process.INode) JSONObject(org.talend.utils.json.JSONObject) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) Point(org.eclipse.swt.graphics.Point)

Example 15 with JSONArray

use of org.talend.utils.json.JSONArray 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)

Aggregations

JSONArray (org.talend.utils.json.JSONArray)20 JSONObject (org.talend.utils.json.JSONObject)20 JSONException (org.talend.utils.json.JSONException)14 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 NoSQLConnection (org.talend.repository.model.nosql.NoSQLConnection)4 Iterator (java.util.Iterator)3 List (java.util.List)3 Map (java.util.Map)3 EHadoopParamName (org.talend.metadata.managment.ui.utils.ExtendedNodeConnectionContextUtils.EHadoopParamName)3 IConnParamName (org.talend.metadata.managment.ui.model.IConnParamName)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ASN1Object (org.bouncycastle.asn1.ASN1Object)1 EMap (org.eclipse.emf.common.util.EMap)1 Point (org.eclipse.swt.graphics.Point)1 IMetadataColumn (org.talend.core.model.metadata.IMetadataColumn)1 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)1 JavaType (org.talend.core.model.metadata.types.JavaType)1 IContextParameter (org.talend.core.model.process.IContextParameter)1