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