001/* 002 * Copyright 2024 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.pagesubscription.generator; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.environment.ObjectModelHelper; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.generation.ServiceableGenerator; 026import org.apache.cocoon.xml.XMLUtils; 027import org.xml.sax.SAXException; 028 029import org.ametys.core.user.CurrentUserProvider; 030import org.ametys.plugins.pagesubscription.FrequencyHelper; 031import org.ametys.plugins.pagesubscription.Subscription; 032import org.ametys.plugins.pagesubscription.context.PageSubscriptionContext; 033import org.ametys.plugins.pagesubscription.type.PageSubscriptionType; 034import org.ametys.plugins.pagesubscription.type.SubscriptionTypeExtensionPoint; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.web.WebHelper; 037import org.ametys.web.repository.site.Site; 038import org.ametys.web.repository.site.SiteManager; 039 040/** 041 * SAX user subscriptions 042 */ 043public class UserPageSubscriptionsGenerator extends ServiceableGenerator 044{ 045 /** The current user provider */ 046 protected CurrentUserProvider _currentUserProvider; 047 048 /** The site manager */ 049 protected SiteManager _siteManager; 050 051 /** The subscription type extension point */ 052 protected SubscriptionTypeExtensionPoint _subscriptionTypeEP; 053 054 /** The ametys object resolver */ 055 protected AmetysObjectResolver _resolver; 056 057 @Override 058 public void service(ServiceManager smanager) throws ServiceException 059 { 060 super.service(manager); 061 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 062 _subscriptionTypeEP = (SubscriptionTypeExtensionPoint) smanager.lookup(SubscriptionTypeExtensionPoint.ROLE); 063 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 064 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 065 } 066 067 @Override 068 public void generate() throws IOException, SAXException, ProcessingException 069 { 070 contentHandler.startDocument(); 071 XMLUtils.startElement(contentHandler, "subscriptions"); 072 073 FrequencyHelper.saxFrequencies(contentHandler); 074 075 Request request = ObjectModelHelper.getRequest(objectModel); 076 String siteName = WebHelper.getSiteName(request); 077 PageSubscriptionType subscriptionType = (PageSubscriptionType) _subscriptionTypeEP.getExtension(PageSubscriptionType.ID); 078 079 Site site = _siteManager.getSite(siteName); 080 for (Subscription subscription : subscriptionType.getUserSubscriptions(site, _currentUserProvider.getUser(), false, PageSubscriptionContext.newInstance())) 081 { 082 subscriptionType.saxSubscription(contentHandler, subscription); 083 } 084 085 XMLUtils.endElement(contentHandler, "subscriptions"); 086 contentHandler.endDocument(); 087 } 088 089}