1 5 package org.exoplatform.faces.core.component; 6 7 import java.io.IOException ; 8 import java.util.List ; 9 import java.util.Map ; 10 import javax.faces.component.UICommand; 11 import javax.faces.component.UIComponent; 12 import javax.faces.context.ExternalContext; 13 import javax.faces.context.FacesContext; 14 import javax.faces.render.Renderer; 15 import org.apache.commons.logging.Log; 16 import org.exoplatform.Constants; 17 import org.exoplatform.container.PortalContainer; 18 import org.exoplatform.container.SessionContainer; 19 import org.exoplatform.faces.ActionListenerManager; 20 import org.exoplatform.faces.UIComponentFactory; 21 import org.exoplatform.faces.context.PortletExternalContext; 22 import org.exoplatform.faces.core.Util; 23 import org.exoplatform.faces.core.event.ExoActionEvent; 24 import org.exoplatform.faces.core.event.ExoActionListener; 25 import org.exoplatform.faces.core.renderer.html.Decorator; 26 import org.exoplatform.portal.session.RequestInfo; 27 import org.exoplatform.services.log.LogService; 28 import org.exoplatform.text.template.DataHandler; 29 import org.exoplatform.text.template.xhtml.Element; 30 36 public class UIExoCommand extends UICommand implements UIExoComponent { 37 38 protected String baseURL_ ; 39 private String clazz_; 40 private Decorator decorator_ ; 41 private transient Renderer cacheRenderer_ ; 42 43 public String getClazz() { return clazz_ ; } 44 public UIExoComponent setClazz(String clazz) { 45 clazz_ = clazz ; 46 return this ; 47 } 48 49 public String getFamily() { return UIExoComponent.COMPONENT_FAMILY ; } 50 51 public boolean canDecodeInvalidState() { return true ; } 52 53 public UIExoComponent setDecorator(Decorator decorator) { 54 decorator_ = decorator ; 55 return this; 56 } 57 58 public void decode(FacesContext context) { 59 Map paramMap = context.getExternalContext().getRequestParameterMap() ; 60 String uicomponent = (String ) paramMap.get(UICOMPONENT) ; 61 if (uicomponent != null && uicomponent.equals(getId())) { 62 String action = (String ) paramMap.get(ACTION) ; 63 if(action != null) { 64 broadcast(new ExoActionEvent(this, action, paramMap)) ; 65 } 66 } 67 } 68 69 public void processDecodes(FacesContext context) { 70 List children = getChildren() ; 71 for(int i = 0 ; i < children.size(); i++) { 72 UIComponent child = (UIComponent) children.get(i); 73 if (child.isRendered()) { 74 child.processDecodes(context) ; 75 if (context.getRenderResponse()) { 76 return ; 77 } 78 } 79 } 80 decode(context) ; 81 } 82 83 public void setId(String id) { 84 super.setId(id) ; 85 baseURL_ = null ; 86 } 87 88 public void setRendererType(String type) { 89 cacheRenderer_ = null ; 90 super.setRendererType(type) ; 91 } 92 93 protected Renderer getRenderer(FacesContext context) { 94 if(cacheRenderer_ == null) { 95 cacheRenderer_ = Util.getRenderer(context, getRendererType(), getFamily()); 96 } 97 return cacheRenderer_ ; 98 } 99 100 public UIExoComponent findComponentById(String id) { 101 return ComponentUtil.findComponentById(this, id) ; 102 } 103 104 public UIExoComponent findRenderedComponentById(String id) { 105 return ComponentUtil.findRenderedComponentById(this, id) ; 106 } 107 108 public Object getChildComponentOfType(Class classType) { 109 return ComponentUtil.getChildComponentOfType(this, classType) ; 110 } 111 112 public UIComponent getAncestorOfType(Class classType) { 113 return ComponentUtil.getAncestorOfType(this, classType) ; 114 } 115 116 public void setRenderedComponent(String id) { 117 ComponentUtil.setRenderedComponent(this, id) ; 118 } 119 120 public void setRenderedComponent(Class type) { 121 ComponentUtil.setRenderedComponent(this, type) ; 122 } 123 124 public void setRenderedSibling(Class type) { 125 ComponentUtil.setRenderedSibling(this, type) ; 126 } 127 128 public UIComponent getSibling(Class type) { 129 return ComponentUtil.getSibling(this, type) ; 130 } 131 132 public List findDescendantsOfType(Class type) { 133 return ComponentUtil.findDescendantsOfType(this, type) ; 134 } 135 136 public String getBaseURL(FacesContext context) { 137 if (baseURL_ == null) { 138 ExternalContext econtext = context.getExternalContext() ; 139 if(econtext instanceof PortletExternalContext) { 140 baseURL_ = econtext.encodeActionURL("") ; 141 baseURL_ += Constants.AMPERSAND + UICOMPONENT + "=" + getId() ; 142 } else { 143 RequestInfo rinfo = (RequestInfo)SessionContainer.getComponent(RequestInfo.class); 144 baseURL_ = rinfo.getOwnerURI(); 145 baseURL_ += "?" + UICOMPONENT + "=" + getId() ; 146 } 147 } 148 return baseURL_ ; 149 } 150 151 public String getBaseURL() { 152 if(baseURL_ != null) return baseURL_ ; 153 return getBaseURL(FacesContext.getCurrentInstance()); 154 } 155 156 static public Log getLog(String name) { 157 PortalContainer manager = PortalContainer.getInstance(); 158 LogService service = (LogService) manager.getComponentInstanceOfType(LogService.class) ; 159 Log log = service.getLog(name); 160 return log ; 161 } 162 163 164 protected boolean hasOwnerRole(String user) { 165 if(user == null) return false ; 166 FacesContext context = FacesContext.getCurrentInstance() ; 167 ExternalContext econtext = context.getExternalContext() ; 168 if(user.equals(econtext.getRemoteUser())) { 169 return true ; 170 } 171 return false ; 172 } 173 174 protected boolean hasRole(String role) { 175 FacesContext context = FacesContext.getCurrentInstance() ; 176 ExternalContext econtext = context.getExternalContext() ; 177 return econtext.isUserInRole(role) ; 178 } 179 180 public void addActionListener(Class clazz, String actionToListen) { 181 PortalContainer pcontainer = PortalContainer.getInstance() ; 182 ActionListenerManager manager = 183 (ActionListenerManager) pcontainer.getComponentInstanceOfType(ActionListenerManager.class) ; 184 ExoActionListener result = manager.getActionListener(clazz, actionToListen) ; 185 addActionListener(result) ; 186 } 187 188 public UIExoComponent addChild(Class cl) throws Exception { 189 UIExoComponent uiComponent = UIComponentFactory.createComponent(cl) ; 190 getChildren().add(uiComponent) ; 191 uiComponent.registerActionListener(this) ; 192 uiComponent.registerComponentObserver(this) ; 193 return uiComponent ; 194 } 195 196 public void decorate(FacesContext context) throws IOException { 197 if(decorator_ != null) { 198 decorator_.decorate(context, this) ; 199 } else { 200 encodeBegin(context) ; 201 encodeChildren(context) ; 202 encodeEnd(context) ; 203 } 204 } 205 206 public void registerActionListener(UIExoComponent parent) { 207 208 } 209 210 public void registerComponentObserver(UIExoComponent parent) { 211 212 } 213 214 public DataHandler getDataHandler(Class dataType) { 215 throw new RuntimeException ("You need to override this method and return at least one default data handler") ; 216 } 217 218 public Element getTemplate() { 219 throw new RuntimeException ("You need to override this method") ; 220 } 221 } | Popular Tags |