Adding Customized Validation When Importing Product Data from Excel
You can add new customized validation when importing data using importable spreadsheets. The customization capability extends the validation without interfering with the out-of-the-box (OOTB) validation or without altering data. For information on importing data from Excel, see How to Import Product Data from Excel.
Three specific validation use cases are supported across:
Different cells within a single row of a worksheet in a workbook.
Different rows of a worksheet in a workbook.
Different worksheets in a workbook.
Customization Allowing Validation Between Different Cells Within a Single Row of a Worksheet in a Workbook
Allows you to add the row-level validation for which different cells within a single row will be provided as a key-value pair.
An interface, SpreadsheetSingleSheetValidatorDelegate provides the APIs required for validating the row-level data. A default implementation of this interface, DefaultSpreadsheetSingleSheetValidatorDelegate provides a ready-to-use class along with a mechanism to handle custom warnings or exceptions, if thrown from custom validations.
* 
It is recommended that you extend DefaultSpreadsheetSingleSheetValidatorDelegate to ensure that the customization logic has been integrated with the Windchill export and import framework.
To add validations for a given row:
1. Override the validateFieldsForSingleRow(String sheetName, String rowNumber, Map rowData, Locale locale) API ensuring it includes the following parameters:
The name of the worksheet to which the given row belongs.
The number of the given row.
Map of all the fields within the given row specified as a key-value pair. For example, Number=00001.
Locale information to display the localized warning or exception message.
2. When the validations are complete, call addException() if you want to throw an exception or warning for your customization logic.
This will ensure that the exception or warning is rendered on the user interface in a manner consistent with the OOTB exceptions or warnings.
3. Register the authored delegate against the desired ImportSheetType (for example, BOM) in the service.xconf file as shown below.
<Service context="default" name="com.ptc.windchill.ixb.importer.custom.validator.SpreadsheetSingleSheetValidatorDelegate">

<Option requestor="null" selector="BOM"

serviceClass=" com.ptc.windchill.ixb.importer.custom.validator.delegate. DefaultSpreadsheetSingleSheetValidatorDelegate"/>
4. Run the xconfmanager utility once you register the delegate.
Customization Allowing Validation Between Different Rows of a Worksheet in a Workbook
Allows you to add the worksheet-level validation across different rows of a worksheet.
An interface, SpreadsheetSingleSheetValidatorDelegate provides the APIs required for validating the sheet-level data. A default implementation of this interface, DefaultSpreadsheetSingleSheetValidatorDelegate provides a ready-to-use class along with a mechanism to handle custom warnings or exceptions, if thrown from custom validations.
* 
It is recommended that you extend DefaultSpreadsheetSingleSheetValidatorDelegate to ensure that the customization logic has been integrated with the Windchill export and import framework.
To add validations for a given worksheet:
1. Override the validateFieldsForSheetWithAllRowElements (String sheetName, Map<String, Map> allRowsMap, Locale locale) API ensuring it includes the following parameters:
The name of the worksheet to which you want to add the validations.
Map of row details specified as a key-value pair (for example, Number=00001), where key represents the row number and value represents the map of all fields within the given row.
Locale information to display the localized warning or exception message.
2. When the validations are complete, call addException() if you want to throw an exception or warning for your customization logic.
This will ensure that the exception or warning is rendered on the user interface in a manner consistent with the OOTB exceptions or warnings.
3. Register the authored delegate against the desired ImportSheetType (for example, BOM) in the service.xconf file as shown below.
<Service context="default" name="com.ptc.windchill.ixb.importer.custom.validator.SpreadsheetSingleSheetValidatorDelegate">

<Option requestor="null" selector="BOM"

serviceClass=" com.ptc.windchill.ixb.importer.custom.validator.delegate. DefaultSpreadsheetSingleSheetValidatorDelegate"/>
4. Run the xconfmanager utility once you register the delegate.
Below is an example that validates duplicate children within an assembly.
public class SampleExampleSpreadsheetImportSingleSheetValdiatorDelegate extends DefaultSpreadsheetSingleSheetValidatorDelegate {

@Override
public void validateFieldsForSheetWithAllRowElements(String sheetName, Map<String, Map> allRowsMap,Locale locale)
throws WTException {
Set children = new HashSet();
if (allRowsMap != null) {
for (Entry<String, Map> entry : allRowsMap.entrySet()) {
String rowNumber = entry.getKey();
Map rowdata = entry.getValue();
String childNumber = (String) rowdata.get("Number");
if(childNumber != null) {
if (children.contains(childNumber)) {
String warningMsg = "Duplicate Child found in Assembly at row number :" + rowNumber;
addException(sheetName, rowNumber, warningMsg, false);
}
else {
children.add(childNumber);
}
}
}
}
}

}
Customization Allowing Validation Between Different Worksheets in a Workbook
Allows you to add validations between different worksheets in a workbook. This will enable you to validate specific scenarios where data from multiple worksheets of a workbook is required for validation.
An interface, SpreadsheetWorkbookDataValidatorDelegate provides the APIs required for validating the workbook-level data. A default implementation of this interface, DefaultSpreadsheetWorkbookDataValidatorDelegate provides a ready-to-use class along with a mechanism to handle custom warnings or exceptions, if thrown from custom validations.
* 
It is recommended that you extend DefaultSpreadsheetWorkbookDataValidatorDelegate to ensure that the customization logic has been integrated with the Windchill export and import framework.
To add validations for a given workbook:
1. Override the validateFieldsForAllSheetsInWorkbook (Map<String, Map<String, Map<String, Map>>> trackableToElementsMap, Locale locale) API ensuring it includes the following parameters:
A map, where
key represents ImportSheetType (for example, BOM).
value represents a map, where
key represents the name of the worksheet.
value represents a map of the details of the rows at worksheet-level, where key represents the row number and value represents a map of row details specified as a key-value pair (for example, Number=00001).
Locale information to display the localized warning or exception message.
2. When the validations are complete, call addException() if you want to throw an exception or warning for your customization logic.
This will ensure that the exception or warning is rendered on the user interface in a manner consistent with the OOTB exceptions or warnings.
3. Register the authored delegate against the desired ImportSheetType (for example, BOM) in the service.xconf file as shown below.
<Service context="default" name="com.ptc.windchill.ixb.importer.custom.validator. SpreadsheetWorkbookDataValidatorDelegate">

<Option requestor="null" selector="All"

serviceClass=" com.ptc.windchill.ixb.importer.custom.validator.delegate. DefaultSpreadsheetWorkbookDataValidatorDelegate"/>
4. Run the xconfmanager utility once you register the delegate.
Below is an example that checks if a usage link exists in a worksheet for substitute link.
public class SampleExampleAllWorkbookValidatorDelegate extends DefaultSpreadsheetWorkbookDataValidatorDelegate {

private static String BOM_TYPE = "bom";
private static String REPLACMENT_TYPE = "replacements";
private static String REPLACMENT_SHEET_NAME = "WTPartSubstituteLink-Sheet";
private static String BOM_SHEET_NAME = "BOMSheet1";
@Override
public void validateFieldsForAllSheetsInWorkbook(
Map<String, Map<String, Map<String, Map>>> allSheetData, Locale locale) throws WTException {

Map<String, Map> allBOMElements = new HashMap<String, Map>();
if (allSheetData != null) {

Map<String, Map<String, Map>> allBomSheetValues = allSheetData.get(BOM_TYPE);
// Read first sheet
if (allBomSheetValues != null) {
Map<String, Map> bomEntries = allBomSheetValues.get(BOM_SHEET_NAME);
for (Entry<String, Map> entry : bomEntries.entrySet()) {
Map rowData = entry.getValue();

String partNumber = (String) rowData.get("Number");
allBOMElements.put(partNumber, rowData);
}

Map<String, Map<String, Map>> replacementSheetValues = allSheetData.get(REPLACMENT_TYPE);
if (replacementSheetValues != null) {
Map<String, Map> replacementEntries = replacementSheetValues.get(REPLACMENT_SHEET_NAME);

for (Entry<String, Map> entry : replacementEntries.entrySet()) {
String rowNumber = entry.getKey();
Map rowData = entry.getValue();

String parentPartNumber = (String) rowData.get("Parent Part Number");
String childPartNumber = (String) rowData.get("Child Part Number");

if (!isUsageLinkExistsForThisSubstituteLinkEntry(allBOMElements, parentPartNumber,
childPartNumber)) {
String warningMsg = "No Usage Link Exists for this substitute Entry at row number :"
+ rowNumber;
addException(REPLACMENT_SHEET_NAME, rowNumber, warningMsg, false);
}
}
}

}
}

}
Было ли это полезно?