Search in sources :

Example 1 with InvalidReferenceException

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;
}
Also used : NestedAttributeType(io.sphere.sdk.products.attributes.NestedAttributeType) AttributeType(io.sphere.sdk.products.attributes.AttributeType) SetAttributeType(io.sphere.sdk.products.attributes.SetAttributeType) ArrayList(java.util.ArrayList) AttributeDefinitionDraft(io.sphere.sdk.products.attributes.AttributeDefinitionDraft) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InvalidReferenceException(com.commercetools.sync.commons.exceptions.InvalidReferenceException) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 2 with InvalidReferenceException

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;
}
Also used : Reference(io.sphere.sdk.models.Reference) State(io.sphere.sdk.states.State) ArrayList(java.util.ArrayList) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InvalidReferenceException(com.commercetools.sync.commons.exceptions.InvalidReferenceException) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 3 with InvalidReferenceException

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);
    }
}
Also used : AttributeType(io.sphere.sdk.products.attributes.AttributeType) SyncException(com.commercetools.sync.commons.exceptions.SyncException) InvalidReferenceException(com.commercetools.sync.commons.exceptions.InvalidReferenceException)

Aggregations

InvalidReferenceException (com.commercetools.sync.commons.exceptions.InvalidReferenceException)3 SyncException (com.commercetools.sync.commons.exceptions.SyncException)3 AttributeType (io.sphere.sdk.products.attributes.AttributeType)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Nonnull (javax.annotation.Nonnull)2 Reference (io.sphere.sdk.models.Reference)1 AttributeDefinitionDraft (io.sphere.sdk.products.attributes.AttributeDefinitionDraft)1 NestedAttributeType (io.sphere.sdk.products.attributes.NestedAttributeType)1 SetAttributeType (io.sphere.sdk.products.attributes.SetAttributeType)1 State (io.sphere.sdk.states.State)1