33import com .commercetools .api .models .common .CentPrecisionMoney ;
44import com .commercetools .api .models .common .LocalizedString ;
55import com .commercetools .api .models .common .Money ;
6+ import com .commercetools .api .models .product .ProductPriceModeEnum ;
67import com .commercetools .api .models .product .ProductProjection ;
78import com .commercetools .api .models .product .ProductVariant ;
89import com .commercetools .api .models .product_type .*;
10+ import com .commercetools .api .models .state .State ;
11+ import com .commercetools .api .models .state .StateReference ;
12+ import com .commercetools .api .models .tax_category .TaxCategory ;
13+ import com .commercetools .api .models .tax_category .TaxCategoryReference ;
914import com .commercetools .importapi .models .common .*;
1015import com .commercetools .importapi .models .productdrafts .PriceDraftImport ;
1116import com .commercetools .importapi .models .productdrafts .ProductDraftImport ;
1722import java .time .LocalDate ;
1823import java .time .ZonedDateTime ;
1924import java .util .*;
25+ import java .util .function .Function ;
2026import java .util .stream .Collectors ;
2127
2228public final class ProductUtil {
2329
2430 public static ProductDraftImport toProductDraftImport (ProductProjection product ) {
2531 var draft = ProductDraftImport .builder ().key (product .getKey ())
26- .productType (p -> p .key (Optional . ofNullable ( product .getProductType ())
27- . map ( ProductTypeReference :: getObj ). map ( ProductType :: getKey ). orElse ( null )))
28- .name (l -> l . values (product .getName (). values ()))
29- .slug ( l -> l . values ( product . getSlug (). values ()))
30- . description ( d -> d . values (
31- Optional . ofNullable ( product . getDescription ()). map ( LocalizedString :: values ).orElse (null ))) // if not null
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 ))
3238 .categories (extractCategoryKeyReference (product ))
33- .metaTitle (t -> t .values (
34- Optional .ofNullable (product .getMetaTitle ()).map (LocalizedString ::values ).orElse (null )))
35- .metaDescription ((com .commercetools .importapi .models .common .LocalizedString )product .getMetaDescription ()) // if not null
36- .metaKeywords ((com .commercetools .importapi .models .common .LocalizedString ) product .getMetaKeywords ()) // if not null
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 ())
3744 .masterVariant (extractProductVariantDraftImport (product .getMasterVariant ()))
3845 .variants (extractProductVariantDraftImport (product ))
39- .taxCategory (t -> t . key (product . getTaxCategory (). getObj (). getKey () ))
40- .state (s -> s . key (product . getState (). getObj (). getKey () ))
41- // .priceMode(product.getPriceMode( ))
46+ .taxCategory (getTaxCategoryKeyReference (product ))
47+ .state (getStateKeyReference (product ))
48+ .priceMode (mapPriceModeToImportApi ( product ))
4249 .attributes (product .getAttributes ().stream ().map (ProductUtil ::mapAttribute ).collect (Collectors .toList ()));
4350 return draft .build ();
4451 }
4552
53+ private static LocalizedStringBuilder getLocalizedStringBuilder (
54+ LocalizedString s ) {
55+ return com .commercetools .importapi .models .common .LocalizedString .builder ().values (s .values ());
56+ }
57+
58+ private static com .commercetools .importapi .models .common .ProductPriceModeEnum mapPriceModeToImportApi (ProductProjection product ) {
59+ if (product .getPriceMode () == null ) {
60+ return null ;
61+ }if (product .getPriceMode ().equals (ProductPriceModeEnum .ProductPriceModeEnumEnum .EMBEDDED )) {
62+ return com .commercetools .importapi .models .common .ProductPriceModeEnum .ProductPriceModeEnumEnum .EMBEDDED ;
63+ }
64+ return com .commercetools .importapi .models .common .ProductPriceModeEnum .ProductPriceModeEnumEnum .STANDALONE ;
65+ }
66+
67+ private static StateKeyReference getStateKeyReference (
68+ ProductProjection product ) {
69+ var key = Optional .ofNullable (product .getState ()).map (StateReference ::getObj ).map (State ::getKey ).orElse (null );
70+ if (key != null ) {
71+ return StateKeyReference .builder ().key (key ).build ();
72+ }
73+ return null ;
74+ }
75+
76+ private static TaxCategoryKeyReference getTaxCategoryKeyReference (
77+ ProductProjection product ) {
78+ var key = Optional .ofNullable (product .getTaxCategory ())
79+ .map (TaxCategoryReference ::getObj )
80+ .map (TaxCategory ::getKey )
81+ .orElse (null );
82+ if (key != null ) {
83+ return TaxCategoryKeyReference .builder ().key (key ).build ();
84+ }
85+ return null ;
86+ }
87+
4688 private static List <ProductVariantDraftImport > extractProductVariantDraftImport (ProductProjection product ) {
4789 return product .getVariants ().stream ().map (ProductUtil ::extractProductVariantDraftImport ).collect (Collectors .toList ());
4890 }
4991
5092 private static ProductVariantDraftImport extractProductVariantDraftImport (ProductVariant variant ) {
5193 return ProductVariantDraftImport .builder ().key (variant .getKey ()).sku (variant .getSku ())
52- .setImages (i -> (Image ) variant .getImages ())
53- .prices (variant .getPrices ().stream ().map (p -> PriceDraftImport .builder ().key (p .getKey ()).value (v ->
54- (p .getValue () instanceof CentPrecisionMoney ) ?
55- v .centPrecisionBuilder ().centAmount (p .getValue ().getCentAmount ()).currencyCode (p .getValue ()
56- .getCurrencyCode ()).fractionDigits (p .getValue ().getFractionDigits ()) :
57- v .highPrecisionBuilder ().centAmount (p .getValue ().getCentAmount ()).currencyCode (p .getValue ()
58- .getCurrencyCode ()).fractionDigits (p .getValue ().getFractionDigits ())
59- ).build ()).collect (Collectors .toList ()))
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+ .prices (mapPricesToImportApi (variant ))
60101 .attributes (variant .getAttributes ().stream ().map (
61102 ProductUtil ::mapAttribute ).collect (Collectors .toList ()))
62- .assets (importAssets (variant .getAssets ())).build ();
103+ .assets (importAssets (variant .getAssets ()))
104+ .build ();
105+ }
106+
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 ());
63115 }
64116
65117 private static List <com .commercetools .importapi .models .common .Asset > importAssets (List <com .commercetools .api .models .common .Asset > assets ){
118+ if (assets == null ) {
119+ return null ;
120+ }
66121 return assets .stream ().map (a -> com .commercetools .importapi .models .common .Asset .builder ().key (a .getKey ())
67- .name (( com . commercetools . importapi . models . common . LocalizedString ) a .getName ()).build ()).collect (
122+ .name (getLocalizedStringBuilder ( a .getName ()). build ()).build ()).collect (
68123 Collectors .toList ());
69124 }
70- private static CategoryKeyReference extractCategoryKeyReference (ProductProjection product ) {
71- return CategoryKeyReference .builder ().key (product .getCategories ().get (0 ).getObj ().getKey ()).build ();
125+ 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 ());
72128 }
73129
74130 private static Attribute mapAttribute (com .commercetools .api .models .product .Attribute attribute ) {
@@ -90,7 +146,8 @@ private static Attribute mapAttribute(com.commercetools.api.models.product.Attri
90146 return Attribute .numberBuilder ().name (attribute .getName ()).value (((Long ) value ).doubleValue ()).build ();
91147 }
92148 if (value instanceof LocalizedString ) {
93- return Attribute .ltextBuilder ().name (attribute .getName ()).value (l -> l .values (((LocalizedString ) value ).values ())).build ();
149+ return Attribute .ltextBuilder ().name (attribute .getName ()).value (
150+ getLocalizedStringBuilder (((LocalizedString ) value )).build ()).build ();
94151 }
95152 if (value instanceof AttributePlainEnumValue ) {
96153 return Attribute .enumBuilder ().name (attribute .getName ()).value (((AttributePlainEnumValue ) value ).getKey ()).build ();
0 commit comments