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.wcm.webui.selector.page;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.exoplatform.portal.mop.user.UserNavigation;
25  import org.exoplatform.portal.mop.user.UserNode;
26  import org.exoplatform.portal.mop.user.UserPortal;
27  import org.exoplatform.portal.webui.util.Util;
28  import org.exoplatform.services.wcm.navigation.NavigationUtils;
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.ComponentConfigs;
33  import org.exoplatform.webui.config.annotation.EventConfig;
34  import org.exoplatform.webui.core.UIComponent;
35  import org.exoplatform.webui.core.UIContainer;
36  import org.exoplatform.webui.core.UIDropDownControl;
37  import org.exoplatform.webui.core.UIRightClickPopupMenu;
38  import org.exoplatform.webui.core.UITree;
39  import org.exoplatform.webui.core.model.SelectItemOption;
40  import org.exoplatform.webui.event.Event;
41  import org.exoplatform.webui.event.Event.Phase;
42  import org.exoplatform.webui.event.EventListener;
43  
44  /**
45   * Created by The eXo Platform SARL
46   * Author : chungnv
47   * nguyenchung136@yahoo.com
48   * Jun 23, 2006
49   * 10:07:15 AM
50   */
51  @ComponentConfigs({
52    @ComponentConfig(
53        template = "classpath:groovy/wcm/webui/selector/page/UIPageNodeSelector.gtmpl" ,
54        events = {
55          @EventConfig(listeners = UIPageNodeSelector.SelectNavigationActionListener.class, phase=Phase.DECODE)
56        }
57    ),
58    @ComponentConfig (
59        type = UIDropDownControl.class ,
60        id = "UIDropDown",
61        template = "classpath:groovy/wcm/webui/selector/page/UINavigationSelector.gtmpl",
62        events = {
63          @EventConfig(listeners = UIPageNodeSelector.SelectNavigationActionListener.class)
64        }
65      )
66  })
67  public class UIPageNodeSelector extends UIContainer {
68  
69    /** The navigations. */
70    private List<UserNavigation> navigations;
71  
72    /** The selected node. */
73    private SelectedNode selectedNode;
74  
75    /** The copy node. */
76    private SelectedNode copyNode;
77  
78    /** The delete navigations. */
79    private List<UserNavigation> deleteNavigations = new ArrayList<UserNavigation>();
80  
81    /** the user portal  */
82    private UserPortal userPortal;
83    
84    /**
85     * Instantiates a new uI page node selector.
86     *
87     * @throws Exception the exception
88     */
89    public UIPageNodeSelector() throws Exception {    
90      userPortal = Util.getPortalRequestContext().getUserPortalConfig().getUserPortal();
91      
92      UIDropDownControl uiDopDownControl = addChild(UIDropDownControl.class, "UIDropDown", "UIDropDown");
93      uiDopDownControl.setParent(this);
94  
95      UITree uiTree = addChild(UITree.class, null, "TreeNodeSelector");        
96      uiTree.setIcon("DefaultPageIcon");
97      uiTree.setSelectedIcon("DefaultPageIcon");
98      uiTree.setBeanIdField("URI");
99      uiTree.setBeanChildCountField("ChildrenCount");
100     uiTree.setBeanLabelField("encodedResolvedLabel");
101     uiTree.setBeanIconField("icon");
102 
103     loadNavigations();
104   }
105 
106   /**
107    * Load navigations.
108    *
109    * @throws Exception the exception
110    */
111   public void loadNavigations() throws Exception {
112     // get all navigations
113     navigations = new ArrayList<UserNavigation>();
114     navigations.addAll(userPortal.getNavigations());
115 
116     // check navigation list
117     if (navigations == null || navigations.size() <= 0) {
118       getChild(UIDropDownControl.class).setOptions(null);
119       getChild(UITree.class).setSibbling(null);
120       return;
121     }
122 
123     // set option values for navigation selector dropdown
124     updateNavigationSelector();
125 
126     // choose one navigation and show it on UI
127     chooseAndShowNavigation();
128   }
129 
130   /**
131    * Choose one navigation and show it on UI
132    *
133    * @throws Exception
134    */
135   private void chooseAndShowNavigation() throws Exception {
136     // select the navigation of current portal
137     
138     String currentPortalName = Util.getPortalRequestContext().getUserPortalConfig().getPortalName();
139     UserNavigation portalSelectedNav = NavigationUtils.getUserNavigationOfPortal( userPortal, currentPortalName);
140 
141     int portalSelectedNavId = getId(portalSelectedNav);
142     if (getUserNavigation(portalSelectedNavId) != null) {
143       selectNavigation(portalSelectedNavId);
144       UserNode portalSelectedNode = Util.getUIPortal().getSelectedUserNode();
145       if (portalSelectedNode != null)
146         selectUserNodeByUri(portalSelectedNode.getURI());
147       return;
148     }
149 
150     // select the first navigation
151     UserNavigation firstNav = navigations.get(0);
152     selectNavigation(getId(firstNav));
153     UserNode rootNode = userPortal.getNode(firstNav,
154                                            NavigationUtils.ECMS_NAVIGATION_SCOPE,
155                                            null, null);
156     Iterator<UserNode> childrenIter = rootNode.getChildren().iterator();
157     if (childrenIter.hasNext()) {
158       selectUserNodeByUri(childrenIter.next().getURI());
159     }
160   }
161 
162   /**
163    */
164   public int getId(UserNavigation nav) {
165     return (nav.getKey().getTypeName() + "::" + nav.getKey().getName()).hashCode();
166   }
167 
168   /**
169    * get index of a navigation in navigation list
170    *
171    * @param navId the identify of navigation
172    * @return the index of the navigation in navigation list
173    */
174   private int getIndex(int navId) {
175     int index = -1;
176 
177     if (navigations == null || navigations.size() <= 0) {
178       return index;
179     }
180 
181     for (int i = 0; i < navigations.size(); i++) {
182       UserNavigation nav = navigations.get(i);
183       if (getId(nav) == navId) {
184         index = i;
185         break;
186       }
187     }
188 
189     return index;
190   }
191 
192   /**
193    * Set option values for navigation selector dropdown
194    */
195   private void updateNavigationSelector() {
196 
197     List<SelectItemOption<String>> options = new ArrayList<SelectItemOption<String>>();
198     for (UserNavigation navigation : navigations) {
199       options.add(new SelectItemOption<String>(navigation.getKey().getTypeName() + ":"
200           + navigation.getKey().getName(), String.valueOf(getId(navigation))));
201     }
202     UIDropDownControl uiNavigationSelector = getChild(UIDropDownControl.class);
203     uiNavigationSelector.setOptions(options);
204     if (options.size() > 0)
205       uiNavigationSelector.setValue(0);
206   }
207 
208   /**
209    * Select navigation.
210    *
211    * @param id the id
212    */
213   public void selectNavigation(int id) throws Exception {
214     UserNavigation selectedNav = getUserNavigation(id);
215     if (selectedNav == null) {
216       return;
217     }
218 
219     UserNode rootNode = userPortal.getNode(selectedNav,
220                                            NavigationUtils.ECMS_NAVIGATION_SCOPE,
221                                            null,
222                                            null);
223     selectedNode = new SelectedNode(selectedNav, rootNode, null, null);
224     selectUserNodeByUri(null);
225 
226     // update tree
227     UITree uiTree = getChild(UITree.class);
228     uiTree.setSibbling(rootNode.getChildren());
229 
230     // update dropdown
231     UIDropDownControl uiDropDownSelector = getChild(UIDropDownControl.class);
232     uiDropDownSelector.setValue(getIndex(id));
233   }
234 
235   /**
236    * Select page node by uri.
237    *
238    * @param uri the uri
239    */
240   public void selectUserNodeByUri(String uri) throws Exception {
241     if (selectedNode == null || uri == null)
242       return;
243     UITree tree = getChild(UITree.class);
244     Collection<?> sibbling = tree.getSibbling();
245     tree.setSibbling(null);
246     tree.setParentSelected(null);
247 
248     UserNavigation selectedNav = selectedNode.getUserNavigation();
249     UserNode userNode = userPortal.resolvePath(selectedNav, null, uri);
250 
251     if (userNode != null) {
252       userPortal.updateNode(userNode, NavigationUtils.ECMS_NAVIGATION_SCOPE, null);
253       if (userNode != null) {
254         // selectedNode.setNode(searchUserNodeByUri(selectedNode.getRootNode(), uri));
255         selectedNode.setNode(userNode);
256         selectedNode.setParentNode(userNode.getParent());
257 
258         tree.setParentSelected(selectedNode.getParentNode());
259         tree.setSibbling(selectedNode.getParentNode().getChildren());
260         tree.setSelected(selectedNode.getNode());
261         tree.setChildren(selectedNode.getNode().getChildren());
262         return;
263       }
264     }
265 
266     tree.setSelected(null);
267     tree.setChildren(null);
268     tree.setSibbling(sibbling);
269   }
270 
271   /**
272    * Gets the user navigations.
273    *
274    * @return the page navigations
275    */
276   public List<UserNavigation> getUserNavigations() {
277     if(navigations == null) navigations = new ArrayList<UserNavigation>();
278     return navigations;
279   }
280 
281   /**
282    * Gets the user navigation.
283    *
284    * @param id the id
285    * @return the page navigation
286    */
287   public UserNavigation getUserNavigation(int id) {
288     for (UserNavigation nav : getUserNavigations()) {
289       if (getId(nav) == id)
290         return nav;
291     }
292     return null;
293   }
294 
295   /* (non-Javadoc)
296    * @see org.exoplatform.webui.core.UIComponent#processRender(org.exoplatform.webui.application.WebuiRequestContext)
297    */
298   public void processRender(WebuiRequestContext context) throws Exception {
299     UIRightClickPopupMenu uiPopupMenu = getChild(UIRightClickPopupMenu.class);
300     if(uiPopupMenu != null) {
301       if(navigations == null || navigations.size() < 1) uiPopupMenu.setRendered(false) ;
302       else uiPopupMenu.setRendered(true) ;
303     }
304     super.processRender(context) ;
305   }
306 
307   /**
308    * Gets the copy node.
309    *
310    * @return the copy node
311    */
312   public SelectedNode getCopyNode() { return copyNode; }
313 
314   /**
315    * Sets the copy node.
316    *
317    * @param copyNode the new copy node
318    */
319   public void setCopyNode(SelectedNode copyNode) { this.copyNode = copyNode; }
320 
321   /**
322    * The listener interface for receiving selectNavigationAction events.
323    * The class that is interested in processing a selectNavigationAction
324    * event implements this interface, and the object created
325    * with that class is registered with a component using the
326    * component's <code>addSelectNavigationActionListener</code> method. When
327    * the selectNavigationAction event occurs, that object's appropriate
328    * method is invoked.
329    */
330   static public class SelectNavigationActionListener  extends EventListener<UIDropDownControl> {
331 
332     /* (non-Javadoc)
333      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
334      */
335     public void execute(Event<UIDropDownControl> event) throws Exception {
336       String id = event.getRequestContext().getRequestParameter(OBJECTID);
337       UIDropDownControl uiDropDownControl = event.getSource();
338       UIPageNodeSelector uiPageNodeSelector = uiDropDownControl.getAncestorOfType(UIPageNodeSelector.class);
339       event.getRequestContext().addUIComponentToUpdateByAjax(uiPageNodeSelector.getParent()) ;
340       if(id != null) uiPageNodeSelector.selectNavigation(Integer.parseInt(id));
341       try {
342         UIPageSelector pageSelector = uiPageNodeSelector.getAncestorOfType(UIPageSelector.class);
343         UIPageSelectorPanel pageSelectorPanel = pageSelector.getChild(UIPageSelectorPanel.class);
344         pageSelectorPanel.setSelectedNode(uiPageNodeSelector.getSelectedNode().getNode());
345         pageSelectorPanel.updateGrid();
346 
347         event.getRequestContext().addUIComponentToUpdateByAjax(pageSelector);
348       } catch (Exception ex) {
349         org.exoplatform.wcm.webui.Utils.createPopupMessage(uiPageNodeSelector,
350                                                            "UIMessageBoard.msg.select-navigation",
351                                                            null,
352                                                            ApplicationMessage.ERROR);
353       }
354       uiPageNodeSelector.<UIComponent> getParent().broadcast(event, event.getExecutionPhase());
355 
356     }
357   }
358 
359   /**
360    * The Class SelectedNode.
361    */
362   public static class SelectedNode {
363 
364     /** The nav. */
365     private UserNavigation nav;
366 
367     /** The parent node. */
368     private UserNode       parentNode;
369 
370     /** The node. */
371     private UserNode       node;
372 
373     private UserNode       rootNode;
374 
375     /** The delete node. */
376     private boolean        deleteNode = false;
377 
378     /** The clone node. */
379     private boolean        cloneNode  = false;
380 
381     /**
382      * Instantiates a new selected node.
383      *
384      * @param nav the nav
385      * @param parentNode the parent node
386      * @param node the node
387      */
388     public SelectedNode(UserNavigation nav, UserNode rootNode, UserNode parentNode, UserNode node) {
389       this.nav = nav;
390       this.rootNode = rootNode;
391       this.parentNode = parentNode;
392       this.node = node;
393     }
394 
395     /**
396      * Gets the user navigation.
397      *
398      * @return the user navigation
399      */
400     public UserNavigation getUserNavigation() {
401       return nav;
402     }
403 
404     /**
405      * Sets the page navigation.
406      *
407      * @param nav the new page navigation
408      */
409     public void setUserNavigation(UserNavigation nav) {
410       this.nav = nav;
411     }
412 
413     /**
414      * Gets the root node
415      *
416      * @return the root node
417      */
418     public UserNode getRootNode() {
419       return rootNode;
420     }
421 
422     /**
423      * Sets the root node
424      *
425      * @param rootNode the root node
426      */
427     public void setRootNode(UserNode rootNode) {
428       this.rootNode = rootNode;
429     }
430 
431     /**
432      * Gets the parent node.
433      *
434      * @return the parent node
435      */
436     public UserNode getParentNode() {
437       return parentNode;
438     }
439 
440     /**
441      * Sets the parent node.
442      *
443      * @param parentNode the new parent node
444      */
445     public void setParentNode(UserNode parentNode) {
446       this.parentNode = parentNode;
447     }
448 
449     /**
450      * Gets the node.
451      *
452      * @return the node
453      */
454     public UserNode getNode() {
455       return node;
456     }
457 
458     /**
459      * Sets the node.
460      *
461      * @param node the new node
462      */
463     public void setNode(UserNode node) {
464       this.node = node;
465     }
466 
467     /**
468      * Checks if is delete node.
469      *
470      * @return true, if is delete node
471      */
472     public boolean isDeleteNode() {
473       return deleteNode;
474     }
475 
476     /**
477      * Sets the delete node.
478      *
479      * @param deleteNode the new delete node
480      */
481     public void setDeleteNode(boolean deleteNode) {
482       this.deleteNode = deleteNode;
483     }
484 
485     /**
486      * Checks if is clone node.
487      *
488      * @return true, if is clone node
489      */
490     public boolean isCloneNode() {
491       return cloneNode;
492     }
493 
494     /**
495      * Sets the clone node.
496      *
497      * @param b the new clone node
498      */
499     public void setCloneNode(boolean b) {
500       cloneNode = b;
501     }
502   }
503 
504   /**
505    * Gets the selected node.
506    *
507    * @return the selected node
508    */
509   public SelectedNode getSelectedNode() {
510     return selectedNode;
511   }
512 
513   /**
514    * Gets the selected navigation.
515    *
516    * @return the selected navigation
517    */
518   public UserNavigation getSelectedNavigation() {
519     return selectedNode == null ? null : selectedNode.getUserNavigation();
520   }
521 
522   /**
523    * Gets the root node of the selected navigation.
524    *
525    * @return the root node of the selected navigation.
526    */
527   public UserNode getRootNodeOfSelectedNav() {
528     return selectedNode == null ? null : selectedNode.getRootNode();
529   }
530 
531   /**
532    * Gets the selected page node.
533    *
534    * @return the selected page node
535    */
536   public UserNode getSelectedUserNode() {
537     return selectedNode == null ? null : selectedNode.getNode();
538   }
539 
540   /**
541    * Gets the up level uri.
542    *
543    * @return the up level uri
544    */
545   public String getUpLevelUri() {
546     return selectedNode.getParentNode().getURI();
547   }
548 
549   /**
550    * Gets the delete navigations.
551    *
552    * @return the delete navigations
553    */
554   public List<UserNavigation> getDeleteNavigations() {
555     return deleteNavigations;
556   }
557 }