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.fit.core.reference.flowable;
20  
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.stream.Collectors;
25  import org.apache.syncope.common.lib.types.AnyTypeKind;
26  import org.apache.syncope.core.flowable.api.DropdownValueProvider;
27  import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
28  import org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond;
29  import org.apache.syncope.core.persistence.api.dao.search.OrderByClause;
30  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
31  import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  public class PrintersValueProvider implements DropdownValueProvider {
35  
36      private static final SearchCond PRINTER_COND;
37  
38      private static final List<OrderByClause> ORDER_BY;
39  
40      static {
41          AnyTypeCond anyTypeCond = new AnyTypeCond();
42          anyTypeCond.setAnyTypeKey("PRINTER");
43          PRINTER_COND = SearchCond.getLeaf(anyTypeCond);
44  
45          OrderByClause orderByNameAsc = new OrderByClause();
46          orderByNameAsc.setField("name");
47          orderByNameAsc.setDirection(OrderByClause.Direction.ASC);
48          ORDER_BY = List.of(orderByNameAsc);
49      }
50  
51      private final AnySearchDAO anySearchDAO;
52  
53      public PrintersValueProvider(final AnySearchDAO anySearchDAO) {
54          this.anySearchDAO = anySearchDAO;
55      }
56  
57      @Transactional(readOnly = true)
58      @Override
59      public Map<String, String> getValues() {
60          return anySearchDAO.<AnyObject>search(PRINTER_COND, ORDER_BY, AnyTypeKind.ANY_OBJECT).stream().
61                  collect(Collectors.toMap(
62                          AnyObject::getKey,
63                          AnyObject::getName,
64                          (u, v) -> {
65                              throw new IllegalStateException(String.format("Duplicate key %s", u));
66                          },
67                          LinkedHashMap::new));
68      }
69  }