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.ui.commons.status;
20  
21  import java.io.Serializable;
22  import java.util.Collection;
23  import java.util.Optional;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.client.ui.commons.ConnIdSpecialName;
26  import org.apache.syncope.client.ui.commons.Constants;
27  import org.apache.syncope.client.ui.commons.panels.LabelPanel;
28  import org.apache.syncope.common.lib.request.StatusR;
29  import org.apache.syncope.common.lib.to.AnyTO;
30  import org.apache.syncope.common.lib.to.ConnObject;
31  import org.apache.syncope.common.lib.to.RealmTO;
32  import org.apache.syncope.common.lib.types.ExecStatus;
33  import org.apache.syncope.common.lib.types.StatusRType;
34  import org.apache.wicket.markup.ComponentTag;
35  import org.apache.wicket.markup.html.basic.Label;
36  import org.apache.wicket.markup.html.panel.Panel;
37  
38  public final class StatusUtils implements Serializable {
39  
40      private static final long serialVersionUID = 7238009174387184309L;
41  
42      public static StatusBean getStatusBean(
43              final AnyTO anyTO,
44              final String resource,
45              final ConnObject connObject,
46              final boolean notUser) {
47  
48          StatusBean statusBean = new StatusBean(anyTO, resource);
49  
50          if (connObject != null) {
51              Boolean enabled = isEnabled(connObject);
52              statusBean.setStatus(Optional.ofNullable(enabled).map(aBoolean -> aBoolean
53                      ? Status.ACTIVE
54                      : Status.SUSPENDED).orElseGet(() -> (notUser ? Status.ACTIVE : Status.UNDEFINED)));
55  
56              statusBean.setConnObjectLink(getConnObjectLink(connObject));
57          }
58  
59          return statusBean;
60      }
61  
62      public static StatusBean getStatusBean(
63              final RealmTO realmTO,
64              final String resource,
65              final ConnObject connObject) {
66  
67          StatusBean statusBean = new StatusBean(realmTO, resource);
68  
69          if (connObject != null) {
70              Boolean enabled = isEnabled(connObject);
71              statusBean.setStatus(Optional.ofNullable(enabled)
72                      .filter(aBoolean -> !aBoolean).map(aBoolean -> Status.SUSPENDED).orElse(Status.ACTIVE));
73  
74              statusBean.setConnObjectLink(getConnObjectLink(connObject));
75          }
76  
77          return statusBean;
78      }
79  
80      public static Boolean isEnabled(final ConnObject connObject) {
81          return connObject.getAttr(ConnIdSpecialName.ENABLE).
82                  filter(s -> !s.getValues().isEmpty()).
83                  map(s -> Boolean.valueOf(s.getValues().get(0))).
84                  orElse(Boolean.FALSE);
85      }
86  
87      public static String getConnObjectLink(final ConnObject connObject) {
88          return connObject.getAttr(ConnIdSpecialName.NAME).
89                  filter(s -> !s.getValues().isEmpty()).
90                  map(s -> s.getValues().get(0)).
91                  orElse(null);
92      }
93  
94      public static StatusR statusR(final String key, final StatusRType type, final Collection<StatusBean> statuses) {
95          StatusR.Builder builder = new StatusR.Builder(key, type).onSyncope(false);
96          statuses.forEach(status -> {
97              if (Constants.SYNCOPE.equalsIgnoreCase(status.getResource())) {
98                  builder.onSyncope(true);
99              } else {
100                 builder.resource(status.getResource());
101             }
102         });
103 
104         return builder.build();
105     }
106 
107     public static Panel getStatusImagePanel(final String componentId, final Status status) {
108         return new LabelPanel(componentId, getStatusImage("label", status));
109     }
110 
111     public static Label getStatusImage(final String componentId, final Status status) {
112         final String alt, title, clazz;
113 
114         switch (status) {
115 
116             case NOT_YET_SUBMITTED:
117                 alt = "undefined icon";
118                 title = "Not yet submitted";
119                 clazz = Constants.UNDEFINED_ICON;
120                 break;
121 
122             case ACTIVE:
123                 alt = "active icon";
124                 title = "Enabled";
125                 clazz = Constants.ACTIVE_ICON;
126                 break;
127 
128             case UNDEFINED:
129                 alt = "undefined icon";
130                 title = "Undefined status";
131                 clazz = Constants.UNDEFINED_ICON;
132                 break;
133 
134             case OBJECT_NOT_FOUND:
135                 alt = "notfound icon";
136                 title = "Not found";
137                 clazz = Constants.NOT_FOUND_ICON;
138                 break;
139 
140             case CREATED:
141                 alt = "created icon";
142                 title = "Created";
143                 clazz = Constants.CREATED_ICON;
144                 break;
145 
146             case SUSPENDED:
147                 alt = "inactive icon";
148                 title = "Disabled";
149                 clazz = Constants.SUSPENDED_ICON;
150                 break;
151 
152             default:
153                 alt = StringUtils.EMPTY;
154                 title = StringUtils.EMPTY;
155                 clazz = StringUtils.EMPTY;
156                 break;
157         }
158 
159         return getLabel(componentId, alt, title, clazz);
160     }
161 
162     public static Panel getStatusImagePanel(final String componentId, final ExecStatus status) {
163         return new LabelPanel(componentId, getStatusImage("label", status));
164     }
165 
166     public static Label getStatusImage(final String componentId, final ExecStatus status) {
167         final String alt, title, clazz;
168 
169         switch (status) {
170 
171             case NOT_ATTEMPTED:
172                 alt = "not attempted";
173                 title = "Not attempted";
174                 clazz = Constants.UNDEFINED_ICON;
175                 break;
176 
177             case CREATED:
178                 alt = "created icon";
179                 title = "Created";
180                 clazz = Constants.CREATED_ICON;
181                 break;
182 
183             case SUCCESS:
184                 alt = "success icon";
185                 title = "Propagation succeded";
186                 clazz = Constants.ACTIVE_ICON;
187                 break;
188 
189             case FAILURE:
190                 alt = "failure icon";
191                 title = "Propagation failed";
192                 clazz = Constants.NOT_FOUND_ICON;
193                 break;
194 
195             default:
196                 alt = StringUtils.EMPTY;
197                 title = StringUtils.EMPTY;
198                 clazz = StringUtils.EMPTY;
199                 break;
200         }
201 
202         return getLabel(componentId, alt, title, clazz);
203     }
204 
205     public static Panel getWarningStatusPanel(final String componentId) {
206         return new LabelPanel(componentId,
207                 getLabel("label", "warning icon", "Propagation failed", Constants.WARNING_ICON));
208     }
209 
210     public static Label getLabel(final String componentId, final String alt, final String title, final String clazz) {
211         return new Label(componentId, StringUtils.EMPTY) {
212 
213             private static final long serialVersionUID = 4755868673082976208L;
214 
215             @Override
216             protected void onComponentTag(final ComponentTag tag) {
217                 super.onComponentTag(tag);
218                 tag.put("alt", alt);
219                 tag.put("title", title);
220                 tag.put("class", clazz);
221             }
222         };
223     }
224 
225     private StatusUtils() {
226         // private constructor for static utility class
227     }
228 }