1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.api.models;
18
19 import static org.forgerock.api.enums.CreateMode.*;
20
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.Objects;
24
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
27 import org.forgerock.api.ApiValidationException;
28 import org.forgerock.api.enums.CreateMode;
29
30
31
32
33 @JsonDeserialize(builder = Create.Builder.class)
34 public final class Create extends Operation {
35
36 private final CreateMode mode;
37 private final Boolean singleton;
38
39
40
41
42
43
44 private Create(Builder builder) {
45 super(builder);
46 this.mode = builder.mode;
47 this.singleton = builder.singleton;
48
49 if (mode == null) {
50 throw new ApiValidationException("mode required");
51 }
52 }
53
54
55
56
57
58
59 public CreateMode getMode() {
60 return mode;
61 }
62
63
64
65
66
67
68 public Boolean isSingleton() {
69 return singleton;
70 }
71
72
73
74
75
76
77 public static final Builder create() {
78 return new Builder();
79 }
80
81
82
83
84
85
86 @Override
87 protected void allocateToResource(Resource.Builder resourceBuilder) {
88 resourceBuilder.create(this);
89 }
90
91
92
93
94
95
96
97
98
99
100 public static Create fromAnnotation(org.forgerock.api.annotations.Create create, boolean instanceOperations,
101 ApiDescription descriptor, Class<?> relativeType) {
102 List<CreateMode> modes = Arrays.asList(create.modes());
103 if ((instanceOperations && !modes.contains(ID_FROM_CLIENT))
104 || (!instanceOperations && !modes.contains(ID_FROM_SERVER))) {
105 return null;
106 }
107 return create()
108 .detailsFromAnnotation(create.operationDescription(), descriptor, relativeType)
109 .mode(instanceOperations ? ID_FROM_CLIENT : ID_FROM_SERVER)
110 .build();
111 }
112
113 @Override
114 public boolean equals(Object o) {
115 if (this == o) {
116 return true;
117 }
118 if (o == null || getClass() != o.getClass()) {
119 return false;
120 }
121 if (!super.equals(o)) {
122 return false;
123 }
124 Create create = (Create) o;
125 return mode == create.mode
126 && Objects.equals(singleton, create.singleton);
127 }
128
129 @Override
130 public int hashCode() {
131 return Objects.hash(super.hashCode(), mode, singleton);
132 }
133
134
135
136
137 public static final class Builder extends Operation.Builder<Builder> {
138
139 private CreateMode mode;
140 private Boolean singleton = false;
141
142 private Builder() {
143 super();
144 }
145
146
147
148
149
150
151
152
153 @JsonProperty("mode")
154 public Builder mode(CreateMode mode) {
155 this.mode = mode;
156 return this;
157 }
158
159
160
161
162
163
164
165 @JsonProperty("singleton")
166 public Builder singleton(Boolean singleton) {
167 this.singleton = singleton;
168 return this;
169 }
170
171
172
173
174
175
176 @Override
177 protected Builder self() {
178 return this;
179 }
180
181
182
183
184
185
186 public Create build() {
187 return new Create(this);
188 }
189 }
190
191 }