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  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.apache.myfaces.tobago.event.CollapsibleActionListener;
23  import org.apache.myfaces.tobago.internal.util.AuthorizationHelper;
24  import org.apache.myfaces.tobago.util.ComponentUtils;
25  
26  import javax.el.MethodExpression;
27  import javax.faces.component.UICommand;
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.ComponentSystemEvent;
31  import javax.faces.event.ComponentSystemEventListener;
32  import javax.faces.event.FacesEvent;
33  import javax.faces.event.ListenerFor;
34  import javax.faces.event.PhaseId;
35  import javax.faces.event.PostAddToViewEvent;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  @ListenerFor(systemEventClass = PostAddToViewEvent.class)
40  public abstract class AbstractUICommandBase extends UICommand
41      implements ComponentSystemEventListener {
42  
43    @Override
44    public void processEvent(ComponentSystemEvent event) {
45      super.processEvent(event);
46  
47      if (event instanceof PostAddToViewEvent) {
48        final List<AbstractUIOperation> list = ComponentUtils.findDescendantList(this, AbstractUIOperation.class);
49        for (AbstractUIOperation operation : list) {
50          addActionListener(new CollapsibleActionListener(operation.getFor()));
51        }
52      }
53    }
54  
55    @Override
56    public void processDecodes(final FacesContext context) {
57      // Skip processing if our rendered flag is false
58      if (!isRendered()) {
59        return;
60      }
61  
62      // Process this component itself
63      try {
64        decode(context);
65      } catch (final RuntimeException e) {
66        context.renderResponse();
67        throw e;
68      }
69  
70      final Iterator kids = getFacetsAndChildren();
71      while (kids.hasNext()) {
72        final UIComponent kid = (UIComponent) kids.next();
73        kid.processDecodes(context);
74      }
75    }
76  
77    @Override
78    public void queueEvent(final FacesEvent facesEvent) {
79      // fix for TOBAGO-262
80      super.queueEvent(facesEvent);
81      if (this == facesEvent.getSource()) {
82        if (isImmediate()) {
83          facesEvent.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
84        } else {
85          facesEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
86        }
87      }
88    }
89  
90    @Override
91    public boolean isRendered() {
92      return super.isRendered() && isAllowed();
93    }
94  
95    /**
96     Flag indicating that this element is disabled.
97     <br>Default: <code>false</code>
98     */
99    public boolean isDisabled() {
100 
101     if (!isAllowed()) {
102       return true;
103     }
104 
105     Boolean bool = (Boolean) getStateHelper().eval(AbstractUICommand.PropertyKeys.disabled);
106     if (bool != null) {
107       return bool;
108     }
109     return false;
110   }
111 
112   private boolean isAllowed() {
113     final FacesContext facesContext = getFacesContext();
114     // todo: get from configuration tobago-config.xml
115     if (true) {
116       final AuthorizationHelper authorizationHelper = AuthorizationHelper.getInstance(facesContext);
117       final MethodExpression actionExpression = getActionExpression();
118       if (actionExpression != null) {
119         final boolean authorized =
120             authorizationHelper.isAuthorized(facesContext, actionExpression.getExpressionString());
121         if (!authorized) {
122           return false;
123         }
124       }
125     }
126     return true;
127   }
128 
129   public void setDisabled(boolean disabled) {
130     getStateHelper().put(AbstractUICommand.PropertyKeys.disabled, disabled);
131   }
132 
133   public abstract String getTarget();
134 
135   public abstract boolean isTransition();
136 
137   public abstract boolean isOmit();
138 
139   public abstract void setOmit(boolean omit);
140 
141   public abstract String getConfirmation();
142 
143   public abstract String getLink();
144 }