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.panels.search;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.syncope.client.console.SyncopeConsoleSession;
28  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
29  import org.apache.syncope.client.console.commons.IdRepoConstants;
30  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
31  import org.apache.syncope.client.console.panels.DirectoryPanel;
32  import org.apache.syncope.client.console.rest.FIQLQueryRestClient;
33  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.KeyPropertyColumn;
34  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
35  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
36  import org.apache.syncope.client.ui.commons.Constants;
37  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
38  import org.apache.syncope.common.lib.SyncopeClientException;
39  import org.apache.syncope.common.lib.to.FIQLQueryTO;
40  import org.apache.wicket.PageReference;
41  import org.apache.wicket.ajax.AjaxRequestTarget;
42  import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
43  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
44  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
45  import org.apache.wicket.model.CompoundPropertyModel;
46  import org.apache.wicket.model.IModel;
47  import org.apache.wicket.model.Model;
48  import org.apache.wicket.model.StringResourceModel;
49  
50  public class FIQLQueryDirectoryPanel extends DirectoryPanel<
51          FIQLQueryTO, FIQLQueryTO, DirectoryDataProvider<FIQLQueryTO>, FIQLQueryRestClient> {
52  
53      private static final long serialVersionUID = -913956855318099854L;
54  
55      private final AbstractSearchPanel searchPanel;
56  
57      private final String target;
58  
59      private final FIQLQueries parent;
60  
61      public FIQLQueryDirectoryPanel(
62              final String id,
63              final FIQLQueryRestClient restClient,
64              final AbstractSearchPanel searchPanel,
65              final String target,
66              final FIQLQueries parent,
67              final PageReference pageRef) {
68  
69          super(id, restClient, pageRef, false);
70          this.target = target;
71          this.searchPanel = searchPanel;
72          this.parent = parent;
73  
74          disableCheckBoxes();
75  
76          initResultTable();
77      }
78  
79      @Override
80      protected List<IColumn<FIQLQueryTO, String>> getColumns() {
81          List<IColumn<FIQLQueryTO, String>> columns = new ArrayList<>();
82  
83          columns.add(new KeyPropertyColumn<>(
84                  new StringResourceModel(Constants.KEY_FIELD_NAME, this), Constants.KEY_FIELD_NAME));
85  
86          columns.add(new PropertyColumn<>(new StringResourceModel(
87                  Constants.NAME_FIELD_NAME, this), Constants.NAME_FIELD_NAME, Constants.NAME_FIELD_NAME));
88  
89          columns.add(new PropertyColumn<>(Model.of("FIQL"), "fiql"));
90  
91          return columns;
92      }
93  
94      @Override
95      protected ActionsPanel<FIQLQueryTO> getActions(final IModel<FIQLQueryTO> model) {
96          ActionsPanel<FIQLQueryTO> panel = super.getActions(model);
97  
98          panel.add(new ActionLink<>() {
99  
100             private static final long serialVersionUID = -3722207913631435501L;
101 
102             @Override
103             public void onClick(final AjaxRequestTarget target, final FIQLQueryTO ignore) {
104                 searchPanel.updateFIQL(target, model.getObject().getFiql());
105                 parent.close(target);
106             }
107         }, ActionLink.ActionType.SELECT, StringUtils.EMPTY);
108 
109         panel.add(new ActionLink<>() {
110 
111             private static final long serialVersionUID = -3722207913631435501L;
112 
113             @Override
114             public void onClick(final AjaxRequestTarget target, final FIQLQueryTO ignore) {
115                 try {
116                     restClient.delete(model.getObject().getKey());
117 
118                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
119                     customActionOnFinishCallback(target);
120                 } catch (SyncopeClientException e) {
121                     LOG.error("While deleting {}", model.getObject().getName(), e);
122                     SyncopeConsoleSession.get().onException(e);
123                 }
124                 ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
125             }
126         }, ActionLink.ActionType.DELETE, StringUtils.EMPTY);
127 
128         return panel;
129     }
130 
131     @Override
132     protected Collection<ActionLink.ActionType> getBatches() {
133         return Set.of();
134     }
135 
136     @Override
137     protected DirectoryDataProvider<FIQLQueryTO> dataProvider() {
138         return new FIQLQueryDataProvider(rows);
139     }
140 
141     @Override
142     protected String paginatorRowsKey() {
143         return IdRepoConstants.PREF_FIQL_QUERIES_PAGINATOR_ROWS;
144     }
145 
146     protected class FIQLQueryDataProvider extends DirectoryDataProvider<FIQLQueryTO> {
147 
148         private static final long serialVersionUID = 4725679400450513556L;
149 
150         protected final SortableDataProviderComparator<FIQLQueryTO> comparator;
151 
152         public FIQLQueryDataProvider(final int paginatorRows) {
153             super(paginatorRows);
154 
155             //Default sorting
156             setSort(Constants.NAME_FIELD_NAME, SortOrder.ASCENDING);
157             comparator = new SortableDataProviderComparator<>(this);
158         }
159 
160         @Override
161         public Iterator<FIQLQueryTO> iterator(final long first, final long count) {
162             List<FIQLQueryTO> list = restClient.list(target);
163             list.sort(comparator);
164             return list.subList((int) first, (int) (first + count)).iterator();
165         }
166 
167         @Override
168         public long size() {
169             return restClient.list(target).size();
170         }
171 
172         @Override
173         public IModel<FIQLQueryTO> model(final FIQLQueryTO object) {
174             return new CompoundPropertyModel<>(object);
175         }
176     }
177 }