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.explorer.popup.info;
18  
19  import java.util.List;
20  
21  import javax.jcr.Node;
22  
23  import org.exoplatform.services.log.Log;
24  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
25  import org.exoplatform.ecm.webui.utils.PermissionUtil;
26  import org.exoplatform.resolver.ResourceResolver;
27  import org.exoplatform.services.cms.metadata.MetadataService;
28  import org.exoplatform.services.log.ExoLogger;
29  import org.exoplatform.web.application.ApplicationMessage;
30  import org.exoplatform.webui.application.WebuiRequestContext;
31  import org.exoplatform.webui.config.annotation.ComponentConfig;
32  import org.exoplatform.webui.config.annotation.EventConfig;
33  import org.exoplatform.webui.core.UIApplication;
34  import org.exoplatform.webui.core.UIContainer;
35  import org.exoplatform.webui.event.Event;
36  import org.exoplatform.webui.event.EventListener;
37  import org.exoplatform.webui.exception.MessageException;
38  
39  /**
40   * Created by The eXo Platform SARL
41   * Author : Dang Van Minh
42   *          minh.dang@exoplatform.com
43   * Jan 25, 2007
44   * 2:05:40 PM
45   */
46  @ComponentConfig(
47      events = {
48        @EventConfig(listeners = UIViewMetadataTemplate.EditPropertyActionListener.class),
49        @EventConfig(listeners = UIViewMetadataTemplate.CancelActionListener.class)
50      }
51  )
52  public class UIViewMetadataTemplate extends UIContainer {
53  
54    private String documentType_ ;
55    private static final Log LOG  = ExoLogger.getLogger(UIViewMetadataTemplate.class.getName());
56    public UIViewMetadataTemplate() throws Exception {
57    }
58  
59    public void setTemplateType(String type) {documentType_ = type ;}
60  
61    public String getViewTemplatePath() {
62      MetadataService metadataService = getApplicationComponent(MetadataService.class) ;
63      try {
64        return metadataService.getMetadataPath(documentType_, false) ;
65      } catch (Exception e) {
66        if (LOG.isErrorEnabled()) {
67          LOG.error("Unexpected error", e);
68        }
69      }
70      return null ;
71    }
72  
73    public String getTemplate() { return getViewTemplatePath() ; }
74  
75    @SuppressWarnings("unused")
76    public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context, String template) {
77      //return getAncestorOfType(UIJCRExplorer.class).getJCRTemplateResourceResolver();
78      getAncestorOfType(UIJCRExplorer.class).newJCRTemplateResourceResolver() ;
79      return getAncestorOfType(UIJCRExplorer.class).getJCRTemplateResourceResolver();
80    }
81  
82    public Node getViewNode(String nodeType) throws Exception {
83      return getAncestorOfType(UIJCRExplorer.class).getViewNode(nodeType) ;
84    }
85  
86    public List<String> getMultiValues(Node node, String name) throws Exception {
87      return getAncestorOfType(UIJCRExplorer.class).getMultiValues(node, name) ;
88    }
89  
90    static public class EditPropertyActionListener extends EventListener<UIViewMetadataTemplate> {
91      public void execute(Event<UIViewMetadataTemplate> event) throws Exception {
92        UIViewMetadataTemplate uiViewTemplate = event.getSource() ;
93        String nodeType = event.getRequestContext().getRequestParameter(OBJECTID) ;
94        UIViewMetadataManager uiMetaManager = uiViewTemplate.getAncestorOfType(UIViewMetadataManager.class) ;
95        UIJCRExplorer uiExplorer = uiViewTemplate.getAncestorOfType(UIJCRExplorer.class) ;
96        UIApplication uiApp = uiViewTemplate.getAncestorOfType(UIApplication.class);
97        Node currentNode = uiExplorer.getCurrentNode();
98        if (!PermissionUtil.canSetProperty(currentNode)) {
99          throw new MessageException(new ApplicationMessage("UIViewMetadataTemplate.msg.access-denied",
100                                                           null, ApplicationMessage.WARNING)) ;
101       }
102       if (!currentNode.isCheckedOut()) {
103         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.node-checkedin", null));
104 
105         return;
106       }
107       uiMetaManager.initMetadataFormPopup(nodeType) ;
108       UIViewMetadataContainer uiContainer = uiViewTemplate.getParent() ;
109       event.getRequestContext().addUIComponentToUpdateByAjax(uiContainer.getParent()) ;
110     }
111   }
112 
113   static public class CancelActionListener extends EventListener<UIViewMetadataTemplate> {
114     public void execute(Event<UIViewMetadataTemplate> event) throws Exception {
115       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class) ;
116       uiExplorer.cancelAction() ;
117     }
118   }
119 }