View Javadoc
1   /*
2    * Copyright (C) 2003-2012 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (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 Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wcm.connector.collaboration;
18  
19  import java.util.Iterator;
20  
21  import javax.ws.rs.GET;
22  import javax.ws.rs.Path;
23  import javax.ws.rs.QueryParam;
24  import javax.ws.rs.core.MediaType;
25  import javax.ws.rs.core.Response;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.transform.dom.DOMSource;
28  
29  import org.exoplatform.portal.application.PortalRequestContext;
30  import org.exoplatform.portal.config.UserPortalConfig;
31  import org.exoplatform.portal.config.UserPortalConfigService;
32  import org.exoplatform.portal.mop.SiteKey;
33  import org.exoplatform.portal.mop.Visibility;
34  import org.exoplatform.portal.mop.navigation.Scope;
35  import org.exoplatform.portal.mop.user.UserNavigation;
36  import org.exoplatform.portal.mop.user.UserNode;
37  import org.exoplatform.portal.mop.user.UserNodeFilterConfig;
38  import org.exoplatform.portal.mop.user.UserPortal;
39  import org.exoplatform.services.rest.resource.ResourceContainer;
40  import org.exoplatform.services.security.ConversationState;
41  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
42  import org.w3c.dom.Document;
43  import org.w3c.dom.Element;
44  /**
45   * Created by The eXo Platform SAS
46   * Author : Nguyen The Vinh From ECM Of eXoPlatform
47   *          vinh_nguyen@exoplatform.com
48   * 29 May 2012
49   */
50  @Path("/content/")
51  public class NavigationConnector implements ResourceContainer{
52    private static ThreadLocal<Boolean> gotNavigationKeeper = new ThreadLocal<Boolean>();
53  
54    /**
55     * Return a JsonString include all navigation node
56     * 
57     * @param       portalName: Destination portal to get the navigation tree
58     * @return
59     * @throws      Exception
60     */
61    @GET
62    @Path("/getFullNavigation/")
63    public Response getFullNavigation ( @QueryParam("portalName") String portalName) throws Exception {
64      String userName = ConversationState.getCurrent().getIdentity().getUserId();
65      Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
66      Element element = document.createElement("navigationXML");
67      element.setTextContent(getNavigationAsJSON(portalName, userName));
68      document.appendChild(element);
69      return Response.ok(new DOMSource(document), MediaType.TEXT_XML).build();
70    }
71    private String getNavigationAsJSON(String portalName, String username) throws Exception {
72  
73      UserPortalConfigService userPortalConfigService = WCMCoreUtils.getService(UserPortalConfigService.class);
74      UserPortalConfig userPortalCfg = userPortalConfigService.getUserPortalConfig(portalName,
75          username,
76          PortalRequestContext.USER_PORTAL_CONTEXT);
77      UserPortal userPortal = userPortalCfg.getUserPortal();
78  
79      //filter nodes
80      UserNodeFilterConfig.Builder filterConfigBuilder = UserNodeFilterConfig.builder();
81      filterConfigBuilder.withReadWriteCheck().withVisibility(Visibility.DISPLAYED, Visibility.TEMPORAL);
82      filterConfigBuilder.withTemporalCheck();
83      UserNodeFilterConfig filterConfig = filterConfigBuilder.build();
84  
85      //get nodes
86      UserNavigation navigation = userPortal.getNavigation(SiteKey.portal(portalName));
87      UserNode root = userPortal.getNode(navigation, Scope.ALL, filterConfig, null);
88  
89      //set gotNavigation=true
90      gotNavigationKeeper.set(true);
91      return createJsonTree(navigation, root);
92    }
93    /**
94     * Return a JsonString include all navigation node, serve for getNavigationAsJSON method
95     * 
96     * @param           navigation: Navigation information to create Json tree
97     * @param           rootNode  : Root node of navigation
98     * @return          A String as Json tree
99     * @throws Exception
100    */
101   private String createJsonTree(UserNavigation navigation, UserNode rootNode) throws Exception {
102     StringBuffer sbJsonTree = new StringBuffer();
103     sbJsonTree.append("[");
104     sbJsonTree.append("{");
105     sbJsonTree.append("\"ownerId\":\"").append(navigation.getKey().getName()).append("\",");
106     sbJsonTree.append("\"ownerType\":\"").append(navigation.getKey().getTypeName()).append("\",");
107     sbJsonTree.append("\"priority\":\"").append(navigation.getPriority()).append("\",");
108     sbJsonTree.append("\"nodes\":").append(addJsonNodes(rootNode.getChildren().iterator()));
109     sbJsonTree.append("}");
110     sbJsonTree.append("]");
111     return sbJsonTree.toString();
112   }
113   /**
114    * Build JsonTree for children nodes of navigation
115    * 
116    * @param           children
117    * @return          StringBuffer contain Json tree of children
118    */
119   private StringBuffer addJsonNodes(Iterator<UserNode> children) {
120     StringBuffer sbJsonTree = new StringBuffer();
121     String resovleLabel = "";
122     sbJsonTree.append("[");
123     boolean first = true;
124 
125     while (children.hasNext()) {
126       UserNode child = children.next();
127       if (!first) {
128         sbJsonTree.append(",");
129       }
130       first = false;
131       sbJsonTree.append("{");
132       sbJsonTree.append("\"icon\":").append(child.getIcon() != null ? "\"" + child.getIcon() + "\""
133                                                                    : "null").append(",");
134       sbJsonTree.append("\"label\":\"").append(child.getLabel()).append("\",");
135       sbJsonTree.append("\"name\":\"").append(child.getName()).append("\",");
136       try {
137         resovleLabel = child.getResolvedLabel();
138       } catch (NullPointerException npe) {
139         resovleLabel = "";
140       }
141       sbJsonTree.append("\"resolvedLabel\":\"").append(resovleLabel).append("\",");
142       sbJsonTree.append("\"uri\":\"").append(child.getURI()).append("\",");
143 
144       sbJsonTree.append("\"getNodeURL\":\"").append(child.getURI().toString()).append("\",");
145       sbJsonTree.append("\"nodes\":").append(addJsonNodes(child.getChildren().iterator()));
146       sbJsonTree.append("}");
147     }
148     sbJsonTree.append("]");
149     return sbJsonTree;
150   }
151 }