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.lang.reflect.Modifier;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.syncope.client.console.SyncopeConsoleSession;
30  import org.apache.syncope.client.console.commons.DirectoryDataProvider;
31  import org.apache.syncope.client.console.commons.IdRepoConstants;
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.rest.AnyTypeRestClient;
35  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.BooleanPropertyColumn;
36  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
37  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
38  import org.apache.syncope.client.ui.commons.Constants;
39  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
40  import org.apache.syncope.client.ui.commons.panels.WizardModalPanel;
41  import org.apache.syncope.client.ui.commons.wizards.AbstractModalPanelBuilder;
42  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
43  import org.apache.syncope.common.lib.to.AnyTypeTO;
44  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
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.extensions.markup.html.repeater.data.table.IColumn;
50  import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
51  import org.apache.wicket.model.CompoundPropertyModel;
52  import org.apache.wicket.model.IModel;
53  import org.apache.wicket.model.ResourceModel;
54  
55  public class AnyTypesPanel extends TypesDirectoryPanel<AnyTypeTO, AnyTypesPanel.AnyTypeProvider, AnyTypeRestClient> {
56  
57      private static final long serialVersionUID = 3905038169553185171L;
58  
59      public AnyTypesPanel(final String id, final AnyTypeRestClient restClient, final PageReference pageRef) {
60          super(id, restClient, false, pageRef);
61          disableCheckBoxes();
62  
63          this.addNewItemPanelBuilder(new AbstractModalPanelBuilder<AnyTypeTO>(new AnyTypeTO(), pageRef) {
64  
65              private static final long serialVersionUID = -6388405037134399367L;
66  
67              @Override
68              public WizardModalPanel<AnyTypeTO> build(final String id, final int index, final AjaxWizard.Mode mode) {
69                  final AnyTypeTO modelObject = newModelObject();
70                  return new AnyTypeModalPanel(modal, modelObject, pageRef) {
71  
72                      private static final long serialVersionUID = -6227956682141146095L;
73  
74                      @Override
75                      public void onSubmit(final AjaxRequestTarget target) {
76                          try {
77                              if (getOriginalItem() == null || StringUtils.isBlank(getOriginalItem().getKey())) {
78                                  restClient.create(modelObject);
79                                  SyncopeConsoleSession.get().refreshAuth(null);
80                              } else {
81                                  restClient.update(modelObject);
82                              }
83  
84                              SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
85                              AnyTypesPanel.this.updateResultTable(target);
86                              modal.close(target);
87                          } catch (Exception e) {
88                              LOG.error("While creating or updating {}", modelObject, e);
89                              SyncopeConsoleSession.get().onException(e);
90                          }
91                          ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
92                      }
93                  };
94              }
95          }, true);
96  
97          initResultTable();
98          MetaDataRoleAuthorizationStrategy.authorize(addAjaxLink, RENDER, IdRepoEntitlement.ANYTYPE_CREATE);
99      }
100 
101     @Override
102     protected AnyTypeProvider dataProvider() {
103         return new AnyTypeProvider(rows);
104     }
105 
106     @Override
107     protected String paginatorRowsKey() {
108         return IdRepoConstants.PREF_ANYTYPE_PAGINATOR_ROWS;
109     }
110 
111     @Override
112     protected Collection<ActionLink.ActionType> getBatches() {
113         return List.of();
114     }
115 
116     @Override
117     protected List<IColumn<AnyTypeTO, String>> getColumns() {
118         final List<IColumn<AnyTypeTO, String>> columns = new ArrayList<>();
119 
120         for (Field field : AnyTypeTO.class.getDeclaredFields()) {
121             if (!field.isSynthetic() && !Modifier.isStatic(field.getModifiers())) {
122                 final String fieldName = field.getName();
123                 if (field.getType().isArray()
124                         || Collection.class.isAssignableFrom(field.getType())
125                         || Map.class.isAssignableFrom(field.getType())) {
126 
127                     columns.add(new PropertyColumn<>(
128                             new ResourceModel(field.getName()), field.getName()));
129                 } else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
130                     columns.add(new BooleanPropertyColumn<>(
131                             new ResourceModel(field.getName()), field.getName(), field.getName()));
132                 } else {
133                     columns.add(new PropertyColumn<>(
134                             new ResourceModel(field.getName()), field.getName(), field.getName()) {
135 
136                         private static final long serialVersionUID = -6902459669035442212L;
137 
138                         @Override
139                         public String getCssClass() {
140                             String css = super.getCssClass();
141                             if (Constants.KEY_FIELD_NAME.equals(fieldName)) {
142                                 css = StringUtils.isBlank(css)
143                                         ? "col-xs-1"
144                                         : css + " col-xs-1";
145                             }
146                             return css;
147                         }
148                     });
149                 }
150             }
151         }
152 
153         return columns;
154     }
155 
156     @Override
157     public ActionsPanel<AnyTypeTO> getActions(final IModel<AnyTypeTO> model) {
158         final ActionsPanel<AnyTypeTO> panel = super.getActions(model);
159 
160         panel.add(new ActionLink<>() {
161 
162             private static final long serialVersionUID = -3722207913631435501L;
163 
164             @Override
165             public void onClick(final AjaxRequestTarget target, final AnyTypeTO ignore) {
166                 send(AnyTypesPanel.this, Broadcast.EXACT,
167                         new AjaxWizard.EditItemActionEvent<>(model.getObject(), target));
168             }
169         }, ActionLink.ActionType.EDIT, IdRepoEntitlement.ANYTYPE_UPDATE);
170         panel.add(new ActionLink<>() {
171 
172             private static final long serialVersionUID = -3722207913631435501L;
173 
174             @Override
175             public void onClick(final AjaxRequestTarget target, final AnyTypeTO ignore) {
176                 try {
177                     restClient.delete(model.getObject().getKey());
178                     SyncopeConsoleSession.get().refreshAuth(null);
179 
180                     SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
181                     target.add(container);
182                 } catch (Exception e) {
183                     LOG.error("While deleting {}", model.getObject(), e);
184                     SyncopeConsoleSession.get().onException(e);
185                 }
186                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
187             }
188         }, ActionLink.ActionType.DELETE, IdRepoEntitlement.ANYTYPE_DELETE, true);
189 
190         return panel;
191     }
192 
193     protected final class AnyTypeProvider extends DirectoryDataProvider<AnyTypeTO> {
194 
195         private static final long serialVersionUID = -185944053385660794L;
196 
197         private final SortableDataProviderComparator<AnyTypeTO> comparator;
198 
199         private AnyTypeProvider(final int paginatorRows) {
200             super(paginatorRows);
201             comparator = new SortableDataProviderComparator<>(this);
202         }
203 
204         @Override
205         public Iterator<AnyTypeTO> iterator(final long first, final long count) {
206             List<AnyTypeTO> list = restClient.listAnyTypes();
207             list.sort(comparator);
208             return list.subList((int) first, (int) first + (int) count).iterator();
209         }
210 
211         @Override
212         public long size() {
213             return restClient.list().size();
214         }
215 
216         @Override
217         public IModel<AnyTypeTO> model(final AnyTypeTO object) {
218             return new CompoundPropertyModel<>(object);
219         }
220     }
221 }