Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ZEE-6771] Add LabelIdentifier to Inventory #73

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/zeenea/connector/common/ItemIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/**
* Represents an identifier for an item, consisting of a list of identification properties.
*
* <p>This ItemIdentifier must be unique for each item in the Zeenea Data Catalog.
*
* <pre>Example : <br>
* {
* "identificationProperties": [
Expand Down
98 changes: 73 additions & 25 deletions src/main/java/zeenea/connector/common/ItemInventory.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
package zeenea.connector.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import zeenea.connector.exception.ExceptionUtils;

/**
* Represents an inventory of items identified by an ItemIdentifier and associated with a label
* path.
* Represents an inventory item, identified by an ItemIdentifier and associated with a
* LabelIdentifier.
*/
public final class ItemInventory {
/** The identifier for the item. */
@NotNull private final ItemIdentifier itemIdentifier;

/**
* The list of labels associated with the item.
*
* <p>This label list will generate import hierarchy in Zeenea Studio import modal view.
*
* <p>It must be not empty to display importable items in the Studio.
*/
@NotNull private final List<String> labels;
/** The label identifier for the item. */
@NotNull private final LabelIdentifier labelIdentifier;

/**
* Constructs an ItemInventory instance using the provided builder.
*
* @param builder the builder used to create the ItemInventory instance
*/
private ItemInventory(Builder builder) {
ExceptionUtils.requireNonNullOrEmpty("labels", builder.labels);
this.itemIdentifier = Objects.requireNonNull(builder.itemIdentifier, "itemIdentifier");
this.labels = List.copyOf(builder.labels);
this.labelIdentifier = Objects.requireNonNull(builder.labelIdentifier, "labelIdentifier");
}

/**
Expand Down Expand Up @@ -61,7 +55,18 @@ public static ItemInventory of(
* @return the list of labels associated with the item
*/
public @NotNull List<String> getLabels() {
return labels;
return labelIdentifier.getIdentificationProperties().stream()
.map(IdentificationProperty::getValue)
.collect(Collectors.toUnmodifiableList());
}

/**
* Gets the label identifier associated with the item.
*
* @return the label identifier associated with the item
*/
public @NotNull LabelIdentifier getLabelIdentifier() {
return labelIdentifier;
}

/**
Expand All @@ -76,7 +81,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
ItemInventory that = (ItemInventory) o;
return Objects.equals(itemIdentifier, that.itemIdentifier)
&& Objects.equals(labels, that.labels);
&& Objects.equals(labelIdentifier, that.labelIdentifier);
}

/**
Expand All @@ -86,7 +91,7 @@ public boolean equals(Object o) {
*/
@Override
public int hashCode() {
return Objects.hash(itemIdentifier, labels);
return Objects.hash(itemIdentifier, labelIdentifier);
}

/**
Expand All @@ -96,7 +101,12 @@ public int hashCode() {
*/
@Override
public String toString() {
return "ItemInventory{" + "itemIdentifier=" + itemIdentifier + ", labels=" + labels + "}";
return "ItemInventory{"
+ "itemIdentifier="
+ itemIdentifier
+ ", labelIdentifier="
+ labelIdentifier
+ "}";
}

/**
Expand All @@ -114,8 +124,8 @@ public static class Builder {
/** The identifier for the item. */
private ItemIdentifier itemIdentifier;

/** The path of labels associated with the item. */
private List<String> labels = new ArrayList<>();
/** The label identifier for the item. */
private LabelIdentifier labelIdentifier;

/**
* Sets the item identifier for the builder.
Expand All @@ -131,11 +141,11 @@ public Builder itemIdentifier(@NotNull ItemIdentifier itemIdentifier) {
/**
* Sets the item identifier for the builder.
*
* @param itemIdentifier the identifier for the item
* @param identificationProperty the identifier for the item
* @return the builder instance
*/
public Builder itemIdentifier(IdentificationProperty itemIdentifier) {
this.itemIdentifier = ItemIdentifier.of(itemIdentifier);
public Builder itemIdentifier(IdentificationProperty identificationProperty) {
this.itemIdentifier = ItemIdentifier.of(identificationProperty);
return this;
}

Expand All @@ -144,9 +154,17 @@ public Builder itemIdentifier(IdentificationProperty itemIdentifier) {
*
* @param labels the list of labels to add
* @return the builder instance
* @deprecated since 2.3.0, use labelIdentifier instead
*/
@Deprecated(since = "2.3.0", forRemoval = true)
public Builder labels(@NotNull List<String> labels) {
this.labels = List.copyOf(labels);
AtomicInteger index = new AtomicInteger(1);
this.labelIdentifier =
LabelIdentifier.of(
labels.stream()
.map(
label -> IdentificationProperty.of("label_" + index.getAndIncrement(), label))
.collect(Collectors.toUnmodifiableList()));
return this;
}

Expand All @@ -155,9 +173,39 @@ public Builder labels(@NotNull List<String> labels) {
*
* @param labels the list of labels to add
* @return the builder instance
* @deprecated since 2.3.0, use labelIdentifier instead
*/
@Deprecated(since = "2.3.0", forRemoval = true)
public Builder labels(String... labels) {
this.labels = List.of(labels);
AtomicInteger index = new AtomicInteger(1);
this.labelIdentifier =
LabelIdentifier.of(
Stream.of(labels)
.map(
label -> IdentificationProperty.of("label_" + index.getAndIncrement(), label))
.collect(Collectors.toUnmodifiableList()));
return this;
}

/**
* Sets the label identifier for the builder.
*
* @param labelIdentifier the labels for the item
* @return the builder instance
*/
public Builder labelIdentifier(@NotNull LabelIdentifier labelIdentifier) {
this.labelIdentifier = labelIdentifier;
return this;
}

/**
* Sets the label identifier for the builder.
*
* @param identificationProperty the labels for the item
* @return the builder instance
*/
public Builder labelIdentifier(IdentificationProperty identificationProperty) {
this.labelIdentifier = LabelIdentifier.of(identificationProperty);
return this;
}

Expand Down
186 changes: 186 additions & 0 deletions src/main/java/zeenea/connector/common/LabelIdentifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package zeenea.connector.common;

import java.util.*;
import org.jetbrains.annotations.NotNull;
import zeenea.connector.exception.ExceptionUtils;

/**
* Represents an ordered list of labels for an item, consisting of a list of identification
* properties.
*
* <p>LabelIdentifier don't have to be unique for each item in the Zeenea Data Catalog.
*
* <p>This label list will generate import hierarchy in Zeenea Studio import modal view with values.
* It must be not empty to display importable items in the Studio.
*
* <pre>Example : <br>
* {
* "identificationProperties": [
* { "key": "workspace_name", "value": "my_workspace" },
* { "key": "report_name", "value": "my_report" },
* ]
* }
* </pre>
*/
public final class LabelIdentifier {

/** The list of label identification properties. */
@NotNull private final List<IdentificationProperty> identificationProperties;

/**
* Private constructor to enforce the use of the builder.
*
* @param builder the builder used to create the LabelIdentifier instance
*/
private LabelIdentifier(Builder builder) {
ExceptionUtils.requireNonNullOrEmpty(
"identificationProperties", builder.identificationProperties);
this.identificationProperties = List.copyOf(builder.identificationProperties);
}

/**
* Creates a new LabelIdentifier instance with the specified list of identification properties.
*
* @param identificationProperties the list of identification properties
* @return a new LabelIdentifier instance
*/
public static LabelIdentifier of(@NotNull List<IdentificationProperty> identificationProperties) {
return new Builder().identificationProperties(identificationProperties).build();
}

/**
* Creates a new LabelIdentifier instance with the specified list of identification properties.
*
* @param identificationProperties the list of identification properties
* @return a new LabelIdentifier instance
*/
public static LabelIdentifier of(IdentificationProperty... identificationProperties) {
return of(List.of(identificationProperties));
}

/**
* Gets the list of identification properties.
*
* @return the list of identification properties
*/
public @NotNull List<IdentificationProperty> getIdentificationProperties() {
return identificationProperties;
}

/**
* Creates a new LabelIdentifier with the specified identification property added as a prefix.
*
* @param identificationProperty the identification property to add as a prefix
* @return a new LabelIdentifier instance with the specified prefix
*/
public LabelIdentifier withPrefix(IdentificationProperty identificationProperty) {
var idEntries = new ArrayList<>(Collections.singletonList(identificationProperty));
idEntries.addAll(this.identificationProperties);
return LabelIdentifier.of(idEntries);
}

/**
* Creates a new LabelIdentifier with the specified identification property added as a prefix.
*
* @param key the key of the identification property.
* @param value the value of the identification property.
* @return a new LabelIdentifier instance with the specified prefix
*/
public LabelIdentifier withPrefix(String key, String value) {
return withPrefix(IdentificationProperty.of(key, value));
}

/**
* Creates a new LabelIdentifier with the specified identification property added as a suffix.
*
* @param identificationProperty the identification property to add as a suffix
* @return a new LabelIdentifier instance with the specified suffix
*/
public LabelIdentifier withSuffix(IdentificationProperty identificationProperty) {
var idEntries = new ArrayList<>(this.identificationProperties);
idEntries.add(identificationProperty);
return LabelIdentifier.of(idEntries);
}

/**
* Creates a new LabelIdentifier with the specified identification property added as a suffix.
*
* @param key the key of the identification property
* @param value the value of the identification property
* @return a new LabelIdentifier instance with the specified suffix
*/
public LabelIdentifier withSuffix(String key, String value) {
return withSuffix(IdentificationProperty.of(key, value));
}

/**
* Creates a new builder for the LabelIdentifier class.
*
* @return a new Builder instance
*/
public static Builder builder() {
return new Builder();
}

/**
* Checks if this LabelIdentifier is equal to another object.
*
* @param o the object to compare with
* @return true if this LabelIdentifier is equal to the specified object, otherwise false
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LabelIdentifier that = (LabelIdentifier) o;
return Objects.equals(identificationProperties, that.identificationProperties);
}

/**
* Computes the hash code for this LabelIdentifier.
*
* @return the hash code of this LabelIdentifier
*/
@Override
public int hashCode() {
return Objects.hashCode(identificationProperties);
}

/**
* Returns a string representation of this LabelIdentifier.
*
* @return a string representation of this LabelIdentifier
*/
@Override
public String toString() {
return "LabelIdentifier{" + "identificationProperties=" + identificationProperties + "}";
}

/** Builder class for creating instances of LabelIdentifier. */
public static class Builder {

/** The list of identification properties. */
private List<IdentificationProperty> identificationProperties = new ArrayList<>();

/**
* Set a collection of identification properties to the builder.
*
* @param identificationProperties the collection of identification properties to add
* @return the builder instance
*/
public Builder identificationProperties(
@NotNull Collection<IdentificationProperty> identificationProperties) {
this.identificationProperties = List.copyOf(identificationProperties);
return this;
}

/**
* Builds and returns an LabelIdentifier instance.
*
* @return the created LabelIdentifier instance
*/
public LabelIdentifier build() {
return new LabelIdentifier(this);
}
}
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.2
2.3.0
Loading