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.sms.action; 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.sms.dao.JCRSubscribersList; 034import org.ametys.plugins.sms.dao.SmsListDAO; 035import org.ametys.plugins.sms.dao.SubscriberDAO; 036 037/** 038 * SAX events for SMS lists 039 * 040 */ 041public class GetSmsListsAction extends ServiceableAction 042{ 043 /** Subscriber list DAO */ 044 protected SmsListDAO _subscriberListDAO; 045 046 /** The subscriber DAO */ 047 protected SubscriberDAO _subscriberDAO; 048 049 @Override 050 public void service(ServiceManager serviceManager) throws ServiceException 051 { 052 _subscriberListDAO = (SmsListDAO) serviceManager.lookup(SmsListDAO.ROLE); 053 _subscriberDAO = (SubscriberDAO) serviceManager.lookup(SubscriberDAO.ROLE); 054 super.service(serviceManager); 055 } 056 057 @Override 058 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 059 { 060 @SuppressWarnings("unchecked") 061 Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 062 063 String siteName = (String) jsParameters.get("site"); 064 String lang = (String) jsParameters.get("lang"); 065 066 List<Map<String, Object>> lists = new ArrayList<>(); 067 for (JCRSubscribersList subscribersList : _subscriberListDAO.getSubscribersLists(siteName, lang)) 068 { 069 lists.add(this.listToJSON(subscribersList)); 070 } 071 072 Map<String, Object> result = new HashMap<>(); 073 result.put("lists", lists); 074 075 Request request = ObjectModelHelper.getRequest(objectModel); 076 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 077 078 return EMPTY_MAP; 079 } 080 081 /** 082 * Gets SMS list's properties to JSON format 083 * @param subscribersList The SMS list of subscribers 084 * @return The SMS list properties 085 */ 086 protected Map<String, Object> listToJSON(JCRSubscribersList subscribersList) 087 { 088 Map<String, Object> infos = new HashMap<>(); 089 090 String subscriberListId = subscribersList.getId(); 091 String nbUsers = "0"; 092 093 try 094 { 095 Map<String, Number> smsListMap = _subscriberDAO.getNbUserFromList(); 096 if (smsListMap.containsKey(subscriberListId)) 097 { 098 nbUsers = smsListMap.get(subscriberListId).toString(); 099 } 100 } 101 catch (Exception e) 102 { 103 getLogger().error("An error occurred while generating the subscribers list", e); 104 } 105 106 infos.put("id", subscriberListId); 107 infos.put("label", subscribersList.getTitle()); 108 infos.put("nbUsers", nbUsers); 109 110 return infos; 111 } 112 113}