View Javadoc
1   /*
2    * Copyright (C) 2003-2008 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.scv;
18  
19  import java.util.Collection;
20  import java.util.Date;
21  
22  import javax.jcr.Node;
23  import javax.portlet.MimeResponse;
24  import javax.portlet.PortletMode;
25  import javax.portlet.PortletPreferences;
26  import javax.portlet.RenderResponse;
27  import javax.portlet.ResourceRequest;
28  import javax.portlet.ResourceURL;
29  
30  import org.exoplatform.portal.mop.navigation.Scope;
31  import org.exoplatform.portal.mop.Visibility;
32  import org.exoplatform.portal.mop.user.UserNavigation;
33  import org.exoplatform.portal.mop.user.UserNode;
34  import org.exoplatform.portal.mop.user.UserNodeFilterConfig;
35  import org.exoplatform.portal.mop.user.UserPortal;
36  import org.exoplatform.portal.webui.util.Util;
37  import org.exoplatform.services.cms.templates.TemplateService;
38  import org.exoplatform.services.security.ConversationRegistry;
39  import org.exoplatform.services.security.ConversationState;
40  import org.exoplatform.services.wcm.core.WCMService;
41  import org.exoplatform.services.wcm.navigation.NavigationUtils;
42  import org.exoplatform.services.wcm.publication.WCMComposer;
43  import org.exoplatform.wcm.webui.Utils;
44  import org.exoplatform.web.application.RequireJS;
45  import org.exoplatform.webui.application.WebuiApplication;
46  import org.exoplatform.webui.application.WebuiRequestContext;
47  import org.exoplatform.webui.application.portlet.PortletRequestContext;
48  import org.exoplatform.webui.config.annotation.ComponentConfig;
49  import org.exoplatform.webui.core.UIPopupContainer;
50  import org.exoplatform.webui.core.UIPortletApplication;
51  import org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle;
52  import org.gatein.portal.controller.resource.ResourceScope;
53  import org.json.JSONArray;
54  import org.json.JSONObject;
55  import org.w3c.dom.Element;
56  
57  /**
58   * Created by The eXo Platform SAS
59   * Author : DANG TAN DUNG
60   * dzungdev@gmail.com
61   * Jun 9, 2008
62   */
63  @ComponentConfig(
64    lifecycle = UIApplicationLifecycle.class,
65    template = "system:/groovy/SingleContentViewer/UISingleContentView.gtmpl"
66  )
67  
68  public class UISingleContentViewerPortlet extends UIPortletApplication {
69  
70    /** The REPOSITORY. */
71    public static String REPOSITORY     = "repository" ;
72  
73    /** The WORKSPACE. */
74    public static String WORKSPACE      = "workspace" ;
75  
76    /** The IDENTIFIER. */
77    public static String IDENTIFIER     = "nodeIdentifier" ;
78  
79    /** The DRIVE. */
80    public static String DRIVE          = "nodeDrive";
81  
82    /** The Parameterized String **/
83    public static String PARAMETER      = "ParameterName";
84  
85    /** The ShowDate **/
86    public static String SHOW_DATE      = "ShowDate";
87  
88    /** The ShowTitle **/
89    public static String SHOW_TITLE     = "ShowTitle";
90  
91    /** The ShowOptionBar **/
92    public static String SHOW_OPTIONBAR = "ShowOptionBar";
93  
94    /** The is ContextualMode **/
95    public static String CONTEXTUAL_MODE= "ContextEnable";
96  
97    /** The Parameterized String for printing**/
98    public static String PRINT_PARAMETER= "PrintParameterName";
99  
100   /** The Page that show the print viewer **/
101   public static String PRINT_PAGE     = "PrintPage";
102   /** The mode_. */
103 
104   /** The Constant PREFERENCE_TARGET_PAGE. */
105   public final static String  PREFERENCE_TARGET_PAGE                = "basePath";
106 
107   /** The Constant PREFERENCE_SHOW_SCL_WITH. */
108   public final static String PREFERENCE_SHOW_SCV_WITH               = "showScvWith";
109 
110   public static final String DEFAULT_SHOW_SCV_WITH                  = "content-id";
111 
112   /** The Cache */
113   public static final String ENABLE_CACHE = "sharedCache";
114   
115   public static final String NAVIGATION_SCOPE = "NavigationScope";
116   
117   public static final String NAVIGATION_SCOPE_SINGLE = "single";
118   public static final String NAVIGATION_SCOPE_CHILDREN = "children";
119   public static final String NAVIGATION_SCOPE_GRAND_CHILDREN = "grandChildren";
120   public static final String NAVIGATION_SCOPE_ALL = "all";
121 
122   private PortletMode mode = null;//PortletMode.VIEW ;
123 
124   public static final String UIPreferencesPopupID = "UIPreferencesPopupWindows";
125 
126   private UISCVPreferences popPreferences;
127   private UIPresentationContainer uiPresentation;
128   PortletPreferences preferences;
129 
130   /**
131    * Instantiates a new uI single content viewer portlet.
132    *
133    * @throws Exception the exception
134    */
135   public UISingleContentViewerPortlet() throws Exception {
136     addChild(UIPopupContainer.class, null, "UIPopupContainer-" + new Date().getTime());
137     PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
138     preferences = portletRequestContext.getRequest().getPreferences();
139   }
140 
141   /**
142    * Activate mode.
143    *
144    * @param newMode the mode
145    *
146    * @throws Exception the exception
147    */
148   public void activateMode(PortletMode newMode) throws Exception{
149     if (getChild(UIPresentationContainer.class) !=null) {
150       removeChild(UIPresentationContainer.class);
151     }
152     if (getChild(UISCVPreferences.class) != null) {
153       removeChild(UISCVPreferences.class);
154     }
155     if(PortletMode.VIEW.equals(newMode)) {
156       PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
157       uiPresentation = addChild(UIPresentationContainer.class, null, UIPresentationContainer.class.getSimpleName() 
158                                 + pContext.getWindowId());
159     } else if (PortletMode.EDIT.equals(newMode)) {
160       popPreferences = addChild(UISCVPreferences.class, null, null);
161       popPreferences.setInternalPreferencesMode(true);
162     }
163   }
164 
165   /*
166    * (non-Javadoc)
167    * @see
168    * org.exoplatform.webui.core.UIPortletApplication#processRender(org.exoplatform
169    * .webui.application.WebuiApplication,
170    * org.exoplatform.webui.application.WebuiRequestContext)
171    */
172   public void processRender(WebuiApplication app, WebuiRequestContext context) throws Exception {
173     PortletRequestContext pContext = (PortletRequestContext) context ;
174     PortletMode newMode = pContext.getApplicationMode() ;
175     PortletPreferences preferences = pContext.getRequest().getPreferences();
176     Boolean sharedCache = "true".equals(preferences.getValue(ENABLE_CACHE, "true"));
177 
178     if ((context.getRemoteUser() == null && !Boolean.parseBoolean(preferences.getValue(UISingleContentViewerPortlet.CONTEXTUAL_MODE, "false")))
179         || (Utils.isLiveMode() && sharedCache && !Utils.isPortalEditMode() && Utils.isPortletViewMode(pContext))) {
180       WCMService wcmService = getApplicationComponent(WCMService.class);
181       pContext.getResponse().setProperty(MimeResponse.EXPIRATION_CACHE, ""+wcmService.getPortletExpirationCache());
182       if (log.isTraceEnabled())
183         log.trace("SCV rendering : cache set to "+wcmService.getPortletExpirationCache());
184     }
185 
186     if(!newMode.equals(mode)) {
187       activateMode(newMode) ;
188       mode = newMode ;
189     }
190 
191     Node nodeView = null;
192     if (uiPresentation!=null) {
193       nodeView = uiPresentation.getNodeView();
194       if (nodeView != null) {
195         TemplateService templateService = getApplicationComponent(TemplateService.class);
196         uiPresentation.getChild(UIPresentation.class).setTemplatePath(templateService.getTemplatePath(nodeView, false));
197       }
198     }
199 
200 //    if (uiPresentation!=null && uiPresentation.isContextual() && nodeView!=null) {
201 //      RenderResponse response = context.getResponse();
202 //      Element title = response.createElement("title");
203 //      title.setTextContent(uiPresentation.getTitle(nodeView));
204 //      response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, title);
205 //    }
206 
207     if (context.getRemoteUser() != null && WCMComposer.MODE_EDIT.equals(Utils.getCurrentMode())) {    	
208       pContext.getJavascriptManager().loadScriptResource(ResourceScope.SHARED, "content-selector");
209       pContext.getJavascriptManager().loadScriptResource(ResourceScope.SHARED, "quick-edit");      
210     }
211 
212     setId(UISingleContentViewerPortlet.class.getSimpleName() + pContext.getWindowId());
213     super.processRender(app, context) ;
214   }
215 
216   public void changeToViewMode() throws Exception{
217       PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
218       portletRequestContext.setApplicationMode(PortletMode.VIEW);
219   }
220 
221   @Override
222   public void serveResource(WebuiRequestContext context) throws Exception {
223     super.serveResource(context);
224 
225     ResourceRequest req = context.getRequest();
226     String nodeURI = req.getResourceID();
227 
228     JSONArray jsChilds = getChildrenAsJSON(nodeURI);
229     if (jsChilds == null) {
230       return;
231     }
232 
233     MimeResponse res = context.getResponse();
234     res.setContentType("text/json");
235     res.getWriter().write(jsChilds.toString());
236   }
237 
238   public JSONArray getChildrenAsJSON(String nodeURI) throws Exception {
239     WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
240     Collection<UserNode> children = null;
241 
242     UserPortal userPortal = Util.getPortalRequestContext().getUserPortalConfig().getUserPortal();
243 
244     // make filter
245     UserNodeFilterConfig.Builder filterConfigBuilder = UserNodeFilterConfig.builder();
246     filterConfigBuilder.withReadWriteCheck().withVisibility(Visibility.DISPLAYED, Visibility.TEMPORAL);
247     filterConfigBuilder.withTemporalCheck();
248     UserNodeFilterConfig filterConfig = filterConfigBuilder.build();
249 
250     // get user node & update children
251     UserNavigation userNav = userPortal.getNavigation(Util.getUIPortal().getSiteKey());
252     UserNode userNode = userPortal.resolvePath(userNav, filterConfig, nodeURI);
253 
254     if (userNode != null) {
255       userPortal.updateNode(userNode, NavigationUtils.ECMS_NAVIGATION_SCOPE, null);
256       children = userNode.getChildren();
257     }
258 
259     // build JSON result
260     JSONArray jsChildren = new JSONArray();
261     if (children == null) {
262       return null;
263     }
264     MimeResponse res = context.getResponse();
265     for (UserNode child : children) {
266       jsChildren.put(toJSON(child, res));
267     }
268     return jsChildren;
269   }
270 
271   private JSONObject toJSON(UserNode node, MimeResponse res) throws Exception {
272     JSONObject json = new JSONObject();
273     String nodeId = node.getId();
274 
275     json.put("label", node.getEncodedResolvedLabel());
276     json.put("hasChild", node.getChildrenCount() > 0);
277 
278     UserNode selectedNode = Util.getUIPortal().getNavPath();
279     json.put("isSelected", nodeId.equals(selectedNode.getId()));
280     json.put("icon", node.getIcon());
281     String nodeURI = "";
282     if(node.getPageRef() != null){
283       nodeURI = node.getURI();
284     }
285     json.put("uri", nodeURI);
286 
287     ResourceURL rsURL = res.createResourceURL();
288     rsURL.setResourceID(res.encodeURL(node.getURI()));
289     json.put("getNodeURL", rsURL.toString());
290 
291     JSONArray jsonChildren = new JSONArray();
292     for (UserNode child : node.getChildren()) {
293       jsonChildren.put(toJSON(child, res));
294     }
295     json.put("childs", jsonChildren);
296     return json;
297   }
298   
299   public String getNavigationScope() throws Exception {
300     PortletPreferences preferences = ((PortletRequestContext)WebuiRequestContext.getCurrentInstance()).
301         getRequest().getPreferences();
302     String navigationScope = preferences.getValue(NAVIGATION_SCOPE, NAVIGATION_SCOPE_CHILDREN);
303     return navigationScope;
304   }
305   
306   public String getNavigation() throws Exception {
307     String userName = ConversationState.getCurrent().getIdentity().getUserId();  
308     String portalName = Util.getPortalRequestContext().getPortalOwner();
309     
310     PortletPreferences preferences = ((PortletRequestContext)WebuiRequestContext.getCurrentInstance()).
311                                      getRequest().getPreferences();
312     String navigationScope = preferences.getValue(NAVIGATION_SCOPE, NAVIGATION_SCOPE_CHILDREN);
313     Scope scope = Scope.CHILDREN;
314     switch (navigationScope) {
315       case NAVIGATION_SCOPE_SINGLE: scope = Scope.SINGLE; break;
316       case NAVIGATION_SCOPE_CHILDREN: scope = Scope.CHILDREN; break;
317       case NAVIGATION_SCOPE_GRAND_CHILDREN: scope = Scope.GRANDCHILDREN; break;
318       case NAVIGATION_SCOPE_ALL: scope = Scope.ALL; break;
319     }
320     return NavigationUtils.getNavigationAsJSON(portalName, userName, scope, navigationScope);
321   }
322 }