|
9 | 9 |
|
10 | 10 | import lombok.Getter;
|
11 | 11 |
|
12 |
| -public abstract class SubscriptionObject { |
| 12 | +public abstract class SubscriptionObject<T extends SubscriptionObject<?>> { |
13 | 13 | private static final String SUBSCRIPTION_TEMPLATE_PATH = "subscriptionsTemplate.json";
|
14 | 14 |
|
| 15 | + // reference to self as the subclass type |
| 16 | + protected final T self; |
| 17 | + |
15 | 18 | @Getter
|
16 | 19 | protected JSONObject subscriptionJson;
|
17 | 20 |
|
18 | 21 | /**
|
19 | 22 | * Creates a subscription with a specific name.
|
| 23 | + * |
20 | 24 | * @param subscriptionName
|
21 | 25 | * @throws IOException
|
22 | 26 | */
|
23 |
| - public SubscriptionObject(String subscriptionName) throws IOException { |
| 27 | + public SubscriptionObject(final Class<T> selfClass, String subscriptionName) throws IOException { |
24 | 28 | subscriptionJson = Utils.getResourceFileAsJsonObject(SUBSCRIPTION_TEMPLATE_PATH);
|
25 | 29 | subscriptionJson.put("subscriptionName", subscriptionName);
|
| 30 | + this.self = selfClass.cast(this); |
26 | 31 | }
|
27 | 32 |
|
28 | 33 | /**
|
29 | 34 | * Adds a notification message to the subscriptionObject as key value.
|
| 35 | + * |
30 | 36 | * @param notificationKey
|
31 | 37 | * @param notificationValue
|
| 38 | + * @return |
32 | 39 | */
|
33 |
| - public void addNotificationMessageKeyValue(String notificationKey, String notificationValue) { |
| 40 | + public T addNotificationMessageKeyValue(String notificationKey, String notificationValue) { |
34 | 41 | JSONObject keyValue = new JSONObject();
|
35 | 42 | keyValue.put("formkey", notificationKey);
|
36 | 43 | keyValue.put("formvalue", notificationValue);
|
37 | 44 |
|
38 |
| - JSONArray notificationMessageKeyValue = subscriptionJson.getJSONArray("notificationMessageKeyValues"); |
| 45 | + JSONArray notificationMessageKeyValue = subscriptionJson.getJSONArray( |
| 46 | + "notificationMessageKeyValues"); |
39 | 47 | notificationMessageKeyValue.put(keyValue);
|
| 48 | + return this.self; |
40 | 49 | }
|
41 | 50 |
|
42 | 51 | /**
|
43 |
| - * Adds a condition to a requirement, the condition is objectNode containing key value for the jmesPath expression |
| 52 | + * Adds a condition to a requirement, the condition is objectNode containing key value for the |
| 53 | + * jmesPath expression |
| 54 | + * |
44 | 55 | * @param requirementIndex
|
45 | 56 | * @param condition
|
| 57 | + * @return |
46 | 58 | */
|
47 |
| - public void addConditionToRequirement(int requirementIndex, JSONObject condition) { |
48 |
| - JSONArray requirements = subscriptionJson.getJSONArray("requirements"); |
| 59 | + public T addConditionToRequirement(int requirementIndex, JSONObject condition) { |
| 60 | + JSONArray requirements = subscriptionJson.getJSONArray("requirements"); |
49 | 61 | JSONObject requirement = requirements.getJSONObject(requirementIndex);
|
50 | 62 | JSONArray conditions = requirement.getJSONArray("conditions");
|
51 | 63 | conditions.put(condition);
|
| 64 | + return this.self; |
52 | 65 | }
|
53 | 66 |
|
54 | 67 | /**
|
55 | 68 | * Sets the field notificationMeta to the wanted value
|
| 69 | + * |
56 | 70 | * @param notificationMeta
|
| 71 | + * @return |
57 | 72 | */
|
58 |
| - public void setNotificationMeta(String notificationMeta) { |
| 73 | + public T setNotificationMeta(String notificationMeta) { |
59 | 74 | subscriptionJson.put("notificationMeta", notificationMeta);
|
| 75 | + return this.self; |
60 | 76 | }
|
61 | 77 |
|
62 | 78 | /**
|
63 |
| - * Set the restPostBodyMediaType field in the subscriptions to wanted value for example "application/x-www-form-urlencoded" |
| 79 | + * Set the restPostBodyMediaType field in the subscriptions to wanted value for example |
| 80 | + * "application/x-www-form-urlencoded" |
| 81 | + * |
64 | 82 | * @param value
|
| 83 | + * @return |
65 | 84 | */
|
66 |
| - public void setRestPostBodyMediaType(String restPostBodyMediaType) { |
| 85 | + public T setRestPostBodyMediaType(String restPostBodyMediaType) { |
67 | 86 | subscriptionJson.put("restPostBodyMediaType", restPostBodyMediaType);
|
| 87 | + return this.self; |
68 | 88 | }
|
69 | 89 |
|
70 | 90 | /**
|
71 |
| - * Returns the subscription as an array with the subscription. (This is the way Eiffel-intelligence stores it.) |
| 91 | + * Returns the subscription as an array with the subscription. (This is the way |
| 92 | + * Eiffel-intelligence stores it.) |
| 93 | + * |
72 | 94 | * @return ArrayNode
|
73 | 95 | */
|
74 | 96 | public JSONArray getAsSubscriptions() {
|
|
0 commit comments