import org.zephyr.data.Record; //导入依赖的package包/类
/**
* This method takes in a List of orderedPairings (key:value pairs). It creates a new Record with a new UUID, then iterates over the list of Pair items.
*
* If the Pair item currently operated upon can be normalized and validated as per the Scheme for that pair.getKey() successfully, it is added to the Record
* and the pair.getKey() is added to the normalizedAndValidatedFields Set - this set keeps track of all the fields that have been successfully normalized, validated, mapped, and security labeled.
*
* After all Pairs have been mapped according to their scheme, we then check the normalizedAndValidatedFields Set against the requiredFields set - unless all requiredFields have been successfully operated over,
* we throw a SchemeException. If all have been operated over successfully, we return the Record.
*
* @param orderedPairings
* @return
* @throws SchemeException
*/
public ProcessingResult> map(List orderedPairings) {
Set normalizedAndValidatedFields = new TreeSet();
Record record = new Record();
record.setFeedName(getFeedName());
for (Pair pair : orderedPairings) {
try {
List> results = normalizeAndValidate(pair);
for (ProcessingResult result : results) {
if (result.wasProcessedSuccessfully()) {
normalizedAndValidatedFields.add(pair.getKey());
record.add(result.getProcessedData());
} else {
logger.debug("A SchemeException error occurred when processing: {}", pair, result.getError());
}
}
} catch (SchemeException e) {
logger.debug("A SchemeException error occurred when processing: {}", pair, e);
}
}
Set requiredFields = getRequiredFields();
if (requiredFields != null) {
if (normalizedAndValidatedFields.containsAll(requiredFields)) {
return new ProcessingResult>(record);
} else {
Set missingRequiredFields = new TreeSet(requiredFields);
missingRequiredFields.removeAll(normalizedAndValidatedFields);
StringBuilder builder = new StringBuilder();
for (String missing : missingRequiredFields) {
builder.append(missing);
builder.append(",");
}
String error = builder.substring(0, builder.length() - 1);
return new ProcessingResult>(orderedPairings, new SchemeException("Of the required fields for this Schema, the following were unsuccessfully normalized or validated: " + error));
}
} else {
return new ProcessingResult>(record);
}
}