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                                                                                     .withItemTagName("metadata"));
067            
068            _foContentCreation.saxContentValues(contentHandler, userContentType, "items", lang);
069        }
070    }
071    
072    @Override
073    protected void saxUserInputs(Request request, ZoneItem zoneItem) throws SAXException
074    {
075        super.saxUserInputs(request, zoneItem);
076        
077        String contentTypeId = zoneItem.getServiceParameters().getValue("user-content-type");
078        if (StringUtils.isNotEmpty(contentTypeId))
079        {
080            ContentType cType = _cTypeEP.getExtension(contentTypeId);
081            if (cType != null && cType.getView("signup") != null)
082            {
083                View view = cType.getView("signup");
084                saxItemsValues(request, view.getViewItems());
085            }
086        }
087    }
088    
089    /**
090     * SAX the items values
091     * @param request the request
092     * @param viewItems the view items
093     * @throws SAXException if an error occurs
094     */
095    protected void saxItemsValues(Request request, List<ViewItem> viewItems) throws SAXException
096    {
097        for (ViewItem viewItem : viewItems)
098        {
099            if (viewItem instanceof ViewElement)
100            {
101                String paramName = ((ViewElement) viewItem).getDefinition().getPath().replace(ModelItem.ITEM_PATH_SEPARATOR, ".");
102                _saxNotEmptyParameter(request, paramName);
103            }
104            else if (viewItem instanceof ModelViewItemGroup)
105            {
106                saxItemsValues(request, ((ModelViewItemGroup) viewItem).getViewItems());
107            }
108        }
109    }
110    
111}