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.notifications;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.stream.Collectors;
25  import org.apache.commons.collections4.CollectionUtils;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.syncope.client.console.panels.search.SearchClause;
28  import org.apache.syncope.client.console.panels.search.SearchUtils;
29  import org.apache.syncope.client.lib.SyncopeClient;
30  import org.apache.syncope.client.ui.commons.wizards.any.EntityWrapper;
31  import org.apache.syncope.common.lib.search.AbstractFiqlSearchConditionBuilder;
32  import org.apache.syncope.common.lib.to.NotificationTO;
33  
34  public class NotificationWrapper extends EntityWrapper<NotificationTO> {
35  
36      private static final long serialVersionUID = 8058288034211558376L;
37  
38      private List<Pair<String, List<SearchClause>>> aboutClauses;
39  
40      private List<SearchClause> recipientClauses;
41  
42      public NotificationWrapper(final NotificationTO notificationTO) {
43          super(notificationTO);
44      }
45  
46      public final String getKey() {
47          return getInnerObject().getKey();
48      }
49  
50      public List<Pair<String, List<SearchClause>>> getAboutClauses() {
51          if (this.aboutClauses == null) {
52              this.aboutClauses = SearchUtils.getSearchClauses(getInnerObject().getAbouts()).entrySet().stream().
53                      map(entry -> Pair.of(entry.getKey(), entry.getValue())).collect(Collectors.toList());
54          }
55  
56          return this.aboutClauses;
57      }
58  
59      public void setAboutClauses(final List<Pair<String, List<SearchClause>>> dynClauses) {
60          this.aboutClauses = dynClauses;
61      }
62  
63      public List<SearchClause> getRecipientClauses() {
64          if (this.recipientClauses == null) {
65              this.recipientClauses = SearchUtils.getSearchClauses(getInnerObject().getRecipientsFIQL());
66          }
67          return this.recipientClauses;
68      }
69  
70      public void setRecipientClauses(final List<SearchClause> dynClauses) {
71          this.recipientClauses = dynClauses;
72      }
73  
74      public Map<String, String> getAboutFIQLs() {
75          if (CollectionUtils.isEmpty(this.aboutClauses) || this.aboutClauses.get(0).getValue().isEmpty()) {
76              return getInnerObject().getAbouts();
77          } else {
78              Map<String, String> res = new HashMap<>();
79              for (Pair<String, List<SearchClause>> pair : this.aboutClauses) {
80                  AbstractFiqlSearchConditionBuilder<?, ?, ?> builder;
81                  switch (pair.getLeft()) {
82                      case "USER":
83                          builder = SyncopeClient.getUserSearchConditionBuilder();
84                          break;
85  
86                      case "GROUP":
87                          builder = SyncopeClient.getGroupSearchConditionBuilder();
88                          break;
89  
90                      default:
91                          builder = SyncopeClient.getAnyObjectSearchConditionBuilder(pair.getLeft());
92                  }
93                  res.put(pair.getLeft(), SearchUtils.buildFIQL(pair.getRight(), builder));
94              }
95              return res;
96          }
97      }
98  
99      private String getRecipientsFIQL() {
100         if (CollectionUtils.isEmpty(this.recipientClauses)) {
101             return null;
102         } else {
103             return SearchUtils.buildFIQL(this.recipientClauses, SyncopeClient.getUserSearchConditionBuilder());
104         }
105     }
106 
107     public NotificationTO fillAboutConditions() {
108         getInnerObject().getAbouts().clear();
109         getInnerObject().getAbouts().putAll(this.getAboutFIQLs());
110         return getInnerObject();
111     }
112 
113     public NotificationTO fillRecipientConditions() {
114         getInnerObject().setRecipientsFIQL(this.getRecipientsFIQL());
115         return getInnerObject();
116     }
117 }