001/*
002 *  Copyright 2012 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.userdirectory.signup;
017
018import java.util.List;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.environment.Request;
023import org.apache.commons.lang.StringUtils;
024import org.xml.sax.SAXException;
025
026import org.ametys.cms.contenttype.ContentType;
027import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
028import org.ametys.runtime.model.DefinitionContext;
029import org.ametys.runtime.model.ModelItem;
030import org.ametys.runtime.model.ModelViewItemGroup;
031import org.ametys.runtime.model.View;
032import org.ametys.runtime.model.ViewElement;
033import org.ametys.runtime.model.ViewItem;
034import org.ametys.web.content.FOContentCreationHelper;
035import org.ametys.web.repository.page.ZoneItem;
036
037/**
038 * Generator for user signup service
039 *
040 */
041public class UserSignupGenerator extends org.ametys.web.usermanagement.UserSignupGenerator
042{
043    /** The content types manager */
044    protected ContentTypeExtensionPoint _cTypeEP;
045    /** The FO content creation helper */
046    protected FOContentCreationHelper _foContentCreation;
047
048    @Override
049    public void service(ServiceManager serviceManager) throws ServiceException
050    {
051        super.service(serviceManager);
052        _cTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
053        _foContentCreation = (FOContentCreationHelper) serviceManager.lookup(FOContentCreationHelper.ROLE);
054    }
055    
056    @Override
057    protected void saxAdditionalInformation(String siteName, String lang, ZoneItem zoneItem) throws SAXException
058    {
059        super.saxAdditionalInformation(siteName, lang, zoneItem);
060        
061        String userContentTypeId = zoneItem.getServiceParameters().getValue("user-content-type");
062        if (StringUtils.isNotBlank(userContentTypeId))
063        {
064            ContentType userContentType = _cTypeEP.getExtension(userContentTypeId);
065            userContentType.getView("signup").toSAX(contentHandler, DefinitionContext.newInstance());
066            
067            _foContentCreation.saxContentValues(contentHandler, userContentType, "items", lang);
068        }
069    }
070    
071    @Override
072    protected void saxUserInputs(Request request, ZoneItem zoneItem) throws SAXException
073    {
074        super.saxUserInputs(request, zoneItem);
075        
076        String contentTypeId = zoneItem.getServiceParameters().getValue("user-content-type");
077        if (StringUtils.isNotEmpty(contentTypeId))
078        {
079            ContentType cType = _cTypeEP.getExtension(contentTypeId);
080            if (cType != null && cType.getView("signup") != null)
081            {
082                View view = cType.getView("signup");
083                saxItemsValues(request, view.getViewItems());
084            }
085        }
086    }
087    
088    /**
089     * SAX the items values
090     * @param request the request
091     * @param viewItems the view items
092     * @throws SAXException if an error occurs
093     */
094    protected void saxItemsValues(Request request, List<ViewItem> viewItems) throws SAXException
095    {
096        for (ViewItem viewItem : viewItems)
097        {
098            if (viewItem instanceof ViewElement)
099            {
100                String paramName = ((ViewElement) viewItem).getDefinition().getPath().replace(ModelItem.ITEM_PATH_SEPARATOR, ".");
101                _saxNotEmptyParameter(request, paramName);
102            }
103            else if (viewItem instanceof ModelViewItemGroup)
104            {
105                saxItemsValues(request, ((ModelViewItemGroup) viewItem).getViewItems());
106            }
107        }
108    }
109    
110}