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