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.client.console.rest;
20  
21  import java.io.Serializable;
22  import java.util.Comparator;
23  import java.util.List;
24  import org.apache.commons.lang3.ObjectUtils;
25  import org.apache.syncope.client.console.SyncopeConsoleSession;
26  import org.apache.syncope.common.lib.SyncopeClientException;
27  import org.apache.syncope.common.lib.SyncopeConstants;
28  import org.apache.syncope.common.lib.to.AnyTypeTO;
29  import org.apache.syncope.common.lib.types.AnyTypeKind;
30  import org.apache.syncope.common.rest.api.service.AnyTypeService;
31  
32  public class AnyTypeRestClient extends BaseRestClient {
33  
34      private static final long serialVersionUID = -2211371717449597247L;
35  
36      private static final Comparator<AnyTypeTO> COMPARATOR = new AnyTypeComparator();
37  
38      public static final Comparator<String> KEY_COMPARATOR = new AnyTypeKeyComparator();
39  
40      protected static class AnyTypeComparator implements Comparator<AnyTypeTO>, Serializable {
41  
42          private static final long serialVersionUID = -8227715253094467138L;
43  
44          @Override
45          public int compare(final AnyTypeTO o1, final AnyTypeTO o2) {
46              if (o1.getKind() == AnyTypeKind.USER) {
47                  return -1;
48              }
49              if (o2.getKind() == AnyTypeKind.USER) {
50                  return 1;
51              }
52              if (o1.getKind() == AnyTypeKind.GROUP) {
53                  return -1;
54              }
55              if (o2.getKind() == AnyTypeKind.GROUP) {
56                  return 1;
57              }
58              return ObjectUtils.compare(o1.getKey(), o2.getKey());
59          }
60      }
61  
62      protected static class AnyTypeKeyComparator implements Comparator<String>, Serializable {
63  
64          private static final long serialVersionUID = -7778622183107320760L;
65  
66          @Override
67          public int compare(final String o1, final String o2) {
68              if (SyncopeConstants.REALM_ANYTYPE.equals(o1)) {
69                  return -1;
70              }
71              if (SyncopeConstants.REALM_ANYTYPE.equals(o2)) {
72                  return 1;
73              }
74              if (AnyTypeKind.USER.name().equals(o1)) {
75                  return -1;
76              }
77              if (AnyTypeKind.USER.name().equals(o2)) {
78                  return 1;
79              }
80              if (AnyTypeKind.GROUP.name().equals(o1)) {
81                  return -1;
82              }
83              if (AnyTypeKind.GROUP.name().equals(2)) {
84                  return 1;
85              }
86              return ObjectUtils.compare(o1, o2);
87          }
88      }
89  
90      public AnyTypeTO read(final String key) {
91          AnyTypeTO type = null;
92  
93          try {
94              type = getService(AnyTypeService.class).read(key);
95          } catch (SyncopeClientException e) {
96              LOG.error("While reading all any types", e);
97          }
98  
99          return type;
100     }
101 
102     public List<AnyTypeTO> listAnyTypes() {
103         List<AnyTypeTO> types = List.of();
104 
105         try {
106             types = getService(AnyTypeService.class).list();
107             types.sort(COMPARATOR);
108         } catch (SyncopeClientException e) {
109             LOG.error("While reading all any types", e);
110         }
111 
112         return types;
113     }
114 
115     public List<String> list() {
116         List<String> types = SyncopeConsoleSession.get().getAnonymousClient().platform().getAnyTypes();
117         types.sort(KEY_COMPARATOR);
118         return types;
119     }
120 
121     public void create(final AnyTypeTO anyTypeTO) {
122         getService(AnyTypeService.class).create(anyTypeTO);
123     }
124 
125     public void update(final AnyTypeTO anyTypeTO) {
126         getService(AnyTypeService.class).update(anyTypeTO);
127     }
128 
129     public void delete(final String key) {
130         getService(AnyTypeService.class).delete(key);
131     }
132 }