001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.blog.observer; 017 018import java.util.Map; 019 020import org.apache.avalon.framework.context.Context; 021import org.apache.avalon.framework.context.ContextException; 022import org.apache.avalon.framework.context.Contextualizable; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.cocoon.components.ContextHelper; 027import org.apache.cocoon.environment.Request; 028import org.apache.commons.lang3.ArrayUtils; 029 030import org.ametys.cms.content.indexing.solr.SolrIndexer; 031import org.ametys.cms.repository.Content; 032import org.ametys.core.observation.Event; 033import org.ametys.core.observation.Observer; 034import org.ametys.plugins.blog.BlogConstants; 035import org.ametys.plugins.blog.BlogPageHandler; 036import org.ametys.plugins.repository.AmetysObjectResolver; 037import org.ametys.plugins.repository.UnknownAmetysObjectException; 038import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector; 039import org.ametys.runtime.plugin.component.AbstractLogEnabled; 040import org.ametys.web.WebConstants; 041import org.ametys.web.indexing.solr.SolrPageIndexer; 042import org.ametys.web.repository.content.WebContent; 043import org.ametys.web.repository.page.Page; 044 045/** 046 * Abstract {@link Observer} for observing event on blog 047 */ 048public abstract class AbstractBlogObserver extends AbstractLogEnabled implements Observer, Contextualizable, Serviceable 049//public abstract class AbstractBlogObserver extends AbstractLiveSolrObserver 050{ 051 /** The context. */ 052 protected Context _context; 053 /** The blog cache manager */ 054 protected BlogPageHandler _blogRootPageHandler; 055 /** The ametys object resolver. */ 056 protected AmetysObjectResolver _resolver; 057 /** The solr page indexer. */ 058 protected SolrPageIndexer _solrPageIndexer; 059 /** The solr indexer */ 060 protected SolrIndexer _solrIndexer; 061 062 @Override 063 public void contextualize(Context context) throws ContextException 064 { 065 _context = context; 066 } 067 068 @Override 069 public void service(ServiceManager smanager) throws ServiceException 070 { 071 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 072 _solrIndexer = (SolrIndexer) smanager.lookup(SolrIndexer.ROLE); 073 _solrPageIndexer = (SolrPageIndexer) smanager.lookup(SolrPageIndexer.ROLE); 074 _blogRootPageHandler = (BlogPageHandler) smanager.lookup(BlogPageHandler.ROLE); 075 } 076 077 @Override 078 public int getPriority(Event event) 079 { 080 // Will be processed after live synchronization observers 081 return MAX_PRIORITY + 3000; 082 } 083 084 @Override 085 public void observe(Event event, Map<String, Object> transientVars) throws Exception 086 { 087 Map<String, Object> args = event.getArguments(); 088 089 Content content = (Content) args.get(org.ametys.cms.ObservationConstants.ARGS_CONTENT); 090 091 if (content != null && ArrayUtils.contains(content.getTypes(), BlogConstants.POST_CONTENT_TYPE)) 092 { 093 // Retrieve current workspace 094 Request request = ContextHelper.getRequest(_context); 095 String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 096 097 try 098 { 099 // Use live workspace 100 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE); 101 102 Content liveContent = null; 103 try 104 { 105 // Retrieve page in the live workspace 106 liveContent = _resolver.resolveById(content.getId()); 107 } 108 catch (UnknownAmetysObjectException e) 109 { 110 // Page is not synchronized 111 } 112 113 if (liveContent != null) 114 { 115 Page postPage = _blogRootPageHandler.getPostPage((WebContent) content); 116 _internalObserve (event, postPage, (WebContent) content); 117 } 118 } 119 finally 120 { 121 // Restore context 122 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp); 123 } 124 } 125 } 126 127 /** 128 * Do the actual work. 129 * @param event the observation event. 130 * @param postPage the Page holding the virtual factory. 131 * @param postContent the post content 132 */ 133 protected abstract void _internalObserve(Event event, Page postPage, WebContent postContent); 134 135}