View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.rest.api.beans;
20  
21  import io.swagger.v3.oas.annotations.Parameter;
22  import io.swagger.v3.oas.annotations.media.Schema;
23  import java.io.Serializable;
24  import javax.validation.constraints.Min;
25  import javax.ws.rs.DefaultValue;
26  import javax.ws.rs.QueryParam;
27  import org.apache.commons.lang3.builder.EqualsBuilder;
28  import org.apache.commons.lang3.builder.HashCodeBuilder;
29  import org.apache.syncope.common.rest.api.service.JAXRSService;
30  
31  public abstract class AbstractQuery implements Serializable {
32  
33      private static final long serialVersionUID = -371488230250055359L;
34  
35      protected abstract static class Builder<Q extends AbstractQuery, B extends Builder<Q, B>> {
36  
37          private Q instance;
38  
39          protected abstract Q newInstance();
40  
41          protected Q getInstance() {
42              if (instance == null) {
43                  instance = newInstance();
44              }
45              return instance;
46          }
47  
48          @SuppressWarnings("unchecked")
49          public B page(final Integer page) {
50              getInstance().setPage(page);
51              return (B) this;
52          }
53  
54          @SuppressWarnings("unchecked")
55          public B size(final Integer size) {
56              getInstance().setSize(size);
57              return (B) this;
58          }
59  
60          @SuppressWarnings("unchecked")
61          public B orderBy(final String orderBy) {
62              getInstance().setOrderBy(orderBy);
63              return (B) this;
64          }
65  
66          public Q build() {
67              return getInstance();
68          }
69      }
70  
71      private Integer page;
72  
73      private Integer size;
74  
75      private String orderBy;
76  
77      @Parameter(name = JAXRSService.PARAM_PAGE, description = "page", schema =
78              @Schema(minimum = "1", implementation = Integer.class, defaultValue = "1"))
79      public Integer getPage() {
80          return page;
81      }
82  
83      @Min(1)
84      @QueryParam(JAXRSService.PARAM_PAGE)
85      @DefaultValue("1")
86      public void setPage(final Integer page) {
87          this.page = page;
88      }
89  
90      @Parameter(name = JAXRSService.PARAM_SIZE, description = "items per page", schema =
91              @Schema(minimum = "1", implementation = Integer.class, defaultValue = "25"))
92      public Integer getSize() {
93          return size;
94      }
95  
96      @Min(1)
97      @QueryParam(JAXRSService.PARAM_SIZE)
98      @DefaultValue("25")
99      public void setSize(final Integer size) {
100         this.size = size;
101     }
102 
103     @Parameter(name = JAXRSService.PARAM_ORDERBY, description = "sorting conditions", schema =
104             @Schema(implementation = String.class, example = "key DESC"))
105     public String getOrderBy() {
106         return orderBy;
107     }
108 
109     @QueryParam(JAXRSService.PARAM_ORDERBY)
110     public void setOrderBy(final String orderBy) {
111         this.orderBy = orderBy;
112     }
113 
114     @Override
115     public boolean equals(final Object obj) {
116         if (this == obj) {
117             return true;
118         }
119         if (obj == null) {
120             return false;
121         }
122         if (getClass() != obj.getClass()) {
123             return false;
124         }
125         AbstractQuery other = (AbstractQuery) obj;
126         return new EqualsBuilder().
127                 append(page, other.page).
128                 append(size, other.size).
129                 append(orderBy, other.orderBy).
130                 build();
131     }
132 
133     @Override
134     public int hashCode() {
135         return new HashCodeBuilder().
136                 append(page).
137                 append(size).
138                 append(orderBy).
139                 build();
140     }
141 }