-
Notifications
You must be signed in to change notification settings - Fork 295
/
ActionAnnotationFacetFactory.java
262 lines (202 loc) · 10.9 KB
/
ActionAnnotationFacetFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.causeway.core.metamodel.facets.actions.action;
import java.util.Optional;
import javax.inject.Inject;
import org.apache.causeway.applib.annotation.Action;
import org.apache.causeway.applib.mixins.system.HasInteractionId;
import org.apache.causeway.core.config.progmodel.ProgrammingModelConstants;
import org.apache.causeway.core.metamodel.context.MetaModelContext;
import org.apache.causeway.core.metamodel.facetapi.FeatureType;
import org.apache.causeway.core.metamodel.facets.FacetFactoryAbstract;
import org.apache.causeway.core.metamodel.facets.actcoll.typeof.TypeOfFacet;
import org.apache.causeway.core.metamodel.facets.actions.action.choicesfrom.ChoicesFromFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.actions.action.explicit.ActionExplicitFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.actions.action.hidden.HiddenFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.actions.action.invocation.ActionDomainEventFacet;
import org.apache.causeway.core.metamodel.facets.actions.action.invocation.ActionInvocationFacetForDomainEvent;
import org.apache.causeway.core.metamodel.facets.actions.action.prototype.PrototypeFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.actions.action.semantics.ActionSemanticsFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.actions.action.typeof.TypeOfFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.actions.fileaccept.FileAcceptFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.members.layout.group.LayoutGroupFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.members.publish.command.CommandPublishingFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.facets.members.publish.execution.ExecutionPublishingActionFacetForActionAnnotation;
import org.apache.causeway.core.metamodel.specloader.validator.MetaModelValidatorForAmbiguousMixinAnnotations;
import lombok.val;
public class ActionAnnotationFacetFactory
extends FacetFactoryAbstract {
@Inject
public ActionAnnotationFacetFactory(final MetaModelContext mmc) {
super(mmc, FeatureType.ACTIONS_ONLY);
}
@Override
public void process(final ProcessMethodContext processMethodContext) {
val actionIfAny = processMethodContext
.synthesizeOnMethodOrMixinType(
Action.class,
() -> MetaModelValidatorForAmbiguousMixinAnnotations
.addValidationFailure(processMethodContext.getFacetHolder(), Action.class));
processExplicit(processMethodContext, actionIfAny);
processInvocation(processMethodContext, actionIfAny);
processHidden(processMethodContext, actionIfAny);
processRestrictTo(processMethodContext, actionIfAny);
processSemantics(processMethodContext, actionIfAny);
// must come after processing semantics
processCommandPublishing(processMethodContext, actionIfAny);
// must come after processing semantics
processExecutionPublishing(processMethodContext, actionIfAny);
processTypeOf(processMethodContext, actionIfAny);
processChoicesFrom(processMethodContext, actionIfAny);
processFileAccept(processMethodContext, actionIfAny);
}
void processExplicit(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val holder = processMethodContext.getFacetHolder();
// check for @Action at all.
addFacetIfPresent(
ActionExplicitFacetForActionAnnotation
.create(actionIfAny, holder));
}
void processInvocation(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val actionMethod = processMethodContext.getMethod();
try {
val returnType = actionMethod.getReturnType();
val returnSpec = getSpecificationLoader().loadSpecification(returnType);
if (returnSpec == null) {
return;
}
val cls = processMethodContext.getCls();
val typeSpec = getSpecificationLoader().loadSpecification(cls);
if(typeSpec.isMixin()) {
/* don't process mixed in actions, as we would require the actual mixee
* (deferred to post processing) */
//TODO[CAUSEWAY-3409] breaks tests ... return;
}
val holder = processMethodContext.getFacetHolder();
//
// Set up ActionDomainEventFacet, which will act as the hiding/disabling/validating advisor
//
// search for @Action(domainEvent=...), else use the default event type
ActionDomainEventFacet
.createRegular(actionIfAny, typeSpec, holder)
.ifPresent(actionDomainEventFacet->{
addFacet(actionDomainEventFacet);
// replace the current actionInvocationFacet with one that will
// emit the appropriate domain event and then delegate onto the underlying
addFacet(
//TODO[CAUSEWAY-3409] we don't install those for the mixin case, if bailing out above
new ActionInvocationFacetForDomainEvent(
actionDomainEventFacet.getEventType(), actionDomainEventFacet.getEventTypeOrigin(),
actionMethod, typeSpec, returnSpec, holder));
});
} finally {
processMethodContext.removeMethod(actionMethod.asMethodForIntrospection());
}
}
void processHidden(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val facetedMethod = processMethodContext.getFacetHolder();
// search for @Action(hidden=...)
addFacetIfPresent(
HiddenFacetForActionAnnotation
.create(actionIfAny, facetedMethod));
}
void processRestrictTo(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val facetedMethod = processMethodContext.getFacetHolder();
// search for @Action(restrictTo=...)
addFacetIfPresent(
PrototypeFacetForActionAnnotation
.create(
actionIfAny, facetedMethod,
()->super.getSystemEnvironment().getDeploymentType()));
}
void processSemantics(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val facetedMethod = processMethodContext.getFacetHolder();
// check for @Action(semantics=...)
addFacet(
ActionSemanticsFacetForActionAnnotation
.create(actionIfAny, facetedMethod));
}
void processCommandPublishing(
final ProcessMethodContext processMethodContext,
final Optional<Action> actionIfAny) {
val facetedMethod = processMethodContext.getFacetHolder();
//
// this rule inspired by a similar rule for auditing and publishing, see DomainObjectAnnotationFacetFactory
//
if(HasInteractionId.class.isAssignableFrom(processMethodContext.getCls())) {
// do not install on any implementation of HasInteractionId
// (ie commands, audit entries, published events).
return;
}
// check for @Action(commandPublishing=...)
addFacetIfPresent(CommandPublishingFacetForActionAnnotation
.create(actionIfAny, getConfiguration(), getServiceInjector(), facetedMethod));
}
void processExecutionPublishing(
final ProcessMethodContext processMethodContext,
final Optional<Action> actionIfAny) {
val facetedMethod = processMethodContext.getFacetHolder();
//
// this rule inspired by a similar rule for auditing and publishing,
// see DomainObjectAnnotationFacetFactory
// and for commands, see above
//
if(HasInteractionId.class.isAssignableFrom(processMethodContext.getCls())) {
// do not install on any implementation of HasInteractionId
// (ie commands, audit entries, published events).
return;
}
// check for @Action(executionPublishing=...)
addFacetIfPresent(
ExecutionPublishingActionFacetForActionAnnotation
.create(actionIfAny, getConfiguration(), facetedMethod)); // check for @Action(executionPublishing=...)
}
void processTypeOf(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val cls = processMethodContext.getCls();
val method = processMethodContext.getMethod();
val facetedMethod = processMethodContext.getFacetHolder();
val methodReturnType = method.getReturnType();
ProgrammingModelConstants.CollectionSemantics.valueOf(methodReturnType)
.ifPresent(collectionType->{
addFacetIfPresent(
TypeOfFacetForActionAnnotation.create(actionIfAny, collectionType, facetedMethod)
.or(
// else infer from generic type arg if any
()->TypeOfFacet.inferFromMethodReturnType(cls, method, facetedMethod)
));
});
}
void processChoicesFrom(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val holder = processMethodContext.getFacetHolder();
// check for @Action(choicesFrom=...)
addFacetIfPresent(
ChoicesFromFacetForActionAnnotation
.create(actionIfAny, holder));
addFacetIfPresent(
LayoutGroupFacetForActionAnnotation
.create(actionIfAny, holder));
}
void processFileAccept(final ProcessMethodContext processMethodContext, final Optional<Action> actionIfAny) {
val holder = processMethodContext.getFacetHolder();
// check for @Action(fileAccept=...)
addFacetIfPresent(
FileAcceptFacetForActionAnnotation
.create(actionIfAny, holder));
}
}