001/* 002 * Copyright 2012 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.Collection; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.core.ui.Callable; 028import org.ametys.core.ui.StaticClientSideElement; 029import org.ametys.plugins.newsletter.category.Category; 030import org.ametys.plugins.newsletter.category.CategoryProvider; 031import org.ametys.plugins.newsletter.category.CategoryProviderExtensionPoint; 032 033/** 034 * Automatic newsletter client side element: indicate which of the selected categories are in "automatic" mode. 035 */ 036public class AutomaticNewsletterClientSideElement extends StaticClientSideElement 037{ 038 039 /** The automatic newsletter extension point. */ 040 protected AutomaticNewsletterExtensionPoint _autoNewsletterEP; 041 042 /** The category provider extension point. */ 043 protected CategoryProviderExtensionPoint _categoryProviderEP; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 super.service(serviceManager); 049 _autoNewsletterEP = (AutomaticNewsletterExtensionPoint) serviceManager.lookup(AutomaticNewsletterExtensionPoint.ROLE); 050 _categoryProviderEP = (CategoryProviderExtensionPoint) serviceManager.lookup(CategoryProviderExtensionPoint.ROLE); 051 } 052 053 /** 054 * Get the parameters of the automatic newletter 055 * @param categoryIds the id of categories 056 * @return the automatic newletter parameters 057 */ 058 @Callable 059 public Map<String, Object> getStatus(List<String> categoryIds) 060 { 061 Map<String, Object> params = new HashMap<>(); 062 063 List<Map<String, Object>> categoryWithAutoNewsletters = new ArrayList<>(); 064 065 for (String categoryId : categoryIds) 066 { 067 Category category = _categoryProviderEP.getCategory(categoryId); 068 CategoryProvider provider = _categoryProviderEP.getCategoryProvider(categoryId); 069 Collection<String> autoIds = provider.getAutomaticIds(categoryId); 070 071 if (!autoIds.isEmpty()) 072 { 073 Map<String, Object> categoryInfo = new HashMap<>(); 074 categoryInfo.put("id", categoryId); 075 categoryInfo.put("title", category.getTitle()); 076 categoryInfo.put("count", autoIds.size()); 077 categoryWithAutoNewsletters.add(categoryInfo); 078 } 079 } 080 081 params.put("categories-with-auto-newsletters", categoryWithAutoNewsletters); 082 params.put("auto-newsletter-count", categoryWithAutoNewsletters.size()); 083 params.put("available-auto-newsletter-count", _autoNewsletterEP.getExtensionsIds().size()); 084 085 return params; 086 } 087 088}