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.control;
18  
19  import java.io.Writer;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.ResourceBundle;
23  
24  import javax.portlet.PortletRequest;
25  import javax.servlet.http.*;
26  
27  import org.exoplatform.ecm.jcr.model.Preference;
28  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
29  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorerPortlet;
30  import org.exoplatform.ecm.webui.component.explorer.sidebar.UIAllItems;
31  import org.exoplatform.portal.webui.util.Util;
32  import org.exoplatform.services.wcm.core.NodetypeConstant;
33  import org.exoplatform.web.application.RequestContext;
34  import org.exoplatform.web.security.csrf.CSRFTokenUtil;
35  import org.exoplatform.webui.application.WebuiRequestContext;
36  import org.exoplatform.webui.config.annotation.ComponentConfig;
37  import org.exoplatform.webui.config.annotation.EventConfig;
38  import org.exoplatform.webui.core.UIPopupComponent;
39  import org.exoplatform.webui.core.UIPopupContainer;
40  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
41  import org.exoplatform.webui.core.model.SelectItemOption;
42  import org.exoplatform.webui.event.Event;
43  import org.exoplatform.webui.event.Event.Phase;
44  import org.exoplatform.webui.event.EventListener;
45  import org.exoplatform.webui.form.UIForm;
46  import org.exoplatform.webui.form.UIFormSelectBox;
47  import org.exoplatform.webui.form.input.UICheckBoxInput;
48  
49  /**
50   * Created by The eXo Platform SARL
51   * Author : Chien Nguyen
52   *          chien.nguyen@exoplatform.org
53   * July 28, 2010
54   * 14:07:15 PM
55   */
56  
57  @ComponentConfig(
58      lifecycle = UIFormLifecycle.class,
59      template = "app:/groovy/webui/component/explorer/UIPreferencesForm.gtmpl",
60      events = {
61        @EventConfig(listeners = UIPreferencesForm.SaveActionListener.class),
62        @EventConfig(phase = Phase.DECODE, listeners = UIPreferencesForm.AdvanceActionListener.class),
63        @EventConfig(phase = Phase.DECODE, listeners = UIPreferencesForm.BackActionListener.class)
64      })
65  public class UIPreferencesForm extends UIForm implements UIPopupComponent {
66  
67    final static public String FIELD_ENABLESTRUCTURE  = "enableStructure";
68  
69    final static public String FIELD_SHOWSIDEBAR      = "showSideBar";
70  
71    final static public String FIELD_SHOWNONDOCUMENT  = "showNonDocument";
72  
73    final static public String FIELD_SHOWREFDOCUMENTS = "showRefDocuments";
74  
75    final static public String FIELD_SHOW_HIDDEN_NODE = "showHiddenNode";
76  
77    final static public String FIELD_SHOW_ITEMS_BY_USER = "showItemsByUserInTimeline";
78  
79    final static public String FIELD_ENABLE_DRAG_AND_DROP = "enableDragAndDrop";
80  
81    final static public String FIELD_SHORTBY          = "sortBy";
82  
83    final static public String FIELD_ORDERBY          = "order";
84  
85    final static public String FIELD_PROPERTY         = "property";
86  
87    final static public String NODES_PER_PAGE         = "nodesPerPage";
88  
89    final static public String FIELD_QUERY_TYPE       = "queryType";
90  
91    private boolean advancePreferences = false;
92  
93    public UIPreferencesForm() throws Exception {
94      RequestContext context = RequestContext.getCurrentInstance();
95      ResourceBundle res = context.getApplicationResourceBundle();
96      String sortByNodeName;
97      String sortByNodeType;
98      String sortByCreatedDate;
99      String sortByModifiedDate;
100     String ascendingOrder;
101     String descendingOrder;
102     String SQLQuery;
103     String XPathQuery;
104     try {
105       sortByNodeName = res.getString("UIPreferencesForm.label." + NodetypeConstant.SORT_BY_NODENAME);
106       sortByNodeType = res.getString("UIPreferencesForm.label." + NodetypeConstant.SORT_BY_NODETYPE);
107       sortByCreatedDate = res.getString("UIPreferencesForm.label." + NodetypeConstant.SORT_BY_CREATED_DATE);
108       sortByModifiedDate = res.getString("UIPreferencesForm.label." + NodetypeConstant.SORT_BY_MODIFIED_DATE);
109       ascendingOrder = res.getString("UIPreferencesForm.label." + Preference.ASCENDING_ORDER);
110       descendingOrder = res.getString("UIPreferencesForm.label." + Preference.DESCENDING_ORDER);
111       SQLQuery = res.getString("UIPreferencesForm.label." + Preference.SQL_QUERY);
112       XPathQuery = res.getString("UIPreferencesForm.label." + Preference.XPATH_QUERY);
113     } catch (Exception e) {
114       sortByNodeName = NodetypeConstant.SORT_BY_NODENAME;
115       sortByNodeType = NodetypeConstant.SORT_BY_NODETYPE;
116       sortByCreatedDate = NodetypeConstant.SORT_BY_CREATED_DATE;
117       sortByModifiedDate = NodetypeConstant.SORT_BY_MODIFIED_DATE;
118       ascendingOrder = Preference.ASCENDING_ORDER;
119       descendingOrder = Preference.DESCENDING_ORDER;
120       SQLQuery = Preference.SQL_QUERY;
121       XPathQuery = Preference.XPATH_QUERY;
122     }
123     List<SelectItemOption<String>> sortOptions = new ArrayList<SelectItemOption<String>>();
124     sortOptions.add(new SelectItemOption<String>(sortByNodeName, NodetypeConstant.SORT_BY_NODENAME));
125     sortOptions.add(new SelectItemOption<String>(sortByNodeType, NodetypeConstant.SORT_BY_NODETYPE));
126     sortOptions.add(new SelectItemOption<String>(sortByCreatedDate, NodetypeConstant.SORT_BY_CREATED_DATE));
127     sortOptions.add(new SelectItemOption<String>(sortByModifiedDate, NodetypeConstant.SORT_BY_MODIFIED_DATE));
128 
129     List<SelectItemOption<String>> orderOption = new ArrayList<SelectItemOption<String>>();
130     orderOption.add(new SelectItemOption<String>(ascendingOrder, Preference.ASCENDING_ORDER));
131     orderOption.add(new SelectItemOption<String>(descendingOrder, Preference.DESCENDING_ORDER));
132 
133     List<SelectItemOption<String>> nodesPerPagesOptions = new ArrayList<SelectItemOption<String>>();
134     nodesPerPagesOptions.add(new SelectItemOption<String>("5", "5"));
135     nodesPerPagesOptions.add(new SelectItemOption<String>("10", "10"));
136     nodesPerPagesOptions.add(new SelectItemOption<String>("15", "15"));
137     nodesPerPagesOptions.add(new SelectItemOption<String>("20", "20"));
138     nodesPerPagesOptions.add(new SelectItemOption<String>("30", "30"));
139     nodesPerPagesOptions.add(new SelectItemOption<String>("40", "40"));
140     nodesPerPagesOptions.add(new SelectItemOption<String>("50", "50"));
141 
142     List<SelectItemOption<String>> queryOption = new ArrayList<SelectItemOption<String>>();
143     queryOption.add(new SelectItemOption<String>(SQLQuery, Preference.SQL_QUERY));
144     queryOption.add(new SelectItemOption<String>(XPathQuery, Preference.XPATH_QUERY));
145 
146     addUIFormInput(new UICheckBoxInput(FIELD_ENABLESTRUCTURE, FIELD_ENABLESTRUCTURE, null));
147     addUIFormInput(new UICheckBoxInput(FIELD_SHOWSIDEBAR, FIELD_SHOWSIDEBAR, null));
148     addUIFormInput(new UICheckBoxInput(FIELD_SHOWNONDOCUMENT, FIELD_SHOWNONDOCUMENT, null));
149     addUIFormInput(new UICheckBoxInput(FIELD_SHOWREFDOCUMENTS, FIELD_SHOWREFDOCUMENTS, null));
150     addUIFormInput(new UICheckBoxInput(FIELD_SHOW_HIDDEN_NODE, FIELD_SHOW_HIDDEN_NODE, null));
151     addUIFormInput(new UICheckBoxInput(FIELD_SHOW_ITEMS_BY_USER, FIELD_SHOW_ITEMS_BY_USER, null));
152     addUIFormInput(new UICheckBoxInput(FIELD_ENABLE_DRAG_AND_DROP, FIELD_ENABLE_DRAG_AND_DROP, null));
153     addUIFormInput(new UIFormSelectBox(FIELD_QUERY_TYPE, FIELD_QUERY_TYPE, queryOption));
154     addUIFormInput(new UIFormSelectBox(FIELD_SHORTBY, FIELD_SHORTBY, sortOptions));
155     addUIFormInput(new UIFormSelectBox(FIELD_ORDERBY, FIELD_ORDERBY, orderOption));
156     addUIFormInput(new UIFormSelectBox(NODES_PER_PAGE, NODES_PER_PAGE, nodesPerPagesOptions));
157   }
158 
159   public boolean isAdvancePreferences() {
160     return advancePreferences;
161   }
162 
163   public void setAdvancePreferences(boolean adPreferences) {
164     advancePreferences = adPreferences;
165   }
166 
167   public void begin() throws Exception {
168     WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
169     String b = context.getURLBuilder().createURL(this, null, null);
170 
171     Writer writer = context.getWriter();
172     writer.append("<form class=\"")
173           .append(getId())
174           .append("\" id=\"")
175           .append(getId())
176           .append("\" action=\"")
177           .append(b)
178           .append('\"');
179     if (getSubmitAction() != null)
180       writer.append(" onsubmit=\"").append(getSubmitAction()).append("\"");
181     if (isMultipart())
182       writer.append(" enctype=\"multipart/form-data\"");
183     writer.append(" method=\"post\">");
184     writer.append("<div><input type=\"hidden\" name=\"")
185           .append(ACTION)
186           .append("\" value=\"\"/>");
187     writer.append("<input type=\"hidden\" name=\"").append(CSRFTokenUtil.CSRF_TOKEN).append("\" value=\"");
188     writer.append(CSRFTokenUtil.getToken(org.exoplatform.webui.Util.getRequest()));
189     writer.append("\"/></div>");
190   }
191 
192   public void activate() {
193   }
194 
195   public void deActivate() {
196   }
197 
198   public void update(Preference pref) {
199     getUICheckBoxInput(FIELD_ENABLESTRUCTURE).setChecked(pref.isJcrEnable());
200     UICheckBoxInput showSideBar = getUICheckBoxInput(FIELD_SHOWSIDEBAR);
201     showSideBar.setChecked(pref.isShowSideBar());
202     showSideBar.setDisabled(!this.getAncestorOfType(UIJCRExplorerPortlet.class).isShowSideBar());
203     getUICheckBoxInput(FIELD_SHOWNONDOCUMENT).setChecked(pref.isShowNonDocumentType());
204     getUICheckBoxInput(FIELD_SHOWREFDOCUMENTS).setChecked(pref.isShowPreferenceDocuments());
205     getUICheckBoxInput(FIELD_SHOW_HIDDEN_NODE).setChecked(pref.isShowHiddenNode());
206     getUICheckBoxInput(FIELD_SHOW_ITEMS_BY_USER).setChecked(pref.isShowItemsByUser());
207     getUICheckBoxInput(FIELD_ENABLE_DRAG_AND_DROP).setChecked(pref.isEnableDragAndDrop());
208     getUIFormSelectBox(FIELD_SHORTBY).setValue(pref.getSortType());
209     getUIFormSelectBox(FIELD_ORDERBY).setValue(pref.getOrder());
210     getUIFormSelectBox(NODES_PER_PAGE).setValue(Integer.toString(pref.getNodesPerPage()));
211     getUIFormSelectBox(FIELD_QUERY_TYPE).setValue(pref.getQueryType());
212   }
213 
214   private Cookie createNewCookie(String cookieName, String cookieValue) {
215     String userId = Util.getPortalRequestContext().getRemoteUser();
216     cookieName += userId;
217     return new Cookie(cookieName, cookieValue);
218   }
219 
220   private void savePreferenceInCookies() {
221     HttpServletResponse response = Util.getPortalRequestContext().getResponse();
222     if (getUICheckBoxInput(FIELD_ENABLESTRUCTURE).isChecked())
223       response.addCookie(createNewCookie(Preference.PREFERENCE_ENABLESTRUCTURE, "true"));
224     else
225       response.addCookie(createNewCookie(Preference.PREFERENCE_ENABLESTRUCTURE, "false"));
226     if (getUICheckBoxInput(FIELD_SHOWSIDEBAR).isChecked())
227       response.addCookie(createNewCookie(Preference.PREFERENCE_SHOWSIDEBAR, "true"));
228     else
229       response.addCookie(createNewCookie(Preference.PREFERENCE_SHOWSIDEBAR, "false"));
230     if (getUICheckBoxInput(FIELD_SHOWNONDOCUMENT).isChecked())
231       response.addCookie(createNewCookie(Preference.SHOW_NON_DOCUMENTTYPE, "true"));
232     else
233       response.addCookie(createNewCookie(Preference.SHOW_NON_DOCUMENTTYPE, "false"));
234     if (getUICheckBoxInput(FIELD_SHOWREFDOCUMENTS).isChecked())
235       response.addCookie(createNewCookie(Preference.PREFERENCE_SHOWREFDOCUMENTS, "true"));
236     else
237       response.addCookie(createNewCookie(Preference.PREFERENCE_SHOWREFDOCUMENTS, "false"));
238     if (getUICheckBoxInput(FIELD_SHOW_HIDDEN_NODE).isChecked())
239       response.addCookie(createNewCookie(Preference.PREFERENCE_SHOW_HIDDEN_NODE, "true"));
240     else
241       response.addCookie(createNewCookie(Preference.PREFERENCE_SHOW_HIDDEN_NODE, "false"));
242     if (getUICheckBoxInput(FIELD_ENABLE_DRAG_AND_DROP).isChecked())
243       response.addCookie(createNewCookie(Preference.ENABLE_DRAG_AND_DROP, "true"));
244     else
245       response.addCookie(createNewCookie(Preference.ENABLE_DRAG_AND_DROP, "false"));
246     response.addCookie(createNewCookie(Preference.PREFERENCE_QUERY_TYPE, getUIFormSelectBox(FIELD_QUERY_TYPE).getValue()));
247     response.addCookie(createNewCookie(Preference.PREFERENCE_SORT_BY, getUIFormSelectBox(FIELD_SHORTBY).getValue()));
248     response.addCookie(createNewCookie(Preference.PREFERENCE_ORDER_BY, getUIFormSelectBox(FIELD_ORDERBY).getValue()));
249     response.addCookie(createNewCookie(Preference.NODES_PER_PAGE, getUIFormSelectBox(NODES_PER_PAGE).getValue()));
250   }
251 
252   static public class SaveActionListener extends EventListener<UIPreferencesForm> {
253     public void execute(Event<UIPreferencesForm> event) throws Exception {
254       UIPreferencesForm uiForm = event.getSource();
255       UIJCRExplorerPortlet explorerPorltet = uiForm.getAncestorOfType(UIJCRExplorerPortlet.class);
256       UIJCRExplorer uiExplorer = explorerPorltet.findFirstComponentOfType(UIJCRExplorer.class);
257       Preference pref = uiExplorer.getPreference();
258       pref.setJcrEnable(uiForm.getUICheckBoxInput(FIELD_ENABLESTRUCTURE).isChecked());
259       pref.setShowSideBar(uiForm.getUICheckBoxInput(FIELD_SHOWSIDEBAR).isChecked());
260       pref.setShowNonDocumentType(uiForm.getUICheckBoxInput(FIELD_SHOWNONDOCUMENT).isChecked());
261       pref.setShowPreferenceDocuments(uiForm.getUICheckBoxInput(FIELD_SHOWREFDOCUMENTS).isChecked());
262       pref.setShowHiddenNode(uiForm.getUICheckBoxInput(FIELD_SHOW_HIDDEN_NODE).isChecked());
263       if (pref.isShowHiddenNode()) {
264         uiExplorer.getAllItemFilterMap().add(NodetypeConstant.HIDDEN);
265       } else {
266         uiExplorer.getAllItemFilterMap().remove(NodetypeConstant.HIDDEN);
267       }
268 
269       pref.setEnableDragAndDrop(uiForm.getUICheckBoxInput(FIELD_ENABLE_DRAG_AND_DROP).isChecked());
270       pref.setSortType(uiForm.getUIFormSelectBox(FIELD_SHORTBY).getValue());
271       pref.setQueryType(uiForm.getUIFormSelectBox(FIELD_QUERY_TYPE).getValue());
272       pref.setOrder(uiForm.getUIFormSelectBox(FIELD_ORDERBY).getValue());
273       pref.setNodesPerPage(Integer.parseInt(uiForm.getUIFormSelectBox(NODES_PER_PAGE).getValue()));
274       uiForm.savePreferenceInCookies();
275       uiExplorer.setPreferencesSaved(true);
276       uiExplorer.refreshExplorer();
277       explorerPorltet.setRenderedChild(UIJCRExplorer.class);
278       uiExplorer.updateAjax(event);
279     }
280   }
281 
282   static public class BackActionListener extends EventListener<UIPreferencesForm> {
283     public void execute(Event<UIPreferencesForm> event) throws Exception {
284       UIPreferencesForm uiForm = event.getSource();
285       UIJCRExplorerPortlet explorerPorltet = uiForm.getAncestorOfType(UIJCRExplorerPortlet.class);
286       UIJCRExplorer uiExplorer = explorerPorltet.findFirstComponentOfType(UIJCRExplorer.class);
287       uiExplorer.getChild(UIPopupContainer.class).cancelPopupAction();
288     }
289   }
290 
291   static public class AdvanceActionListener extends EventListener<UIPreferencesForm> {
292     public void execute(Event<UIPreferencesForm> event) throws Exception {
293       UIPreferencesForm uiPreferencesForm = event.getSource();
294       if (uiPreferencesForm.isAdvancePreferences()) {
295         uiPreferencesForm.setAdvancePreferences(false);
296       }
297       else {
298         uiPreferencesForm.setAdvancePreferences(true);
299       }
300       event.getRequestContext().addUIComponentToUpdateByAjax(uiPreferencesForm.getParent());
301     }
302   }
303 
304 }