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.time.LocalDate; 019import java.time.format.DateTimeFormatter; 020import java.util.ArrayList; 021import java.util.Date; 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.avalon.framework.parameters.Parameters; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.cocoon.acting.ServiceableAction; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Redirector; 032import org.apache.cocoon.environment.Request; 033import org.apache.cocoon.environment.SourceResolver; 034 035import org.ametys.core.cocoon.JSonReader; 036import org.ametys.plugins.sms.dao.SubscriberDAO; 037import org.ametys.runtime.parameter.ParameterHelper; 038 039/** 040 * SAX events for subscribers (phone numbers) 041 * 042 */ 043public class GetSubscribersAction extends ServiceableAction 044{ 045 /** The subscriber DAO */ 046 protected SubscriberDAO _subscriberDAO; 047 048 @Override 049 public void service(ServiceManager serviceManager) throws ServiceException 050 { 051 _subscriberDAO = (SubscriberDAO) serviceManager.lookup(SubscriberDAO.ROLE); 052 super.service(serviceManager); 053 } 054 055 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 056 { 057 @SuppressWarnings("unchecked") 058 Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 059 060 String listId = (String) jsParameters.get("id"); 061 062 List<Map<String, Object>> subscribers = new ArrayList<>(); 063 064 Map<String, Date> listNumber = _subscriberDAO.getPhoneNumbersFromList(listId); 065 for (String number : listNumber.keySet()) 066 { 067 subscribers.add(this.subscriberToJSON(number, listNumber)); 068 } 069 070 Map<String, Object> result = new HashMap<>(); 071 result.put("subscribers", subscribers); 072 073 Request request = ObjectModelHelper.getRequest(objectModel); 074 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 075 076 return EMPTY_MAP; 077 } 078 079 /** 080 * Gets subscriber's properties to JSON format 081 * @param number the subscriber's numbers 082 * @param listNumber the mapping of numbers -> date 083 * @return The subscriber properties 084 */ 085 protected Map<String, Object> subscriberToJSON(String number, Map<String, Date> listNumber) 086 { 087 Map<String, Object> infos = new HashMap<>(); 088 089 infos.put("value", number); 090 Date date = listNumber.get(number); 091 092 infos.put("date", date != null ? ParameterHelper.valueToString(LocalDate.parse(date.toString(), DateTimeFormatter.ISO_LOCAL_DATE)) : null); 093 094 return infos; 095 } 096 097}