Skip to content

Commit

Permalink
Added docs exxample for product integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
KSGRelewise committed Sep 19, 2024
1 parent 499c6e1 commit eb32e04
Showing 1 changed file with 169 additions and 1 deletion.
170 changes: 169 additions & 1 deletion src/src/test/java/com/relewise/client/TrackerTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.relewise.client;

import com.relewise.client.factory.DataValueFactory;
import com.relewise.client.factory.MultilingualCollectionValue;
import com.relewise.client.factory.UserFactory;
import com.relewise.client.model.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;

public class TrackerTest extends TestBase {
Expand All @@ -25,8 +28,173 @@ public void testProductView() throws Exception {
assertDoesNotThrow(action);
}

private void testDocsSampleProductUpdateWithVariant() throws Exception {
var tracker = new Tracker(GetDatasetId(), GetApiKey(), "https://api.relewise.com");

// Create a timestamp to distinguish active and inactive entities
// Read more at
// https://docs.relewise.com/docs/developer/bestpractices/product-integration.html
long importedAtTimestamp = System.currentTimeMillis();

// Language can be any string, and doesn't have to be a valid iso-standard.
var english = Language.create("en");
var dkk = Currency.create("DKK");

var updates = new ArrayList<Trackable>();

// Foreach product that needs to be imported, do the following:
{
// The Id should be the primary id of the product
var product = Product.create("Product-SKU-01");

// We only set the English translation in this example
// but more can be set by parsing more MultilingualValue
// to the Multilingual.create method.
product.setDisplayName(
Multilingual.create(
MultilingualValue.create(english, "The english display name")
)
);

product.setSalesPrice(MultiCurrency.create(Money.create(dkk, 199.0)));
product.setListPrice(MultiCurrency.create(Money.create(dkk, 199.0)));

product.setBrand(
// Displayname can be left out, but Id is required
Brand.create("brandId")
.setDisplayName("Brand display name")
);

// We only set the English translation in this example
// but more can be set by parsing more MultilingualValue
// to the Multilingual.create method.
product.addToData("ShortDescription", DataValueFactory.create(
Multilingual.create(
MultilingualValue.create(english,"The short english description")
)
));

product.addToCategoryPaths(CategoryPath.create(
CategoryNameAndId.create("74", Multilingual.create(
MultilingualValue.create(english, "Play")
)),
CategoryNameAndId.create("2", Multilingual.create(
MultilingualValue.create(english, "Swings")
)),
CategoryNameAndId.create("529", Multilingual.create(
MultilingualValue.create(english, "Swing Seats")
))
));

product.addToData("ImportedAt", DataValueFactory.create(importedAtTimestamp));
product.addToData("MinimumAge", DataValueFactory.create(4));
product.addToData("InStock", DataValueFactory.create(true));
product.addToData("USPs", DataValueFactory.create("first usp", "second usp", "third usp"));
// Add any additional fields you would want returned from Relewise

var variants = new ArrayList<ProductVariant>();

// Foreach variant of this product
{
var variant = ProductVariant.create("The variant id");

variant.addToData("Materials", DataValueFactory.create(
new MultilingualCollectionValue[] {
new MultilingualCollectionValue(
english, new String[] { "Wood", "Metal" }
)
}
));

variant.addToData("Colors", DataValueFactory.create(
new MultilingualCollectionValue[] {
new MultilingualCollectionValue(
english, new String[] { "Red", "Green" }
)
}
));

variant.addToData("PrimaryMaterial", DataValueFactory.create(
Multilingual.create(MultilingualValue.create(english, "Wood")))
);
variant.addToData("PrimaryColor", DataValueFactory.create(
Multilingual.create(MultilingualValue.create(english, "Red")))
);

// Add any additional fields you would want returned from Relewise

variants.add(variant);
}

var productUpdate = ProductUpdate.create(
product, ProductUpdateUpdateKind.ReplaceProvidedProperties
);
productUpdate.setVariants(variants.toArray(new ProductVariant[variants.size()]));
productUpdate.setVariantUpdateKind(ProductUpdateUpdateKind.ReplaceProvidedProperties);
productUpdate.setReplaceExistingVariants(true);
// Replace existing variants = true will delete all variants in Relewise
// (for the listed products) not included in this update request.

updates.add(productUpdate);
}
// Foreach product END

// If this is a full-import (not delta), disable all non-included products
{
// Setting this to false will make it disable all products that don't have the "ImportedAt" key.
boolean onlyDisableOldProductsThatHaveTheImportedAtKey = true;

boolean negated = true;
var nonUpdatedProductFilter = FilterCollection.create(
ProductDataFilter.create(
"ImportedAt",
ValueConditionCollection.create().setItems(
EqualsCondition.create(DataValueFactory.create(importedAtTimestamp), negated)
),
true,
onlyDisableOldProductsThatHaveTheImportedAtKey,
null,
null)
);

var disableProductsWithoutNewestTimestamp = ProductAdministrativeAction.create(
Language.UNDEFINED,
Currency.UNDEFINED,
nonUpdatedProductFilter,
ProductAdministrativeActionUpdateKind.Disable,
ProductAdministrativeActionUpdateKind.Disable
);

updates.add(disableProductsWithoutNewestTimestamp);
}

boolean negated = false;
var updatedProductFilter = FilterCollection.create(
ProductDataFilter.create(
"ImportedAt",
ValueConditionCollection.create().setItems(
EqualsCondition.create(DataValueFactory.create(importedAtTimestamp), negated)
),
true,
true,
null,
null)
);
var enabledProductsWithNewestTimestamp = ProductAdministrativeAction.create(
Language.UNDEFINED,
Currency.UNDEFINED,
updatedProductFilter,
ProductAdministrativeActionUpdateKind.Enable,
ProductAdministrativeActionUpdateKind.Enable
);

updates.add(enabledProductsWithNewestTimestamp);

tracker.track(BatchedTrackingRequest.create(updates.toArray(new Trackable[updates.size()])));
}

@Test
public void testProductUpdateWithVariant() throws Exception {
public void testCompactProductUpdateWithVariant() throws Exception {
// Create Product by tracking it.
var tracker = new Tracker(GetDatasetId(), GetApiKey(), "https://api.relewise.com");

Expand Down

0 comments on commit eb32e04

Please sign in to comment.