Skip to content

Commit

Permalink
add Builder for TDocumentation
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Harzenetter <lharzenetter@gmx.de>
  • Loading branch information
lharzenetter committed Jul 2, 2021
1 parent f79201a commit 124c04e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 86 deletions.
Expand Up @@ -52,7 +52,14 @@ public class TDocumentation implements Serializable {
protected String lang;

@Deprecated // used for XML deserialization of API request content
public TDocumentation() { }
public TDocumentation() {
}

public TDocumentation(Builder builder) {
this.content = builder.content;
this.source = builder.source;
this.lang = builder.lang;
}

@Override
public boolean equals(Object o) {
Expand Down Expand Up @@ -98,4 +105,45 @@ public void setLang(@Nullable String value) {
public void accept(Visitor visitor) {
visitor.visit(this);
}

public static class Builder {

private List<Object> content;
private String source;
private String lang;

public Builder() {
}

public Builder(List<Object> content) {
this.content = content;
}

public Builder addContent(List<Object> content) {
this.content = content;
return this;
}

public Builder setSource(String source) {
this.source = source;
return this;
}

public Builder setLang(String lang) {
this.lang = lang;
return this;
}

public TDocumentation build() {
return new TDocumentation(this);
}

public Builder addContent(String documentation) {
if (this.content == null) {
this.content = new ArrayList<>();
}
this.content.add(documentation);
return this;
}
}
}
Expand Up @@ -69,38 +69,39 @@
TDataType.class,
})
public abstract class TEntityType extends TExtensibleElements implements HasName, HasInheritance, HasTargetNamespace {
public static final String NS_SUFFIX_PROPERTIESDEFINITION_WINERY = "propertiesdefinition/winery";

public static final String NS_SUFFIX_PROPERTIES_DEFINITION_WINERY = "propertiesdefinition/winery";

@XmlElement(name = "Tags")
protected TTags tags;

@XmlElement(name = "DerivedFrom")
protected TEntityType.DerivedFrom derivedFrom;

@XmlElementRef(name = "PropertiesDefinition")
@JsonProperty("propertiesDefinition")
@JsonSerialize(using = PropertiesDefinitionSerializer.class)
protected TEntityType.PropertiesDefinition properties;

@XmlAttribute(name = "name", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NCName")
protected String name;

@XmlAttribute(name = "abstract")
@XmlJavaTypeAdapter(type = boolean.class, value = BooleanToYesNo.class)
@JsonProperty("abstract")
@JsonSerialize(using = YesNo.Serializer.class)
@JsonDeserialize(using = YesNo.Deserializer.class)
protected boolean _abstract;

@XmlAttribute(name = "final")
@XmlJavaTypeAdapter(type = boolean.class, value = BooleanToYesNo.class)
@JsonProperty("final")
@JsonSerialize(using = YesNo.Serializer.class)
@JsonDeserialize(using = YesNo.Deserializer.class)
protected boolean _final;

@XmlAttribute(name = "targetNamespace")
@XmlSchemaType(name = "anyURI")
protected String targetNamespace;
Expand All @@ -109,7 +110,8 @@ public abstract class TEntityType extends TExtensibleElements implements HasName
protected List<AttributeDefinition> attributeDefinitions;

@Deprecated // used for XML deserialization of API request content
public TEntityType() { }
public TEntityType() {
}

public TEntityType(Builder<?> builder) {
super(builder);
Expand Down Expand Up @@ -254,7 +256,7 @@ public WinerysPropertiesDefinition getWinerysPropertiesDefinition() {
if (!ns.endsWith("/")) {
ns += "/";
}
ns += NS_SUFFIX_PROPERTIESDEFINITION_WINERY;
ns += NS_SUFFIX_PROPERTIES_DEFINITION_WINERY;
res.setNamespace(ns);
}
return res;
Expand Down Expand Up @@ -307,7 +309,7 @@ public int hashCode() {
return Objects.hash(typeRef);
}
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class YamlPropertyDefinition {
Expand All @@ -330,7 +332,7 @@ public static class YamlPropertyDefinition {
public YamlPropertyDefinition() {
// added for xml serialization!
}

private YamlPropertyDefinition(Builder builder) {
this.name = builder.name;
this.type = builder.type;
Expand All @@ -342,14 +344,14 @@ private YamlPropertyDefinition(Builder builder) {
this.keySchema = builder.keySchema;
this.entrySchema = builder.entrySchema;
}

@XmlEnum(String.class)
public enum Status {
supported,
unsupported,
experimental,
deprecated;

@Nullable
public static Status getStatus(String status) {
// Could possibly be replaced by wrapping with Status.getValue(status)?
Expand All @@ -370,7 +372,7 @@ public static class Builder {
private List<ConstraintClauseKV> constraints;
private TSchema entrySchema;
private TSchema keySchema;

public Builder(String name) {
this.name = name;
}
Expand Down Expand Up @@ -424,7 +426,7 @@ public YamlPropertyDefinition build() {
return new YamlPropertyDefinition(this);
}
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -511,21 +513,22 @@ public void setKeySchema(@Nullable TSchema keySchema) {
* This is specifically implemented as an abstract class because JAXB refuses to handle interfaces54
*/
@XmlType(name = "")
@XmlSeeAlso({
@XmlSeeAlso( {
YamlPropertiesDefinition.class,
WinerysPropertiesDefinition.class,
XmlElementDefinition.class,
XmlTypeDefinition.class
})
@JsonSubTypes({
@JsonSubTypes( {
@JsonSubTypes.Type(YamlPropertiesDefinition.class),
@JsonSubTypes.Type(WinerysPropertiesDefinition.class),
@JsonSubTypes.Type(XmlElementDefinition.class),
@JsonSubTypes.Type(XmlTypeDefinition.class),
})
@JsonDeserialize(using = PropertiesDefinitionDeserializer.class)
@JsonSerialize(using = PropertiesDefinitionSerializer.class)
public abstract static class PropertiesDefinition { }
public abstract static class PropertiesDefinition {
}

@NonNullByDefault
@XmlRootElement(name = "PropertiesDefinition")
Expand All @@ -544,9 +547,8 @@ public void setProperties(List<YamlPropertyDefinition> properties) {
}

/**
* The XML standard defines two mutually exclusive ways of specifying a property.
* Option 1 is a QName pointing to an element, the other option is a type reference,
* see {@link XmlTypeDefinition}.
* The XML standard defines two mutually exclusive ways of specifying a property. Option 1 is a QName pointing to an
* element, the other option is a type reference, see {@link XmlTypeDefinition}.
*/
@XmlRootElement(name = "PropertiesDefinition")
@JsonDeserialize(as = XmlElementDefinition.class)
Expand All @@ -555,7 +557,8 @@ public static class XmlElementDefinition extends PropertiesDefinition {
private QName element;

// required for jaxb
public XmlElementDefinition() { }
public XmlElementDefinition() {
}

public XmlElementDefinition(QName element) {
this.element = element;
Expand All @@ -571,9 +574,8 @@ public void setElement(QName element) {
}

/**
* The XML standard defines two mutually exclusive ways of specifying a property.
* Option 1 is a QName pointing to an element, see {@link XmlElementDefinition},
* the other option is a type reference.
* The XML standard defines two mutually exclusive ways of specifying a property. Option 1 is a QName pointing to an
* element, see {@link XmlElementDefinition}, the other option is a type reference.
*/
@XmlRootElement(name = "PropertiesDefinition")
@JsonDeserialize(as = XmlTypeDefinition.class)
Expand All @@ -582,7 +584,8 @@ public static class XmlTypeDefinition extends PropertiesDefinition {
private QName type;

// required for JAXB
public XmlTypeDefinition() { }
public XmlTypeDefinition() {
}

public XmlTypeDefinition(QName type) {
this.type = type;
Expand All @@ -599,6 +602,7 @@ public void setType(QName type) {

@ADR(11)
public abstract static class Builder<T extends Builder<T>> extends TExtensibleElements.Builder<T> {

private final String name;

private TTags tags;
Expand Down
Expand Up @@ -72,7 +72,8 @@ public abstract class TExtensibleElements implements Serializable {
private Map<QName, String> otherAttributes = new HashMap<>();

@Deprecated // used for XML deserialization of API request content
public TExtensibleElements() { }
public TExtensibleElements() {
}

public TExtensibleElements(Builder<?> builder) {
this.documentation = builder.documentation;
Expand Down Expand Up @@ -189,9 +190,11 @@ public T addDocumentation(String documentation) {
return self();
}

TDocumentation tmp = new TDocumentation();
tmp.getContent().add(documentation);
return self().addDocumentation(tmp);
return self().addDocumentation(
new TDocumentation.Builder()
.addContent(documentation)
.build()
);
}

public T addDocumentation(Map<String, String> documentation) {
Expand Down
Expand Up @@ -13,14 +13,15 @@
*******************************************************************************/
package org.eclipse.winery.repository.rest.resources.documentation;

import org.eclipse.winery.model.tosca.TDocumentation;
import org.eclipse.winery.repository.rest.RestUtils;
import org.eclipse.winery.repository.rest.resources._support.AbstractComponentInstanceResource;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.core.Response;
import java.util.List;

import org.eclipse.winery.model.tosca.TDocumentation;
import org.eclipse.winery.repository.rest.RestUtils;
import org.eclipse.winery.repository.rest.resources._support.AbstractComponentInstanceResource;

public class DocumentationResource {

Expand Down Expand Up @@ -48,10 +49,11 @@ public String onGet() {
@PUT
public Response onPost(String documentation) {
this.documentation.clear();
TDocumentation tDocumentation = new TDocumentation();
tDocumentation.getContent().add(documentation);
this.documentation.add(tDocumentation);
this.documentation.add(
new TDocumentation.Builder()
.addContent(documentation)
.build()
);
return RestUtils.persist(this.abstractComponentInstanceResource);
}

}
Expand Up @@ -76,7 +76,7 @@ public String generatePrefixProposal(String namespace, int round) {
// Fallback if no slashes are in the namespace - just count from ns0 onwards
return String.format("ns%d", round);
} else {
boolean isWineryPropertiesDefinitionNamespace = namespace.endsWith(TEntityType.NS_SUFFIX_PROPERTIESDEFINITION_WINERY);
boolean isWineryPropertiesDefinitionNamespace = namespace.endsWith(TEntityType.NS_SUFFIX_PROPERTIES_DEFINITION_WINERY);
String suffix;
if (isWineryPropertiesDefinitionNamespace) {
suffix = "pd";
Expand Down

0 comments on commit 124c04e

Please sign in to comment.