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