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 de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22  import java.io.Serializable;
23  import java.text.MessageFormat;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.List;
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.rest.ConnectorRestClient;
33  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
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.console.wizards.WizardMgtPanel;
37  import org.apache.syncope.client.console.wizards.resources.ConnectorWizardBuilder;
38  import org.apache.syncope.client.ui.commons.Constants;
39  import org.apache.syncope.client.ui.commons.rest.RestClient;
40  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
41  import org.apache.syncope.common.lib.to.ConnInstanceTO;
42  import org.apache.syncope.common.lib.types.IdMEntitlement;
43  import org.apache.wicket.PageReference;
44  import org.apache.wicket.ajax.AjaxRequestTarget;
45  import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
46  import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
47  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
48  import org.apache.wicket.markup.html.basic.Label;
49  import org.apache.wicket.markup.repeater.Item;
50  import org.apache.wicket.model.CompoundPropertyModel;
51  import org.apache.wicket.model.IModel;
52  import org.apache.wicket.model.Model;
53  import org.apache.wicket.model.ResourceModel;
54  import org.apache.wicket.spring.injection.annot.SpringBean;
55  
56  public class ConnidLocations extends
57          DirectoryPanel<Serializable, Serializable, ConnidLocations.ConnidLocationsDataProvider, RestClient> {
58  
59      private static final long serialVersionUID = -1328140415494501001L;
60  
61      @SpringBean
62      protected ConnectorRestClient connectorRestClient;
63  
64      public ConnidLocations(final String id, final Builder builder) {
65          super(id, builder);
66  
67          disableCheckBoxes();
68          setShowResultPanel(true);
69  
70          modal.size(Modal.Size.Large);
71          initResultTable();
72      }
73  
74      @Override
75      protected ConnidLocationsDataProvider dataProvider() {
76          return new ConnidLocationsDataProvider(rows);
77      }
78  
79      @Override
80      protected String paginatorRowsKey() {
81          return IdRepoConstants.PREF_DYNREALM_PAGINATOR_ROWS;
82      }
83  
84      @Override
85      protected List<IColumn<Serializable, String>> getColumns() {
86          final List<IColumn<Serializable, String>> columns = new ArrayList<>();
87  
88          columns.add(new AbstractColumn<>(
89                  new ResourceModel(Constants.KEY_FIELD_NAME), Constants.KEY_FIELD_NAME) {
90  
91              @Override
92              public void populateItem(
93                      final Item<ICellPopulator<Serializable>> cellItem,
94                      final String componentId,
95                      final IModel<Serializable> rowModel) {
96  
97                  cellItem.add(new Label(componentId, rowModel.getObject().toString()));
98              }
99          });
100 
101         return columns;
102     }
103 
104     @Override
105     public ActionsPanel<Serializable> getActions(final IModel<Serializable> model) {
106         final ActionsPanel<Serializable> panel = super.getActions(model);
107 
108         panel.add(new ActionLink<>() {
109 
110             private static final long serialVersionUID = 293293495682202660L;
111 
112             @Override
113             public void onClick(final AjaxRequestTarget target, final Serializable ignore) {
114                 final ConnInstanceTO modelObject = new ConnInstanceTO();
115                 modelObject.setLocation((String) ignore);
116 
117                 final IModel<ConnInstanceTO> model = new CompoundPropertyModel<>(modelObject);
118                 modal.setFormModel(model);
119 
120                 target.add(modal.setContent(new ConnectorWizardBuilder(modelObject, connectorRestClient, pageRef).
121                         build(BaseModal.CONTENT_ID, AjaxWizard.Mode.CREATE)));
122 
123                 modal.header(new Model<>(MessageFormat.format(getString("connector.new"), ignore)));
124                 modal.show(true);
125             }
126 
127         }, ActionLink.ActionType.CREATE, String.format("%s", IdMEntitlement.CONNECTOR_CREATE));
128 
129         return panel;
130     }
131 
132     @Override
133     protected Collection<ActionLink.ActionType> getBatches() {
134         return Collections.emptyList();
135     }
136 
137     protected static class ConnidLocationsDataProvider extends DirectoryDataProvider<Serializable> {
138 
139         private static final long serialVersionUID = 3161906945317209169L;
140 
141         public ConnidLocationsDataProvider(final int paginatorRows) {
142             super(paginatorRows);
143         }
144 
145         @Override
146         public Iterator<String> iterator(final long first, final long count) {
147             List<String> result = new ArrayList<>(SyncopeConsoleSession.get().getPlatformInfo().getConnIdLocations());
148             return result.subList((int) first, (int) first + (int) count).iterator();
149         }
150 
151         @Override
152         public long size() {
153             return SyncopeConsoleSession.get().getPlatformInfo().getConnIdLocations().size();
154         }
155 
156         @Override
157         public IModel<Serializable> model(final Serializable object) {
158             return new CompoundPropertyModel<>(object);
159         }
160     }
161 
162     public abstract static class Builder
163             extends DirectoryPanel.Builder<Serializable, Serializable, RestClient> {
164 
165         private static final long serialVersionUID = 4448348557808690524L;
166 
167         public Builder(final PageReference pageRef) {
168             super(null, pageRef);
169         }
170 
171         @Override
172         protected WizardMgtPanel<Serializable> newInstance(final String id, final boolean wizardInModal) {
173             return new ConnidLocations(id, this);
174         }
175     }
176 }