Skip to content

Commit

Permalink
#534 implement record relation within the pojo class
Browse files Browse the repository at this point in the history
  • Loading branch information
csun-cpointe committed Jan 23, 2025
1 parent 1249381 commit 6a092b7
Show file tree
Hide file tree
Showing 29 changed files with 746 additions and 89 deletions.
3 changes: 3 additions & 0 deletions DRAFT_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Data access through [GraphQL](https://graphql.org/) has been deprecated and repl
## Spark Upgrade
Spark and PySpark have been upgraded from version 3.5.2 to 3.5.4.

## Record Relation
To enable nested data records, we have added a new relation feature to the record metamodel. This allows records to reference other records. For more details, refer to the [Record Relation Options].(https://boozallen.github.io/aissemble/aissemble/current-dev/record-metamodel.html#_record_relation_options)

# Breaking Changes
_Note: instructions for adapting to these changes are outlined in the upgrade instructions below._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public List<Framework> getFrameworks() {
* {@inheritDoc}
*/
@Override
public Relation getRelation(String type) {
return wrapped.getRelation(type);
public Relation getRelation(String name) {
return wrapped.getRelation(name);
}

/**
Expand Down Expand Up @@ -171,7 +171,7 @@ public boolean hasFieldValidations() {
/**
* Get a JSON representation of the record's fields
* @return a JSON representation of the record's fields
* @throws JsonProcessingException
* @throws JsonProcessingException the JsonProcessingException
*/
public String getSchema() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
Expand All @@ -196,5 +196,21 @@ public List<Record> getInverseRelations() {
return wrappedInverseRelations;
}

/**
* check if the records have any relations
* @return true or false value
*/
public boolean hasRelations() {
return !wrapped.getRelations().isEmpty();
}

/**
* check if the records have any fields
* @return true or false value
*/
public boolean hasFields() {
return !wrapped.getFields().isEmpty();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.boozallen.aiops.mda.metamodel.element;

/*-
* #%L
* AIOps Foundation::AIOps MDA
* %%
* Copyright (C) 2021 Booz Allen
* %%
* This software package is licensed under the Booz Allen Public License. All Rights Reserved.
* #L%
*/

import org.technologybrewery.fermenter.mda.metamodel.element.MetamodelUtils;

/**
* Provides baseline decorator functionality for {@link Relation}.
*
* The goal is to make it easier to apply the decorator pattern in various implementations of generators (e.g., Java,
* python, Docker) so that each concrete decorator only has to decorate those aspects of the class that are needed, not
* all the pass-through methods that each decorator would otherwise need to implement (that add no real value).
*/
public class BaseRecordRelationDecorator implements Relation {

protected Relation wrapped;

/**
* New decorator for {@link Relation}.
*
* @param relationToDecorate
* instance to decorate
*/
public BaseRecordRelationDecorator(Relation relationToDecorate) {
MetamodelUtils.validateWrappedInstanceIsNonNull(getClass(), relationToDecorate);
wrapped = relationToDecorate;
}

@Override
public String getDocumentation() {
return wrapped.getDocumentation();
}

@Override
public Multiplicity getMultiplicity() {
return wrapped.getMultiplicity();
}

@Override
public String getPackage() {
return wrapped.getPackage();
}

@Override
public String getFileName() {
return wrapped.getFileName();
}

@Override
public String getName() {
return wrapped.getName();
}

@Override
public void validate() {
wrapped.validate();
}

/**
* Check if the relation multiplicity is One to Many
* @return true or false value
*/
public boolean isOneToManyRelation() {
return wrapped.getMultiplicity().equals(Multiplicity.ONE_TO_MANY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,13 @@

import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.commons.lang3.StringUtils;
import org.technologybrewery.fermenter.mda.metamodel.element.Validatable;
//import org.technologybrewery.fermenter.mda.metamodel.element.Field;
import org.technologybrewery.fermenter.mda.metamodel.element.NamespacedMetamodel;

/**
* Defines the contract for a record that has a child of another record.
*/
public interface Relation extends NamespacedMetamodel {

/**
* Returns the type package of relation.
*
* @return relation type package
*/
// String getPackage();

/**
* Returns the type of relation.
*
* @return relation type
*/
// String getName();

/**
* Returns relation-level documentation.
*
Expand All @@ -52,13 +36,13 @@ public interface Relation extends NamespacedMetamodel {
/**
* Enumerated values representing multiplicity options.
*/
public enum Multiplicity {
enum Multiplicity {

ONE_TO_MANY("1-M"), ONE_TO_ONE("1-1"), MANY_TO_MANY("M-M");
ONE_TO_MANY("1-M"), ONE_TO_ONE("1-1"), MANY_TO_ONE("M-1");

private String value;
private final String value;

private Multiplicity(String value) {
Multiplicity(String value) {
this.value = value;
}

Expand All @@ -82,9 +66,9 @@ public static Multiplicity fromString(String value) {
|| ("one-to-one".equals(lowerCasedValue)) || ("one-one".equals(lowerCasedValue))) {
matchedMultiplicity = ONE_TO_ONE;

} else if ((MANY_TO_MANY.toString().equalsIgnoreCase(lowerCasedValue))
|| ("many-to-many".equals(lowerCasedValue)) || ("many-many".equals(lowerCasedValue))) {
matchedMultiplicity = MANY_TO_MANY;
} else if ((MANY_TO_ONE.toString().equalsIgnoreCase(lowerCasedValue))
|| ("many-to-one".equals(lowerCasedValue)) || ("many-one".equals(lowerCasedValue))) {
matchedMultiplicity = MANY_TO_ONE;

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,27 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.google.common.base.MoreObjects;
import org.apache.commons.lang3.StringUtils;
import org.technologybrewery.fermenter.mda.metamodel.element.NamespacedMetamodelElement;
import org.technologybrewery.fermenter.mda.util.MessageTracker;

/**
* Represents a reference on an record.
* Represents a reference on a record.
*/
@JsonPropertyOrder({ "type", "package", "multiplicity" })
public class RelationElement implements Relation {
protected static final String PACKAGE = "package";
@JsonPropertyOrder({ "name", "package", "multiplicity" })
public class RelationElement extends NamespacedMetamodelElement implements Relation {

@JsonIgnore
private static MessageTracker messageTracker = MessageTracker.getInstance();

@JsonInclude(Include.NON_NULL)
@JsonProperty(value = PACKAGE)
protected String packageName;

@JsonInclude(Include.NON_NULL)
protected String name;
private static final MessageTracker messageTracker = MessageTracker.getInstance();

@JsonInclude(Include.NON_NULL)
protected String documentation;

@JsonInclude(Include.NON_NULL)
protected Multiplicity multiplicity;

/**
* {@inheritDoc}
*/
@Override
public String getPackage() {
return packageName;
}

/**
* {@inheritDoc}
*/
@Override
public String getName() {
return name;
}

/**
* {@inheritDoc}
*/
@JsonIgnore
public String getFileName() {
throw new UnsupportedOperationException("This method is not implemented.");
}

/**
* {@inheritDoc}
Expand All @@ -91,26 +60,11 @@ public void validate() {
if (multiplicity == null) {
multiplicity = Multiplicity.ONE_TO_MANY;
}
}

/**
* Sets the relation type package.
*
* @param packageName
* relation type packageName
*/
public void setPackage(String packageName) {
this.packageName = packageName;
}

/**
* Sets the relation type.
*
* @param name
* relation type
*/
public void setName(String name) {
this.name = name;
super.validate();
//TODO: this can be removed when upgrade to fermenter 2.10.6
if (getPackage() == null) {
messageTracker.addErrorMessage("Package is a required attribute!");
}
}

/**
Expand Down Expand Up @@ -146,4 +100,8 @@ public String toString() {
return MoreObjects.toStringHelper(this).add("name", name).toString();
}

@Override
public String getSchemaFileName() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;
import java.util.TreeSet;

import com.boozallen.aiops.mda.metamodel.element.Relation;
import org.apache.commons.lang3.StringUtils;
import org.technologybrewery.fermenter.mda.TypeManager;
import org.technologybrewery.fermenter.mda.generator.GenerationException;
Expand All @@ -29,7 +30,7 @@
*/
public class JavaRecord extends BaseRecordDecorator {

private Set<String> imports = new TreeSet<>();
private final Set<String> imports = new TreeSet<>();

/**
* {@inheritDoc}
Expand Down Expand Up @@ -66,6 +67,8 @@ public Set<String> getBaseImports() {
imports.add(JavaElementUtils.MAP_IMPORT);
imports.add(JavaElementUtils.HASH_MAP_IMPORT);

addRelationImports();

return imports;
}

Expand Down Expand Up @@ -150,4 +153,28 @@ private void addSimpleTypeImport(JavaRecordField field) {
}
}

private void addRelationImports() {
for (Relation relation: wrapped.getRelations()) {
JavaRecordRelation relationDecorator = new JavaRecordRelation(relation);
String classImport = relationDecorator.getGeneratedClassImport();
if (classImport !=null) {
imports.add(classImport);
}
}
}

/**
* {@inheritDoc}
*/
@Override
public List<Relation> getRelations() {
List<Relation> wrappedRelations = new ArrayList<>();
for (Relation relation : wrapped.getRelations()) {
JavaRecordRelation wrappedRelation = new JavaRecordRelation(relation);
wrappedRelations.add(wrappedRelation);
}

return wrappedRelations;
}

}
Loading

0 comments on commit 6a092b7

Please sign in to comment.