Change the type of the return variable in a component

Requirement

Need to change the type of the return variable because of the incorrect component design.

Occurence

The return variable could show up as the following:
  1. In the field properties of the components in the Job.
  2. In the table properties of the components in the Job.
  3. In the connector between the components, for example, run if.

Steps

  1. Extend the class AbstractJobMigrationTask.
  2. Overwrite the method: execute(Item item) to do the migration task
  3. implement getOrder() to help order all these migration tasks
  4. Register the migration task in plugin: org.talend.repository

example

In this example, we will take the return variable: SERVER_ALIVE_RESULT in tServerAlive as example.

<RETURN NAME="SERVER_ALIVE_RESULT" TYPE="id_String" AVAILABILITY="AFTER"/>

and now we are going to change the return type from String to Boolean:

<RETURN NAME="SERVER_ALIVE_RESULT" TYPE="id_Boolean" AVAILABILITY="AFTER"/>

Create a class

The class is named as ChangeReturnTypeOfServerAliveResult
  1. Use org.talend.repository plugin
  2. use org.talend.repository.model.migration package
  3. Class must extends AbstractJobMigrationTask

Replace

Override the method: execute(Item item), it is used to do the migration work
  1. get the process type: ProcessItem and JobletProcessItem(only in tis).
    public ExecutionResult execute(Item item) {
        ProcessType processType = //getProcessType(item)//;
        if (processType == null) {
            return ExecutionResult.NOTHING_TO_DO;
        }
        try {
            convertItem(item, processType);
            return ExecutionResult.SUCCESS_WITH_ALERT;
        } catch (Exception e) {
            ExceptionHandler.process(e);
            return ExecutionResult.FAILURE;
        }
    }
  1. get the language of the generated code: Java.
  2. get all these nodes and connections in the imported job.
        ECodeLanguage language = getProject().getLanguage(); // used to judge the language of job: java
        EList node = processType.getNode(); // get all these nodes in the job
        EList connections = processType.getConnection(); // get all these connectors in the job
  1. replace the String type to Boolean type
        for (Object n : node) {
            NodeType type = (NodeType) n;
            EList elementParameterList = type.getElementParameter();
            for (Object elem : elementParameterList) {
                // find the field property containing "tServerAlive" and replace
                ElementParameterType elemType = (ElementParameterType) elem;
                if (language.equals(ECodeLanguage.JAVA)) {
                    if (elemType.getValue() != null && elemType.getValue().contains("tServerAlive")) { //$NON-NLS-1$
                        Matcher match = PATTERN_GET.matcher(elemType.getValue());
                        String replace = getReplaceValue(match);
                        if (replace != null && replace.length() > 0) {
                            elemType.setValue(replace);
                            modified = true;
                        }
                    }
                }
 
                // // find the table property containing "tServerAlive" and replace
 
                EList elemValue = elemType.getElementValue();
                for (Object elemV : elemValue) {
                    ElementValueType elemVal = (ElementValueType) elemV;
                    if (language.equals(ECodeLanguage.JAVA)) {
                        if (elemVal.getValue() != null && elemVal.getValue().contains("tServerAlive")) { //$NON-NLS-1$
                            Matcher match = PATTERN_GET.matcher(elemVal.getValue());
                            String replace = getReplaceValue(match);
                            if (replace != null && replace.length() > 0) {
                                elemVal.setValue(replace);
                                modified = true;
                            }
                        }
                    }
                }
 
            }
 
        }
        // find the value in the connector containing "tServerAlive" and replace
        for (Object n : connections) {
            ConnectionType type = (ConnectionType) n;
            EList elementParameterList = type.getElementParameter();
            for (Object elem : elementParameterList) {
                ElementParameterType elemType = (ElementParameterType) elem;
                if (language.equals(ECodeLanguage.JAVA)) {
                    if (elemType.getValue() != null && elemType.getValue().contains("tServerAlive")) { //$NON-NLS-1$
                        Matcher match = PATTERN_GET.matcher(elemType.getValue());
                        String replace = getReplaceValue(match);
                        if (replace != null && replace.length() > 0) {
                            elemType.setValue(replace);
                            modified = true;
                        }
                    }
                }
            }
        }
  1. save the modification
    private boolean convertItem(Item item, ProcessType processType) throws PersistenceException {
        ProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
        boolean modified = false;
// get the language
// get the nodes
// get the connectors
// replace the old value with the new value
..........................
 
        if (modified) {
            factory.save(item, true);
        }
 
        return modified;
    }

Order

Implement getOrder() to help order all these migration tasks
    public Date getOrder() {
        GregorianCalendar gc = new GregorianCalendar(2009, 6, 1, 12, 0, 0);
        return gc.getTime();
    }

Register

Register the migration task in plugin: org.talend.repository

:doc:addmigrationtaskextensionpoint.gif

Test

The following is a job that created in tos3.1.4 with a global variable:

((String)globalMap.get("tServerAlive_1_SERVER_ALIVE_RESULT"))

:doc:testChangeTypeOfReturnVar.jpg

And the settings for tFixedFlowInput, If and tJava are:


:doc:testBeforeChangeTypeOfReturnVar.JPG

After importing the job to tos4.1, then the setting are changed to these:


:doc:testAfterChangeTypeOfReturnVar.JPG

And you should see this line in “talend.project” file:

<migrationTasks>org.talend.repository.model.migration.changeReturnTypeOfServerAliveResult</migrationTasks>

note

You can find the configuration in the plugin.xml of org.talend.repository

       <projecttask
             beforeLogon="false"
             class="org.talend.repository.model.migration.ChangeReturnTypeOfServerAliveResult"
             description="change the type of the return value Server_Alive_Result from String to Boolean"
             id="org.talend.repository.model.migration.changeReturnTypeOfServerAliveResult"
             name="change the type of the return value Server_Alive_Result from String to Boolean">
       </projecttask>
 
doc/changereturnvaluetype.txt · Last modified: 2012/01/19 05:56 by ychen
 
 
Recent changes RSS feed Driven by DokuWiki