View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.component.admin;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.portlet.PortletPreferences;
23  
24  import org.exoplatform.services.log.Log;
25  import org.exoplatform.ecm.webui.utils.Utils;
26  import org.exoplatform.services.jcr.RepositoryService;
27  import org.exoplatform.services.log.ExoLogger;
28  import org.exoplatform.webui.application.WebuiRequestContext;
29  import org.exoplatform.webui.application.portlet.PortletRequestContext;
30  import org.exoplatform.webui.config.annotation.ComponentConfig;
31  import org.exoplatform.webui.core.UIComponent;
32  import org.exoplatform.webui.core.UIContainer;
33  import org.exoplatform.webui.ext.manager.UIAbstractManager;
34  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
35  
36  /**
37   * Created by The eXo Platform SARL
38   * Author : Tran The Trong
39   *          trongtt@gmail.com
40   * Sep 19, 2006
41   * 8:30:33 AM
42   */
43  @ComponentConfig(
44      template = "app:/groovy/webui/component/admin/UIECMAdminWorkingArea.gtmpl"
45  )
46  public class UIECMAdminWorkingArea extends UIContainer {
47  
48    /**
49     * Logger.
50     */
51    private static final Log LOG  = ExoLogger.getLogger(UIECMAdminWorkingArea.class.getName());
52    private String renderedCompId_ ;
53  
54    public String getRenderedCompId() { return renderedCompId_ ; }
55    public void setRenderedCompId(String renderedId) { this.renderedCompId_ = renderedId ; }
56  
57    public <T extends UIComponent> void setChild(Class<T> type) {
58      renderedCompId_ = getChild(type).getId();
59      setRenderedChild(type);
60    }
61  
62    public UIECMAdminWorkingArea() throws Exception {}
63  
64    public void init() throws Exception {
65      UIECMAdminPortlet portlet = getAncestorOfType(UIECMAdminPortlet.class);
66      UIECMAdminControlPanel controlPanel = portlet.getChild(UIECMAdminControlPanel.class);
67      List<UIAbstractManagerComponent> managers = controlPanel.getManagers();
68      List<UIAbstractManagerComponent> rejectedManagers = null;
69      if (managers == null) {
70        return;
71      }
72      for (UIAbstractManagerComponent manager : managers) {
73        UIAbstractManager uiManager = getChild(manager.getUIAbstractManagerClass());
74        if (uiManager == null) {
75          uiManager = addChild(manager.getUIAbstractManagerClass(), null, null);
76          if (renderedCompId_ == null) {
77            try {
78              uiManager.init();
79              renderedCompId_ = uiManager.getId();
80            } catch (Exception e) {
81              if (LOG.isErrorEnabled()) {
82                LOG.error("The manager " + uiManager.getClass() + " cannot be initialized, it will be unregistered", e);
83              }
84              if (rejectedManagers == null) {
85                rejectedManagers = new ArrayList<UIAbstractManagerComponent>();
86              }
87              rejectedManagers.add(manager);
88              removeChild(manager.getUIAbstractManagerClass());
89            }
90          } else {
91            uiManager.setRendered(false);
92          }
93        } else {
94          uiManager.refresh();
95        }
96      }
97      if (rejectedManagers != null) {
98        for (UIAbstractManagerComponent manager : rejectedManagers) {
99          controlPanel.unregister(manager);
100       }
101     }
102   }
103 
104   public void checkRepository() throws Exception{
105     PortletRequestContext pcontext = (PortletRequestContext) WebuiRequestContext
106         .getCurrentInstance();
107     PortletPreferences pref = pcontext.getRequest().getPreferences();
108     try {
109       getApplicationComponent(RepositoryService.class).getCurrentRepository();
110     } catch (Exception e) {
111       String defaultRepo = getApplicationComponent(RepositoryService.class).getCurrentRepository()
112           .getConfiguration().getName();
113       pref.setValue(Utils.REPOSITORY, defaultRepo);
114       pref.store();
115     }
116   }
117 
118 }