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   **************************************************************************/
18  package org.exoplatform.ecm.webui.component.admin.taxonomy;
19  
20  import javax.jcr.Node;
21  import javax.jcr.RepositoryException;
22  import javax.jcr.Session;
23  
24  import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
25  import org.exoplatform.portal.webui.util.Util;
26  import org.exoplatform.services.cms.taxonomy.TaxonomyService;
27  import org.exoplatform.services.cms.taxonomy.TaxonomyTreeData;
28  import org.exoplatform.services.cms.taxonomy.impl.TaxonomyNodeAlreadyExistsException;
29  import org.exoplatform.services.jcr.ext.common.SessionProvider;
30  import org.exoplatform.services.jcr.util.Text;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  import org.exoplatform.wcm.webui.Utils;
33  import org.exoplatform.web.application.ApplicationMessage;
34  import org.exoplatform.webui.config.annotation.ComponentConfig;
35  import org.exoplatform.webui.config.annotation.EventConfig;
36  import org.exoplatform.webui.core.UIApplication;
37  import org.exoplatform.webui.core.UIPopupWindow;
38  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
39  import org.exoplatform.webui.event.Event;
40  import org.exoplatform.webui.event.Event.Phase;
41  import org.exoplatform.webui.event.EventListener;
42  import org.exoplatform.webui.form.UIForm;
43  import org.exoplatform.webui.form.UIFormInputInfo;
44  import org.exoplatform.webui.form.UIFormStringInput;
45  import org.exoplatform.webui.form.validator.MandatoryValidator;
46  
47  /**
48   * Created by The eXo Platform SARL
49   * Author : Hoang Van Hung
50   *          hunghvit@gmail.com
51   * Apr 15, 2009
52   */
53  
54  @ComponentConfig(
55      lifecycle = UIFormLifecycle.class,
56      template =  "system:/groovy/webui/form/UIForm.gtmpl",
57      events = {
58        @EventConfig(listeners = UITaxonomyTreeCreateChildForm.SaveActionListener.class),
59        @EventConfig(listeners = UITaxonomyTreeCreateChildForm.CancelActionListener.class, phase = Phase.DECODE)
60      }
61  )
62  
63  public class UITaxonomyTreeCreateChildForm extends UIForm {
64  
65    final static private String FIELD_PARENT = "parentPath";
66  
67    final static private String FIELD_NAME   = "taxonomyName";
68  
69    public UITaxonomyTreeCreateChildForm() throws Exception {
70      addUIFormInput(new UIFormInputInfo(FIELD_PARENT, FIELD_PARENT, null));
71      addUIFormInput(new UIFormStringInput(FIELD_NAME, FIELD_NAME, null).addValidator(MandatoryValidator.class).addValidator(ECMNameValidator.class));
72    }
73  
74    public void setParent(String path) {
75      getUIFormInputInfo(FIELD_PARENT).setValue(path);
76      getUIStringInput(FIELD_NAME).setValue(null);
77    }
78  
79    public static class SaveActionListener extends EventListener<UITaxonomyTreeCreateChildForm> {
80      public void execute(Event<UITaxonomyTreeCreateChildForm> event) throws Exception {
81        UITaxonomyTreeCreateChildForm uiForm = event.getSource();
82        UITaxonomyTreeCreateChild uiCreateChild = uiForm.getAncestorOfType(UITaxonomyTreeCreateChild.class);
83        UITaxonomyTreeContainer uiTaxonomyTreeContainer = uiForm.getAncestorOfType(UITaxonomyTreeContainer.class);
84        UITaxonomyManagerTrees uiTaxonomyManageTrees = uiForm.getAncestorOfType(UITaxonomyManagerTrees.class);
85        TaxonomyTreeData taxoTreeData = uiTaxonomyTreeContainer.getTaxonomyTreeData();
86        UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class);
87        String title = uiForm.getUIStringInput(FIELD_NAME).getValue();
88        
89        String name = Utils.cleanString(title);
90        
91        if (name == null || name.length() == 0) {
92          uiApp.addMessage(new ApplicationMessage("UITaxonomyTreeCreateChildForm.msg.name-null",
93              null, ApplicationMessage.WARNING));
94          
95          return;
96        }
97  
98        try {
99          TaxonomyService taxonomyService = uiForm.getApplicationComponent(TaxonomyService.class);
100         if (title.length() > Integer.parseInt(taxonomyService.getCategoryNameLength())) {
101           Object[] args = { taxonomyService.getCategoryNameLength() };
102           uiApp.addMessage(new ApplicationMessage("UITaxonomyTreeCreateChildForm.msg.name-too-long",
103                                                   args,
104                                                   ApplicationMessage.WARNING));
105 
106           return;
107         }
108         String parentPath = uiForm.getUIFormInputInfo(FIELD_PARENT).getValue();
109         taxonomyService.addTaxonomyNode(taxoTreeData
110             .getTaxoTreeWorkspace(), parentPath, Text.escapeIllegalJcrChars(name), Util.getPortalRequestContext().getRemoteUser());
111         
112         SessionProvider p = WCMCoreUtils.getSystemSessionProvider();
113         Session session = p.getSession(taxoTreeData.getTaxoTreeWorkspace(), WCMCoreUtils.getRepository());
114         Node newNode = (Node)session.getItem(parentPath + "/" + name); 
115         
116         if (newNode.canAddMixin("exo:rss-enable")) {
117           newNode.addMixin("exo:rss-enable");
118           newNode.setProperty("exo:title", title);
119         }
120         newNode.save();
121         
122         uiCreateChild.update();
123       } catch (TaxonomyNodeAlreadyExistsException e) {
124         Object[] arg = { name };
125         uiApp.addMessage(new ApplicationMessage("UITaxonomyTreeCreateChildForm.msg.exist", arg,
126             ApplicationMessage.WARNING));
127         
128         return;
129       } catch (RepositoryException e) {
130         Object[] arg = { name };
131         uiApp.addMessage(new ApplicationMessage("UITaxonomyTreeCreateChildForm.msg.error", arg,
132             ApplicationMessage.WARNING));
133         
134         return;
135       }
136       uiForm.reset();
137       UIPopupWindow uiPopup = uiForm.getParent();
138       uiPopup.setRendered(false);
139       uiPopup.setShow(false);
140       event.getRequestContext().addUIComponentToUpdateByAjax(uiTaxonomyManageTrees);
141     }
142   }
143 
144   public static class CancelActionListener extends EventListener<UITaxonomyTreeCreateChildForm> {
145     public void execute(Event<UITaxonomyTreeCreateChildForm> event) throws Exception {
146       UITaxonomyTreeCreateChildForm uiForm = event.getSource();
147       UIPopupWindow uiPopup = uiForm.getParent();
148       uiPopup.setRendered(false);
149       uiPopup.setShow(false);
150       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup);
151     }
152   }
153 }