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.search;
18  
19  import javax.jcr.RepositoryException;
20  import javax.jcr.query.Query;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
24  import org.exoplatform.services.security.IdentityConstants;
25  import org.exoplatform.web.application.ApplicationMessage;
26  import org.exoplatform.webui.config.annotation.ComponentConfig;
27  import org.exoplatform.webui.config.annotation.EventConfig;
28  import org.exoplatform.webui.core.UIApplication;
29  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
30  import org.exoplatform.webui.event.Event;
31  import org.exoplatform.webui.event.EventListener;
32  import org.exoplatform.webui.event.Event.Phase;
33  import org.exoplatform.webui.form.UIForm;
34  import org.exoplatform.webui.form.UIFormInputInfo;
35  import org.exoplatform.webui.form.UIFormStringInput;
36  import org.exoplatform.webui.form.validator.MandatoryValidator;
37  
38  /**
39   * Created by The eXo Platform SAS
40   * Author : Hoa Pham
41   *          hoa.pham@exoplatform.com
42   * Oct 2, 2007
43   */
44  @ComponentConfig(
45      lifecycle = UIFormLifecycle.class,
46      template =  "system:/groovy/webui/form/UIForm.gtmpl",
47      events = {
48        @EventConfig(listeners = UIContentNameSearch.SearchActionListener.class),
49        @EventConfig(listeners = UIContentNameSearch.CancelActionListener.class, phase=Phase.DECODE)
50      }
51  )
52  public class UIContentNameSearch extends UIForm {
53  
54    private static String       KEYWORD             = "keyword";
55  
56    private static String       SEARCH_LOCATION     = "location";
57  
58    private static final String ROOT_PATH_SQL_QUERY = "select * from nt:base where " +
59                                                      "contains(exo:name, '$1') or contains(exo:title, '$1') or  " +
60                                                      "lower(exo:name) like '%$2%' order by exo:title ASC";
61  
62    private static final String PATH_SQL_QUERY      = "select * from nt:base where jcr:path like '$0/%' AND " +
63                                                      "( contains(exo:name, '$1') or contains(exo:title, '$1') or " + 
64                                                      "lower(exo:name) like '%$2%') order by exo:title ASC";
65  
66    public UIContentNameSearch() throws Exception {
67      addChild(new UIFormInputInfo(SEARCH_LOCATION,null,null));
68      addChild(new UIFormStringInput(KEYWORD,null).addValidator(MandatoryValidator.class));
69    }
70  
71    public void setLocation(String location) {
72      getUIFormInputInfo(SEARCH_LOCATION).setValue(location);
73    }
74  
75    static public class SearchActionListener extends EventListener<UIContentNameSearch> {
76      public void execute(Event<UIContentNameSearch> event) throws Exception {
77        UIContentNameSearch contentNameSearch = event.getSource();
78        UIECMSearch uiECMSearch = contentNameSearch.getAncestorOfType(UIECMSearch.class);
79        UISearchResult uiSearchResult = uiECMSearch.getChild(UISearchResult.class);
80        UIApplication application = contentNameSearch.getAncestorOfType(UIApplication.class);
81        try {
82          String keyword = contentNameSearch.getUIStringInput(KEYWORD).getValue();
83          keyword = keyword.trim();
84          String escapedText = org.exoplatform.services.cms.impl.Utils.escapeIllegalCharacterInQuery(keyword);
85          UIJCRExplorer explorer = contentNameSearch.getAncestorOfType(UIJCRExplorer.class);
86          String currentNodePath = explorer.getCurrentNode().getPath();
87          String statement = null;
88          if("/".equalsIgnoreCase(currentNodePath)) {
89            statement = StringUtils.replace(ROOT_PATH_SQL_QUERY,"$1",escapedText);
90            statement = StringUtils.replace(statement,"$2",escapedText.toLowerCase());
91          }else {
92            statement = StringUtils.replace(PATH_SQL_QUERY,"$0",currentNodePath);
93            statement = StringUtils.replace(statement,"$1",escapedText);
94            statement = StringUtils.replace(statement,"$2",escapedText.toLowerCase());
95          }
96          long startTime = System.currentTimeMillis();
97          uiSearchResult.setQuery(statement, explorer.getTargetSession().getWorkspace().getName(), Query.SQL, 
98                                  IdentityConstants.SYSTEM.equals(explorer.getTargetSession()), null);
99          uiSearchResult.updateGrid();
100         long time = System.currentTimeMillis() - startTime;
101         uiSearchResult.setSearchTime(time);
102         contentNameSearch.getUIFormInputInfo(SEARCH_LOCATION).setValue(currentNodePath);
103         uiECMSearch.setSelectedTab(uiSearchResult.getId());
104       } catch (RepositoryException reEx) {
105         application.addMessage(new ApplicationMessage("UIContentNameSearch.msg.keyword-not-allowed",
106                                                       null,
107                                                       ApplicationMessage.WARNING));
108         event.getRequestContext().addUIComponentToUpdateByAjax(contentNameSearch);
109         return;
110       } catch (Exception e) {
111         uiSearchResult.setQuery(null, null, null, false, null);
112         uiSearchResult.updateGrid();
113       }
114     }
115   }
116 
117   static public class CancelActionListener extends EventListener<UIContentNameSearch> {
118     public void execute(Event<UIContentNameSearch> event) throws Exception {
119       event.getSource().getAncestorOfType(UIJCRExplorer.class).cancelAction();
120     }
121   }
122 
123 }