View Javadoc
1   /*
2    * Copyright (C) 2003-2009 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.explorer.control.action;
18  
19  import java.util.Arrays;
20  import java.util.Enumeration;
21  import java.util.Hashtable;
22  import java.util.List;
23  
24  import javax.jcr.Node;
25  
26  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
27  import org.exoplatform.ecm.webui.component.explorer.control.UIActionBar;
28  import org.exoplatform.ecm.webui.component.explorer.control.filter.HasMetadataTemplatesFilter;
29  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIActionBarActionListener;
30  import org.exoplatform.ecm.webui.component.explorer.popup.info.UIViewMetadataContainer;
31  import org.exoplatform.ecm.webui.component.explorer.popup.info.UIViewMetadataManager;
32  import org.exoplatform.ecm.webui.component.explorer.popup.info.UIViewMetadataTemplate;
33  import org.exoplatform.services.cms.impl.Utils;
34  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
35  import org.exoplatform.webui.config.annotation.ComponentConfig;
36  import org.exoplatform.webui.config.annotation.EventConfig;
37  import org.exoplatform.webui.core.UIComponent;
38  import org.exoplatform.webui.core.UIPopupContainer;
39  import org.exoplatform.webui.event.Event;
40  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
41  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
42  
43  /**
44   * Created by The eXo Platform SAS
45   * Author : eXoPlatform
46   *          nicolas.filotto@exoplatform.com
47   * 6 mai 2009
48   */
49  @ComponentConfig(
50       events = {
51         @EventConfig(listeners = ViewMetadatasActionComponent.ViewMetadatasActionListener.class)
52       }
53   )
54  public class ViewMetadatasActionComponent extends UIComponent {
55  
56    private static final List<UIExtensionFilter> FILTERS = Arrays.asList(new UIExtensionFilter[]{new HasMetadataTemplatesFilter()});
57  
58    @UIExtensionFilters
59    public List<UIExtensionFilter> getFilters() {
60      return FILTERS;
61    }
62  
63    public static class ViewMetadatasActionListener extends UIActionBarActionListener<ViewMetadatasActionComponent> {
64      public void processEvent(Event<ViewMetadatasActionComponent> event) throws Exception {
65        UIActionBar uiActionBar = event.getSource().getAncestorOfType(UIActionBar.class);
66        UIJCRExplorer uiJCRExplorer = uiActionBar.getAncestorOfType(UIJCRExplorer.class);
67        Node currentNode = uiJCRExplorer.getCurrentNode() ;
68        
69        
70        Hashtable<String, String> metaDataTemp = WCMCoreUtils.getMetadataTemplates(currentNode);
71        UIPopupContainer UIPopupContainer = uiJCRExplorer.getChild(UIPopupContainer.class);
72        UIPopupContainer.activate(UIViewMetadataManager.class, 700);
73        UIViewMetadataManager uiMetadataManager =
74          UIPopupContainer.findFirstComponentOfType(UIViewMetadataManager.class);
75        UIViewMetadataContainer uiMetadataContainer = uiMetadataManager.getChild(UIViewMetadataContainer.class);
76        int i = 0;
77        Enumeration<String> enu = metaDataTemp.keys();
78        while (enu.hasMoreElements()) {
79          String key = (String) enu.nextElement();
80          String template = metaDataTemp.get(key);
81          if(template != null && template.length() > 0) {
82            UIViewMetadataTemplate uiMetaView =
83              uiMetadataContainer.createUIComponent(UIViewMetadataTemplate.class, null, Utils.cleanString(key)) ;
84            uiMetaView.setTemplateType(key) ;
85            uiMetadataContainer.addChild(uiMetaView) ;
86            uiMetaView.setRendered(true);
87            i++ ;
88          }
89        }
90        uiMetadataContainer.setSelectedTab(1);
91        uiMetadataContainer.setHasMoreOneMetaData(i > 1);
92        event.getRequestContext().addUIComponentToUpdateByAjax(UIPopupContainer);
93      }
94    }
95  }