View Javadoc
1   /*
2    * Copyright (C) 2003-2009 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.clv;
18  
19  import javax.jcr.Node;
20  import javax.jcr.PathNotFoundException;
21  import javax.jcr.Session;
22  import javax.jcr.query.Row;
23  import javax.portlet.PortletPreferences;
24  
25  import org.exoplatform.commons.api.search.data.SearchResult;
26  import org.exoplatform.ecm.resolver.JCRResourceResolver;
27  import org.exoplatform.resolver.ResourceResolver;
28  import org.exoplatform.services.cms.impl.DMSConfiguration;
29  import org.exoplatform.services.jcr.access.PermissionType;
30  import org.exoplatform.services.jcr.core.ExtendedNode;
31  import org.exoplatform.services.wcm.core.NodeLocation;
32  import org.exoplatform.services.wcm.search.base.SearchDataCreator;
33  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
34  import org.exoplatform.wcm.webui.Utils;
35  import org.exoplatform.webui.application.WebuiRequestContext;
36  import org.exoplatform.webui.application.portlet.PortletRequestContext;
37  import org.exoplatform.webui.core.UIContainer;
38  import org.exoplatform.webui.event.Event;
39  import org.exoplatform.webui.event.EventListener;
40  
41  /**
42   * Created by The eXo Platform SAS
43   * Author : anh.do
44   * anh.do@exoplatform.com, anhdn86@gmail.com
45   * Feb 23, 2009
46   */
47  public abstract class UICLVContainer extends UIContainer {
48  
49    /** The message key. */
50    protected String  messageKey;
51  
52    /**
53     * Inits the.
54     *
55     * @throws Exception the exception
56     */
57    public abstract void init() throws Exception;
58    
59    /**
60     * Get portlet name.
61     *
62     * @throws Exception the exception
63     */
64    public abstract String getPortletName() throws Exception;
65  
66    /**
67     * Gets the message.
68     *
69     * @return the message
70     *
71     * @throws Exception the exception
72     */
73    public String getMessageKey() throws Exception {
74      return messageKey;
75    }
76  
77    /**
78     * Gets the portlet id.
79     *
80     * @return the portlet id
81     */
82    public String getPortletId() {
83      PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
84      return pContext.getWindowId();
85    }
86  
87    /* (non-Javadoc)
88     * @see org.exoplatform.webui.core.UIComponent#processRender(org.exoplatform.webui.application.WebuiRequestContext)
89     */
90    public void processRender(WebuiRequestContext context) throws Exception {
91      if(!Boolean.parseBoolean(Utils.getCurrentMode()) || context.getFullRender()) {
92        init();
93      }
94      super.processRender(context);
95    }
96  
97    public String getEditLink(boolean isEditable, boolean isNew) throws Exception {
98      String folderPath = this.getAncestorOfType(UICLVPortlet.class).getFolderPath();
99      if (folderPath==null) folderPath="";
100     Node folderNode = null;
101     try{
102       folderNode = getFolderNode(folderPath);
103     }catch(PathNotFoundException e){
104       folderNode = getFolderNode("");
105     }
106     return Utils.getEditLink(folderNode, isEditable, isNew);
107   }
108 
109   public Node getFolderNode() {
110     return NodeLocation.getNodeByExpression(
111           Utils.getPortletPreference(UICLVPortlet.PREFERENCE_ITEM_PATH));
112   }
113 
114   private Node getFolderNode(String oldPath) throws Exception {
115       if ((oldPath==null) || ((oldPath!=null) && (oldPath.length()==0))) return null;
116       int slashIndex = oldPath.indexOf("/");
117       String path = oldPath.substring(slashIndex);
118       String[] repoWorkspace = oldPath.substring(0, slashIndex).split(":");
119       String strWorkspace = repoWorkspace[1];
120       Session session = WCMCoreUtils.getUserSessionProvider().getSession(strWorkspace, WCMCoreUtils.getRepository());
121       return (Node)session.getItem(path);
122   }
123 
124 
125   /**
126    * Gets the template resource resolver.
127    *
128    * @return the template resource resolver
129    *
130    * @throws Exception the exception
131    */
132   public ResourceResolver getTemplateResourceResolver() throws Exception {
133     DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
134     String workspace = dmsConfiguration.getConfig().getSystemWorkspace();
135     return new JCRResourceResolver(workspace);
136   }
137 
138   /**
139    * The listener interface for receiving quickEditAction events.
140    * The class that is interested in processing a quickEditAction
141    * event implements this interface, and the object created
142    * with that class is registered with a component using the
143    * component's <code>addQuickEditActionListener</code> method. When
144    * the quickEditAction event occurs, that object's appropriate
145    * method is invoked.
146    *
147    */
148   public static class PreferencesActionListener extends EventListener<UICLVFolderMode> {
149     /* (non-Javadoc)
150      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
151      */
152     public void execute(Event<UICLVFolderMode> event) throws Exception {
153       UICLVContainer clvContainer = event.getSource();
154       UICLVConfig viewerManagementForm = clvContainer.createUIComponent(UICLVConfig.class, null, null);
155       Utils.createPopupWindow(clvContainer, viewerManagementForm, "UIViewerManagementPopupWindow", 800);
156     }
157   }
158 
159 
160   public void onRefresh(Event<UICLVPresentation> event) throws Exception {
161     UICLVPresentation clvPresentation = event.getSource();
162     UICLVContainer uiListViewerBase = clvPresentation.getParent();
163     uiListViewerBase.getChildren().clear();
164     uiListViewerBase.init();
165   }
166 
167   public boolean isModeByFolder() {
168     PortletPreferences portletPreferences = Utils.getAllPortletPreferences();
169     String currentApplicationMode = portletPreferences.getValue(UICLVPortlet.PREFERENCE_APPLICATION_TYPE, null);    
170     if (currentApplicationMode.equals(UICLVPortlet.APPLICATION_CLV_BY_QUERY)) 
171       return false;
172     
173     return UICLVPortlet.DISPLAY_MODE_AUTOMATIC.equals(
174               Utils.getPortletPreference(UICLVPortlet.PREFERENCE_DISPLAY_MODE));
175   }
176   
177   public boolean hasFolderPath() {
178     PortletPreferences portletPreferences = Utils.getAllPortletPreferences();
179     String itemPath = portletPreferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null);
180         
181     return (itemPath != null && itemPath.length() > 0) ? true : false;
182   }
183 
184   public boolean isShowManageContent() {
185     return (Utils.isShowQuickEdit() && isModeByFolder() && hasFolderPath());
186   }
187 
188   public boolean isShowAddContent() {
189     if (isShowManageContent()) {
190       PortletPreferences portletPreferences = ((PortletRequestContext) WebuiRequestContext.
191           getCurrentInstance()).getRequest().getPreferences();
192       String itemPath = portletPreferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null);
193       try {
194         Node content = NodeLocation.getNodeByExpression(itemPath);
195         ((ExtendedNode) content).checkPermission(PermissionType.ADD_NODE);
196       } catch (Exception e) {
197         return false;
198       }
199       return true;
200     } else return false;
201   }
202 
203   public boolean isShowPreferences() {
204     try {
205       return Utils.isShowQuickEdit() && Utils.hasEditPermissionOnPage();
206     } catch (Exception e) {
207       return false;
208     }
209   }
210 
211   public static class CLVNodeCreator implements SearchDataCreator<NodeLocation> {
212 
213     @Override
214     public NodeLocation createData(Node node, Row row, SearchResult searchResult) {
215       return NodeLocation.getNodeLocationByNode(node);
216     }
217   }
218   
219 }