1+
12package com .commercetools .sdk ;
23
4+ import java .time .LocalDate ;
5+ import java .time .ZonedDateTime ;
6+ import java .util .*;
7+ import java .util .stream .Collectors ;
8+
39import com .commercetools .api .models .common .CentPrecisionMoney ;
410import com .commercetools .api .models .common .LocalizedString ;
511import com .commercetools .api .models .common .Money ;
1925import com .commercetools .importapi .models .productvariants .BooleanAttribute ;
2026import com .commercetools .importapi .models .productvariants .NumberAttribute ;
2127
22- import java .time .LocalDate ;
23- import java .time .ZonedDateTime ;
24- import java .util .*;
25- import java .util .function .Function ;
26- import java .util .stream .Collectors ;
27-
2828public final class ProductUtil {
2929
3030 public static ProductDraftImport toProductDraftImport (ProductProjection product ) {
31- var draft = ProductDraftImport .builder ().key (product .getKey ())
32- .productType (p -> p .key (product .getProductType ().getObj ().getKey ()))
33- .name (l -> getLocalizedStringBuilder (product .getName ()))
34- .slug (l -> getLocalizedStringBuilder (product .getSlug ()))
35- .description (
36- Optional .ofNullable (product .getDescription ()).map (ProductUtil ::getLocalizedStringBuilder ).map (
37- LocalizedStringBuilder ::build ).orElse (null ))
38- .categories (extractCategoryKeyReference (product ))
39- .metaTitle (
40- Optional .ofNullable (product .getMetaTitle ()).map (ProductUtil ::getLocalizedStringBuilder ).map (
41- LocalizedStringBuilder ::build ).orElse (null ))
42- .metaDescription ((com .commercetools .importapi .models .common .LocalizedString )product .getMetaDescription ())
43- .metaKeywords ((com .commercetools .importapi .models .common .LocalizedString ) product .getMetaKeywords ())
44- .masterVariant (extractProductVariantDraftImport (product .getMasterVariant ()))
45- .variants (extractProductVariantDraftImport (product ))
46- .taxCategory (getTaxCategoryKeyReference (product ))
47- .state (getStateKeyReference (product ))
48- .priceMode (mapPriceModeToImportApi (product ))
49- .attributes (product .getAttributes ().stream ().map (ProductUtil ::mapAttribute ).collect (Collectors .toList ()));
50- return draft .build ();
31+ var draft = ProductDraftImport .builder ()
32+ .key (product .getKey ())
33+ .productType (p -> p .key (product .getProductType ().getObj ().getKey ()))
34+ .name (l -> getLocalizedStringBuilder (product .getName ()))
35+ .slug (l -> getLocalizedStringBuilder (product .getSlug ()))
36+ .description (Optional .ofNullable (product .getDescription ())
37+ .map (ProductUtil ::getLocalizedStringBuilder )
38+ .map (LocalizedStringBuilder ::build )
39+ .orElse (null ))
40+ .categories (extractCategoryKeyReference (product ))
41+ .metaTitle (Optional .ofNullable (product .getMetaTitle ())
42+ .map (ProductUtil ::getLocalizedStringBuilder )
43+ .map (LocalizedStringBuilder ::build )
44+ .orElse (null ))
45+ .metaDescription (
46+ (com .commercetools .importapi .models .common .LocalizedString ) product .getMetaDescription ())
47+ .metaKeywords ((com .commercetools .importapi .models .common .LocalizedString ) product .getMetaKeywords ())
48+ .masterVariant (extractProductVariantDraftImport (product .getMasterVariant ()))
49+ .variants (extractProductVariantDraftImport (product ))
50+ .taxCategory (getTaxCategoryKeyReference (product ))
51+ .state (getStateKeyReference (product ))
52+ .priceMode (mapPriceModeToImportApi (product ))
53+ .attributes (
54+ product .getAttributes ().stream ().map (ProductUtil ::mapAttribute ).collect (Collectors .toList ()));
55+ return draft .build ();
5156 }
5257
53- private static LocalizedStringBuilder getLocalizedStringBuilder (
54- LocalizedString s ) {
58+ private static LocalizedStringBuilder getLocalizedStringBuilder (LocalizedString s ) {
5559 return com .commercetools .importapi .models .common .LocalizedString .builder ().values (s .values ());
5660 }
5761
58- private static com .commercetools .importapi .models .common .ProductPriceModeEnum mapPriceModeToImportApi (ProductProjection product ) {
62+ private static com .commercetools .importapi .models .common .ProductPriceModeEnum mapPriceModeToImportApi (
63+ ProductProjection product ) {
5964 if (product .getPriceMode () == null ) {
6065 return null ;
61- }if (product .getPriceMode ().equals (ProductPriceModeEnum .ProductPriceModeEnumEnum .EMBEDDED )) {
66+ }
67+ if (product .getPriceMode ().equals (ProductPriceModeEnum .ProductPriceModeEnumEnum .EMBEDDED )) {
6268 return com .commercetools .importapi .models .common .ProductPriceModeEnum .ProductPriceModeEnumEnum .EMBEDDED ;
6369 }
6470 return com .commercetools .importapi .models .common .ProductPriceModeEnum .ProductPriceModeEnumEnum .STANDALONE ;
6571 }
6672
67- private static StateKeyReference getStateKeyReference (
68- ProductProjection product ) {
73+ private static StateKeyReference getStateKeyReference (ProductProjection product ) {
6974 var key = Optional .ofNullable (product .getState ()).map (StateReference ::getObj ).map (State ::getKey ).orElse (null );
7075 if (key != null ) {
7176 return StateKeyReference .builder ().key (key ).build ();
7277 }
7378 return null ;
7479 }
7580
76- private static TaxCategoryKeyReference getTaxCategoryKeyReference (
77- ProductProjection product ) {
81+ private static TaxCategoryKeyReference getTaxCategoryKeyReference (ProductProjection product ) {
7882 var key = Optional .ofNullable (product .getTaxCategory ())
7983 .map (TaxCategoryReference ::getObj )
8084 .map (TaxCategory ::getKey )
@@ -86,52 +90,76 @@ private static TaxCategoryKeyReference getTaxCategoryKeyReference(
8690 }
8791
8892 private static List <ProductVariantDraftImport > extractProductVariantDraftImport (ProductProjection product ) {
89- return product .getVariants ().stream ().map (ProductUtil ::extractProductVariantDraftImport ).collect (Collectors .toList ());
93+ return product .getVariants ()
94+ .stream ()
95+ .map (ProductUtil ::extractProductVariantDraftImport )
96+ .collect (Collectors .toList ());
9097 }
9198
9299 private static ProductVariantDraftImport extractProductVariantDraftImport (ProductVariant variant ) {
93- return ProductVariantDraftImport .builder ().key (variant .getKey ()).sku (variant .getSku ())
94- .images (variant .getImages ().stream ().map (i -> com .commercetools .importapi .models .common .Image .builder ()
95- .dimensions (d ->
96- com .commercetools .importapi .models .common .AssetDimensions .builder ().w (i .getDimensions ().getW ()).h (i .getDimensions ().getH ()))
97- .url (i .getUrl ())
98- .label (i .getLabel ())
99- .build ()).collect (Collectors .toList ()))
100+ return ProductVariantDraftImport .builder ()
101+ .key (variant .getKey ())
102+ .sku (variant .getSku ())
103+ .images (variant .getImages ()
104+ .stream ()
105+ .map (i -> com .commercetools .importapi .models .common .Image .builder ()
106+ .dimensions (d -> com .commercetools .importapi .models .common .AssetDimensions .builder ()
107+ .w (i .getDimensions ().getW ())
108+ .h (i .getDimensions ().getH ()))
109+ .url (i .getUrl ())
110+ .label (i .getLabel ())
111+ .build ())
112+ .collect (Collectors .toList ()))
100113 .prices (mapPricesToImportApi (variant ))
101- .attributes (variant . getAttributes (). stream (). map (
102- ProductUtil ::mapAttribute ).collect (Collectors .toList ()))
114+ .attributes (
115+ variant . getAttributes (). stream (). map ( ProductUtil ::mapAttribute ).collect (Collectors .toList ()))
103116 .assets (importAssets (variant .getAssets ()))
104117 .build ();
105118 }
106119
107- private static List <PriceDraftImport > mapPricesToImportApi (ProductVariant variant ){
108- return variant .getPrices ().stream ().map (p -> PriceDraftImport .builder ().key (p .getKey ()).value (v ->
109- (p .getValue () instanceof CentPrecisionMoney ) ?
110- v .centPrecisionBuilder ().centAmount (p .getValue ().getCentAmount ()).currencyCode (p .getValue ()
111- .getCurrencyCode ()).fractionDigits (p .getValue ().getFractionDigits ()) :
112- v .highPrecisionBuilder ().centAmount (p .getValue ().getCentAmount ()).currencyCode (p .getValue ()
113- .getCurrencyCode ()).fractionDigits (p .getValue ().getFractionDigits ())
114- ).build ()).collect (Collectors .toList ());
120+ private static List <PriceDraftImport > mapPricesToImportApi (ProductVariant variant ) {
121+ return variant .getPrices ()
122+ .stream ()
123+ .map (p -> PriceDraftImport .builder ()
124+ .key (p .getKey ())
125+ .value (v -> (p .getValue () instanceof CentPrecisionMoney )
126+ ? v .centPrecisionBuilder ()
127+ .centAmount (p .getValue ().getCentAmount ())
128+ .currencyCode (p .getValue ().getCurrencyCode ())
129+ .fractionDigits (p .getValue ().getFractionDigits ())
130+ : v .highPrecisionBuilder ()
131+ .centAmount (p .getValue ().getCentAmount ())
132+ .currencyCode (p .getValue ().getCurrencyCode ())
133+ .fractionDigits (p .getValue ().getFractionDigits ()))
134+ .build ())
135+ .collect (Collectors .toList ());
115136 }
116137
117- private static List <com .commercetools .importapi .models .common .Asset > importAssets (List <com .commercetools .api .models .common .Asset > assets ){
138+ private static List <com .commercetools .importapi .models .common .Asset > importAssets (
139+ List <com .commercetools .api .models .common .Asset > assets ) {
118140 if (assets == null ) {
119141 return null ;
120142 }
121- return assets .stream ().map (a -> com .commercetools .importapi .models .common .Asset .builder ().key (a .getKey ())
122- .name (getLocalizedStringBuilder (a .getName ()).build ()).build ()).collect (
123- Collectors .toList ());
143+ return assets .stream ()
144+ .map (a -> com .commercetools .importapi .models .common .Asset .builder ()
145+ .key (a .getKey ())
146+ .name (getLocalizedStringBuilder (a .getName ()).build ())
147+ .build ())
148+ .collect (Collectors .toList ());
124149 }
150+
125151 private static List <CategoryKeyReference > extractCategoryKeyReference (ProductProjection product ) {
126- return product .getCategories ().stream ()
127- .map (c -> CategoryKeyReference .builder ().key (c .getObj ().getKey ()).build ()).collect (Collectors .toList ());
152+ return product .getCategories ()
153+ .stream ()
154+ .map (c -> CategoryKeyReference .builder ().key (c .getObj ().getKey ()).build ())
155+ .collect (Collectors .toList ());
128156 }
129157
130158 private static Attribute mapAttribute (com .commercetools .api .models .product .Attribute attribute ) {
131159 Object value = attribute .getValue ();
132160
133161 if (value instanceof String ) {
134- return Attribute .textBuilder ().name (attribute .getName ()).value ((String ) value ).build ();
162+ return Attribute .textBuilder ().name (attribute .getName ()).value ((String ) value ).build ();
135163 }
136164 if (value instanceof Integer ) {
137165 return Attribute .numberBuilder ().name (attribute .getName ()).value (((Integer ) value ).doubleValue ()).build ();
@@ -146,14 +174,22 @@ private static Attribute mapAttribute(com.commercetools.api.models.product.Attri
146174 return Attribute .numberBuilder ().name (attribute .getName ()).value (((Long ) value ).doubleValue ()).build ();
147175 }
148176 if (value instanceof LocalizedString ) {
149- return Attribute .ltextBuilder ().name (attribute .getName ()).value (
150- getLocalizedStringBuilder (((LocalizedString ) value )).build ()).build ();
177+ return Attribute .ltextBuilder ()
178+ .name (attribute .getName ())
179+ .value (getLocalizedStringBuilder (((LocalizedString ) value )).build ())
180+ .build ();
151181 }
152182 if (value instanceof AttributePlainEnumValue ) {
153- return Attribute .enumBuilder ().name (attribute .getName ()).value (((AttributePlainEnumValue ) value ).getKey ()).build ();
183+ return Attribute .enumBuilder ()
184+ .name (attribute .getName ())
185+ .value (((AttributePlainEnumValue ) value ).getKey ())
186+ .build ();
154187 }
155188 if (value instanceof AttributeLocalizedEnumValue ) {
156- return Attribute .enumBuilder ().name (attribute .getName ()).value (((AttributeLocalizedEnumValue ) value ).getKey ()).build ();
189+ return Attribute .enumBuilder ()
190+ .name (attribute .getName ())
191+ .value (((AttributeLocalizedEnumValue ) value ).getKey ())
192+ .build ();
157193 }
158194 if (value instanceof Money ) {
159195 return Attribute .moneyBuilder ().name (attribute .getName ()).value ((TypedMoney ) value ).build ();
@@ -171,8 +207,10 @@ private static Attribute mapAttribute(com.commercetools.api.models.product.Attri
171207 return Attribute .referenceBuilder ().name (attribute .getName ()).value ((KeyReference ) value ).build ();
172208 }
173209 if (value instanceof AttributeConstraintEnum ) {
174- return Attribute .enumBuilder ().name (attribute .getName ()).value (
175- String .valueOf ((AttributeConstraintEnum ) value )).build ();
210+ return Attribute .enumBuilder ()
211+ .name (attribute .getName ())
212+ .value (String .valueOf ((AttributeConstraintEnum ) value ))
213+ .build ();
176214 }
177215 if (value instanceof AttributeSetType ) {
178216 var elementType = ((AttributeSetType ) value ).getElementType ();
0 commit comments