com.shopify.model.ShopifyProductCreationRequest Maven / Gradle / Ivy
package com.shopify.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
public class ShopifyProductCreationRequest implements ShopifyProductRequest {
private final ShopifyProduct request;
private final Map variantPositionToImagePosition;
public static interface TitleStep {
public MetafieldsGlobalTitleTagStep withTitle(final String title);
}
public static interface MetafieldsGlobalTitleTagStep {
public ProductTypeStep withMetafieldsGlobalTitleTag(final String metafieldsGlobalTitleTag);
}
public static interface ProductTypeStep {
public BodyHtmlStep withProductType(final String productType);
}
public static interface BodyHtmlStep {
public MetafieldsGlobalDescriptionTagStep withBodyHtml(final String bodyHtml);
}
public static interface MetafieldsGlobalDescriptionTagStep {
public VendorStep withMetafieldsGlobalDescriptionTag(final String metafieldsGlobalDescriptionTag);
}
public static interface VendorStep {
public TagsStep withVendor(final String vendor);
}
public static interface TagsStep {
public SortedOptionNamesStep withTags(final Set tags);
}
public static interface SortedOptionNamesStep {
public ImageSourcesStep withSortedOptionNames(final List sortedOptionNames);
}
public static interface ImageSourcesStep {
public VariantCreationRequestsStep withImageSources(final List imageSources);
}
public static interface VariantCreationRequestsStep {
public PublishedStep withVariantCreationRequests(
final List variantCreationRequests);
}
public static interface PublishedStep {
public BuildStep withPublished(final boolean published);
}
public static interface BuildStep {
public ShopifyProductCreationRequest build();
}
public static TitleStep newBuilder() {
return new Steps();
}
@Override
public ShopifyProduct getRequest() {
return request;
}
@Override
public int getVariantImagePosition(final int variantPosition) {
return variantPositionToImagePosition.get(variantPosition);
}
@Override
public boolean hasVariantImagePosition(final int variantPosition) {
return variantPositionToImagePosition.containsKey(variantPosition);
}
@Override
public boolean hasChanged() {
return true;
}
private ShopifyProductCreationRequest(final ShopifyProduct shopifyProduct,
final Map variantPositionToImagePosition) {
this.request = shopifyProduct;
this.variantPositionToImagePosition = variantPositionToImagePosition;
}
private static class Steps implements TitleStep, MetafieldsGlobalTitleTagStep, MetafieldsGlobalDescriptionTagStep,
ProductTypeStep, BodyHtmlStep, VendorStep, TagsStep, SortedOptionNamesStep, ImageSourcesStep,
VariantCreationRequestsStep, PublishedStep, BuildStep {
private final ShopifyProduct shopifyProduct = new ShopifyProduct();
private final Map variantPositionToImagePosition = new HashMap<>();
@Override
public ShopifyProductCreationRequest build() {
return new ShopifyProductCreationRequest(shopifyProduct, variantPositionToImagePosition);
}
@Override
public PublishedStep withVariantCreationRequests(
final List variantCreationRequests) {
final List shopifyVariants = new ArrayList<>(variantCreationRequests.size());
Collections.sort(variantCreationRequests, new ShopifyVariantRequestOption1Comparator());
for (int i = 0; i < variantCreationRequests.size(); i++) {
final ShopifyVariantCreationRequest shopifyVariantCreationRequest = variantCreationRequests.get(i);
final ShopifyVariant shopifyVariant = shopifyVariantCreationRequest.getRequest();
final int position = i + 1;
shopifyVariant.setPosition(position);
if (shopifyVariantCreationRequest.hasImageSource()) {
final String imageSource = shopifyVariantCreationRequest.getImageSource();
shopifyProduct.getImages().stream().filter(image -> image.getSource().equals(imageSource))
.findFirst().ifPresent(image -> {
variantPositionToImagePosition.put(position, image.getPosition());
});
}
shopifyVariants.add(shopifyVariant);
}
shopifyProduct.setVariants(shopifyVariants);
return this;
}
@Override
public VariantCreationRequestsStep withImageSources(final List imageSources) {
final List images = new ArrayList<>(imageSources.size());
int position = 1;
final Iterator imageSourceIterator = imageSources.iterator();
while (imageSourceIterator.hasNext()) {
final String imageSource = imageSourceIterator.next();
final Image image = new Image();
image.setPosition(position);
image.setSource(imageSource);
final List metafields = ImageAltTextCreationRequest.newBuilder()
.withImageAltText(shopifyProduct.getTitle()).build();
image.setMetafields(metafields);
position++;
images.add(image);
}
shopifyProduct.setImages(images);
return this;
}
@Override
public ImageSourcesStep withSortedOptionNames(final List sortedOptionNames) {
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy