001/*
002 *  Copyright 2021 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.workspaces.members;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Locale;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.cms.contenttype.ContentTypesHelper;
032import org.ametys.cms.repository.Content;
033import org.ametys.plugins.workspaces.WorkspacesConstants;
034import org.ametys.runtime.model.View;
035
036/**
037 * Sax a list of related members with the current member content
038 *
039 */
040public class RelatedMembersGenerator extends ServiceableGenerator
041{
042    private MemberHelper _memberHelper;
043    private ContentTypesHelper _contentTypeHelper;
044
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _memberHelper = (MemberHelper) smanager.lookup(MemberHelper.ROLE);
050        _contentTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
051    }
052
053    public void generate() throws IOException, SAXException, ProcessingException
054    {
055        contentHandler.startDocument();
056        XMLUtils.startElement(contentHandler, "related-members");
057        
058        Request request = ObjectModelHelper.getRequest(objectModel);
059        Content content = (Content) request.getAttribute(Content.class.getName());
060        
061        try
062        {
063            List<Content> relatedMembers = _memberHelper.getRelatedMembers(content, 6);
064            for (Content relatedMember : relatedMembers)
065            {
066                View view = _contentTypeHelper.getView("abstract", new String[] {WorkspacesConstants.MEMBER_CONTENT_TYPE_ID}, new String[0]);
067                relatedMember.toSAX(contentHandler, new Locale(content.getLanguage()), view, false);
068            }
069        }
070        catch (Exception e)
071        {
072            getLogger().error("Failed to search related members with common skills and keywords", e);
073        }
074        
075        XMLUtils.endElement(contentHandler, "related-members");
076        contentHandler.endDocument();
077    }
078}