001
014
015 package com.liferay.util.bridges.alloy;
016
017 import com.liferay.counter.service.CounterLocalServiceUtil;
018 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
019 import com.liferay.portal.kernel.bean.ConstantsBeanFactoryUtil;
020 import com.liferay.portal.kernel.dao.search.SearchContainer;
021 import com.liferay.portal.kernel.language.LanguageUtil;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.messaging.Destination;
025 import com.liferay.portal.kernel.messaging.InvokerMessageListener;
026 import com.liferay.portal.kernel.messaging.MessageBus;
027 import com.liferay.portal.kernel.messaging.MessageBusUtil;
028 import com.liferay.portal.kernel.messaging.MessageListener;
029 import com.liferay.portal.kernel.messaging.SerialDestination;
030 import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
031 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
032 import com.liferay.portal.kernel.portlet.PortletBag;
033 import com.liferay.portal.kernel.portlet.PortletBagPool;
034 import com.liferay.portal.kernel.portlet.PortletResponseUtil;
035 import com.liferay.portal.kernel.scheduler.CronText;
036 import com.liferay.portal.kernel.scheduler.CronTrigger;
037 import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
038 import com.liferay.portal.kernel.scheduler.StorageType;
039 import com.liferay.portal.kernel.scheduler.Trigger;
040 import com.liferay.portal.kernel.search.Field;
041 import com.liferay.portal.kernel.search.Hits;
042 import com.liferay.portal.kernel.search.Indexer;
043 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
044 import com.liferay.portal.kernel.search.SearchContext;
045 import com.liferay.portal.kernel.search.SearchContextFactory;
046 import com.liferay.portal.kernel.search.Sort;
047 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
048 import com.liferay.portal.kernel.servlet.SessionMessages;
049 import com.liferay.portal.kernel.transaction.Isolation;
050 import com.liferay.portal.kernel.transaction.Propagation;
051 import com.liferay.portal.kernel.transaction.Transactional;
052 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
053 import com.liferay.portal.kernel.util.ContentTypes;
054 import com.liferay.portal.kernel.util.GetterUtil;
055 import com.liferay.portal.kernel.util.JavaConstants;
056 import com.liferay.portal.kernel.util.ParamUtil;
057 import com.liferay.portal.kernel.util.ServiceBeanMethodInvocationFactoryUtil;
058 import com.liferay.portal.kernel.util.StringBundler;
059 import com.liferay.portal.kernel.util.StringPool;
060 import com.liferay.portal.kernel.util.Validator;
061 import com.liferay.portal.kernel.util.WebKeys;
062 import com.liferay.portal.model.AttachedModel;
063 import com.liferay.portal.model.AuditedModel;
064 import com.liferay.portal.model.BaseModel;
065 import com.liferay.portal.model.Company;
066 import com.liferay.portal.model.GroupedModel;
067 import com.liferay.portal.model.PersistedModel;
068 import com.liferay.portal.model.Portlet;
069 import com.liferay.portal.model.User;
070 import com.liferay.portal.theme.ThemeDisplay;
071 import com.liferay.portal.util.PortalUtil;
072
073 import java.io.Serializable;
074
075 import java.lang.reflect.Method;
076
077 import java.util.Date;
078 import java.util.HashMap;
079 import java.util.List;
080 import java.util.Locale;
081 import java.util.Map;
082 import java.util.Set;
083
084 import javax.portlet.ActionRequest;
085 import javax.portlet.ActionResponse;
086 import javax.portlet.EventRequest;
087 import javax.portlet.EventResponse;
088 import javax.portlet.MimeResponse;
089 import javax.portlet.PortletContext;
090 import javax.portlet.PortletRequest;
091 import javax.portlet.PortletRequestDispatcher;
092 import javax.portlet.PortletResponse;
093 import javax.portlet.PortletURL;
094 import javax.portlet.RenderRequest;
095 import javax.portlet.RenderResponse;
096 import javax.portlet.ResourceRequest;
097 import javax.portlet.ResourceResponse;
098
099 import javax.servlet.ServletConfig;
100 import javax.servlet.ServletContext;
101 import javax.servlet.http.HttpServletRequest;
102 import javax.servlet.http.HttpServletResponse;
103 import javax.servlet.jsp.PageContext;
104
105
108 public abstract class BaseAlloyControllerImpl implements AlloyController {
109
110 public static final String TOUCH =
111 BaseAlloyControllerImpl.class.getName() + "#TOUCH#";
112
113 @Override
114 public void afterPropertiesSet() {
115 initClass();
116 initServletVariables();
117 initPortletVariables();
118 initThemeDisplayVariables();
119 initMethods();
120 initPaths();
121 initIndexer();
122 initMessageListeners();
123 }
124
125 @Override
126 public void execute() throws Exception {
127 Method method = getMethod(actionPath);
128
129 if (method == null) {
130 if (log.isDebugEnabled()) {
131 log.debug("No method found for action " + actionPath);
132 }
133 }
134
135 if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
136 Class<?> superClass = clazz.getSuperclass();
137
138 Method executeActionMethod = superClass.getDeclaredMethod(
139 "executeAction", new Class<?>[] {Method.class});
140
141 ServiceBeanMethodInvocationFactoryUtil.proceed(
142 this, BaseAlloyControllerImpl.class, executeActionMethod,
143 new Object[] {method}, new String[] {"transactionAdvice"});
144 }
145 else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
146 executeRender(method);
147 }
148 else if (lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
149 executeResource(method);
150 }
151 }
152
153 @Override
154 public Portlet getPortlet() {
155 return portlet;
156 }
157
158 @Override
159 public HttpServletRequest getRequest() {
160 return request;
161 }
162
163 @Override
164 public ThemeDisplay getThemeDisplay() {
165 return themeDisplay;
166 }
167
168 @Override
169 public long increment() throws Exception {
170 return CounterLocalServiceUtil.increment();
171 }
172
173 @Override
174 public void setPageContext(PageContext pageContext) {
175 this.pageContext = pageContext;
176 }
177
178 @Override
179 public void updateModel(BaseModel<?> baseModel) throws Exception {
180 BeanPropertiesUtil.setProperties(baseModel, request);
181
182 if (baseModel.isNew()) {
183 baseModel.setPrimaryKeyObj(increment());
184 }
185
186 updateAuditedModel(baseModel);
187 updateGroupedModel(baseModel);
188 updateAttachedModel(baseModel);
189
190 if (baseModel instanceof PersistedModel) {
191 PersistedModel persistedModel = (PersistedModel)baseModel;
192
193 persistedModel.persist();
194 }
195
196 if ((indexer != null) &&
197 indexerClassName.equals(baseModel.getModelClassName())) {
198
199 indexer.reindex(baseModel);
200 }
201 else {
202 Indexer baseModelIndexer = IndexerRegistryUtil.getIndexer(
203 baseModel.getModelClass());
204
205 if (baseModelIndexer != null) {
206 baseModelIndexer.reindex(baseModel);
207 }
208 }
209 }
210
211 protected void addSuccessMessage() {
212 String successMessage = ParamUtil.getString(
213 portletRequest, "successMessage");
214
215 SessionMessages.add(portletRequest, "requestProcessed", successMessage);
216 }
217
218 protected MessageListener buildControllerMessageListener() {
219 return null;
220 }
221
222 protected String buildIncludePath(String viewPath) {
223 if (viewPath.equals(_VIEW_PATH_ERROR)) {
224 return "/WEB-INF/jsp/".concat(
225 portlet.getFriendlyURLMapping()).concat("/views/error.jsp");
226 }
227
228 StringBundler sb = new StringBundler(7);
229
230 sb.append("/WEB-INF/jsp/");
231 sb.append(portlet.getFriendlyURLMapping());
232 sb.append("/views/");
233 sb.append(controllerPath);
234 sb.append(StringPool.SLASH);
235 sb.append(viewPath);
236 sb.append(".jsp");
237
238 return sb.toString();
239 }
240
241 protected Indexer buildIndexer() {
242 return null;
243 }
244
245 protected MessageListener buildSchedulerMessageListener() {
246 return null;
247 }
248
249 @Transactional(
250 isolation = Isolation.PORTAL, propagation = Propagation.REQUIRES_NEW,
251 rollbackFor = {Exception.class}
252 )
253 protected void executeAction(Method method) throws Exception {
254 if (method != null) {
255 method.invoke(this);
256 }
257
258 actionRequest.setAttribute(
259 CALLED_PROCESS_ACTION, Boolean.TRUE.toString());
260
261 if (Validator.isNotNull(viewPath)) {
262 actionRequest.setAttribute(VIEW_PATH, viewPath);
263
264 PortalUtil.copyRequestParameters(actionRequest, actionResponse);
265 }
266 else if (Validator.isNotNull(redirect)) {
267 actionResponse.sendRedirect(redirect);
268 }
269 }
270
271 protected void executeRender(Method method) throws Exception {
272 boolean calledProcessAction = GetterUtil.getBoolean(
273 (String)request.getAttribute(CALLED_PROCESS_ACTION));
274
275 if (!calledProcessAction) {
276 if (method != null) {
277 method.invoke(this);
278 }
279 }
280
281 if (Validator.isNull(viewPath)) {
282 viewPath = actionPath;
283 }
284
285 String includePath = buildIncludePath(viewPath);
286
287 PortletRequestDispatcher portletRequestDispatcher =
288 portletContext.getRequestDispatcher(includePath);
289
290 if (portletRequestDispatcher == null) {
291 log.error(includePath + " is not a valid include");
292 }
293 else {
294 portletRequestDispatcher.include(portletRequest, portletResponse);
295 }
296
297 Boolean touch = (Boolean)portletContext.getAttribute(
298 TOUCH + portlet.getRootPortletId());
299
300 if (touch != null) {
301 return;
302 }
303
304 String touchPath =
305 "/WEB-INF/jsp/" + portlet.getFriendlyURLMapping() +
306 "/views/touch.jsp";
307
308 if (log.isDebugEnabled()) {
309 log.debug(
310 "Touch " + portlet.getRootPortletId() + " by including " +
311 touchPath);
312 }
313
314 portletContext.setAttribute(
315 TOUCH + portlet.getRootPortletId(), Boolean.FALSE);
316
317 portletRequestDispatcher = portletContext.getRequestDispatcher(
318 touchPath);
319
320 if (portletRequestDispatcher != null) {
321 portletRequestDispatcher.include(portletRequest, portletResponse);
322 }
323 }
324
325 protected void executeResource(Method method) throws Exception {
326 if (method != null) {
327 method.invoke(this);
328 }
329 }
330
331 protected Object getConstantsBean(Class<?> clazz) {
332 return ConstantsBeanFactoryUtil.getConstantsBean(clazz);
333 }
334
335 protected String getControllerDestinationName() {
336 return "liferay/alloy/controller/".concat(
337 getMessageListenerGroupName());
338 }
339
340 protected String getMessageListenerGroupName() {
341 String rootPortletId = portlet.getRootPortletId();
342
343 return rootPortletId.concat(StringPool.SLASH).concat(controllerPath);
344 }
345
346 protected Method getMethod(String methodName, Class<?>... parameterTypes) {
347 String methodKey = getMethodKey(methodName, parameterTypes);
348
349 return methodsMap.get(methodKey);
350 }
351
352 protected String getMethodKey(
353 String methodName, Class<?>... parameterTypes) {
354
355 StringBundler sb = new StringBundler(parameterTypes.length * 2 + 2);
356
357 sb.append(methodName);
358 sb.append(StringPool.POUND);
359
360 for (Class<?> parameterType : parameterTypes) {
361 sb.append(parameterType.getName());
362 sb.append(StringPool.POUND);
363 }
364
365 return sb.toString();
366 }
367
368 protected String getSchedulerDestinationName() {
369 return "liferay/alloy/scheduler/".concat(getMessageListenerGroupName());
370 }
371
372 protected String getSchedulerJobName() {
373 return getMessageListenerGroupName();
374 }
375
376 protected Trigger getSchedulerTrigger() {
377 CronText cronText = new CronText(
378 CalendarFactoryUtil.getCalendar(), CronText.DAILY_FREQUENCY, 1);
379
380 return new CronTrigger(
381 getSchedulerJobName(), getMessageListenerGroupName(),
382 cronText.toString());
383 }
384
385 protected Map<String, Serializable> getSearchAttributes(
386 Object... attributes)
387 throws Exception {
388
389 Map<String, Serializable> attributesMap =
390 new HashMap<String, Serializable>();
391
392 if ((attributes.length == 0) || ((attributes.length % 2) != 0)) {
393 throw new Exception("Arguments length is not an even number");
394 }
395
396 for (int i = 0; i < attributes.length; i += 2) {
397 String name = String.valueOf(attributes[i]);
398
399 Serializable value = (Serializable)attributes[i + 1];
400
401 attributesMap.put(name, value);
402 }
403
404 return attributesMap;
405 }
406
407 protected long increment(String name) throws Exception {
408 return CounterLocalServiceUtil.increment(name);
409 }
410
411 protected void initClass() {
412 clazz = getClass();
413 classLoader = clazz.getClassLoader();
414 }
415
416 protected void initIndexer() {
417 indexer = buildIndexer();
418
419 if (indexer == null) {
420 return;
421 }
422
423 indexerClassName = indexer.getClassNames()[0];
424
425 Indexer existingIndexer = IndexerRegistryUtil.getIndexer(
426 indexerClassName);
427
428 if ((existingIndexer != null) && (existingIndexer == indexer)) {
429 BaseAlloyIndexer baseAlloyIndexer = (BaseAlloyIndexer)indexer;
430
431 alloyServiceInvoker = baseAlloyIndexer.getAlloyServiceInvoker();
432
433 return;
434 }
435
436 alloyServiceInvoker = new AlloyServiceInvoker(indexerClassName);
437
438 BaseAlloyIndexer baseAlloyIndexer = (BaseAlloyIndexer)indexer;
439
440 baseAlloyIndexer.setAlloyServiceInvoker(alloyServiceInvoker);
441 baseAlloyIndexer.setPortletId(portlet.getRootPortletId());
442
443 PortletBag portletBag = PortletBagPool.get(portlet.getPortletId());
444
445 List<Indexer> indexerInstances = portletBag.getIndexerInstances();
446
447 if (existingIndexer != null) {
448 IndexerRegistryUtil.unregister(existingIndexer);
449
450 indexerInstances.remove(existingIndexer);
451 }
452
453 IndexerRegistryUtil.register(indexer);
454
455 indexerInstances.add(indexer);
456 }
457
458 protected void initMessageListener(
459 String destinationName, MessageListener messageListener,
460 boolean enableScheduler) {
461
462 MessageBus messageBus = MessageBusUtil.getMessageBus();
463
464 Destination destination = messageBus.getDestination(destinationName);
465
466 if (destination != null) {
467 Set<MessageListener> messageListeners =
468 destination.getMessageListeners();
469
470 for (MessageListener curMessageListener : messageListeners) {
471 if (!(curMessageListener instanceof InvokerMessageListener)) {
472 continue;
473 }
474
475 InvokerMessageListener invokerMessageListener =
476 (InvokerMessageListener)curMessageListener;
477
478 curMessageListener =
479 invokerMessageListener.getMessageListener();
480
481 if (messageListener == curMessageListener) {
482 return;
483 }
484
485 Class<?> messageListenerClass = messageListener.getClass();
486
487 String messageListenerClassName =
488 messageListenerClass.getName();
489
490 Class<?> curMessageListenerClass =
491 curMessageListener.getClass();
492
493 if (!messageListenerClassName.equals(
494 curMessageListenerClass.getName())) {
495
496 continue;
497 }
498
499 try {
500 if (enableScheduler) {
501 SchedulerEngineHelperUtil.unschedule(
502 getSchedulerJobName(),
503 getMessageListenerGroupName(),
504 StorageType.MEMORY_CLUSTERED);
505 }
506
507 MessageBusUtil.unregisterMessageListener(
508 destinationName, curMessageListener);
509 }
510 catch (Exception e) {
511 log.error(e, e);
512 }
513
514 break;
515 }
516 }
517 else {
518 SerialDestination serialDestination = new SerialDestination();
519
520 serialDestination.setName(destinationName);
521
522 serialDestination.open();
523
524 MessageBusUtil.addDestination(serialDestination);
525 }
526
527 try {
528 MessageBusUtil.registerMessageListener(
529 destinationName, messageListener);
530
531 if (enableScheduler) {
532 SchedulerEngineHelperUtil.schedule(
533 getSchedulerTrigger(), StorageType.MEMORY_CLUSTERED, null,
534 destinationName, null, 0);
535 }
536 }
537 catch (Exception e) {
538 log.error(e, e);
539 }
540 }
541
542 protected void initMessageListeners() {
543 controllerMessageListener = buildControllerMessageListener();
544
545 if (controllerMessageListener != null) {
546 initMessageListener(
547 getControllerDestinationName(), controllerMessageListener,
548 false);
549 }
550
551 schedulerMessageListener = buildSchedulerMessageListener();
552
553 if (schedulerMessageListener != null) {
554 initMessageListener(
555 getSchedulerDestinationName(), schedulerMessageListener, true);
556 }
557 }
558
559 protected void initMethods() {
560 methodsMap = new HashMap<String, Method>();
561
562 Method[] methods = clazz.getMethods();
563
564 for (Method method : methods) {
565 String methodKey = getMethodKey(
566 method.getName(), method.getParameterTypes());
567
568 methodsMap.put(methodKey, method);
569 }
570 }
571
572 protected void initPaths() {
573 controllerPath = ParamUtil.getString(request, "controller");
574
575 if (Validator.isNull(controllerPath)) {
576 Map<String, String> defaultRouteParameters =
577 alloyPortlet.getDefaultRouteParameters();
578
579 controllerPath = defaultRouteParameters.get("controller");
580 }
581
582 if (log.isDebugEnabled()) {
583 log.debug("Controller path " + controllerPath);
584 }
585
586 actionPath = ParamUtil.getString(request, "action");
587
588 if (Validator.isNull(actionPath)) {
589 Map<String, String> defaultRouteParameters =
590 alloyPortlet.getDefaultRouteParameters();
591
592 actionPath = defaultRouteParameters.get("action");
593 }
594
595 if (log.isDebugEnabled()) {
596 log.debug("Action path " + actionPath);
597 }
598
599 viewPath = GetterUtil.getString(
600 (String)request.getAttribute(VIEW_PATH));
601
602 request.removeAttribute(VIEW_PATH);
603
604 if (log.isDebugEnabled()) {
605 log.debug("View path " + viewPath);
606 }
607
608 if (mimeResponse != null) {
609 portletURL = mimeResponse.createRenderURL();
610
611 portletURL.setParameter("action", actionPath);
612 portletURL.setParameter("controller", controllerPath);
613 portletURL.setParameter("format", "html");
614
615 if (log.isDebugEnabled()) {
616 log.debug("Portlet URL " + portletURL);
617 }
618 }
619 }
620
621 protected void initPortletVariables() {
622 liferayPortletConfig = (LiferayPortletConfig)request.getAttribute(
623 JavaConstants.JAVAX_PORTLET_CONFIG);
624
625 portletContext = liferayPortletConfig.getPortletContext();
626
627 portlet = liferayPortletConfig.getPortlet();
628
629 alloyPortlet = (AlloyPortlet)request.getAttribute(
630 JavaConstants.JAVAX_PORTLET_PORTLET);
631
632 alloyPortlet.registerAlloyController(this);
633
634 portletRequest = (PortletRequest)request.getAttribute(
635 JavaConstants.JAVAX_PORTLET_REQUEST);
636 portletResponse = (PortletResponse)request.getAttribute(
637 JavaConstants.JAVAX_PORTLET_RESPONSE);
638
639 liferayPortletResponse = (LiferayPortletResponse)portletResponse;
640
641 lifecycle = GetterUtil.getString(
642 (String)request.getAttribute(PortletRequest.LIFECYCLE_PHASE));
643
644 if (log.isDebugEnabled()) {
645 log.debug("Lifecycle " + lifecycle);
646 }
647
648 if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
649 actionRequest = (ActionRequest)portletRequest;
650 actionResponse = (ActionResponse)portletResponse;
651 }
652 else if (lifecycle.equals(PortletRequest.EVENT_PHASE)) {
653 eventRequest = (EventRequest)portletRequest;
654 eventResponse = (EventResponse)portletResponse;
655 }
656 else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
657 mimeResponse = (MimeResponse)portletResponse;
658 renderRequest = (RenderRequest)portletRequest;
659 renderResponse = (RenderResponse)portletResponse;
660 }
661 else if (lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
662 mimeResponse = (MimeResponse)portletResponse;
663 resourceRequest = (ResourceRequest)portletRequest;
664 resourceResponse = (ResourceResponse)portletResponse;
665 }
666 }
667
668 protected void initServletVariables() {
669 servletConfig = pageContext.getServletConfig();
670 servletContext = pageContext.getServletContext();
671 request = (HttpServletRequest)pageContext.getRequest();
672 response = (HttpServletResponse)pageContext.getResponse();
673 }
674
675 protected void initThemeDisplayVariables() {
676 themeDisplay = (ThemeDisplay)request.getAttribute(
677 WebKeys.THEME_DISPLAY);
678
679 company = themeDisplay.getCompany();
680 locale = themeDisplay.getLocale();
681 user = themeDisplay.getUser();
682 }
683
684 protected void redirectTo(PortletURL portletURL) {
685 redirectTo(portletURL.toString());
686 }
687
688 protected void redirectTo(String redirect) {
689 if (!lifecycle.equals(PortletRequest.ACTION_PHASE)) {
690 throw new IllegalArgumentException(
691 "redirectTo can only be called during the action phase");
692 }
693
694 if (Validator.isNotNull(viewPath)) {
695 throw new IllegalArgumentException(
696 "redirectTo cannot be called if render has been called");
697 }
698
699 this.redirect = redirect;
700 }
701
702 protected void render(String actionPath) {
703 if (Validator.isNotNull(redirect)) {
704 throw new IllegalArgumentException(
705 "render cannot be called if redirectTo has been called");
706 }
707
708 viewPath = actionPath;
709 }
710
711 protected void renderError(String pattern, Object... arguments) {
712 portletRequest.setAttribute("arguments", arguments);
713 portletRequest.setAttribute("pattern", pattern);
714
715 render(_VIEW_PATH_ERROR);
716 }
717
718 protected AlloySearchResult search(
719 Map<String, Serializable> attributes, String keywords, Sort sort)
720 throws Exception {
721
722 return search(attributes, keywords, new Sort[] {sort});
723 }
724
725 protected AlloySearchResult search(
726 Map<String, Serializable> attributes, String keywords, Sort[] sorts)
727 throws Exception {
728
729 if (indexer == null) {
730 throw new Exception("No indexer found for " + controllerPath);
731 }
732
733 AlloySearchResult alloySearchResult = new AlloySearchResult();
734
735 alloySearchResult.setAlloyServiceInvoker(alloyServiceInvoker);
736
737 SearchContainer<BaseModel<?>> searchContainer =
738 new SearchContainer<BaseModel<?>>(
739 portletRequest, portletURL, null, null);
740
741 SearchContext searchContext = SearchContextFactory.getInstance(request);
742
743 if ((attributes != null) && !attributes.isEmpty()) {
744 searchContext.setAttributes(attributes);
745 }
746
747 searchContext.setEnd(searchContainer.getEnd());
748
749 Class<?> indexerClass = Class.forName(indexerClassName);
750
751 try {
752 indexerClass.getField(Field.GROUP_ID);
753 }
754 catch (Exception e) {
755 searchContext.setGroupIds(null);
756 }
757
758 if (Validator.isNotNull(keywords)) {
759 searchContext.setKeywords(keywords);
760 }
761
762 if ((sorts != null) && (sorts.length > 0)) {
763 searchContext.setSorts(sorts);
764 }
765
766 searchContext.setStart(searchContainer.getStart());
767
768 Hits hits = indexer.search(searchContext);
769
770 alloySearchResult.setHits(hits);
771
772 alloySearchResult.setPortletURL(portletURL);
773
774 alloySearchResult.afterPropertiesSet();
775
776 return alloySearchResult;
777 }
778
779 protected AlloySearchResult search(String keywords) throws Exception {
780 return search(keywords, (Sort[])null);
781 }
782
783 protected AlloySearchResult search(String keywords, Sort sort)
784 throws Exception {
785
786 return search(keywords, new Sort[] {sort});
787 }
788
789 protected AlloySearchResult search(String keywords, Sort[] sorts)
790 throws Exception {
791
792 return search(null, keywords, sorts);
793 }
794
795 protected String translate(String pattern, Object... arguments) {
796 return LanguageUtil.format(locale, pattern, arguments);
797 }
798
799 protected void updateAttachedModel(BaseModel<?> baseModel)
800 throws Exception {
801
802 if (!(baseModel instanceof AttachedModel)) {
803 return;
804 }
805
806 AttachedModel attachedModel = (AttachedModel)baseModel;
807
808 long classNameId = 0;
809
810 String className = ParamUtil.getString(request, "className");
811
812 if (Validator.isNotNull(className)) {
813 classNameId = PortalUtil.getClassNameId(className);
814 }
815
816 if (classNameId > 0) {
817 attachedModel.setClassNameId(classNameId);
818 }
819
820 long classPK = ParamUtil.getLong(request, "classPK");
821
822 if (classPK > 0) {
823 attachedModel.setClassPK(classPK);
824 }
825 }
826
827 protected void updateAuditedModel(BaseModel<?> baseModel) throws Exception {
828 if (!(baseModel instanceof AuditedModel)) {
829 return;
830 }
831
832 AuditedModel auditedModel = (AuditedModel)baseModel;
833
834 if (baseModel.isNew()) {
835 auditedModel.setCompanyId(company.getCompanyId());
836 auditedModel.setUserId(user.getUserId());
837 auditedModel.setUserName(user.getFullName());
838 auditedModel.setCreateDate(new Date());
839 auditedModel.setModifiedDate(auditedModel.getCreateDate());
840 }
841 else {
842 auditedModel.setModifiedDate(new Date());
843 }
844 }
845
846 protected void updateGroupedModel(BaseModel<?> baseModel) throws Exception {
847 if (!(baseModel instanceof GroupedModel) || !baseModel.isNew()) {
848 return;
849 }
850
851 GroupedModel groupedModel = (GroupedModel)baseModel;
852
853 groupedModel.setGroupId(themeDisplay.getScopeGroupId());
854 }
855
856 protected void writeJSON(Object json) throws Exception {
857 if (actionResponse != null) {
858 HttpServletResponse response = PortalUtil.getHttpServletResponse(
859 actionResponse);
860
861 response.setContentType(ContentTypes.APPLICATION_JSON);
862
863 ServletResponseUtil.write(response, json.toString());
864 }
865 else if (mimeResponse != null) {
866 mimeResponse.setContentType(ContentTypes.APPLICATION_JSON);
867
868 PortletResponseUtil.write(mimeResponse, json.toString());
869 }
870 }
871
872 protected static final String CALLED_PROCESS_ACTION =
873 BaseAlloyControllerImpl.class.getName() + "#CALLED_PROCESS_ACTION";
874
875 protected static final String VIEW_PATH =
876 BaseAlloyControllerImpl.class.getName() + "#VIEW_PATH";
877
878 protected static Log log = LogFactoryUtil.getLog(
879 BaseAlloyControllerImpl.class);
880
881 protected String actionPath;
882 protected ActionRequest actionRequest;
883 protected ActionResponse actionResponse;
884 protected AlloyPortlet alloyPortlet;
885 protected AlloyServiceInvoker alloyServiceInvoker;
886 protected ClassLoader classLoader;
887 protected Class<?> clazz;
888 protected Company company;
889 protected MessageListener controllerMessageListener;
890 protected String controllerPath;
891 protected EventRequest eventRequest;
892 protected EventResponse eventResponse;
893 protected Indexer indexer;
894 protected String indexerClassName;
895 protected String lifecycle;
896 protected LiferayPortletConfig liferayPortletConfig;
897 protected LiferayPortletResponse liferayPortletResponse;
898 protected Locale locale;
899 protected Map<String, Method> methodsMap;
900 protected MimeResponse mimeResponse;
901 protected PageContext pageContext;
902 protected Portlet portlet;
903 protected PortletContext portletContext;
904 protected PortletRequest portletRequest;
905 protected PortletResponse portletResponse;
906 protected PortletURL portletURL;
907 protected String redirect;
908 protected RenderRequest renderRequest;
909 protected RenderResponse renderResponse;
910 protected HttpServletRequest request;
911 protected ResourceRequest resourceRequest;
912 protected ResourceResponse resourceResponse;
913 protected HttpServletResponse response;
914 protected MessageListener schedulerMessageListener;
915 protected ServletConfig servletConfig;
916 protected ServletContext servletContext;
917 protected ThemeDisplay themeDisplay;
918 protected User user;
919 protected String viewPath;
920
921 private static final String _VIEW_PATH_ERROR = "VIEW_PATH_ERROR";
922
923 }