View Javadoc
1   /*
2    * Copyright 2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.acl.spring.data.mongodb.convert;
18  
19  import java.util.List;
20  import java.util.Objects;
21  import org.bremersee.acl.Ace;
22  import org.bson.Document;
23  import org.springframework.core.convert.converter.Converter;
24  import org.springframework.data.convert.ReadingConverter;
25  import org.springframework.lang.NonNull;
26  
27  /**
28   * The document to ace converter.
29   *
30   * @author Christian Bremer
31   */
32  @ReadingConverter
33  public class DocumentToAceConverter implements Converter<Document, Ace> {
34  
35    /**
36     * Instantiates a new document to ace converter.
37     */
38    public DocumentToAceConverter() {
39    }
40  
41    @Override
42    public Ace convert(@NonNull Document source) {
43      return Ace.builder()
44          .guest(source.getBoolean(Ace.GUEST, false))
45          .users(source.getList(Ace.USERS, String.class, List.of()))
46          .roles(source.getList(Ace.ROLES, String.class, List.of()))
47          .groups(source.getList(Ace.GROUPS, String.class, List.of()))
48          .build();
49    }
50  
51    @Override
52    public boolean equals(Object o) {
53      if (this == o) {
54        return true;
55      }
56      return o != null && getClass() == o.getClass();
57    }
58  
59    @Override
60    public int hashCode() {
61      return Objects.hashCode(getClass());
62    }
63  }