001/* 002 * Copyright 2015 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.auto; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.cocoon.ProcessingException; 028import org.apache.commons.lang.StringUtils; 029 030import org.ametys.core.ui.Callable; 031import org.ametys.plugins.newsletter.category.CategoryProvider; 032import org.ametys.plugins.newsletter.category.CategoryProviderExtensionPoint; 033 034/** 035 * DAO for manipulating automatic newsletter categories. 036 * 037 */ 038public class AutomaticNewsletterDAO implements Serviceable, Component 039{ 040 private CategoryProviderExtensionPoint _categoryProviderEP; 041 private AutomaticNewsletterExtensionPoint _autoNewsletterEP; 042 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 _categoryProviderEP = (CategoryProviderExtensionPoint) manager.lookup(CategoryProviderExtensionPoint.ROLE); 046 _autoNewsletterEP = (AutomaticNewsletterExtensionPoint) manager.lookup(AutomaticNewsletterExtensionPoint.ROLE); 047 } 048 049 /** 050 * Set the automatic newsletters property on a newsletter category. 051 * @param categoryId The id of category 052 * @param autoNewslettersIds the list of automatic newsletter ids 053 * @throws ProcessingException if an error occurred when setting up the automatic newsletters 054 */ 055 @Callable 056 public void setAutomaticNewsletters (String categoryId, List<String> autoNewslettersIds) throws ProcessingException 057 { 058 if (StringUtils.isEmpty(categoryId) || !_categoryProviderEP.hasCategory(categoryId)) 059 { 060 throw new ProcessingException("Unknown category ID: " + categoryId); 061 } 062 063 CategoryProvider provider = _categoryProviderEP.getCategoryProvider(categoryId); 064 065 provider.setAutomatic(categoryId, autoNewslettersIds); 066 } 067 068 /** 069 * Get the automatic newsletters 070 * @return the automatic newsletters 071 */ 072 @Callable 073 public List<Map<String, Object>> getAutomaticNewsletters () 074 { 075 List<Map<String, Object>> autoNewsletters = new ArrayList<>(); 076 077 for (String autoNewsletterId : _autoNewsletterEP.getExtensionsIds()) 078 { 079 AutomaticNewsletter autoNewsletter = _autoNewsletterEP.getExtension(autoNewsletterId); 080 081 Map<String, Object> infos = new HashMap<>(); 082 infos.put("id", autoNewsletterId); 083 infos.put("label", autoNewsletter.getLabel()); 084 infos.put("description", autoNewsletter.getDescription()); 085 086 Map<String, Object> frequency = new HashMap<>(); 087 frequency.put("type", autoNewsletter.getFrequencyType().toString().toLowerCase()); 088 frequency.put("dayNumbers", autoNewsletter.getDayNumbers()); 089 infos.put("frequency", frequency); 090 091 infos.put("filters", autoNewsletter.getFilters()); 092 093 autoNewsletters.add(infos); 094 } 095 096 return autoNewsletters; 097 } 098}