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