/
ObjectAction.java
476 lines (398 loc) · 18.7 KB
/
ObjectAction.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* 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.isis.core.metamodel.spec.feature;
import java.util.List;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.isis.applib.Identifier;
import org.apache.isis.applib.annotation.ActionLayout;
import org.apache.isis.applib.annotation.ActionSemantics;
import org.apache.isis.applib.annotation.Bulk;
import org.apache.isis.applib.annotation.Where;
import org.apache.isis.applib.filter.Filter;
import org.apache.isis.applib.value.Blob;
import org.apache.isis.applib.value.Clob;
import org.apache.isis.core.commons.authentication.AuthenticationSession;
import org.apache.isis.core.commons.lang.StringFunctions;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.consent.Consent;
import org.apache.isis.core.metamodel.consent.InteractionInvocationMethod;
import org.apache.isis.core.metamodel.facetapi.Facet;
import org.apache.isis.core.metamodel.facetapi.FacetFilters;
import org.apache.isis.core.metamodel.facets.actions.bulk.BulkFacet;
import org.apache.isis.core.metamodel.facets.actions.position.ActionPositionFacet;
import org.apache.isis.core.metamodel.facets.all.named.NamedFacet;
import org.apache.isis.core.metamodel.facets.members.cssclass.CssClassFacet;
import org.apache.isis.core.metamodel.facets.members.cssclassfa.CssClassFaFacet;
import org.apache.isis.core.metamodel.facets.members.order.MemberOrderFacet;
import org.apache.isis.core.metamodel.facets.object.wizard.WizardFacet;
import org.apache.isis.core.metamodel.interactions.AccessContext;
import org.apache.isis.core.metamodel.interactions.ActionInvocationContext;
import org.apache.isis.core.metamodel.interactions.ValidatingInteractionAdvisor;
import org.apache.isis.core.metamodel.spec.ActionType;
import org.apache.isis.core.metamodel.spec.ObjectSpecification;
public interface ObjectAction extends ObjectMember {
// //////////////////////////////////////////////////////
// semantics, realTarget, getOnType
// //////////////////////////////////////////////////////
/**
* The semantics of this action.
*/
ActionSemantics.Of getSemantics();
/**
* Returns the specification for the type of object that this action can be
* invoked upon.
*/
ObjectSpecification getOnType();
boolean promptForParameters(ObjectAdapter target);
// //////////////////////////////////////////////////////////////////
// Type
// //////////////////////////////////////////////////////////////////
ActionType getType();
// //////////////////////////////////////////////////////////////////
// ReturnType
// //////////////////////////////////////////////////////////////////
/**
* Returns the specifications for the return type.
*/
ObjectSpecification getReturnType();
/**
* Returns <tt>true</tt> if the represented action returns a non-void object,
* else returns false.
*/
boolean hasReturn();
// //////////////////////////////////////////////////////////////////
// execute, executeWithRuleChecking
// //////////////////////////////////////////////////////////////////
/**
* Invokes the action's method on the target object given the specified set
* of parameters, checking the visibility, usability and validity first.
*/
ObjectAdapter executeWithRuleChecking(
final ObjectAdapter target,
final ObjectAdapter[] parameters,
final AuthenticationSession authenticationSession,
final Where where) throws AuthorizationException;
/**
* Invokes the action's method on the target object given the specified set
* of parameters.
*/
ObjectAdapter execute(ObjectAdapter target, ObjectAdapter[] parameters);
// //////////////////////////////////////////////////////////////////
// valid
// //////////////////////////////////////////////////////////////////
/**
* Creates an {@link ActionInvocationContext interaction context}
* representing an attempt to invoke this action.
*
* <p>
* Typically it is easier to just call
* {@link #isProposedArgumentSetValid(ObjectAdapter, ObjectAdapter[])
*
* @link #isProposedArgumentSetValidResultSet(ObjectAdapter,
* ObjectAdapter[])}; this is provided as API for symmetry with
* interactions (such as {@link AccessContext} accesses) have no
* corresponding vetoing methods.
*/
public ActionInvocationContext createActionInvocationInteractionContext(AuthenticationSession session, InteractionInvocationMethod invocationMethod, ObjectAdapter targetObject, ObjectAdapter[] proposedArguments);
/**
* Whether the provided argument set is valid, represented as a
* {@link Consent}.
*/
Consent isProposedArgumentSetValid(ObjectAdapter object, ObjectAdapter[] proposedArguments);
// //////////////////////////////////////////////////////
// Parameters (declarative)
// //////////////////////////////////////////////////////
/**
* Returns the number of parameters used by this method.
*/
int getParameterCount();
/**
* Returns set of parameter information.
*
* <p>
* Implementations may build this array lazily or eagerly as required.
*
* @return
*/
List<ObjectActionParameter> getParameters();
/**
* Returns the {@link ObjectSpecification type} of each of the
* {@link #getParameters() parameters}.
*/
List<ObjectSpecification> getParameterTypes();
/**
* Returns set of parameter information matching the supplied filter.
*
* @return
*/
List<ObjectActionParameter> getParameters(
@SuppressWarnings("deprecation") Filter<ObjectActionParameter> filter);
/**
* Returns the parameter with provided id.
*/
ObjectActionParameter getParameterById(String paramId);
/**
* Returns the parameter with provided name.
*/
ObjectActionParameter getParameterByName(String paramName);
// //////////////////////////////////////////////////////
// Parameters (per instance)
// //////////////////////////////////////////////////////
/**
* Returns the defaults references/values to be used for the action.
*/
ObjectAdapter[] getDefaults(ObjectAdapter target);
/**
* Returns a list of possible references/values for each parameter, which
* the user can choose from.
*/
ObjectAdapter[][] getChoices(ObjectAdapter target);
// //////////////////////////////////////////////////////
// Utils
// //////////////////////////////////////////////////////
public static final class Utils {
private Utils() {
}
public static String nameFor(final ObjectAction objAction) {
final String actionName = objAction.getName();
if (actionName != null) {
return actionName;
}
final NamedFacet namedFacet = objAction.getFacet(NamedFacet.class);
if (namedFacet != null) {
return namedFacet.value();
}
return "(no name)";
}
public static boolean returnsBlobOrClob(final ObjectAction objectAction) {
boolean blobOrClob = false;
final ObjectSpecification returnType = objectAction.getReturnType();
if(returnType != null) {
Class<?> cls = returnType.getCorrespondingClass();
if (Blob.class.isAssignableFrom(cls) || Clob.class.isAssignableFrom(cls)) {
blobOrClob = true;
}
}
return blobOrClob;
}
public static boolean isExplorationOrPrototype(final ObjectAction action) {
return action.getType().isExploration() || action.getType().isPrototype();
}
public static String actionIdentifierFor(final ObjectAction action) {
@SuppressWarnings("unused")
final Identifier identifier = action.getIdentifier();
final String className = action.getOnType().getShortIdentifier();
final String actionId = action.getId();
return className + "-" + actionId;
}
public static String descriptionOf(ObjectAction action) {
return action.getDescription();
}
public static ActionLayout.Position actionLayoutPositionOf(ObjectAction action) {
final ActionPositionFacet layoutFacet = action.getFacet(ActionPositionFacet.class);
return layoutFacet != null? layoutFacet.position(): ActionLayout.Position.BELOW;
}
public static String cssClassFaFor(final ObjectAction action) {
final CssClassFaFacet cssClassFaFacet = action.getFacet(CssClassFaFacet.class);
return cssClassFaFacet != null ? cssClassFaFacet.value() : null;
}
public static ActionLayout.CssClassFaPosition cssClassFaPositionFor(final ObjectAction action) {
CssClassFaFacet facet = action.getFacet(CssClassFaFacet.class);
return facet != null ? facet.getPosition() : ActionLayout.CssClassFaPosition.LEFT;
}
public static String cssClassFor(final ObjectAction action, final ObjectAdapter objectAdapter) {
final CssClassFacet cssClassFacet = action.getFacet(CssClassFacet.class);
return cssClassFacet != null ? cssClassFacet.cssClass(objectAdapter) : null;
}
}
// //////////////////////////////////////////////////////
// Predicates
// //////////////////////////////////////////////////////
public static final class Predicates {
private Predicates(){}
public static Predicate<ObjectAction> dynamicallyVisible(final AuthenticationSession session, final ObjectAdapter target, final Where where) {
return org.apache.isis.applib.filter.Filters.asPredicate(Filters.dynamicallyVisible(session, target, where));
}
public static Predicate<ObjectAction> withId(final String actionId) {
return org.apache.isis.applib.filter.Filters.asPredicate(Filters.withId(actionId));
}
public static Predicate<ObjectAction> withNoValidationRules() {
return org.apache.isis.applib.filter.Filters.asPredicate(Filters.withNoValidationRules());
}
public static Predicate<ObjectAction> ofType(final ActionType type) {
return org.apache.isis.applib.filter.Filters.asPredicate(Filters.ofType(type));
}
public static Predicate<ObjectAction> bulk() {
return org.apache.isis.applib.filter.Filters.asPredicate(Filters.bulk());
}
public static Predicate<ObjectAction> notBulkOnly() {
return org.apache.isis.applib.filter.Filters.asPredicate(Filters.notBulkOnly());
}
public static Predicate<ObjectAction> memberOrderOf(ObjectAssociation association) {
return org.apache.isis.applib.filter.Filters.asPredicate(Filters.memberOrderOf(association));
}
}
// //////////////////////////////////////////////////////
// Filters
// //////////////////////////////////////////////////////
public static final class Filters {
private Filters(){}
/**
* @deprecated -use {@link com.google.common.base.Predicate equivalent}
*/
@Deprecated
public static Filter<ObjectAction> dynamicallyVisible(final AuthenticationSession session, final ObjectAdapter target, final Where where) {
return new Filter<ObjectAction>() {
@Override
public boolean accept(final ObjectAction objectAction) {
final Consent visible = objectAction.isVisible(session, target, where);
return visible.isAllowed();
}
};
}
/**
* @deprecated -use {@link com.google.common.base.Predicate equivalent}
*/
@Deprecated
public static Filter<ObjectAction> withId(final String actionId) {
return new Filter<ObjectAction>(){
@Override
public boolean accept(ObjectAction objectAction) {
return objectAction.getId().equals(actionId);
}
};
}
/**
* @deprecated -use {@link com.google.common.base.Predicate equivalent}
*/
@Deprecated
public static Filter<ObjectAction> withNoValidationRules() {
return new Filter<ObjectAction>(){
@Override
public boolean accept(final ObjectAction objectAction) {
final List<Facet> validatingFacets = objectAction.getFacets(FacetFilters.isA(ValidatingInteractionAdvisor.class));
return validatingFacets.isEmpty();
}};
}
/**
* @deprecated -use {@link com.google.common.base.Predicate equivalent}
*/
@Deprecated
public static Filter<ObjectAction> ofType(final ActionType type) {
return new Filter<ObjectAction>(){
@Override
public boolean accept(ObjectAction oa) {
return oa.getType() == type;
}
};
}
/**
* @deprecated -use {@link com.google.common.base.Predicate equivalent}
*/
@Deprecated
public static Filter<ObjectAction> bulk() {
return new Filter<ObjectAction>(){
@Override
public boolean accept(ObjectAction oa) {
if( !oa.containsDoOpFacet(BulkFacet.class)) {
return false;
}
if (oa.getParameterCount() != 0) {
return false;
}
// currently don't support returning Blobs or Clobs
// (because haven't figured out how to rerender the current page, but also to do a download)
ObjectSpecification returnSpec = oa.getReturnType();
if(returnSpec != null) {
Class<?> returnType = returnSpec.getCorrespondingClass();
if(returnType == Blob.class || returnType == Clob.class) {
return false;
}
}
return true;
}};
}
@Deprecated
public static Filter<ObjectAction> notBulkOnly() {
return new Filter<ObjectAction>(){
@Override
public boolean accept(ObjectAction t) {
BulkFacet facet = t.getFacet(BulkFacet.class);
return facet == null || facet.value() != Bulk.AppliesTo.BULK_ONLY;
}};
}
public static Filter<ObjectAction> excludeWizardActions(final ObjectSpecification objectSpecification) {
return org.apache.isis.applib.filter.Filters.not(wizardActions(objectSpecification));
//return wizardActions(objectSpecification);
}
private static Filter<ObjectAction> wizardActions(final ObjectSpecification objectSpecification) {
return new Filter<ObjectAction>() {
@Override
public boolean accept(ObjectAction input) {
if(objectSpecification == null) {
return false;
}
final WizardFacet wizardFacet = objectSpecification.getFacet(WizardFacet.class);
return wizardFacet != null && wizardFacet.isWizardAction(input);
}
};
}
@SuppressWarnings("deprecation")
public static Filter<ObjectAction> memberOrderOf(ObjectAssociation association) {
final String assocName = association.getName();
final String assocId = association.getId();
return new Filter<ObjectAction>() {
@Override
public boolean accept(ObjectAction t) {
final MemberOrderFacet memberOrderFacet = t.getFacet(MemberOrderFacet.class);
if(memberOrderFacet == null || Strings.isNullOrEmpty(memberOrderFacet.name())) {
return false;
}
final String memberOrderName = memberOrderFacet.name().toLowerCase();
if(Strings.isNullOrEmpty(memberOrderName)) {
return false;
}
return memberOrderName.equalsIgnoreCase(assocName) || memberOrderName.equalsIgnoreCase(assocId);
}
};
}
public static Filter<ObjectAction> memberOrderNotAssociationOf(final ObjectSpecification adapterSpec) {
final List<ObjectAssociation> associations = adapterSpec.getAssociations(Contributed.INCLUDED);
final List<String> associationNames = Lists.transform(associations, Functions.compose(StringFunctions.toLowerCase(), ObjectAssociation.Functions.toName()));
final List<String> associationIds = Lists.transform(associations, Functions.compose(StringFunctions.toLowerCase(), ObjectAssociation.Functions.toId()));
return new Filter<ObjectAction>() {
@Override
public boolean accept(ObjectAction t) {
final MemberOrderFacet memberOrderFacet = t.getFacet(MemberOrderFacet.class);
if(memberOrderFacet == null || Strings.isNullOrEmpty(memberOrderFacet.name())) {
return true;
}
String memberOrderName = memberOrderFacet.name().toLowerCase();
if(Strings.isNullOrEmpty(memberOrderName)) {
return false;
}
return !associationNames.contains(memberOrderName) && !associationIds.contains(memberOrderName);
}
};
}
}
}