KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import javax.faces.context.ExternalContext;
11 import javax.faces.context.FacesContext;
12 import org.exoplatform.faces.application.ExoFacesMessage;
13 import org.exoplatform.faces.core.component.InformationProvider;
14 import org.exoplatform.faces.core.component.UIExoCommand;
15 import org.exoplatform.faces.core.event.ExoActionEvent;
16 import org.exoplatform.faces.core.event.ExoActionListener;
17 import org.exoplatform.services.portal.skin.SkinConfigService;
18 import org.exoplatform.services.portal.skin.model.Style;
19 import org.exoplatform.services.portletcontainer.pci.ExoWindowID;
20 import org.exoplatform.services.portletregistery.Portlet;
21 import org.exoplatform.services.portletregistery.PortletRegisteryService;
22 import org.exoplatform.services.portletregistery.PortletRole;
23 /**
24  * Created y the eXo platform team
25  * User: Benjamin Mestrallet
26  * Date: 18 juin 2004
27  */

28 public class UIPortletInfo extends UIExoCommand {
29   public static final String JavaDoc SELECT_PORTLET = "selectPortlet";
30
31   protected Portlet portlet_ ;
32   private UIContainer portletContainer_ ;
33   private SkinConfigService skinService_ ;
34   private PortletRegisteryService registryService_ ;
35   private PortalComponentIDGenerator idGenerator_ ;
36   
37   public UIPortletInfo() {
38     setRendererType("PortletInfoRenderer");
39     setId("UIPortletInfo");
40     }
41
42   public UIPortletInfo(SkinConfigService service,
43                          PortletRegisteryService portletRegisteryService,
44                        PortalComponentIDGenerator idGenerator) {
45     this() ;
46     idGenerator_ = idGenerator ;
47     skinService_ = service ;
48     registryService_ = portletRegisteryService ;
49     addActionListener(SelectPortletActionListener.class, SELECT_PORTLET) ;
50     }
51   
52   public String JavaDoc getFamily() {
53     return "org.exoplatform.portal.faces.component.UIPortletInfo";
54   }
55
56   public UIContainer getPortletContainer() { return portletContainer_ ; }
57   public void setPortletContainer(UIContainer container) {
58     portletContainer_ = container ;
59   }
60   
61   public void setPortletData(Portlet portlet) { portlet_ = portlet; }
62
63   public Portlet getPortletData() { return portlet_ ;}
64   
65   static public class SelectPortletActionListener extends ExoActionListener {
66     public void execute(ExoActionEvent event) throws Exception JavaDoc {
67         UIPortletInfo uiPortletInfo = (UIPortletInfo) event.getComponent() ;
68         ExternalContext econtext = FacesContext.getCurrentInstance().getExternalContext() ;
69         Collection JavaDoc temp =
70             uiPortletInfo.registryService_.getPortletRoles(uiPortletInfo.portlet_.getId());
71         Iterator JavaDoc i = temp.iterator() ;
72         boolean hasRole = false ;
73         while(i.hasNext() && !hasRole) {
74             PortletRole role = (PortletRole) i.next() ;
75             hasRole = econtext.isUserInRole(role.getPortletRoleName()) ;
76         }
77         
78         if (!hasRole) {
79             InformationProvider iprovider = findInformationProvider(uiPortletInfo) ;
80             iprovider.addMessage(new ExoFacesMessage("#{UIPortletInfo.msg.require-role}")) ;
81             return ;
82         }
83         
84         UIPortal uiPortal = (UIPortal) uiPortletInfo.getAncestorOfType(UIPortal.class) ;
85         String JavaDoc appName = uiPortletInfo.portlet_.getPortletApplicationName() ;
86         String JavaDoc portletName = uiPortletInfo.portlet_.getPortletName() ;
87         String JavaDoc portletId = appName + "/" + portletName ;
88       String JavaDoc styleName = "default" ;
89       String JavaDoc rendererName = "default" ;
90       List JavaDoc styles = uiPortletInfo.skinService_.getPortletStyles(portletId) ;
91       if(styles != null) {
92          styleName = ((Style) styles.get(0)).getName() ;
93        }
94        String JavaDoc id = uiPortletInfo.idGenerator_.generatePortletId(uiPortal, portletName) ;
95        UIPortlet uiPortlet = new UIPortlet(uiPortal.getOwner(), appName, portletName, id);
96        uiPortlet.setComponentMode(UIBasicComponent.COMPONENT_EDIT_MODE) ;
97        org.exoplatform.services.portal.model.Portlet model = uiPortlet.getEditablePortletModel() ;
98        model.setRenderer(rendererName) ;
99        model.setPortletStyle(styleName) ;
100        model.setDecorator("default") ;
101        uiPortletInfo.portletContainer_.getChildren().add(uiPortlet) ;
102        uiPortletInfo.portletContainer_.setComponentModified(true) ;
103        UIPage uiPage = (UIPage)uiPortlet.getAncestorOfType(UIPage.class) ;
104        if(uiPage != null) {
105          ExoWindowID windowId = uiPortlet.getWindowId() ;
106          windowId.setConfigurationSource(uiPage.getPageModel().getPageId()) ;
107        }
108        uiPortal.setLastBodyComponent() ;
109     }
110   }
111 }
Popular Tags