View Javadoc
1   package org.exoplatform.wcm.webui.selector.content;
2   
3   import java.text.DateFormat;
4   import java.util.Calendar;
5   import java.util.Date;
6   import java.util.Locale;
7   
8   import javax.jcr.Node;
9   import javax.jcr.Session;
10  import javax.portlet.PortletPreferences;
11  
12  import org.exoplatform.ecm.webui.selector.UISelectable;
13  import org.exoplatform.portal.webui.util.Util;
14  import org.exoplatform.services.cms.templates.TemplateService;
15  import org.exoplatform.services.ecm.publication.PublicationService;
16  import org.exoplatform.services.jcr.RepositoryService;
17  import org.exoplatform.services.jcr.core.ManageableRepository;
18  import org.exoplatform.services.wcm.core.NodeLocation;
19  import org.exoplatform.services.wcm.core.WCMConfigurationService;
20  import org.exoplatform.services.wcm.publication.WCMComposer;
21  import org.exoplatform.services.wcm.search.ResultNode;
22  import org.exoplatform.services.wcm.search.base.AbstractPageList;
23  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
24  import org.exoplatform.wcm.webui.Utils;
25  import org.exoplatform.wcm.webui.viewer.UIContentViewer;
26  import org.exoplatform.web.application.ApplicationMessage;
27  import org.exoplatform.webui.application.WebuiRequestContext;
28  import org.exoplatform.webui.application.portlet.PortletRequestContext;
29  import org.exoplatform.webui.config.annotation.ComponentConfig;
30  import org.exoplatform.webui.config.annotation.EventConfig;
31  import org.exoplatform.webui.core.UIApplication;
32  import org.exoplatform.webui.core.UIGrid;
33  import org.exoplatform.webui.event.Event;
34  import org.exoplatform.webui.event.EventListener;
35  
36  /**
37   * Author : TAN DUNG DANG
38   * dzungdev@gmail.com
39   * Feb 10, 2009
40   */
41  
42  @ComponentConfig(
43    events = {
44      @EventConfig(listeners = UIContentSearchResult.SelectActionListener.class),
45      @EventConfig(listeners = UIContentSearchResult.ViewActionListener.class)
46    }
47  )
48  
49  public class UIContentSearchResult extends UIGrid {
50  
51    /** The Constant TITLE. */
52    public static final String TITLE = "title";
53  
54    /** The Constant NODE_EXPECT. */
55    public static final String NODE_EXPECT = "excerpt";
56  
57    /** The Constant SCORE. */
58    public static final String SCORE = "score";
59  
60    /** The Constant CREATE_DATE. */
61    public static final String CREATE_DATE = "CreateDate";
62  
63    /** The Constant PUBLICATION_STATE. */
64    public static final String PUBLICATION_STATE = "publicationstate";
65  
66    /** The Constant NODE_PATH. */
67    public static final String NODE_PATH = "path";
68  
69    /** The Actions. */
70    public String[] Actions = {"Select", "View"};
71  
72    /** The BEA n_ fields. */
73    public String[] BEAN_FIELDS = {TITLE, SCORE, PUBLICATION_STATE};
74  
75  
76    /**
77     * Instantiates a new uIWCM search result.
78     *
79     * @throws Exception the exception
80     */
81    public UIContentSearchResult() throws Exception {
82      configure(NODE_PATH, BEAN_FIELDS, Actions);
83      getUIPageIterator().setId("UIWCMSearchResultPaginator");
84    }
85  
86    /**
87     * Gets the date format.
88     *
89     * @return the date format
90     */
91    public DateFormat getDateFormat() {
92      Locale locale = Util.getPortalRequestContext().getLocale();
93      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
94      return dateFormat;
95    }
96  
97    /**
98     * Update grid.
99     *
100    * @param pageList the paginated result
101    *
102    * @throws Exception the exception
103    */
104   public void updateGrid(AbstractPageList<ResultNode> pageList) throws Exception {
105     getUIPageIterator().setPageList(pageList);
106   }
107 
108 
109   /**
110    * Gets the title node.
111    *
112    * @param node the node
113    *
114    * @return the title node
115    *
116    * @throws Exception the exception
117    */
118   public String getTitleNode(Node node) throws Exception {
119     return node.hasProperty("exo:title") ?
120                                           node.getProperty("exo:title").getValue().getString() : node.getName();
121   }
122 
123   /**
124    * Gets the creates the date.
125    *
126    * @param node the node
127    *
128    * @return the creates the date
129    *
130    * @throws Exception the exception
131    */
132   public Date getCreateDate(Node node) throws Exception {
133     if(node.hasProperty("exo:dateCreated")) {
134       Calendar cal = node.getProperty("exo:dateCreated").getValue().getDate();
135       return cal.getTime();
136     }
137     return null;
138   }
139 
140   /**
141    * Gets the expect.
142    *
143    * @param expect the expect
144    *
145    * @return the expect
146    */
147   public String getExpect(String expect) {
148     expect = expect.replaceAll("<[^>]*/?>", "");
149     return expect;
150   }
151 
152   /**
153    * Gets the current state.
154    *
155    * @param node the node
156    *
157    * @return the current state
158    *
159    * @throws Exception the exception
160    */
161   public String getCurrentState(Node node) throws Exception {
162     PublicationService pubService = getApplicationComponent(PublicationService.class);
163     return pubService.getCurrentState(node);
164   }
165 
166   /**
167    * Gets the session.
168    *
169    * @return the session
170    *
171    * @throws Exception the exception
172    */
173   public Session getSession() throws Exception {
174     RepositoryService repoService = getApplicationComponent(RepositoryService.class);
175     ManageableRepository maRepository = repoService.getCurrentRepository();
176     PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
177     PortletPreferences prefs = pContext.getRequest().getPreferences();
178     String workspace = prefs.getValue("workspace", null);
179     if(workspace == null) {
180       WCMConfigurationService wcmConfService =
181         getApplicationComponent(WCMConfigurationService.class);
182       NodeLocation nodeLocation = wcmConfService.getLivePortalsLocation();
183       workspace = nodeLocation.getWorkspace();
184     }
185     Session session = WCMCoreUtils.getUserSessionProvider().getSession(workspace, maRepository);
186     return session;
187   }
188 
189   /**
190    * Gets the workspace name.
191    * @param node the node
192    * @return name of workspace
193    * @throws Exception the exception
194    */
195   public String getWorkspaceName(Node node) throws Exception {
196     return node.getSession().getWorkspace().getName();
197   }
198 
199   public String getRepository() throws Exception {
200     RepositoryService repoService = getApplicationComponent(RepositoryService.class);
201     ManageableRepository maRepository = repoService.getCurrentRepository();
202     return maRepository.getConfiguration().getName();
203   }
204 
205   /**
206    * The listener interface for receiving selectAction events.
207    * The class that is interested in processing a selectAction
208    * event implements this interface, and the object created
209    * with that class is registered with a component using the
210    * component's <code>addSelectActionListener</code> method. When
211    * the selectAction event occurs, that object's appropriate
212    * method is invoked.
213    */
214   public static class SelectActionListener extends EventListener<UIContentSearchResult> {
215 
216     /* (non-Javadoc)
217      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
218      */
219     public void execute(Event<UIContentSearchResult> event) throws Exception {
220       UIContentSearchResult contentSearchResult = event.getSource();
221       UIContentSelector contentSelector = contentSearchResult.getAncestorOfType(UIContentSelector.class);
222       UIContentBrowsePanel contentBrowsePanel = contentSelector.getChild(UIContentBrowsePanel.class);
223       ((UISelectable) (contentBrowsePanel.getSourceComponent())).doSelect(contentBrowsePanel.getReturnFieldName(),
224                                                                           event.getRequestContext()
225                                                                                .getRequestParameter(OBJECTID));
226     }
227   }
228 
229   /**
230    * The listener interface for receiving viewAction events.
231    * The class that is interested in processing a viewAction
232    * event implements this interface, and the object created
233    * with that class is registered with a component using the
234    * component's <code>addViewActionListener</code> method. When
235    * the viewAction event occurs, that object's appropriate
236    * method is invoked.
237    */
238   public static class ViewActionListener extends EventListener<UIContentSearchResult> {
239 
240     /* (non-Javadoc)
241      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
242      */
243     public void execute(Event<UIContentSearchResult> event) throws Exception {
244       UIContentSearchResult contentSearchResult = event.getSource();
245       UIApplication uiApp = contentSearchResult.getAncestorOfType(UIApplication.class);
246       String expression = event.getRequestContext().getRequestParameter(OBJECTID);
247       NodeLocation nodeLocation = NodeLocation.getNodeLocationByExpression(expression);
248       String repository = nodeLocation.getRepository();
249       String workspace = nodeLocation.getWorkspace();
250       String webcontentPath = nodeLocation.getPath();
251       Node originalNode = Utils.getViewableNodeByComposer(repository,
252                                                           workspace,
253                                                           webcontentPath,
254                                                           WCMComposer.BASE_VERSION);
255       Node viewNode = Utils.getViewableNodeByComposer(repository, workspace, webcontentPath);
256       
257       TemplateService templateService = WCMCoreUtils.getService(TemplateService.class);
258       String nodeType = originalNode.getPrimaryNodeType().getName();
259       if (templateService.isManagedNodeType(nodeType)) {
260         UIContentSelector contentSelector = contentSearchResult.getAncestorOfType(UIContentSelector.class);
261         UIContentViewer contentResultViewer = contentSelector.getChild(UIContentViewer.class);
262         if (contentResultViewer == null)
263           contentResultViewer = contentSelector.addChild(UIContentViewer.class, null, null);
264         contentResultViewer.setNode(viewNode);
265         contentResultViewer.setOriginalNode(originalNode);
266         event.getRequestContext().addUIComponentToUpdateByAjax(contentSelector);
267         contentSelector.setSelectedTab(contentResultViewer.getId());
268       } else {
269         uiApp.addMessage(new ApplicationMessage("UIContentSearchResult.msg.template-not-support",
270                                                 null,
271                                                 ApplicationMessage.WARNING));
272       }  
273     }
274   }
275 }