001/*
002 *  Copyright 2022 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.project.generators;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.xml.XMLUtils;
026import org.xml.sax.SAXException;
027
028import org.ametys.core.group.GroupIdentity;
029import org.ametys.core.observation.Event;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.plugins.core.group.GroupHelper;
032import org.ametys.plugins.workspaces.ObservationConstants;
033import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType;
034import org.ametys.plugins.workspaces.project.objects.Project;
035
036/**
037 * Generator for the welcome mail of a new user
038 */
039public class MemberEventMailNotifierGenerator extends AbstractMailNotifierGenerator
040{
041    
042    private GroupHelper _groupHelper;
043
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _groupHelper = (GroupHelper) smanager.lookup(GroupHelper.ROLE);
049    }
050    
051    // This generator is called with an event as param instead of an activity
052    // so we need to rework the generation
053    @Override
054    public void generate() throws IOException, SAXException, ProcessingException
055    {
056        contentHandler.startDocument();
057        
058        @SuppressWarnings("unchecked")
059        Map<String, Object> parentContextAttr = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
060        Event event = (Event) parentContextAttr.get("event");
061        Project project = (Project) parentContextAttr.get("project");
062        XMLUtils.startElement(contentHandler, AbstractMailNotifierGenerator.ROOT_ELEMENT);
063        
064        XMLUtils.createElement(contentHandler, "eventKey", getEventIdAsI18nKey(event.getId()));
065        
066        saxProject(project);
067        
068        _saxMember(event, project);
069        
070        _saxProjectUserIdentity(event.getIssuer(), project, "issuer");
071        
072        XMLUtils.endElement(contentHandler, AbstractMailNotifierGenerator.ROOT_ELEMENT);
073        contentHandler.endDocument();
074    }
075    
076    private void _saxMember(Event event, Project project) throws SAXException
077    {
078        Map<String, Object> args = event.getArguments();
079        
080        MemberType type = (MemberType) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY_TYPE);
081        String identity = (String) args.get(ObservationConstants.ARGS_MEMBER_IDENTITY);
082        
083        if (MemberType.USER == type)
084        {
085            UserIdentity user = UserIdentity.stringToUserIdentity(identity);
086            _saxProjectUserIdentity(user, project, "member");
087        }
088        else
089        {
090            // Group
091            GroupIdentity group = GroupIdentity.stringToGroupIdentity(identity);
092            _saxGroupIdentity(group, "member");
093        }
094    }
095    
096    private void _saxGroupIdentity(GroupIdentity group, String tagname) throws SAXException
097    {
098        XMLUtils.startElement(contentHandler, tagname);
099        _groupHelper.saxGroupIdentity(group, contentHandler);
100        XMLUtils.endElement(contentHandler, tagname);
101    }
102    
103    @Override
104    protected String _getModuleId()
105    {
106        // not used
107        return null;
108    }
109
110
111
112}