001/*
002 *  Copyright 2018 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.ugc.generators;
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.apache.commons.lang.StringUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.cms.contenttype.ContentType;
031import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
032import org.ametys.core.right.RightManager;
033import org.ametys.core.user.CurrentUserProvider;
034import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
035import org.ametys.plugins.ugc.UGCConstants;
036import org.ametys.runtime.model.DefinitionContext;
037import org.ametys.web.WebConstants;
038import org.ametys.web.cache.PageHelper;
039import org.ametys.web.content.FOContentCreationHelper;
040import org.ametys.web.repository.page.Page;
041import org.ametys.web.repository.page.ZoneItem;
042
043/**
044 * UGC service generator
045 */
046public class UGCGenerator extends ServiceableGenerator
047{
048    /** The {@link ContentType} manager */
049    protected ContentTypeExtensionPoint _cTypeExtPt;
050    /** The right manager */
051    protected RightManager _rightManager;
052    /** The current user provider */
053    protected CurrentUserProvider _currentUserProvider;
054    /** The FO content creation helper */
055    protected FOContentCreationHelper _foContentCreationHelper;
056    /** The page helper */
057    protected PageHelper _pageHelper;
058    
059    @Override
060    public void service(ServiceManager smanager) throws ServiceException
061    {
062        super.service(smanager);
063        _cTypeExtPt = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
064        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
065        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
066        _foContentCreationHelper = (FOContentCreationHelper) smanager.lookup(FOContentCreationHelper.ROLE);
067        _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE);
068    }
069    
070    @Override
071    public void generate() throws IOException, SAXException, ProcessingException
072    {
073        Request request = ObjectModelHelper.getRequest(objectModel);
074
075        Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE);
076        ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
077        
078        ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters();
079        String cType = (String) serviceParameters.getValue("content-type");
080        if (StringUtils.isBlank(cType))
081        {
082            throw new IllegalArgumentException("A content type must be defined");
083        }
084        
085        contentHandler.startDocument();
086        
087        XMLUtils.startElement(contentHandler, "ugc");
088        
089        // SAX inputs for UGC mixin
090        XMLUtils.startElement(contentHandler, "mixin");
091        ContentType ugcMixin = _cTypeExtPt.getExtension(UGCConstants.UGC_MIXIN_TYPE);
092        ugcMixin.getView("main").toSAX(contentHandler, DefinitionContext.newInstance());
093        XMLUtils.endElement(contentHandler, "mixin");
094        
095        // SAX view
096        XMLUtils.startElement(contentHandler, "content-type");
097        ContentType contentType = _cTypeExtPt.getExtension(cType);
098        contentType.getView("main").toSAX(contentHandler, DefinitionContext.newInstance());
099        XMLUtils.endElement(contentHandler, "content-type");
100        
101        // SAX contents values for metadata of type CONTENT
102        _foContentCreationHelper.saxContentValues(contentHandler, contentType, "items", page.getSitemapName());
103        
104        XMLUtils.createElement(contentHandler, "has-captcha", String.valueOf(_pageHelper.isCaptchaRequired(page)));
105        
106        XMLUtils.endElement(contentHandler, "ugc");
107        
108        contentHandler.endDocument();
109    }
110    
111    
112
113}