Servigistics InService Customization > Customizing System Integrations > Validating Cart Items > Example Code for Validating Cart Items
  
Example Code for Validating Cart Items
These examples for validating cart items are for reference purposes only. This code is used in a custom Java source file of the custom delegate that implements the com.ptc.sc.services.plugins.CommerceDelegate interface. You can modify these examples to validate other properties of cart items, such as quantity and notes.
Example Code for Checking the Validity of Serviceable Cart Items
The following example code checks the validity of serviceable cart items:
public List<SCItem> validateCartItems(SCList cartItems, SCId cartSCId)
throws Exception {
List<SCItem> invalidItems = new ArrayList<>();
for (SCItem cartItem : cartItems.getItems())
{
SCItem part = cartItem.getObject(CartService.CART_ITEM_
RELATIONSHIP_OBJECT_CONTENT_REF);
if (!validatePart(part, cartSCId))
{
invalidItems.add(cartItem);
}
cartItem.putMetadata(ValidationUtil.METADATA_KEY_PART_VALIDATION,
part.getMetadataValue(ValidationUtil.METADATA_KEY_PART_VALIDATION),
part.getMetadataTitle(ValidationUtil.METADATA_KEY_PART_VALIDATION));
}
return invalidItems; // This is the list of invalid items
}
Note the following points in the code:
The validateCartItems() method uses the validatePart() function to check whether the given part and associated part item added to the cart is serviceable.
The validatePart() function further checks the validity of the part. Any invalid cart items are added to the invalidItems list. For more information about the validatePart() function, see The Function to Validate Parts.
Example Code for Disabling Addition of Non-Serviceable Parts to the Cart
The following example code disables the addition of non-serviceable parts to the cart:
public List<SCItem> validateParts(SCList parts) throws Exception {
List<SCItem> invalidParts = new ArrayList<>();
for (SCItem part : parts.getItems()) {
if (!validatePart(part, null)) {
invalidParts.add(part);
}
}
return invalidParts;
}
Note the following points in the code:
The validateParts method uses the validatePart() function to check whether the given part is serviceable. For more information about the validatePart() function, see The Function to Validate Parts.
If a part is not serviceable, the Add to Cart icon is not visible for that part on the Servigistics InService user interface.
The Function to Validate Parts
The following example code demonstrates the validatePart() function. This function validates whether the part line item and associated parts are serviceable:
protected boolean validatePart(SCItem part, SCId cartSCId) throws
Exception {
SCQueryInfo queryinfo = SCRequestContextUtility.getSCQueryInfo();
String serviceAttrVal = part.getAttribute(PartService.ATTRIBUTE
_SERVICEABLE);

if (serviceAttrVal == null || Boolean.parseBoolean(serviceAttr
Val)) {
//Incase of partline items,check if the part itself is also
serviceable.
SCItem associatedPart = part.getObject(PartsListService.
RELATIONSHIP_COLLECTION_ASSOCIATED_PART) != null ?
part.getObject(PartsListService.RELATIONSHIP_
COLLECTION_ASSOCIATED_PART) : null;
if (associatedPart != null) {
String associatedPartServiceability = associatedPart
.getAttribute(PartService.ATTRIBUTE_SERVICEABLE);
if (associatedPartServiceability == null || Boolean.
parseBoolean(associatedPartServiceability)) {
part.putMetadata(ValidationUtil.METADATA_KEY_PART
_VALIDATION, ValidationUtil.METADATA_VALUE_VALID, null);
return true;
} else {
boolean isShoppingCart = cartSCId != null &&
SCECartHelper.isSCEShoppingCart(cartSCId);
String message = SCEResourceBundleUtility.
getLocalizedMessage(SCECartRB.class.getName(),
isShoppingCart ? SCECartRB.ITEM_NOT
_SERVICEABLE_CART : SCECartRB.ITEM_NOT_SERVICEABLE,
queryinfo);
part.putMetadata(ValidationUtil.METADATA_KEY
_PART_VALIDATION, ValidationUtil.METADATA_VALUE_INVALID, message);
return false;
}
}
part.putMetadata(ValidationUtil.METADATA_KEY_PART
_VALIDATION, ValidationUtil.METADATA_VALUE_VALID, null);
return true;
} else {
boolean isShoppingCart = cartSCId != null &&
SCECartHelper.isSCEShopping
Cart(cartSCId);
String message = SCEResourceBundleUtility.getLocalized
Message(SCECartRB.class.getName(),
isShoppingCart ? SCECartRB.ITEM_NOT_SERVICEABLE
_CART : SCECartRB.ITEM_NOT_SERVICEABLE,
queryinfo);
part.putMetadata(ValidationUtil.METADATA_KEY_PART
_VALIDATION, ValidationUtil.METADATA_VALUE_INVALID, message);
return false;
}
}