1 /*
2 * The contents of this file are subject to the terms of the Common Development and
3 * Distribution License (the License). You may not use this file except in compliance with the
4 * License.
5 *
6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7 * specific language governing permission and limitations under the License.
8 *
9 * When distributing Covered Software, include this CDDL Header Notice in each file and include
10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11 * Header, with the fields enclosed by brackets [] replaced by your own identifying
12 * information: "Portions copyright [year] [name of copyright owner]".
13 *
14 * Copyright 2016 ForgeRock AS.
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 * Class that represents the Create Operation type in API descriptor.
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 * Protected contstructor of the Create.
41 *
42 * @param builder Operation Builder
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 * Getter of the mode.
56 *
57 * @return Mode
58 */
59 public CreateMode getMode() {
60 return mode;
61 }
62
63 /**
64 * Informs if operation creates singleton resources.
65 *
66 * @return {@code true} if operation creates singleton resources and {@code false} otherwise
67 */
68 public Boolean isSingleton() {
69 return singleton;
70 }
71
72 /**
73 * Creates a new builder for Create.
74 *
75 * @return New builder instance
76 */
77 public static final Builder create() {
78 return new Builder();
79 }
80
81 /**
82 * Allocates the Create operation type to the given Resource Builder.
83 *
84 * @param resourceBuilder - Resource Builder to add the operation
85 */
86 @Override
87 protected void allocateToResource(Resource.Builder resourceBuilder) {
88 resourceBuilder.create(this);
89 }
90
91 /**
92 * Builds a Create object from the data in the annotation.
93 *
94 * @param create Create annotation that holds the data
95 * @param instanceOperations True if the resource is performing instance operations.
96 * @param descriptor The root descriptor to add definitions to.
97 * @param relativeType The type relative to which schema resources should be resolved.
98 * @return Create instance
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 * Builder for the Create.
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 * Setter for create-mode.
149 *
150 * @param mode Create-mode
151 * @return Builder
152 */
153 @JsonProperty("mode")
154 public Builder mode(CreateMode mode) {
155 this.mode = mode;
156 return this;
157 }
158
159 /**
160 * Setter for singleton.
161 *
162 * @param singleton Specifies that create operates on a singleton as opposed to a collection.
163 * @return Builder
164 */
165 @JsonProperty("singleton")
166 public Builder singleton(Boolean singleton) {
167 this.singleton = singleton;
168 return this;
169 }
170
171 /**
172 * Returns the builder so this.
173 *
174 * @return this
175 */
176 @Override
177 protected Builder self() {
178 return this;
179 }
180
181 /**
182 * Builds the Create instance.
183 *
184 * @return Create instance
185 */
186 public Create build() {
187 return new Create(this);
188 }
189 }
190
191 }