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
38
39
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
52 public static final String TITLE = "title";
53
54
55 public static final String NODE_EXPECT = "excerpt";
56
57
58 public static final String SCORE = "score";
59
60
61 public static final String CREATE_DATE = "CreateDate";
62
63
64 public static final String PUBLICATION_STATE = "publicationstate";
65
66
67 public static final String NODE_PATH = "path";
68
69
70 public String[] Actions = {"Select", "View"};
71
72
73 public String[] BEAN_FIELDS = {TITLE, SCORE, PUBLICATION_STATE};
74
75
76
77
78
79
80
81 public UIContentSearchResult() throws Exception {
82 configure(NODE_PATH, BEAN_FIELDS, Actions);
83 getUIPageIterator().setId("UIWCMSearchResultPaginator");
84 }
85
86
87
88
89
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
99
100
101
102
103
104 public void updateGrid(AbstractPageList<ResultNode> pageList) throws Exception {
105 getUIPageIterator().setPageList(pageList);
106 }
107
108
109
110
111
112
113
114
115
116
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
125
126
127
128
129
130
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
142
143
144
145
146
147 public String getExpect(String expect) {
148 expect = expect.replaceAll("<[^>]*/?>", "");
149 return expect;
150 }
151
152
153
154
155
156
157
158
159
160
161 public String getCurrentState(Node node) throws Exception {
162 PublicationService pubService = getApplicationComponent(PublicationService.class);
163 return pubService.getCurrentState(node);
164 }
165
166
167
168
169
170
171
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
191
192
193
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
207
208
209
210
211
212
213
214 public static class SelectActionListener extends EventListener<UIContentSearchResult> {
215
216
217
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
231
232
233
234
235
236
237
238 public static class ViewActionListener extends EventListener<UIContentSearchResult> {
239
240
241
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 }