18
18
import org .entityc .compiler .model .entity .MTRelationship ;
19
19
import org .entityc .compiler .model .visitor .MTVisitor ;
20
20
21
+ import java .util .ArrayList ;
22
+ import java .util .Collection ;
23
+ import java .util .List ;
24
+
21
25
@ ModelClass (type = ModelClassType .DOMAIN ,
22
26
description = "Represents a relationship in your model in the context of a domain." )
23
27
public class MTDERelationship extends MTNode implements MTNamed , MTDomainBased , MTReferenceResolution {
24
28
25
- private final String relationshipName ;
26
- private MTDomain domain ;
27
- private MTDEntity domainEntity ;
28
- private MTRelationship relationship ;
29
- private String explicitName ;
30
- private String withViewName ; // to-entity should map to a specific view
31
- private boolean withPrimaryKey ; // to-entity should map as its primary key only
32
- private MTDERelationshipHalf to ;
33
- private boolean resolvedReferences = false ;
29
+ private final String relationshipName ;
30
+ private MTDomain domain ;
31
+ private MTDEntity domainEntity ;
32
+ private MTRelationship relationship ;
33
+ private String explicitName ;
34
+ private String withViewName ; // to-entity should map to a specific view
35
+ private boolean withPrimaryKey ; // to-entity should map as its primary key only
36
+ private MTDERelationshipHalf to ;
37
+ private boolean resolvedReferences = false ;
38
+ private List <MTDERelationshipField > fields ;
34
39
35
40
public MTDERelationship (ParserRuleContext ctx , MTDEntity domainEntity , String relationshipName ) {
36
41
super (ctx );
37
- this .domain = domainEntity .getDomain ();
38
- this .domainEntity = domainEntity ;
42
+ this .domain = domainEntity .getDomain ();
43
+ this .domainEntity = domainEntity ;
39
44
this .relationshipName = relationshipName ;
40
45
}
41
46
42
47
public MTDERelationship (ParserRuleContext ctx , MTDomain domain , MTRelationship relationship ) {
43
48
super (ctx );
44
- this .domain = domain ;
45
- this .relationship = relationship ;
49
+ this .domain = domain ;
50
+ this .relationship = relationship ;
46
51
this .relationshipName = relationship .getName ();
47
52
}
48
53
@@ -77,7 +82,7 @@ public void setWithViewName(String withViewName) {
77
82
@ ModelMethod (category = ModelMethodCategory .RELATIONSHIP ,
78
83
description =
79
84
"If this relationship was explicitly renamed within its domain, it will return that name. Otherwise "
80
- + "it will return `null`." )
85
+ + "it will return `null`." )
81
86
public String getExplicitName () {
82
87
return explicitName ;
83
88
}
@@ -108,6 +113,22 @@ public boolean isManyToMany() {
108
113
return relationship != null && relationship .isManyToMany ();
109
114
}
110
115
116
+ public void addField (MTDERelationshipField field ) {
117
+ if (this .fields == null ) {
118
+ this .fields = new ArrayList <>();
119
+ }
120
+ this .fields .add (field );
121
+ }
122
+
123
+ public boolean hasFields () {
124
+ return this .fields != null && this .fields .size () > 0 ;
125
+ }
126
+ @ ModelMethod (category = ModelMethodCategory .RELATIONSHIP ,
127
+ description = "Returns all the declared fields of this relationship." )
128
+ public Collection <MTDERelationshipField > getFields () {
129
+ return this .fields ;
130
+ }
131
+
111
132
@ Override
112
133
public boolean resolveReferences (MTSpace space , int pass ) {
113
134
if (domain != null && domain .isSpecialized ()) {
@@ -126,12 +147,19 @@ public boolean resolveReferences(MTSpace space, int pass) {
126
147
}
127
148
if (relationship != null && to == null ) {
128
149
to = new MTDERelationshipHalf (relationship .getParserRuleContext (), domain , relationship .getTo (),
129
- withViewName );
150
+ withViewName );
130
151
to .setAsPrimaryKey (withPrimaryKey );
131
152
if (to .resolveReferences (space , pass )) {
132
153
anotherPass = true ;
133
154
}
134
155
}
156
+ if (fields != null ) {
157
+ for (MTDERelationshipField field : fields ) {
158
+ if (field .resolveReferences (space , pass )) {
159
+ anotherPass = true ;
160
+ }
161
+ }
162
+ }
135
163
if (!anotherPass ) {
136
164
resolvedReferences = true ;
137
165
}
@@ -146,21 +174,21 @@ public void accept(MTVisitor visitor) {
146
174
@ ModelMethod (category = ModelMethodCategory .RELATIONSHIP ,
147
175
description =
148
176
"This returns the full name of this domain relationship which includes not only its domain based name "
149
- + "but is also preceded with the domain's entity's full name. The delimiter can be provided which "
150
- + "is used between all parts of the full name." )
177
+ + "but is also preceded with the domain's entity's full name. The delimiter can be provided which "
178
+ + "is used between all parts of the full name." )
151
179
@ Override
152
180
public String getFullname (
153
181
@ ModelMethodParameter (description = "A delimiter to place between the segments of the domain namespace as well "
154
- + "as between that namespace and the domain entity name and between the "
155
- + "domain entity name and the domain relationship name." )
156
- String delim ) {
182
+ + "as between that namespace and the domain entity name and between the "
183
+ + "domain entity name and the domain relationship name." )
184
+ String delim ) {
157
185
return domainEntity .getFullname (delim ) + delim + getName ();
158
186
}
159
187
160
188
@ ModelMethod (category = ModelMethodCategory .RELATIONSHIP ,
161
189
description =
162
190
"Returns the domain relationship name which is the result of applying any defined naming conventions "
163
- + "or explicit renaming." )
191
+ + "or explicit renaming." )
164
192
@ Override
165
193
public String getName () {
166
194
if (explicitName != null ) {
@@ -230,7 +258,7 @@ public boolean isPrimaryParent() {
230
258
description = "Returns whether this relationship's \" to\" entity is tagged with the specified tag." )
231
259
public boolean hasToEntityTagged (
232
260
@ ModelMethodParameter (description = "The tag with which to check." )
233
- String tag ) {
261
+ String tag ) {
234
262
return to != null && to .getEntity ().hasTag (tag );
235
263
}
236
264
}
0 commit comments