001/* 002 * Copyright 2010 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.newsletter.subscribe; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Set; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.acting.ServiceableAction; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Redirector; 028import org.apache.cocoon.environment.Request; 029import org.apache.cocoon.environment.SourceResolver; 030 031import org.ametys.plugins.newsletter.category.Category; 032import org.ametys.plugins.newsletter.category.CategoryProvider; 033import org.ametys.plugins.newsletter.category.CategoryProviderExtensionPoint; 034import org.ametys.plugins.newsletter.daos.Subscriber; 035import org.ametys.plugins.newsletter.daos.SubscribersDAO; 036import org.ametys.web.repository.site.Site; 037import org.ametys.web.repository.site.SiteManager; 038 039/** 040 * Unsubscribes user thanks to his unique token 041 * 042 */ 043public class UnsubscribeAction extends ServiceableAction 044{ 045 private SubscribersDAO _subscribersDao; 046 private CategoryProviderExtensionPoint _categoryProviderEP; 047 private SiteManager _siteManager; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _subscribersDao = (SubscribersDAO) smanager.lookup(SubscribersDAO.ROLE); 054 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 055 _categoryProviderEP = (CategoryProviderExtensionPoint) smanager.lookup(CategoryProviderExtensionPoint.ROLE); 056 } 057 058 @Override 059 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 060 { 061 Map<String, String> result = new HashMap<>(); 062 063 Request request = ObjectModelHelper.getRequest(objectModel); 064 065 boolean unsubscribe = request.getParameter("unsubscribe") != null; 066 String token = request.getParameter("token"); 067 result.put("token", "token"); 068 069 if (unsubscribe) 070 { 071 _subscribersDao.unsubscribe(token); 072 String categoryID = request.getParameter("categoryId"); 073 Category category = _getCategory(categoryID); 074 if (category != null) 075 { 076 result.put("categoryId", category.getId()); 077 result.put("categoryTitle", category.getTitle().getLabel()); 078 } 079 result.put("unsubcribe", "true"); 080 } 081 else 082 { 083 Subscriber subscriber = _subscribersDao.getSubscriberByToken(token); 084 if (subscriber != null) 085 { 086 String email = subscriber.getEmail(); 087 result.put("email", email); 088 089 String sitename = subscriber.getSiteName(); 090 Site site = _siteManager.getSite(sitename); 091 if (site != null) 092 { 093 result.put("siteTitle", site.getTitle()); 094 } 095 096 String categoryID = subscriber.getCategoryId(); 097 Category category = _getCategory(categoryID); 098 if (category != null) 099 { 100 result.put("categoryId", category.getId()); 101 result.put("categoryTitle", category.getTitle().getLabel()); 102 } 103 else 104 { 105 result.put("unknown-category", "true"); 106 } 107 } 108 else 109 { 110 result.put("unknown-subscriber", "true"); 111 } 112 } 113 114 return result; 115 } 116 117 private Category _getCategory (String categoryID) 118 { 119 Set<String> ids = _categoryProviderEP.getExtensionsIds(); 120 for (String id : ids) 121 { 122 CategoryProvider provider = _categoryProviderEP.getExtension(id); 123 if (provider.hasCategory(categoryID)) 124 { 125 return provider.getCategory(categoryID); 126 } 127 } 128 129 return null; 130 } 131 132}