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