KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portal > faces > component > UIPortal


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portal.faces.component;
6
7 import java.util.List JavaDoc;
8 import java.util.ResourceBundle JavaDoc;
9 import javax.faces.application.FacesMessage;
10 import javax.faces.component.UIComponent;
11 import javax.faces.context.FacesContext;
12 import org.exoplatform.container.SessionContainer;
13 import org.exoplatform.container.client.http.HttpClientInfo;
14 import org.exoplatform.container.monitor.SessionMonitor;
15 import org.exoplatform.faces.core.component.InformationProvider;
16 import org.exoplatform.faces.core.component.model.Information;
17 import org.exoplatform.portal.PortalConstants;
18 import org.exoplatform.portal.faces.listener.page.PageActionListener;
19 import org.exoplatform.portal.faces.listener.portal.NodeActionListener;
20 import org.exoplatform.portal.faces.listener.portal.PortalActionListener;
21 import org.exoplatform.portal.session.ExoPortal;
22 import org.exoplatform.portal.session.RequestInfo;
23 import org.exoplatform.services.portal.PortalACL;
24 import org.exoplatform.services.portal.PortalConfigService;
25 import org.exoplatform.services.portal.model.*;
26 import org.exoplatform.services.portal.skin.SkinConfigService;
27 import org.exoplatform.services.portal.skin.model.Style;
28 import org.exoplatform.services.portletcontainer.pci.ExoWindowID;
29 /**
30  * Date: Aug 11, 2003
31  * @author : Mestrallet Benjamin
32  * @email: benjmestrallet@users.sourceforge.net
33  * @author: Tuan Nguyen
34  * @email: tuan08@users.sourceforge.net
35  * @version: $Id: UIPortal.java,v 1.36 2004/10/27 02:53:05 tuan08 Exp $
36  */

37 public class UIPortal extends UIBasicComponent
38   implements InformationProvider, ExoPortal {
39   public static final String JavaDoc DEFAULT_RENDERER_TYPE = "PortalRenderer";
40   
41   private String JavaDoc userCss_ ;
42   private Node root_ ;
43   private Node selectNode_ ;
44   private boolean modified_ ;
45   private PortalConfig componentModel_ ;
46   private boolean dirty_ = false ;
47   private PortalACL portalACL_ ;
48   private Information information_ = new Information() ;
49   private String JavaDoc portalOwner_ ;
50   private boolean queueEvent_ ;
51   private int portalMode_ ;
52   
53     public UIPortal(PortalConfigService configService,
54                   SessionMonitor monitor, RequestInfo rinfo) throws Exception JavaDoc {
55     portalACL_ = configService.getPortalACL() ;
56     portalOwner_ = rinfo.getPortalOwner() ;
57     componentModel_ = configService.getPortalConfig(portalOwner_) ;
58     initBasicComponent(componentModel_, "default") ;
59     HttpClientInfo clientInfo =(HttpClientInfo) monitor.getClientInfo();
60     List JavaDoc children = getChildren() ;
61     root_ = configService.getNodeNavigation(getOwner()) ;
62     root_.setVisible(portalACL_, rinfo.getPortalOwner(), clientInfo.getRemoteUser()) ;
63     selectNode_ = root_.findNode(rinfo.getPageName()) ;
64     if(selectNode_ == null) selectNode_ = root_ ;
65     UIContainer uiPortalLayout = null ;
66     if(ExoPortal.XHTMLMP_MIME_TYPE.equals(clientInfo.getPreferredMimeType())) {
67       uiPortalLayout =
68         new UIContainer(componentModel_.getMobilePortalLayout(), "default", ExoWindowID.MOBILE_PORTAL_CONFIG);
69     } else {
70       uiPortalLayout =
71         new UIContainer(componentModel_.getLayout(), "default", ExoWindowID.DEFAULT_PORTAL_CONFIG);
72     }
73         children.add(uiPortalLayout) ;
74     new ChangeLocaleVisitor().changeLocale(this) ;
75    
76     if(RequestInfo.PRIVATE_ACCESS.equals(rinfo.getAccessibility()) ||
77        RequestInfo.ADMIN_ACCESS.equals(rinfo.getAccessibility())){
78       setComponentAdminRole(true) ;
79     } else {
80       setComponentAdminRole(false) ;
81     }
82     registerListeners() ;
83     UIBody uiBody = findUIBody(uiPortalLayout) ;
84     uiBody.initDefaultBodyComponent() ;
85     userCss_ = CssStyleBuilderVisitor.getCSS(getRootContainer()) ;
86     setId("portal") ;
87     }
88   
89   private void registerListeners() {
90     addActionListener(NodeActionListener.class, PortalConstants.EDIT_NODE_ACTION) ;
91     addActionListener(NodeActionListener.class, PortalConstants.ADD_NODE_ACTION) ;
92     addActionListener(NodeActionListener.class, PortalConstants.DELETE_NODE_ACTION) ;
93     addActionListener(NodeActionListener.class, PortalConstants.SAVE_NODE_ACTION) ;
94     addActionListener(NodeActionListener.class, PortalConstants.BROWSE_PAGE_ACTION) ;
95     addActionListener(NodeActionListener.class, PortalConstants.MOVE_UP_NODE_ACTION) ;
96     addActionListener(NodeActionListener.class, PortalConstants.MOVE_DOWN_NODE_ACTION) ;
97     
98     addActionListener(PageActionListener.class, PortalConstants.SAVE_PAGE_ACTION) ;
99     
100     addActionListener(PortalActionListener.class, PortalConstants.EDIT_PORTAL_ACTION) ;
101     addActionListener(PortalActionListener.class, PortalConstants.SAVE_PORTAL_ACTION) ;
102     addActionListener(PortalActionListener.class, PortalConstants.CHANGE_LANGUAGE_ACTION) ;
103   }
104     
105   public String JavaDoc getFamily() { return "org.exoplatform.portal.faces.component.UIPortal" ; }
106   protected String JavaDoc getIdPrefix() { return "portal" ; }
107   
108   public Component getComponentModel() { return componentModel_ ; }
109   public PortalConfig getPortalConfigModel() { return componentModel_ ; }
110   public PortalConfig getEditablePortalConfigModel() {
111     if(!modified_) {
112         componentModel_ = (PortalConfig)componentModel_.softCloneObject() ;
113     }
114     return componentModel_ ;
115   }
116   
117   public String JavaDoc getOwner() { return portalOwner_ ; }
118
119   public String JavaDoc getUserCss() { return userCss_ ; }
120   
121   protected String JavaDoc getDefaultRendererType() { return DEFAULT_RENDERER_TYPE ; }
122
123   /**Override default set editable in UIBasicComponent*/
124   //public void setComponentAdminRole(boolean b) { componentAdminRole_ = b ; }
125

126   public boolean isDirty() { return dirty_ ; }
127   /**Override default setDirty in UIBasicComponent*/
128   public void setComponentDirty(boolean b) { dirty_ = b ; }
129   
130   public void setPortalViewlMode() {
131     setSelectedNode(selectNode_) ;
132     setComponentMode(UIBasicComponent.COMPONENT_VIEW_MODE);
133     findUIBody().setBodyMode(UIBasicComponent.COMPONENT_VIEW_MODE);
134   }
135   
136   public boolean hasEditPortalCapability() {
137     SessionMonitor monitor =
138       (SessionMonitor)SessionContainer.getComponent(SessionMonitor.class) ;
139     return hasComponentAdminRole() &&
140            portalACL_.hasEditPortalPermission(componentModel_, monitor.getRemoteUser()) ;
141   }
142   
143   public void setPortalEditMode() {
144     if(hasEditPortalCapability()) {
145         setComponentMode(UIBasicComponent.COMPONENT_EDIT_MODE);
146         findUIBody().setBodyMode(UIBasicComponent.COMPONENT_VIEW_MODE);
147         mode_ = UIBasicComponent.COMPONENT_VIEW_MODE ;
148     }
149   }
150   
151   public void setPortalEditPageMode() {
152     setComponentMode(UIBasicComponent.COMPONENT_VIEW_MODE);
153     setSelectedNode(getSelectedNode()) ;
154     findUIBody().setBodyMode(UIBasicComponent.COMPONENT_EDIT_MODE) ;
155   }
156   
157   public boolean hasEditNavigationCapability() {
158     SessionMonitor monitor =
159       (SessionMonitor)SessionContainer.getComponent(SessionMonitor.class) ;
160     return hasComponentAdminRole() &&
161            portalACL_.hasEditNodePermission(root_, getOwner(), monitor.getRemoteUser());
162   }
163   
164   public void setPortalEditNavigationMode() {
165     if(hasEditNavigationCapability()) {
166         setComponentMode(UIBasicComponent.COMPONENT_VIEW_MODE);
167         findUIBody().setBodyMode(UIBasicComponent.COMPONENT_VIEW_MODE);
168         mode_ = UIBasicComponent.COMPONENT_EDIT_MODE ;
169     }
170   }
171   
172   public int getPortalMode() { return portalMode_ ; }
173   public void setPortalMode(int mode) {
174     portalMode_ = mode ;
175     if(mode == PORTAL_EDIT_MODE) setPortalEditMode() ;
176     else if(mode == PORTAL_EDIT_PAGE_MODE) setPortalEditPageMode() ;
177     else if(mode == PORTAL_EDIT_NAVIGATION_MODE) setPortalEditNavigationMode() ;
178     else setPortalViewlMode() ;
179   }
180   
181   public Node getRootNode() { return root_ ; }
182   public Node getSelectedNode() { return selectNode_ ; }
183   public void setSelectedNode(Node node) {
184     if(selectNode_!= null) {
185       selectNode_.setSelectedPath(false) ;
186       selectNode_ = node ;
187       selectNode_.setSelectedPath(true) ;
188       PortalComponentCache cache =
189         (PortalComponentCache) SessionContainer.getComponent(PortalComponentCache.class) ;
190       UIPage uiPage = cache.getUIPage(selectNode_, this) ;
191       new ChangeLocaleVisitor().changeLocale(uiPage) ;
192       setBodyComponent(uiPage) ;
193     }
194   }
195   
196   public void removeSelectedNode() {
197     Node parent = selectNode_.getParent() ;
198     if (parent != null && !selectNode_.isShare()) {
199       parent.removeChild(selectNode_.getUri()) ;
200       setSelectedNode(parent) ;
201     }
202   }
203   
204   public Object JavaDoc getPortalComponent(Class JavaDoc clazz) {
205     PortalComponentCache cache =
206       (PortalComponentCache) SessionContainer.getComponent(PortalComponentCache.class) ;
207     return cache.getPortalComponent(clazz) ;
208   }
209   
210   public UIBody findUIBody(UIContainer container) {
211     List JavaDoc children = container.getChildren();
212     int childrenSize = children.size() ;
213     UIBody uiBody = null ;
214     for(int i = 0; i < childrenSize; i++) {
215       Object JavaDoc child = children.get(i) ;
216       if(child instanceof UIBody) {
217         uiBody = (UIBody) child ;
218       } else if (child instanceof UIContainer) {
219         uiBody = findUIBody((UIContainer) child) ;
220       }
221       if(uiBody != null) return uiBody ;
222     }
223     return null ;
224   }
225   
226   final public UIBody findUIBody() { return findUIBody(getRootContainer()) ; }
227   
228   final public UIContainer getRootContainer() { return (UIContainer) getChildren().get(0) ; }
229   
230   public UIPage getCurrentUIPage() {
231     UIBody uiBody = findUIBody() ;
232     if(uiBody.getBodyComponent() instanceof UIPage) {
233       return (UIPage)uiBody.getBodyComponent() ;
234     }
235     PortalComponentCache cache =
236       (PortalComponentCache) SessionContainer.getComponent(PortalComponentCache.class) ;
237     return cache.getUIPage(selectNode_, this) ;
238   }
239   
240   public void setBodyComponent(UIComponent component) {
241     UIBody uiBody = findUIBody() ;
242     uiBody.setBodyComponent(component) ;
243     if(portalMode_ == ExoPortal.PORTAL_EDIT_PAGE_MODE) {
244       uiBody.setBodyMode(UIBasicComponent.COMPONENT_EDIT_MODE) ;
245     } else {
246       uiBody.setBodyMode(UIBasicComponent.COMPONENT_VIEW_MODE) ;
247     }
248     userCss_ = CssStyleBuilderVisitor.getCSS(getRootContainer()) ;
249   }
250   
251   public void setLastBodyComponent() {
252     UIContainer uiLayout = (UIContainer)getChildren().get(0) ;
253     UIBody uiBody = findUIBody(uiLayout) ;
254     uiBody.setLastBodyComponent() ;
255     userCss_ = CssStyleBuilderVisitor.getCSS(getRootContainer()) ;
256   }
257   
258   final public void processDecodes(FacesContext context) {
259     RequestInfo pparams = (RequestInfo) SessionContainer.getComponent(RequestInfo.class);
260         String JavaDoc targetComponent = pparams.getTargetComponentId();
261         if (targetComponent != null) {
262             UIComponent uiTargetComponent = (UIComponent)findComponentById(targetComponent);
263       if(uiTargetComponent == null) {
264         //log_.info("Cannot find the the target component id: " + targetComponent) ;
265
} else {
266         if(uiTargetComponent == this) decode(context) ;
267         else uiTargetComponent.processDecodes(context);
268       }
269         } else {
270             UIBody uiBody = findUIBody((UIContainer) getChildren().get(0));
271             uiBody.processDecodes(context);
272         }
273   }
274   
275   final public void processValidators(FacesContext context) {
276     RequestInfo pparams = (RequestInfo) SessionContainer.getComponent(RequestInfo.class);
277     if(pparams.getPortalAction() != null) {
278       super.processValidators(context) ;
279     }
280   }
281
282   protected String JavaDoc getComponentIdPrefix() { return "" ; }
283   protected Style getDecoratorStyle(SkinConfigService service, String JavaDoc renderer, String JavaDoc style) {
284     return null ;
285   }
286   
287   public void changeLocale(ResourceBundle JavaDoc res) {
288     root_.visit(new ResolveLabelVisitor(res)) ;
289   }
290   
291   public void buildTreeModel(Container parent) { }
292   
293   public int getDisplayMessageType() { return BODY_MESSAGE_TYPE ;}
294   public void setDisplayMessageType(int type) { }
295   public void addMessage(FacesMessage message) { information_.addMessage(message); }
296   public void clearMessages() { information_.clearMessages() ;}
297   public List JavaDoc getMessages() { return information_.getMessages() ; }
298   public boolean hasMessage() { return information_.hasMessage() ;}
299   
300   public boolean hasQueuEvent() { return queueEvent_ ; }
301   public void setQueueEvent(boolean b) { queueEvent_ = b ;}
302   public void queueEvent(javax.faces.event.FacesEvent event) {
303     queueEvent_ = true ;
304     super.queueEvent(event) ;
305   }
306   
307   class ResolveLabelVisitor implements NodeVisitor {
308     ResourceBundle JavaDoc res_ ;
309     
310     public ResolveLabelVisitor(ResourceBundle JavaDoc res) { res_ = res ; }
311     
312     public void visit(Node node) { node.setResolvedLabel(res_) ; }
313   }
314 }
Popular Tags