use of com.commercetools.sync.commons.exceptions.InvalidReferenceException in project commercetools-sync-java by commercetools.
the class ProductTypeBatchValidator method getReferencedProductTypeKeys.
@Nonnull
private static Set<String> getReferencedProductTypeKeys(@Nonnull final ProductTypeDraft productTypeDraft) throws SyncException {
final List<AttributeDefinitionDraft> attributeDefinitionDrafts = productTypeDraft.getAttributes();
if (attributeDefinitionDrafts == null || attributeDefinitionDrafts.isEmpty()) {
return emptySet();
}
final Set<String> referencedProductTypeKeys = new HashSet<>();
final List<String> invalidAttributeDefinitionNames = new ArrayList<>();
for (AttributeDefinitionDraft attributeDefinitionDraft : attributeDefinitionDrafts) {
if (attributeDefinitionDraft != null) {
final AttributeType attributeType = attributeDefinitionDraft.getAttributeType();
try {
getProductTypeKey(attributeType).ifPresent(referencedProductTypeKeys::add);
} catch (InvalidReferenceException invalidReferenceException) {
invalidAttributeDefinitionNames.add(attributeDefinitionDraft.getName());
}
}
}
if (!invalidAttributeDefinitionNames.isEmpty()) {
final String errorMessage = format(PRODUCT_TYPE_HAS_INVALID_REFERENCES, productTypeDraft.getKey(), invalidAttributeDefinitionNames);
throw new SyncException(errorMessage, new InvalidReferenceException(BLANK_ID_VALUE_ON_REFERENCE));
}
return referencedProductTypeKeys;
}
use of com.commercetools.sync.commons.exceptions.InvalidReferenceException in project commercetools-sync-java by commercetools.
the class StateBatchValidator method getTransitionKeys.
@Nonnull
private static Set<String> getTransitionKeys(@Nonnull final StateDraft stateDraft) throws SyncException {
final Set<Reference<State>> transitions = stateDraft.getTransitions();
if (transitions == null || transitions.isEmpty()) {
return emptySet();
}
final Set<String> referencedStateKeys = new HashSet<>();
final List<Reference<State>> invalidStates = new ArrayList<>();
for (Reference<State> transition : transitions) {
if (transition != null) {
try {
referencedStateKeys.add(getStateKey(transition));
} catch (InvalidReferenceException invalidReferenceException) {
invalidStates.add(transition);
}
}
}
if (!invalidStates.isEmpty()) {
final String errorMessage = format(STATE_HAS_INVALID_REFERENCES, stateDraft.getKey());
throw new SyncException(errorMessage, new InvalidReferenceException(BLANK_ID_VALUE_ON_REFERENCE));
}
return referencedStateKeys;
}
use of com.commercetools.sync.commons.exceptions.InvalidReferenceException in project commercetools-sync-java by commercetools.
the class ProductTypeSync method removeAndKeepTrackOfMissingNestedAttribute.
/**
* Attempts to find a nested product type in the attribute type of {@code
* attributeDefinitionDraft}, if it has a nested type. It checks if the key, of the productType
* reference, is cached in {@code keyToIdCache}. If it is, then it means the referenced product
* type exists in the target project, so there is no need to remove or keep track of it. However,
* if it is not, it means it doesn't exist yet, which means we need to keep track of it as a
* missing reference and also remove this attribute definition from the supplied {@code draftCopy}
* to be able to create product type without this attribute containing the missing reference.
*
* <p>Note: This method mutates in the supplied {@code productTypeDraft} attribute definition list
* by removing the attribute containing a missing reference.
*
* @param attributeDefinitionDraft the attribute definition being checked for any product
* references.
* @param productTypeDraft the productTypeDraft containing the attribute which should be updated
* by removing the attribute which contains the missing reference.
* @param keyToIdCache a map of productType key to id. It represents a cache of the existing
* productTypes in the target project.
*/
@SuppressWarnings(// since the batch is validate before, key is assured to be non-blank
"ConstantConditions")
private // here.
void removeAndKeepTrackOfMissingNestedAttribute(@Nonnull final AttributeDefinitionDraft attributeDefinitionDraft, @Nonnull final ProductTypeDraft productTypeDraft, @Nonnull final Map<String, String> keyToIdCache) {
final AttributeType attributeType = attributeDefinitionDraft.getAttributeType();
try {
getProductTypeKey(attributeType).ifPresent(key -> {
if (!keyToIdCache.keySet().contains(key)) {
productTypeDraft.getAttributes().remove(attributeDefinitionDraft);
statistics.putMissingNestedProductType(key, productTypeDraft.getKey(), attributeDefinitionDraft);
}
});
} catch (InvalidReferenceException invalidReferenceException) {
handleError(new SyncException("This exception is unexpectedly thrown since the draft batch has been" + "already validated for blank keys at an earlier stage, which means this draft should" + " have a valid reference. Please communicate this error with the maintainer of the library.", invalidReferenceException), 1);
}
}
Aggregations