11package com .commercetools .sync .producttypes .utils ;
22
3+ import com .commercetools .sync .commons .exceptions .DifferentTypeException ;
4+ import com .commercetools .sync .commons .exceptions .DuplicateKeyException ;
35import io .sphere .sdk .commands .UpdateAction ;
46import io .sphere .sdk .models .LocalizedString ;
57import io .sphere .sdk .products .attributes .AttributeDefinition ;
68import io .sphere .sdk .products .attributes .AttributeDefinitionDraft ;
9+ import io .sphere .sdk .products .attributes .EnumAttributeType ;
10+ import io .sphere .sdk .products .attributes .LocalizedEnumAttributeType ;
711import io .sphere .sdk .producttypes .ProductType ;
812import io .sphere .sdk .producttypes .commands .updateactions .ChangeAttributeDefinitionLabel ;
913import io .sphere .sdk .producttypes .commands .updateactions .SetInputTip ;
1721import java .util .stream .Stream ;
1822
1923import static com .commercetools .sync .commons .utils .CommonTypeUpdateActionUtils .buildUpdateAction ;
24+ import static com .commercetools .sync .producttypes .utils .ProductTypeUpdateLocalizedEnumActionUtils .buildLocalizedEnumValuesUpdateActions ;
25+ import static com .commercetools .sync .producttypes .utils .ProductTypeUpdatePlainEnumActionUtils .buildEnumValuesUpdateActions ;
2026import static java .util .stream .Collectors .toList ;
27+ import static java .lang .String .format ;
2128
2229
2330public final class AttributeDefinitionUpdateActionUtils {
@@ -29,24 +36,92 @@ public final class AttributeDefinitionUpdateActionUtils {
2936 *
3037 * @param oldAttributeDefinition the attribute definition which should be updated.
3138 * @param newAttributeDefinitionDraft the attribute definition draft where we get the new fields.
39+ *
3240 * @return A list with the update actions or an empty list if the attribute definition fields are identical.
3341 */
3442 @ Nonnull
3543 public static List <UpdateAction <ProductType >> buildActions (
3644 @ Nonnull final AttributeDefinition oldAttributeDefinition ,
37- @ Nonnull final AttributeDefinitionDraft newAttributeDefinitionDraft ) {
38-
39- return Stream
40- .of (
41- buildChangeLabelUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
42- buildSetInputTipUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
43- buildChangeIsSearchableUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
44- buildChangeInputHintUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
45- buildChangeAttributeConstraintUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft )
46- )
47- .filter (Optional ::isPresent )
48- .map (Optional ::get )
49- .collect (toList ());
45+ @ Nonnull final AttributeDefinitionDraft newAttributeDefinitionDraft )
46+ throws DuplicateKeyException , DifferentTypeException {
47+
48+ final List <UpdateAction <ProductType >> updateActions ;
49+
50+ if (haveSameAttributeType (oldAttributeDefinition , newAttributeDefinitionDraft )) {
51+ updateActions = Stream
52+ .of (
53+ buildChangeLabelUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
54+ buildSetInputTipUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
55+ buildChangeIsSearchableUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
56+ buildChangeInputHintUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft ),
57+ buildChangeAttributeConstraintUpdateAction (oldAttributeDefinition , newAttributeDefinitionDraft )
58+ )
59+ .filter (Optional ::isPresent )
60+ .map (Optional ::get )
61+ .collect (toList ());
62+
63+ if (isPlainEnumAttribute (oldAttributeDefinition )) {
64+ updateActions .addAll (buildEnumValuesUpdateActions (
65+ oldAttributeDefinition .getName (),
66+ ((EnumAttributeType ) oldAttributeDefinition .getAttributeType ()).getValues (),
67+ ((EnumAttributeType ) newAttributeDefinitionDraft .getAttributeType ()).getValues ()
68+ ));
69+ } else if (isLocalizedEnumAttribute (oldAttributeDefinition )) {
70+ updateActions .addAll (buildLocalizedEnumValuesUpdateActions (
71+ oldAttributeDefinition .getName (),
72+ ((LocalizedEnumAttributeType ) oldAttributeDefinition .getAttributeType ()).getValues (),
73+ ((LocalizedEnumAttributeType ) newAttributeDefinitionDraft .getAttributeType ()).getValues ()
74+ ));
75+ }
76+ } else {
77+ throw new DifferentTypeException (format ("The attribute type of the attribute definitions are different. "
78+ + "Attribute name: '%s'. Old attribute type: '%s', new attribute type: '%s'. "
79+ + "Attribute type has to remain the same for the same attribute name." ,
80+ oldAttributeDefinition .getName (),
81+ oldAttributeDefinition .getAttributeType ().getClass ().getName (),
82+ newAttributeDefinitionDraft .getAttributeType ().getClass ().getName ()));
83+ }
84+
85+
86+ return updateActions ;
87+ }
88+
89+ /**
90+ * Compares the attribute types of the {@code attributeDefinitionA} and the {@code attributeDefinitionB} and
91+ * returns true if both attribute definitions have the same attribute type, false otherwise.
92+ *
93+ * @param attributeDefinitionA the first attribute definition to compare.
94+ * @param attributeDefinitionB the second attribute definition to compare.
95+ *
96+ * @return true if both attribute definitions have the same attribute type, false otherwise.
97+ */
98+ private static boolean haveSameAttributeType (
99+ @ Nonnull final AttributeDefinition attributeDefinitionA ,
100+ @ Nonnull final AttributeDefinitionDraft attributeDefinitionB ) {
101+
102+ return attributeDefinitionA .getAttributeType ().getClass () == attributeDefinitionB .getAttributeType ().getClass ();
103+ }
104+
105+ /**
106+ * Indicates if the attribute definition is a plain enum value or not.
107+ *
108+ * @param attributeDefiniton the attribute definition.
109+ *
110+ * @return true if the attribute definition is a plain enum value, false otherwise.
111+ */
112+ private static boolean isPlainEnumAttribute (@ Nonnull final AttributeDefinition attributeDefiniton ) {
113+ return attributeDefiniton .getAttributeType ().getClass () == EnumAttributeType .class ;
114+ }
115+
116+ /**
117+ * Indicates if the attribute definition is a localized enum value or not.
118+ *
119+ * @param attributeDefiniton the attribute definition.
120+ *
121+ * @return true if the attribute definition is a localized enum value, false otherwise.
122+ */
123+ private static boolean isLocalizedEnumAttribute (@ Nonnull final AttributeDefinition attributeDefiniton ) {
124+ return attributeDefiniton .getAttributeType ().getClass () == LocalizedEnumAttributeType .class ;
50125 }
51126
52127 /**
0 commit comments