Skip to content

Commit e1f619d

Browse files
committed
handle embedded types
1 parent bb212d0 commit e1f619d

File tree

5 files changed

+166
-58
lines changed

5 files changed

+166
-58
lines changed

build.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
relevant_versions = [v for v in schema_loader.get_schema_versions() if v not in ignored_versions]
2121

2222

23-
def build_central():
23+
def build_central(embedded_only):
2424
env = Environment(
2525
loader=PackageLoader("generator"),
2626
autoescape=select_autoescape()
2727
)
2828
template = env.get_template("OpenMINDS.java.j2")
2929
result = template.render(
3030
relevant_versions = built_versions,
31-
packages_by_version = packages_by_version
31+
packages_by_version = packages_by_version,
32+
embedded_only = embedded_only
3233
)
3334
target_file = "target/src/main/java/org/openmetadatainitiative/openminds/OpenMINDS.java"
3435
os.makedirs(os.path.dirname(target_file), exist_ok=True)
@@ -45,13 +46,19 @@ def build_central():
4546
type_to_class_name = {}
4647
implemented_interfaces = {}
4748
builders = []
49+
linked_types = set()
50+
embedded_types = set()
51+
52+
4853
for schema_file_path in schemas_file_paths:
4954
builder = JavaBuilder(schema_file_path, schema_loader.schemas_sources)
5055
if builder.version not in packages_by_version:
5156
packages_by_version[builder.version] = {}
5257
if builder.relative_path_without_extension[0] not in packages_by_version[builder.version]:
5358
packages_by_version[builder.version][builder.relative_path_without_extension[0]] = []
54-
packages_by_version[builder.version][builder.relative_path_without_extension[0]].append((builder.class_name, builder.canonical_class_name()))
59+
packages_by_version[builder.version][builder.relative_path_without_extension[0]].append((builder.class_name, builder.canonical_class_name(), builder.type))
60+
linked_types.update(builder.linked_types)
61+
embedded_types.update(builder.embedded_types)
5562
if builder.version not in built_versions:
5663
built_versions.append(builder.version)
5764
for property, types in builder.additional_interfaces.items():
@@ -62,9 +69,9 @@ def build_central():
6269
type_to_class_name[builder.type]=builder.canonical_class_name()
6370
builders.append(builder)
6471

65-
72+
embedded_only = embedded_types-linked_types
6673
for builder in builders:
6774
# Step 3 - translate and build each openMINDS schema as JSON-Schema
68-
builder.build(type_to_class_name, implemented_interfaces)
75+
builder.build(type_to_class_name, implemented_interfaces, embedded_types, embedded_only)
6976

70-
build_central()
77+
build_central(embedded_only)

generator/templates/OpenMINDS.java.j2

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
11
package org.openmetadatainitiative.openminds;
22

3-
import org.openmetadatainitiative.openminds.utils.Instance;
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
47
import org.openmetadatainitiative.openminds.utils.OpenMINDSContext;
5-
import org.openmetadatainitiative.openminds.utils.ParsingUtils;
68
import org.openmetadatainitiative.openminds.utils.LocalId;
9+
import org.openmetadatainitiative.openminds.utils.ParsingUtils;
710
import org.openmetadatainitiative.openminds.utils.PostProcessor;
811

9-
import java.io.File;
10-
import java.io.IOException;
1112
import java.util.ArrayList;
1213
import java.util.List;
13-
import java.util.stream.Stream;
1414

1515
/**
1616
* ATTENTION! This is an autogenerated file based on the openMINDS schema - do not apply manual changes since they are going to be overwritten.
1717
*/
18+
@SuppressWarnings("unused")
1819
public class OpenMINDS {
1920

21+
public static class UnknownEntity{}
22+
2023
private OpenMINDS(){}
2124

22-
{% for version in relevant_versions %}public static OpenMINDS.{{version[0]|upper}}{{version[1:]}} {{version}}() {
23-
return new OpenMINDS().new {{version[0]|upper}}{{version[1:]}}();
25+
{% for version in relevant_versions|sort %}public static OpenMINDS.{{version[0]|upper}}{{version[1:]}} {{version}}() {
26+
return new {{version[0]|upper}}{{version[1:]}}();
2427
}
2528

26-
public final class {{version[0]|upper}}{{version[1:]}} {
29+
public static final class {{version[0]|upper}}{{version[1:]}} {
2730
private {{version[0]|upper}}{{version[1:]}}(){}
31+
@JsonTypeInfo(
32+
use = JsonTypeInfo.Id.NAME,
33+
defaultImpl = UnknownEntity.class
34+
)
35+
@JsonSubTypes({
36+
{% for package in packages_by_version[version].keys()|sort %}{% for class in packages_by_version[version][package] | sort %}{%if class[2] not in embedded_only %}@JsonSubTypes.Type(value = {{class[1]}}.class, name = {{class[1]}}.SEMANTIC_NAME),
37+
{% endif %}{% endfor %}{% endfor %}
38+
})
39+
public interface Entity {
40+
}
41+
42+
public <T extends Entity> T load(String payload) throws JsonProcessingException {
43+
return (T) ParsingUtils.OBJECT_MAPPER.readValue(payload, Entity.class);
44+
}
45+
2846
private final List<org.openmetadatainitiative.openminds.utils.Builder<?>> builders = new ArrayList<>();
2947

30-
{% for package in packages_by_version[version].keys() %}public final OpenMINDS.{{version[0]|upper}}{{version[1:]}}.{{package[0]|upper}}{{package[1:]}} {{package}} = new {{package[0]|upper}}{{package[1:]}}();
48+
{% for package in packages_by_version[version].keys()|sort %}public final OpenMINDS.{{version[0]|upper}}{{version[1:]}}.{{package[0]|upper}}{{package[1:]}} {{package}} = new {{package[0]|upper}}{{package[1:]}}();
3149

3250
public final class {{package[0]|upper}}{{package[1:]}}{
3351
private {{package[0]|upper}}{{package[1:]}}(){}
34-
{% for class in packages_by_version[version][package] %}
52+
{% for class in packages_by_version[version][package] | sort %}{%if class[2] not in embedded_only %}
3553
public {{class[1]}}.Builder create{{class[0]}}(String localId){
3654
final {{class[1]}}.Builder builder = {{class[1]}}.create(new LocalId(localId));
3755
builders.add(builder);
3856
return builder;
3957
}
40-
{% endfor %}
58+
{% endif %}{% endfor %}
4159
}
4260
{% endfor %}
4361

generator/templates/interface.java.j2

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@ import org.openmetadatainitiative.openminds.utils.Reference;
77
/**
88
* ATTENTION! This is an autogenerated file based on the openMINDS schema - do not apply manual changes since they are going to be overwritten.
99
*/
10+
@SuppressWarnings("unused")
1011
public interface {{additional_interface}} extends Entity {
1112
Reference<? extends {{additional_interface}}> getReference();
1213

1314
class Deserializer extends ByTypeDeserializer<{{additional_interface}}> {
1415
public Deserializer() {
15-
super({{types|join(', ')}});
16+
super({{types_with_file_ending|join(', ')}});
1617
}
1718
}
19+
{% if embedded %}
20+
static {{additional_interface}}.EmbeddedBuilder createEmbedded(){
21+
return new EmbeddedBuilder();
22+
}
23+
24+
class EmbeddedBuilder {
25+
{% for t in types %}
26+
public {{t}}.EmbeddedBuilder {{simple_type_names[t][0]|lower}}{{simple_type_names[t][1:]}}(){
27+
return {{t}}.createEmbedded();
28+
}
29+
{% endfor %}
30+
31+
}
32+
{% endif %}
1833
}

generator/templates/schema_class.java.j2

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package {{package_name}};
33
import com.fasterxml.jackson.annotation.JsonIgnore;
44
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
55
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.JsonInclude;
67
import org.openmetadatainitiative.openminds.utils.*;
8+
import java.util.function.Function;
79

810
import java.util.ArrayList;
911
import java.util.List;
@@ -20,8 +22,10 @@ import static {{package_name}}.{{ class_name }}.SEMANTIC_NAME;
2022
*/
2123
@InstanceType(SEMANTIC_NAME)
2224
@JsonIgnoreProperties(ignoreUnknown = true)
23-
public class {{ class_name }} extends Instance {% if implemented_interfaces %}implements {{ implemented_interfaces|join(', ') }}{% endif %}{
24-
static final String SEMANTIC_NAME = "{{ type }}";
25+
@JsonInclude(JsonInclude.Include.NON_NULL)
26+
@SuppressWarnings("unused")
27+
public class {{ class_name }} extends Instance implements org.openmetadatainitiative.openminds.OpenMINDS.{{version[0]|upper}}{{version[1:]}}.Entity{% if implemented_interfaces %}, {{ implemented_interfaces|join(', ') }}{% endif %}{
28+
public static final String SEMANTIC_NAME = "{{ type }}";
2529

2630
@JsonIgnore
2731
public Reference<{{ class_name }}> getReference() {
@@ -32,25 +36,51 @@ public class {{ class_name }} extends Instance {% if implemented_interfaces %}im
3236
return new Reference<>(new InstanceId(instanceId));
3337
}
3438

39+
/** For deserialization **/
40+
private {{ class_name }}() {
41+
this(null);
42+
}
43+
3544
private {{ class_name }}(LocalId localId ) {
36-
super(localId);
45+
super(localId, SEMANTIC_NAME);
46+
}
47+
48+
{% if type in embedded_types %}
49+
public class EmbeddedBuilder {
50+
51+
{% for property in properties %}public EmbeddedBuilder {{ builder_for_properties[property] }}
52+
{% endfor %}
53+
54+
public {{ class_name }} build(){
55+
return {{ class_name }}.this;
56+
}
3757
}
3858

59+
public static {{ class_name }}.EmbeddedBuilder createEmbedded(){
60+
return new {{ class_name }}(null).new EmbeddedBuilder();
61+
}
62+
{% endif %}
3963

64+
{%if type not in embedded_types or type not in embedded_only %}
4065
public class Builder implements org.openmetadatainitiative.openminds.utils.Builder<{{ class_name }}>{
41-
{% for property in properties %}{% for line in builder_for_properties[property] %}
42-
{{ line }}
43-
{% endfor %}{% endfor %}
66+
{% for property in properties %}public Builder {{ builder_for_properties[property] }}
67+
{% endfor %}
4468

4569
public {{ class_name }} build(OpenMINDSContext context) {
46-
if ({{ class_name }}.this.id == null) {
47-
{{ class_name }}.this.id = InstanceId.withPrefix(UUID.randomUUID().toString(), context.idPrefix());
48-
}
49-
{{ class_name }}.this.atType = SEMANTIC_NAME;
70+
{{ class_name }}.super.build(context);
5071
return {{ class_name }}.this;
5172
}
5273
}
5374

75+
public static {{ class_name }}.Builder create(LocalId localId){
76+
return new {{ class_name }}(localId).new Builder();
77+
}
78+
79+
public {{ class_name }}.Builder copy(){
80+
return ParsingUtils.OBJECT_MAPPER.convertValue(this, {{ class_name }}.class).new Builder();
81+
}
82+
{% endif %}
83+
5484
{% for property in properties %} @JsonProperty(value = "{%if property in absolute_property_translations%}{{absolute_property_translations[property]}}{%else%}{{property}}{%endif%}")
5585
{{member_for_properties[property]}}
5686
{%if property in property_descriptions and property_descriptions[property] %}
@@ -60,11 +90,5 @@ public class {{ class_name }} extends Instance {% if implemented_interfaces %}im
6090
{{getter_for_properties[property]}}
6191

6292
{% endfor %}
63-
public static {{ class_name }}.Builder create(LocalId localId){
64-
return new {{ class_name }}(localId).new Builder();
65-
}
6693

67-
public {{ class_name }}.Builder copy(){
68-
return ParsingUtils.OBJECT_MAPPER.convertValue(this, {{ class_name }}.class).new Builder();
69-
}
7094
}

0 commit comments

Comments
 (0)