io.sphere.sdk.types.Custom Maven / Gradle / Ivy
package io.sphere.sdk.types;
import io.sphere.sdk.categories.Category;
import io.sphere.sdk.categories.commands.updateactions.SetCustomType;
import javax.annotation.Nullable;
/**
Interface to mark objects which can have custom fields, it has nothing to do with CustomObjects.
To use custom fields of a {@link Custom} resource it is necessary to connect them to an existing {@link Type}, {@link Type}s define the field names and types like {@link String}, {@link javax.money.MonetaryAmount} and {@link Long}.
In the example scenario we create a type for categories.
The example type will contain:
- an enum field "{@code state}" to indicate the state of of the category like "published" and "draft"
- a String field "{@code imageUrl}" to show an icon for the category
- a set field with category references "{@code relatedCategories}" to suggest other categories with accessoires for example
If you work with types it is a good idea to import {@code io.sphere.sdk.types.*;} so you don't get confused with
classes from product attributes which have sometimes the same class name but are in another package.
Create a type
A type can be assigned to different kinds of objects, e.g., a type can be used for categories, orders and carts.
In the type creation the property {@link TypeDraft#getResourceTypeIds()} contains a set of resourceTypeIds which can be
find on the class of the object like in {@link Category#resourceTypeId()} for categories. Be careful to not confuse this with
{@link Category#referenceTypeId()} which contains the id for {@link io.sphere.sdk.models.Reference}s. Cart and order have the same
resourceTypeId so the type can be automatically be used for {@link io.sphere.sdk.orders.Order} and {@link io.sphere.sdk.carts.Cart} if it is for one of them enabled.
Execution example:
{@include.example io.sphere.sdk.types.CreateTypeDemo}
Assign a type to an object at the creation of the object
{@include.example io.sphere.sdk.types.CreateCategoryWithTypeDemo}
Assign a type to an object in an update action
It is not necessary to assign a type to the object at it's creation. You can retrofit objects without a type by using update actions like {@link SetCustomType SetCustomType for categories}.
{@include.example io.sphere.sdk.types.TypeAssigningInUpdateActionDemo}
Update a field
By using update actions you can overwrite or erase the value of custom field.
{@include.example io.sphere.sdk.types.UpdateFieldValueDemo}
Unassign a type
{@include.example io.sphere.sdk.types.RemoveTypeFromObjectDemo}
Update a type
Have a look at {@link io.sphere.sdk.types.commands.TypeUpdateCommand}.
Example for updating the description:
{@include.example io.sphere.sdk.types.commands.TypeUpdateCommandTest#setDescription()}
Delete a type
See {@link io.sphere.sdk.types.commands.TypeDeleteCommand}.
*/
public interface Custom {
@Nullable
CustomFields getCustom();
}