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.joboffer.generator;
017
018import java.io.IOException;
019import java.util.Locale;
020import java.util.Map;
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.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.cms.contenttype.ContentTypesHelper;
031import org.ametys.cms.repository.Content;
032import org.ametys.plugins.joboffer.JobOfferConstants;
033import org.ametys.runtime.model.DefinitionContext;
034import org.ametys.runtime.model.View;
035
036/**
037 * Generator for mail to notify of a new application
038 *
039 */
040public class NewJobApplicationMailGenerator extends ServiceableGenerator
041{
042    private ContentTypesHelper _cTypeHelper;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _cTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
049    }
050
051    public void generate() throws IOException, SAXException, ProcessingException
052    {
053        @SuppressWarnings("unchecked")
054        Map<String, Object> params = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
055        
056        Content content = (Content) params.get("content");
057        
058        contentHandler.startDocument();
059        XMLUtils.startElement(contentHandler, "mail");
060
061        View view = _cTypeHelper.getView("main", new String[] {JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE}, new String[0]);
062        content.toSAX(contentHandler, new Locale(content.getLanguage()), view, false);
063        view.toSAX(contentHandler, DefinitionContext.newInstance());
064        
065        XMLUtils.endElement(contentHandler, "mail");
066        contentHandler.endDocument();
067    }
068}