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.wcm.webui.validator;
18  
19  import java.util.MissingResourceException;
20  import java.util.ResourceBundle;
21  
22  import org.exoplatform.portal.webui.util.Util;
23  import org.exoplatform.services.cms.templates.TemplateService;
24  import org.exoplatform.web.application.ApplicationMessage;
25  import org.exoplatform.webui.core.UIComponent;
26  import org.exoplatform.webui.exception.MessageException;
27  import org.exoplatform.webui.form.UIForm;
28  import org.exoplatform.webui.form.UIFormInput;
29  
30  /**
31   * Created by The eXo Platform SAS
32   * Author : Phan Le Thanh Chuong
33   *          chuong.phan@exoplatform.com, phan.le.thanh.chuong@gmail.com
34   * Modified: - Extends from org.exoplatform.webui.form.validator.MandatoryValidator
35   *             Use template name to generate a label for input instead of use form name
36   *           - Only use for Form Generator feature
37   * Nov 11, 2009
38   */
39  public class MandatoryValidator extends org.exoplatform.webui.form.validator.MandatoryValidator {
40  
41    @SuppressWarnings("unchecked")
42    public void validate(UIFormInput uiInput) throws Exception {
43      if((uiInput.getValue() != null) && ((String)uiInput.getValue()).trim().length() > 0) {
44        return ;
45      }
46  
47      UIComponent uiComponent = (UIComponent) uiInput ;
48      UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class) ;
49      String templatePath = uiForm.getTemplate();
50      String nodetypeName = "";
51      String[] temps = templatePath.split("/");
52      for (String temp : temps){
53        if (temp.contains("_fg_n")) {
54          nodetypeName = temp;
55          break;
56        }
57      }
58      TemplateService templateService = uiForm.getApplicationComponent(TemplateService.class);
59      String nodetypeLabel = templateService.getTemplateLabel(nodetypeName).trim();
60      ResourceBundle res = Util.getPortalRequestContext().getApplicationResourceBundle();
61      String label = "";
62      try {
63        label = res.getString(nodetypeLabel + ".label." + uiInput.getName());
64      } catch (MissingResourceException e) {
65        label = uiInput.getName();
66      }
67      Object[]  args = {label, uiInput.getBindingField() } ;
68      throw new MessageException(new ApplicationMessage("EmptyFieldValidator.msg.empty-input", args, ApplicationMessage.WARNING)) ;
69    }
70  }