ImportFieldChange
In order to convert an oldValueObject and a newValueObject, as provided by @ImportFieldHistoryValue, into an appropriate TrackerItemHistoryConfiguration, an @CustomField extension can provide an @CustomField.ImportFieldChange method.
The method has access to the following context information using parameters of the appropriate type:
JiraImportController, to receive the controller of the current JIRA import.
ProjectConfiguration or ProjectDto, to receive the target project of the JIRA import.
JiraTrackerSyncConfig, to receive the target tracker of the JIRA import.
TrackerItemDto, to receive the target tracker item, into which the custom field value should be stored.
TrackerLayoutLabelDto, to receive the target field, whose value to import.
TrackerItemHistoryConfiguration, to receive the current tracker item history configuration to import.
JiraRestClient, to receive the REST API of the remote JIRA instance from which to import.
ImporterSupport, to receive the current importer.
ImportStatistics, to receive the current import statistics.
TrackerItemFieldHandler, to receive the current tracker item field handler.
The method should process the specified TrackerItemHistoryConfiguration, whose oldValueObject and newValueObject are the values provided by @ImportFieldHistoryValue, whose op is initialized to Set and optionally return it.
For example: Checklist for Jira


package com.intland.codebeamer.controller.jira;



import org.springframework.stereotype.Component;



import com.fasterxml.jackson.databind.node.JsonNode;



import com.intland.codebeamer.controller.jira.CustomField;

import com.intland.codebeamer.controller.jira.JiraImportController;

import com.intland.codebeamer.controller.jira.JiraTrackerSyncConfig;

import com.intland.codebeamer.manager.util.ImportStatistics;

import com.intland.codebeamer.manager.util.ImporterSupport;

import com.intland.codebeamer.manager.util.TrackerItemHistoryConfiguration;

import com.intland.codebeamer.persistence.dto.TrackerItemDto;

import com.intland.codebeamer.persistence.dto.TrackerLayoutLabelDto;

import com.intland.codebeamer.wiki.plugins.ChecklistPlugin;



@Component("com.okapya.jira.checklist:checklist")

@CustomField(type="WikiText", of="Checklist")

public class ChecklistForJiraField {



public static Map<Integer,Change> getChanges(Object value) {

return value instanceof Map ? (Map)value : null;

}



/**

* Convert the specified incremental checklist change into an appropriate Checklist Wikitext change

* @param tracker is the tracker sync configuration

* @param item is the tracker item to import

* @param fieldChange is the incremental custom checklist field change

* @param importer is the current import cache/support

* @param statistic are the current import statistics

*/

@CustomField.ImportFieldChange

public void buildTrackerItemHistoryConfiguration(JiraTrackerSyncConfig tracker, TrackerItemDto item, TrackerItemHistoryConfiguration fieldChange, ImporterSupport importer, ImportStatistics statistic) {

TrackerLayoutLabelDto field;

Map<Integer,Change> newValues;



if (fieldChange != null && (field = fieldChange.getField()) != null

&& (newValues = getChanges(fieldChange.getNewValueObject())) != null && newValues.size() > 0) {

Map<Integer,Change> oldValues = getChanges(fieldChange.getOldValueObject());



fieldChange.setOldValueObject(null);

fieldChange.setNewValueObject(null);



JsonNode oldItems = getChecklist(tracker, item, field, importer, statistic);

Checklist modified = new Checklist(oldItems.deepCopy());



for (Map.Entry<Integer,Change> change : newValues.entrySet()) {

Change newItem = change.getValue();

Change oldItem = oldValues.get(change.getKey());



if (newItem.wasAdded()) {

newItem.apply(tracker, modified.addItem());

} else if (newItem.wasRemoved()) {

modified.removeItem(StringUtils.defaultString(oldItem != null ? oldItem.getName() : null, newItem.getName()));

} else if (newItem.wasReordered()) {

modified.reorderItems();

} else {

// We must NOT add the item here, if no such item exists (any more), because it was most probably removed locally and MUST remain removed !

ObjectNode node = modified.getItem(StringUtils.defaultString(newItem.wasRenamed() && oldItem != null ? oldItem.getName() : null, newItem.getName()));

if (node != null) {

if (!newItem.isHeader() && oldItem != null && oldItem.isHeader()) {

node.remove(HEADER);

}



newItem.apply(tracker, node);

}

}

}



modified.applyOrder(getChecklist(tracker, item, field));



fieldChange.setOldValue(ChecklistPlugin.wrapChecklist(oldItems));

fieldChange.setNewValue(ChecklistPlugin.wrapChecklist(modified.getItems()));



setChecklist(item, field, modified.getItems(), importer);

}

}



}
In order to convert incremental changes to the Checklist for Jira extension into the appropriate oldValue and newValue of the Codebeamer Checklist Wiki field before and after the change, the ChecklistForJira extension needs to maintain a cache of checklist values for each field.
Since a custom field extension must be stateless, this cache must be stored in the import context, represented by the ImporterSupport instance:
getChecklist(tracker, item, field, importer, statistic)
setChecklist(item, field, checklist, importer)
The full example source code is attached).
Was this helpful?