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;
20  
21  import java.lang.reflect.Field;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.syncope.client.console.SyncopeConsoleSession;
29  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
30  import org.apache.syncope.client.console.commons.IdRepoConstants;
31  import org.apache.syncope.client.console.commons.KeywordSearchEvent;
32  import org.apache.syncope.client.console.commons.SortableDataProviderComparator;
33  import org.apache.syncope.client.console.pages.BasePage;
34  import org.apache.syncope.client.console.panels.SchemaTypePanel.SchemaProvider;
35  import org.apache.syncope.client.console.rest.SchemaRestClient;
36  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.BooleanPropertyColumn;
37  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
38  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
39  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
40  import org.apache.syncope.client.ui.commons.Constants;
41  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
42  import org.apache.syncope.common.lib.to.SchemaTO;
43  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
44  import org.apache.syncope.common.lib.types.SchemaType;
45  import org.apache.wicket.PageReference;
46  import org.apache.wicket.ajax.AjaxRequestTarget;
47  import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
48  import org.apache.wicket.event.Broadcast;
49  import org.apache.wicket.event.IEvent;
50  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
51  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
52  import org.apache.wicket.model.CompoundPropertyModel;
53  import org.apache.wicket.model.IModel;
54  import org.apache.wicket.model.ResourceModel;
55  import org.springframework.util.ReflectionUtils;
56  
57  public class SchemaTypePanel extends TypesDirectoryPanel<SchemaTO, SchemaProvider, SchemaRestClient> {
58  
59      private static final long serialVersionUID = 3905038169553185171L;
60  
61      protected static final Map<SchemaType, List<String>> COL_NAMES = Map.of(
62              SchemaType.PLAIN,
63              List.of(Constants.KEY_FIELD_NAME,
64                      "type", "mandatoryCondition", "uniqueConstraint", "multivalue", "readonly"),
65              SchemaType.DERIVED,
66              List.of(Constants.KEY_FIELD_NAME, "expression"),
67              SchemaType.VIRTUAL,
68              List.of(Constants.KEY_FIELD_NAME, "resource", "anyType", "extAttrName", "readonly"));
69  
70      protected final SchemaType schemaType;
71  
72      protected String keyword;
73  
74      public SchemaTypePanel(
75              final String id,
76              final SchemaRestClient restClient,
77              final SchemaType schemaType,
78              final PageReference pageRef) {
79  
80          super(id, restClient, true, pageRef);
81          this.schemaType = schemaType;
82  
83          disableCheckBoxes();
84  
85          try {
86              addNewItemPanelBuilder(new SchemaTypeWizardBuilder(
87                      schemaType.getToClass().getDeclaredConstructor().newInstance(), restClient, pageRef), true);
88          } catch (Exception e) {
89              LOG.error("Error creating instance of {}", schemaType, e);
90          }
91  
92          initResultTable();
93          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.SCHEMA_CREATE);
94      }
95  
96      @Override
97      protected void setWindowClosedReloadCallback(final BaseModal<?> modal) {
98          modal.setWindowClosedCallback(target -> {
99              target.add(SchemaTypePanel.this);
100             modal.show(false);
101         });
102     }
103 
104     @Override
105     protected SchemaProvider dataProvider() {
106         return new SchemaProvider(rows, schemaType);
107     }
108 
109     @Override
110     protected String paginatorRowsKey() {
111         return IdRepoConstants.PREF_ANYTYPE_PAGINATOR_ROWS;
112     }
113 
114     @Override
115     protected Collection<ActionLink.ActionType> getBatches() {
116         return List.of();
117     }
118 
119     @Override
120     protected List<IColumn<SchemaTO, String>> getColumns() {
121         final List<IColumn<SchemaTO, String>> columns = new ArrayList<>();
122 
123         for (String field : COL_NAMES.get(schemaType)) {
124             Field clazzField = ReflectionUtils.findField(schemaType.getToClass(), field);
125 
126             if (clazzField != null && !clazzField.isSynthetic()) {
127                 if (clazzField.getType().equals(Boolean.class) || clazzField.getType().equals(boolean.class)) {
128                     columns.add(new BooleanPropertyColumn<>(new ResourceModel(field), field, field));
129                 } else {
130                     IColumn<SchemaTO, String> column = new PropertyColumn<>(
131                             new ResourceModel(field), field, field) {
132 
133                         private static final long serialVersionUID = 3282547854226892169L;
134 
135                         @Override
136                         public String getCssClass() {
137                             String css = super.getCssClass();
138                             if (Constants.KEY_FIELD_NAME.equals(field)) {
139                                 css = StringUtils.isBlank(css)
140                                         ? "col-xs-1"
141                                         : css + " col-xs-1";
142                             }
143                             return css;
144                         }
145                     };
146                     columns.add(column);
147                 }
148             }
149         }
150 
151         return columns;
152     }
153 
154     @Override
155     public ActionsPanel<SchemaTO> getActions(final IModel<SchemaTO> model) {
156         ActionsPanel<SchemaTO> panel = super.getActions(model);
157         panel.add(new ActionLink<>() {
158 
159             private static final long serialVersionUID = -3722207913631435501L;
160 
161             @Override
162             public void onClick(final AjaxRequestTarget target, final SchemaTO ignore) {
163                 send(SchemaTypePanel.this, Broadcast.EXACT, new AjaxWizard.EditItemActionEvent<>(
164                         restClient.read(schemaType, model.getObject().getKey()), target));
165             }
166         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.SCHEMA_UPDATE);
167         panel.add(new ActionLink<>() {
168 
169             private static final long serialVersionUID = -3722207913631435501L;
170 
171             @Override
172             public void onClick(final AjaxRequestTarget target, final SchemaTO ignore) {
173                 try {
174                     restClient.delete(schemaType, model.getObject().getKey());
175 
176                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
177                     target.add(container);
178                 } catch (Exception e) {
179                     LOG.error("While deleting {}", model.getObject(), e);
180                     SyncopeConsoleSession.get().onException(e);
181                 }
182                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
183             }
184         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.SCHEMA_DELETE, true);
185 
186         return panel;
187     }
188 
189     @Override
190     public void onEvent(final IEvent<?> event) {
191         if (event.getPayload() instanceof KeywordSearchEvent) {
192             KeywordSearchEvent payload = KeywordSearchEvent.class.cast(event.getPayload());
193 
194             keyword = payload.getKeyword();
195             if (StringUtils.isNotBlank(keyword)) {
196                 if (!StringUtils.startsWith(keyword, "*")) {
197                     keyword = "*" + keyword;
198                 }
199                 if (!StringUtils.endsWith(keyword, "*")) {
200                     keyword += "*";
201                 }
202             }
203 
204             updateResultTable(payload.getTarget());
205         } else {
206             super.onEvent(event);
207         }
208     }
209 
210     protected final class SchemaProvider extends DirectoryDataProvider<SchemaTO> {
211 
212         private static final long serialVersionUID = -185944053385660794L;
213 
214         private final SortableDataProviderComparator<SchemaTO> comparator;
215 
216         private final SchemaType schemaType;
217 
218         private SchemaProvider(final int paginatorRows, final SchemaType schemaType) {
219             super(paginatorRows);
220             this.schemaType = schemaType;
221             comparator = new SortableDataProviderComparator<>(this);
222         }
223 
224         @Override
225         public Iterator<SchemaTO> iterator(final long first, final long count) {
226             List<SchemaTO> schemas = restClient.getSchemas(this.schemaType, keyword);
227             schemas.sort(comparator);
228             return schemas.subList((int) first, (int) first + (int) count).iterator();
229         }
230 
231         @Override
232         public long size() {
233             return restClient.getSchemas(this.schemaType, keyword).size();
234         }
235 
236         @Override
237         public IModel<SchemaTO> model(final SchemaTO object) {
238             return new CompoundPropertyModel<>(object);
239         }
240     }
241 }