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.actions; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 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; 031 032import org.ametys.core.cocoon.JSonReader; 033import org.ametys.plugins.newsletter.daos.Subscriber; 034import org.ametys.plugins.newsletter.daos.SubscribersDAO; 035import org.ametys.runtime.parameter.ParameterHelper; 036 037/** 038 * SAX events for newsletter subscribers 039 * 040 */ 041public class GetSubscribersAction extends ServiceableAction 042{ 043 private SubscribersDAO _subscribersDao; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 _subscribersDao = (SubscribersDAO) serviceManager.lookup(SubscribersDAO.ROLE); 049 super.service(serviceManager); 050 } 051 052 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 053 { 054 @SuppressWarnings("unchecked") 055 Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 056 057 String categoryId = (String) jsParameters.get("categoryID"); 058 String siteName = (String) jsParameters.get("siteName"); 059 int begin = jsParameters.get("start") != null ? (Integer) jsParameters.get("start") : 0; // Index of search 060 int offset = jsParameters.get("limit") != null ? (Integer) jsParameters.get("limit") : Integer.MAX_VALUE; // Number of results to SAX 061 062 List<Map<String, Object>> subscribersResult = new ArrayList<>(); 063 064 List<Subscriber> subscribers; 065 int total = 0; 066 if (siteName != null && categoryId != null) 067 { 068 subscribers = _subscribersDao.getSubscribers(siteName, categoryId, begin, offset); 069 total = _subscribersDao.getSubscribersCount(siteName, categoryId); 070 } 071 else 072 { 073 subscribers = _subscribersDao.getSubscribers(); 074 total = subscribers.size(); 075 } 076 077 for (Subscriber subscriber : subscribers) 078 { 079 subscribersResult.add(subscriberToJSON(subscriber)); 080 } 081 082 Map<String, Object> result = new HashMap<>(); 083 result.put("subscribers", subscribersResult); 084 result.put("total", total); 085 086 Request request = ObjectModelHelper.getRequest(objectModel); 087 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 088 089 return EMPTY_MAP; 090 } 091 092 /** 093 * Gets category provider's properties to JSON format 094 * @param subscriber the subscriber 095 * @return The category provider properties 096 */ 097 protected Map<String, Object> subscriberToJSON (Subscriber subscriber) 098 { 099 Map<String, Object> infos = new HashMap<>(); 100 101 infos.put("email", subscriber.getEmail()); 102 infos.put("siteName", subscriber.getSiteName()); 103 infos.put("category", subscriber.getCategoryId()); 104 infos.put("subscribedAt", ParameterHelper.valueToString(subscriber.getSubscribedAt())); 105 106 return infos; 107 } 108 109}