Search in sources :

Example 21 with JSONException

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

the class LoginProjectPage method fillUIBranches.

private void fillUIBranches(final Project project, boolean lastUsedBranch) throws JSONException {
    final String storage = getStorage(project);
    if (LoginHelper.isRemoteConnection(getConnection())) {
        currentProjectSettings = project;
        final List<String> projectBranches = new ArrayList<String>();
        if ("svn".equals(storage)) {
            //$NON-NLS-1$
            projectBranches.add("trunk");
            branchesViewer.setInput(projectBranches);
            //$NON-NLS-1$
            branchesViewer.setSelection(new StructuredSelection(new Object[] { "trunk" }));
        } else if ("git".equals(storage)) {
            //$NON-NLS-1$
            //$NON-NLS-1$
            String master = "master";
            projectBranches.add(master);
            branchesViewer.setInput(projectBranches);
            if (projectBranches.size() != 0) {
                branchesViewer.setSelection(new StructuredSelection(new Object[] { projectBranches.contains(master) ? master : projectBranches.get(0) }));
            }
        }
        branchesViewer.getCombo().setEnabled(false);
        if (backgroundGUIUpdate == null) /* || (backgroundGUIUpdate.getState() == Job.NONE) */
        {
            backgroundGUIUpdate = new //$NON-NLS-1$
            Job(//$NON-NLS-1$
            "List Branches") {

                @Override
                protected IStatus run(IProgressMonitor monitor) {
                    projectBranches.clear();
                    try {
                        projectBranches.addAll(loginHelper.getProjectBranches(currentProjectSettings));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        ExceptionHandler.process(e);
                    }
                    return org.eclipse.core.runtime.Status.OK_STATUS;
                }
            };
            Job.getJobManager().addJobChangeListener(new JobChangeAdapter() {

                @Override
                public void done(IJobChangeEvent event) {
                    if (event.getJob().equals(backgroundGUIUpdate)) {
                        if (branchesViewer != null && !branchesViewer.getCombo().isDisposed()) {
                            branchesViewer.getCombo().getDisplay().syncExec(new Runnable() {

                                @Override
                                public void run() {
                                    if (branchesViewer == null || branchesViewer.getControl() == null || branchesViewer.getControl().isDisposed()) {
                                        return;
                                    }
                                    branchesViewer.setInput(projectBranches);
                                    //branchesViewer.setSelection(new StructuredSelection(new Object[] { projectBranches.get(0) })); //$NON-NLS-1$ 
                                    if ("svn".equals(storage) && projectBranches.size() != 0) {
                                        branchesViewer.setSelection(new StructuredSelection(new Object[] { projectBranches.contains("trunk") ? "trunk" : projectBranches.get(0) }));
                                    } else if ("git".equals(storage) && projectBranches.size() != 0) {
                                        branchesViewer.setSelection(new StructuredSelection(new Object[] { projectBranches.contains("master") ? "master" : projectBranches.get(0) }));
                                    }
                                    // svnBranchCombo.getCombo().setFont(originalFont);
                                    branchesViewer.getCombo().setEnabled(projectViewer.getControl().isEnabled());
                                }
                            });
                        }
                    }
                }
            });
            branchesViewer.getCombo().addDisposeListener(new DisposeListener() {

                @Override
                public void widgetDisposed(DisposeEvent e) {
                    backgroundGUIUpdate = null;
                }
            });
        }
        backgroundGUIUpdate.schedule();
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) IStatus(org.eclipse.core.runtime.IStatus) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) JSONException(org.talend.utils.json.JSONException) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) JSONObject(org.talend.utils.json.JSONObject)

Example 22 with JSONException

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

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

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

the class HCatalogForm method initHadoopProperties.

// private void addHadoopPropertiesFields() {
// // table view
// Composite compositeTable = Form.startNewDimensionnedGridLayout(this, 1, this.getBorderWidth(), 150);
// GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
// gridData.horizontalSpan = 4;
// compositeTable.setLayoutData(gridData);
// CommandStackForComposite commandStack = new CommandStackForComposite(compositeTable);
// properties = new ArrayList<HashMap<String, Object>>();
// initHadoopProperties();
// HadoopPropertiesFieldModel model = new HadoopPropertiesFieldModel(properties, "Hadoop Properties");
// propertiesTableView = new HadoopPropertiesTableView(model, compositeTable);
// propertiesTableView.getExtendedTableViewer().setCommandStack(commandStack);
// final Composite fieldTableEditorComposite = propertiesTableView.getMainComposite();
// gridData = new GridData(GridData.FILL_HORIZONTAL);
// gridData.heightHint = 180;
// fieldTableEditorComposite.setLayoutData(gridData);
// fieldTableEditorComposite.setBackground(null);
// }
private void initHadoopProperties() {
    String hadoopProperties = getConnection().getHadoopProperties();
    try {
        if (StringUtils.isNotEmpty(hadoopProperties)) {
            JSONArray jsonArr = new JSONArray(hadoopProperties);
            for (int i = 0; i < jsonArr.length(); i++) {
                HashMap<String, Object> map = new HashMap();
                JSONObject object = jsonArr.getJSONObject(i);
                Iterator it = object.keys();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    map.put(key, object.get(key));
                }
                properties.add(map);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.talend.utils.json.JSONObject) HashMap(java.util.HashMap) JSONArray(org.talend.utils.json.JSONArray) Iterator(java.util.Iterator) JSONException(org.talend.utils.json.JSONException) JSONObject(org.talend.utils.json.JSONObject)

Example 25 with JSONException

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

the class DefaultConfigurationManagerTest method testGetValue_Whole.

@Test
public void testGetValue_Whole() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("key1", "123");
    String value = DefaultConfigurationManager.getValue(json, "");
    assertNotNull(value);
    JSONObject getJson = null;
    try {
        getJson = new JSONObject(value);
    } catch (JSONException e) {
    // 
    }
    assertNotNull("Can't get the whole JSON", getJson);
    assertTrue(getJson.has("key1"));
    assertEquals("123", getJson.getString("key1"));
}
Also used : JSONObject(org.talend.utils.json.JSONObject) JSONException(org.talend.utils.json.JSONException) Test(org.junit.Test)

Aggregations

JSONException (org.talend.utils.json.JSONException)26 JSONObject (org.talend.utils.json.JSONObject)21 JSONArray (org.talend.utils.json.JSONArray)14 Project (org.talend.core.model.general.Project)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 PersistenceException (org.talend.commons.exception.PersistenceException)4 NoSQLConnection (org.talend.repository.model.nosql.NoSQLConnection)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 ConnectionBean (org.talend.core.model.general.ConnectionBean)3 EHadoopParamName (org.talend.metadata.managment.ui.utils.ExtendedNodeConnectionContextUtils.EHadoopParamName)3 Iterator (java.util.Iterator)2 SystemException (org.talend.commons.exception.SystemException)2 Context (org.talend.core.context.Context)2 RepositoryContext (org.talend.core.context.RepositoryContext)2 User (org.talend.core.model.properties.User)2 IConnParamName (org.talend.metadata.managment.ui.model.IConnParamName)2 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)2