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.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.apache.commons.lang3.StringUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.cms.contenttype.ContentTypesHelper;
033import org.ametys.cms.data.ContentValue;
034import org.ametys.cms.repository.Content;
035import org.ametys.cms.repository.ModifiableContent;
036import org.ametys.plugins.joboffer.JobOfferConstants;
037import org.ametys.runtime.model.DefinitionContext;
038import org.ametys.runtime.model.View;
039
040/**
041 * Generator for mail to notify of a new application
042 *
043 */
044public class NewJobApplicationMailGenerator extends ServiceableGenerator
045{
046    private ContentTypesHelper _cTypeHelper;
047    
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _cTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
053    }
054
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        @SuppressWarnings("unchecked")
058        Map<String, Object> params = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
059        
060        Content content = (Content) params.get("content");
061        
062        contentHandler.startDocument();
063        XMLUtils.startElement(contentHandler, "mail");
064
065        ContentValue jobOffer = content.getValue(JobOfferConstants.JOB_APPLICATION_ATTRIBUTE_PATH_JOB_OFFER);
066        ModifiableContent jobOfferContent = jobOffer.getContent();
067        saxJobOffer(jobOfferContent);
068        
069        // View and values
070        View view = _cTypeHelper.getView("main", new String[] {JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE}, new String[0]);
071        content.toSAX(contentHandler, new Locale(content.getLanguage()), view, false);
072        view.toSAX(contentHandler, DefinitionContext.newInstance());
073        
074        XMLUtils.endElement(contentHandler, "mail");
075        contentHandler.endDocument();
076    }
077    
078    /**
079     * SAX the related job offer
080     * @param content the job offer content
081     * @throws SAXException if an exception occurred while saxing
082     */
083    protected void saxJobOffer(Content content) throws SAXException
084    {
085        AttributesImpl attrs = new AttributesImpl();
086        attrs.addCDATAAttribute("id", content.getId());
087        attrs.addCDATAAttribute("title", content.getTitle());
088        
089        XMLUtils.startElement(contentHandler, "jobOffer", attrs);
090        XMLUtils.createElement(contentHandler, JobOfferConstants.JOB_OFFER_ATTRIBUTE_PATH_REF_ID, content.getValue(JobOfferConstants.JOB_OFFER_ATTRIBUTE_PATH_REF_ID, false, StringUtils.EMPTY));
091        XMLUtils.endElement(contentHandler, "jobOffer");
092    }
093
094}