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.wicket.markup.html.form;
20  
21  import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22  import java.io.Serializable;
23  import java.util.UUID;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.client.console.panels.ConfParam;
26  import org.apache.syncope.client.console.panels.TogglePanel;
27  import org.apache.syncope.client.console.panels.ToggleableTarget;
28  import org.apache.syncope.client.console.policies.PolicyRuleWrapper;
29  import org.apache.syncope.client.console.tasks.CommandWrapper;
30  import org.apache.syncope.client.console.wizards.any.AnyObjectWrapper;
31  import org.apache.syncope.client.console.wizards.any.GroupWrapper;
32  import org.apache.syncope.client.ui.commons.status.StatusBean;
33  import org.apache.syncope.client.ui.commons.wizards.any.EntityWrapper;
34  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
35  import org.apache.syncope.common.keymaster.client.api.model.Domain;
36  import org.apache.syncope.common.lib.Attr;
37  import org.apache.syncope.common.lib.command.CommandTO;
38  import org.apache.syncope.common.lib.policy.PolicyTO;
39  import org.apache.syncope.common.lib.to.AccessTokenTO;
40  import org.apache.syncope.common.lib.to.AnyObjectTO;
41  import org.apache.syncope.common.lib.to.EntityTO;
42  import org.apache.syncope.common.lib.to.ExecTO;
43  import org.apache.syncope.common.lib.to.GroupTO;
44  import org.apache.syncope.common.lib.to.JobTO;
45  import org.apache.syncope.common.lib.to.NamedEntityTO;
46  import org.apache.syncope.common.lib.to.SecurityQuestionTO;
47  import org.apache.syncope.common.lib.to.UserTO;
48  import org.apache.wicket.PageReference;
49  import org.apache.wicket.ajax.AjaxRequestTarget;
50  import org.apache.wicket.event.IEvent;
51  import org.apache.wicket.markup.html.WebMarkupContainer;
52  import org.apache.wicket.markup.html.panel.Fragment;
53  import org.apache.wicket.model.ResourceModel;
54  
55  public class ActionLinksTogglePanel<T extends Serializable> extends TogglePanel<Serializable> {
56  
57      private static final long serialVersionUID = -2025535531121434056L;
58  
59      private final WebMarkupContainer container;
60  
61      public ActionLinksTogglePanel(final String id, final PageReference pageRef) {
62          super(id, UUID.randomUUID().toString(), pageRef);
63  
64          modal.size(Modal.Size.Large);
65          setFooterVisibility(false);
66  
67          container = new WebMarkupContainer("container");
68          container.setOutputMarkupPlaceholderTag(true);
69          addInnerObject(container);
70  
71          container.add(getEmptyFragment());
72      }
73  
74      public void updateHeader(final AjaxRequestTarget target, final Serializable modelObject) {
75          final String header;
76          if (modelObject instanceof UserTO) {
77              header = ((UserTO) modelObject).getUsername();
78          } else if (modelObject instanceof UserWrapper) {
79              header = ((UserWrapper) modelObject).getInnerObject().getUsername();
80          } else if (modelObject instanceof GroupTO) {
81              header = ((GroupTO) modelObject).getName();
82          } else if (modelObject instanceof GroupWrapper) {
83              header = ((GroupWrapper) modelObject).getInnerObject().getName();
84          } else if (modelObject instanceof AnyObjectTO) {
85              header = ((AnyObjectTO) modelObject).getName();
86          } else if (modelObject instanceof AnyObjectWrapper) {
87              header = ((AnyObjectWrapper) modelObject).getInnerObject().getName();
88          } else if (modelObject instanceof Attr) {
89              header = ((Attr) modelObject).getSchema();
90          } else if (modelObject instanceof ConfParam) {
91              header = ((ConfParam) modelObject).getSchema();
92          } else if (modelObject instanceof PolicyTO) {
93              header = ((PolicyTO) modelObject).getName();
94          } else if (modelObject instanceof SecurityQuestionTO) {
95              header = ((SecurityQuestionTO) modelObject).getContent();
96          } else if (modelObject instanceof AccessTokenTO) {
97              header = ((AccessTokenTO) modelObject).getOwner();
98          } else if (modelObject instanceof ExecTO) {
99              header = ((ExecTO) modelObject).getKey();
100         } else if (modelObject instanceof StatusBean) {
101             header = ((StatusBean) modelObject).getResource();
102         } else if (modelObject instanceof PolicyRuleWrapper) {
103             header = ((PolicyRuleWrapper) modelObject).getImplementationKey();
104         } else if (modelObject instanceof CommandWrapper) {
105             header = ((CommandWrapper) modelObject).getCommand().getKey();
106         } else if (modelObject instanceof JobTO) {
107             header = ((JobTO) modelObject).getRefKey() == null
108                     ? ((JobTO) modelObject).getRefDesc() : ((JobTO) modelObject).getRefKey();
109         } else if (modelObject instanceof ToggleableTarget) {
110             header = ((ToggleableTarget) modelObject).getAnyType();
111         } else if (modelObject instanceof Domain) {
112             header = ((Domain) modelObject).getKey();
113         } else if (modelObject instanceof CommandTO) {
114             header = ((CommandTO) modelObject).getKey();
115         } else if (modelObject instanceof NamedEntityTO) {
116             header = ((NamedEntityTO) modelObject).getName();
117         } else if (modelObject instanceof EntityTO) {
118             header = ((EntityTO) modelObject).getKey();
119         } else if (modelObject instanceof EntityWrapper) {
120             EntityTO inner = ((EntityWrapper) modelObject).getInnerObject();
121             header = inner instanceof NamedEntityTO ? ((NamedEntityTO) inner).getName() : inner.getKey();
122         } else {
123             header = new ResourceModel("actions", StringUtils.EMPTY).getObject();
124         }
125 
126         setHeader(target, header);
127     }
128 
129     public void toggleWithContent(
130             final AjaxRequestTarget target,
131             final ActionsPanel<T> actionsPanel,
132             final T modelObject) {
133 
134         updateHeader(target, modelObject);
135 
136         modal.setWindowClosedCallback(t -> modal.show(false));
137 
138         Fragment frag = new Fragment("actions", "actionsFragment", this);
139         frag.setOutputMarkupId(true);
140         frag.add(actionsPanel);
141 
142         container.addOrReplace(frag);
143         target.add(this.container);
144 
145         toggle(target, modelObject, true);
146     }
147 
148     private Fragment getEmptyFragment() {
149         return new Fragment("actions", "emptyFragment", this);
150     }
151 
152     @Override
153     public void onEvent(final IEvent<?> event) {
154         if (event.getPayload() instanceof ActionLinkToggleCloseEventPayload) {
155             close(ActionLinkToggleCloseEventPayload.class.cast(event.getPayload()).getTarget());
156         }
157     }
158 
159     public static class ActionLinkToggleCloseEventPayload {
160 
161         private final AjaxRequestTarget target;
162 
163         public ActionLinkToggleCloseEventPayload(final AjaxRequestTarget target) {
164             this.target = target;
165         }
166 
167         public AjaxRequestTarget getTarget() {
168             return target;
169         }
170     }
171 }