001/* 002 * Copyright 2018 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.action; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021import java.util.stream.Collectors; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.acting.ServiceableAction; 027import org.apache.cocoon.environment.ObjectModelHelper; 028import org.apache.cocoon.environment.Redirector; 029import org.apache.cocoon.environment.Request; 030import org.apache.cocoon.environment.SourceResolver; 031import org.apache.commons.lang3.ArrayUtils; 032import org.apache.commons.lang3.StringUtils; 033 034import org.ametys.cms.contenttype.ContentType; 035import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 036import org.ametys.cms.repository.Content; 037import org.ametys.core.captcha.CaptchaHelper; 038import org.ametys.core.cocoon.ActionResultGenerator; 039import org.ametys.core.util.JSONUtils; 040import org.ametys.plugins.joboffer.JobOfferConstants; 041import org.ametys.plugins.repository.AmetysObjectResolver; 042import org.ametys.runtime.i18n.I18nizableText; 043import org.ametys.web.cache.PageHelper; 044import org.ametys.web.content.FOContentCreationHelper; 045import org.ametys.web.repository.page.SitemapElement; 046import org.ametys.web.repository.page.ZoneItem; 047 048import com.google.common.collect.ArrayListMultimap; 049import com.google.common.collect.Multimap; 050 051/** 052 * Action to apply to a job offer 053 */ 054public class ApplyJobOfferAction extends ServiceableAction 055{ 056 private static final int _INITIAL_ACTION_ID = 1; 057 058 /** Ametys object resolver. */ 059 protected AmetysObjectResolver _resolver; 060 061 /** The content type extension point */ 062 protected ContentTypeExtensionPoint _cTypeEP; 063 064 /** The JSON Utils */ 065 protected JSONUtils _jsonUtils; 066 067 /** Helper for FO content creation */ 068 protected FOContentCreationHelper _foContentCreationHelper; 069 070 /** The page helper */ 071 protected PageHelper _pageHelper; 072 073 @Override 074 public void service(ServiceManager serviceManager) throws ServiceException 075 { 076 super.service(serviceManager); 077 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 078 _cTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE); 079 _jsonUtils = (JSONUtils) serviceManager.lookup(JSONUtils.ROLE); 080 _foContentCreationHelper = (FOContentCreationHelper) serviceManager.lookup(FOContentCreationHelper.ROLE); 081 _pageHelper = (PageHelper) serviceManager.lookup(PageHelper.ROLE); 082 } 083 084 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 085 { 086 Map<String, Object> result = new HashMap<>(); 087 088 Multimap<String, I18nizableText> errors = ArrayListMultimap.create(); 089 090 Request request = ObjectModelHelper.getRequest(objectModel); 091 092 String zoneId = request.getParameter("zoneId"); 093 ZoneItem zoneItem = _resolver.resolveById(zoneId); 094 SitemapElement sitemapElement = zoneItem.getZone().getSitemapElement(); 095 096 String siteName = request.getParameter("site"); 097 String language = request.getParameter("lang"); 098 099 ContentType contentType = _cTypeEP.getExtension(JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE); 100 String workflowName = contentType.getDefaultWorkflowName().get(); 101 102 Map<String, Object> values = _foContentCreationHelper.getAndValidateFormValues(request, contentType, "main", errors); 103 104 // Check captcha if needed 105 if (_pageHelper.isCaptchaRequired(sitemapElement)) 106 { 107 String captchaValue = request.getParameter("captcha"); 108 String captchaKey = request.getParameter("captcha-key"); 109 110 // Validate the input 111 if (!CaptchaHelper.checkAndInvalidate(captchaKey, captchaValue)) 112 { 113 errors.put("captcha", new I18nizableText("plugin.job-offer", "PLUGINS_JOB_OFFER_APPLY_FORM_ERROR_INVALID_CAPTCHA")); 114 } 115 } 116 117 // If there are no errors 118 if (errors.isEmpty()) 119 { 120 String title = getTitle(request); 121 122 result = _foContentCreationHelper.createAndEditContent(_INITIAL_ACTION_ID, new String[] {JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE}, ArrayUtils.EMPTY_STRING_ARRAY, siteName, title, title, language, values, workflowName, null, new HashMap<String, Object>()); 123 124 if (result.containsKey(Content.class.getName())) 125 { 126 result.put("success", true); 127 } 128 else 129 { 130 result.put("success", false); 131 } 132 } 133 else 134 { 135 result.put("success", false); 136 result.put("errors", _jsonUtils.convertObjectToJson(errors.asMap())); 137 } 138 139 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result); 140 return EMPTY_MAP; 141 } 142 143 /** 144 * Get title from request 145 * @param request the request 146 * @return the computed title 147 */ 148 protected String getTitle(Request request) 149 { 150 Object[] array = List.of(request.getParameter("lastname"), request.getParameter("firstname")) 151 .stream() 152 .filter(StringUtils::isNotEmpty) 153 .collect(Collectors.toList()) 154 .toArray(); 155 156 return StringUtils.join(array, " "); 157 } 158 159}