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.pages;
20  
21  import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Optional;
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.syncope.client.console.BookmarkablePageLinkBuilder;
28  import org.apache.syncope.client.console.SyncopeConsoleSession;
29  import org.apache.syncope.client.console.panels.RealmDirectoryPanel;
30  import org.apache.syncope.client.console.panels.search.RealmSearchPanel;
31  import org.apache.syncope.client.console.panels.search.SearchClause;
32  import org.apache.syncope.client.console.panels.search.SearchClausePanel;
33  import org.apache.syncope.client.console.panels.search.SearchUtils;
34  import org.apache.syncope.client.console.rest.RealmRestClient;
35  import org.apache.syncope.client.console.tasks.TemplatesTogglePanel;
36  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
37  import org.apache.syncope.client.lib.SyncopeClient;
38  import org.apache.syncope.client.ui.commons.Constants;
39  import org.apache.syncope.client.ui.commons.panels.WizardModalPanel;
40  import org.apache.syncope.client.ui.commons.wicket.markup.html.bootstrap.tabs.Accordion;
41  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
42  import org.apache.syncope.common.lib.to.AnyTO;
43  import org.apache.syncope.common.lib.to.RealmTO;
44  import org.apache.syncope.common.lib.to.TemplatableTO;
45  import org.apache.wicket.Component;
46  import org.apache.wicket.ajax.AjaxRequestTarget;
47  import org.apache.wicket.ajax.markup.html.AjaxLink;
48  import org.apache.wicket.event.IEvent;
49  import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
50  import org.apache.wicket.extensions.markup.html.tabs.ITab;
51  import org.apache.wicket.markup.html.WebMarkupContainer;
52  import org.apache.wicket.model.CompoundPropertyModel;
53  import org.apache.wicket.model.IModel;
54  import org.apache.wicket.model.Model;
55  import org.apache.wicket.model.ResourceModel;
56  import org.apache.wicket.model.util.ListModel;
57  import org.apache.wicket.request.mapper.parameter.PageParameters;
58  import org.apache.wicket.spring.injection.annot.SpringBean;
59  
60  public class Realms extends BasePage {
61  
62      private static final long serialVersionUID = -810612679853772333L;
63  
64      @SpringBean
65      protected RealmRestClient realmRestClient;
66  
67      protected final RealmDirectoryPanel realmDirectoryPanel;
68  
69      protected RealmSearchPanel searchPanel;
70  
71      protected final TemplatesTogglePanel templates;
72  
73      protected final BaseModal<Serializable> templateModal;
74  
75      public Realms(final PageParameters parameters) {
76          super(parameters);
77  
78          body.add(BookmarkablePageLinkBuilder.build("dashboard", "dashboardBr", Dashboard.class));
79  
80          List<SearchClause> clauses = new ArrayList<>();
81  
82          Model<Integer> model = Model.of(-1);
83          body.add(new Accordion("accordionPanel",
84                  List.of(new AbstractTab(new ResourceModel("search.result")) {
85  
86                      private static final long serialVersionUID = 1037272333056449377L;
87  
88                      @Override
89                      public WebMarkupContainer getPanel(final String panelId) {
90                          searchPanel = new RealmSearchPanel.Builder(new ListModel<>(clauses), getPageReference()).
91                                  required(true).enableSearch().build(panelId);
92                          return searchPanel;
93                      }
94                  }), model) {
95  
96              private static final long serialVersionUID = -3056452800492734900L;
97  
98              @Override
99              protected Component newTitle(final String markupId, final ITab tab, final Accordion.State state) {
100                 return new AjaxLink<Integer>(markupId) {
101 
102                     private static final long serialVersionUID = 6250423506463465679L;
103 
104                     @Override
105                     public void onClick(final AjaxRequestTarget target) {
106                         model.setObject(model.getObject() == 0 ? -1 : 0);
107                     }
108                 }.setBody(tab.getTitle()).setEscapeModelStrings(false);
109             }
110         });
111 
112         realmDirectoryPanel = new RealmDirectoryPanel("directoryPanel", realmRestClient, getPageReference());
113         realmDirectoryPanel.setOutputMarkupId(true);
114         body.add(realmDirectoryPanel);
115 
116         templates = new TemplatesTogglePanel(BaseModal.CONTENT_ID, this, getPageReference()) {
117 
118             private static final long serialVersionUID = 4828350561653999922L;
119 
120             @Override
121             protected Serializable onApplyInternal(
122                     final TemplatableTO targetObject, final String type, final AnyTO anyTO) {
123 
124                 targetObject.getTemplates().put(type, anyTO);
125                 realmRestClient.update(RealmTO.class.cast(targetObject));
126                 return targetObject;
127             }
128         };
129         body.add(templates);
130 
131         templateModal = new BaseModal<>("templateModal") {
132 
133             private static final long serialVersionUID = 5787433530654262016L;
134 
135             @Override
136             protected void onConfigure() {
137                 super.onConfigure();
138                 setFooterVisible(false);
139             }
140         };
141         templateModal.size(Modal.Size.Extra_large);
142         body.add(templateModal);
143 
144         templateModal.setWindowClosedCallback(target -> templateModal.show(false));
145     }
146 
147     @Override
148     public void onEvent(final IEvent<?> event) {
149         if (event.getPayload() instanceof SearchClausePanel.SearchEvent payload && searchPanel != null) {
150             List<SearchClause> clauses = searchPanel.getModel().getObject();
151             String base = clauses.stream().
152                     filter(Realms::isBaseClause).
153                     map(SearchClause::getValue).
154                     filter(StringUtils::isNotBlank).
155                     findFirst().
156                     orElse(StringUtils.EMPTY);
157 
158             List<SearchClause> fiqlClauses = clauses.stream()
159                     .filter(clause -> !isBaseClause(clause))
160                     .filter(Realms::isValid)
161                     .toList();
162 
163             realmDirectoryPanel.search(base, Optional.ofNullable(SearchUtils.buildFIQL(
164                     fiqlClauses,
165                     SyncopeClient.getRealmFiqlSearchConditionBuilder())).orElse(StringUtils.EMPTY),
166                     payload.getTarget());
167         } else if (event.getPayload() instanceof AjaxWizard.NewItemEvent<?> newItemEvent) {
168             WizardModalPanel<?> modalPanel = newItemEvent.getModalPanel();
169 
170             if (event.getPayload() instanceof AjaxWizard.NewItemActionEvent && modalPanel != null) {
171                 final IModel<Serializable> model = new CompoundPropertyModel<>(modalPanel.getItem());
172                 templateModal.setFormModel(model);
173                 templateModal.header(newItemEvent.getTitleModel());
174                 newItemEvent.getTarget().ifPresent(t -> t.add(templateModal.setContent(modalPanel)));
175                 templateModal.show(true);
176             } else if (event.getPayload() instanceof AjaxWizard.NewItemCancelEvent) {
177                 newItemEvent.getTarget().ifPresent(templateModal::close);
178             } else if (event.getPayload() instanceof AjaxWizard.NewItemFinishEvent) {
179                 SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
180                 newItemEvent.getTarget().ifPresent(t -> {
181                     ((BasePage) getPage()).getNotificationPanel().refresh(t);
182                     templateModal.close(t);
183                 });
184             }
185         } else if (event.getPayload() instanceof TemplatesTogglePanel.ShowTemplatesTogglePanelEvent action) {
186             templates.setTargetObject(action.realmTO());
187             templates.toggle(action.target(), true);
188         } else {
189             super.onEvent(event);
190         }
191     }
192 
193     private static boolean isBaseClause(final SearchClause clause) {
194         return clause.getType() == SearchClause.Type.CUSTOM && "fullPath".equalsIgnoreCase(clause.getProperty());
195     }
196 
197     private static boolean isValid(final SearchClause clause) {
198         return clause.getProperty() != null && clause.getComparator() != null;
199     }
200 }