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.ZoneId; 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; 037 038/** 039 * SAX events for subscribers (phone numbers) 040 * 041 */ 042public class GetSubscribersAction extends ServiceableAction 043{ 044 /** The subscriber DAO */ 045 protected SubscriberDAO _subscriberDAO; 046 047 @Override 048 public void service(ServiceManager serviceManager) throws ServiceException 049 { 050 _subscriberDAO = (SubscriberDAO) serviceManager.lookup(SubscriberDAO.ROLE); 051 super.service(serviceManager); 052 } 053 054 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 055 { 056 @SuppressWarnings("unchecked") 057 Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 058 059 String listId = (String) jsParameters.get("id"); 060 061 List<Map<String, Object>> subscribers = new ArrayList<>(); 062 063 Map<String, Date> listNumber = _subscriberDAO.getPhoneNumbersFromList(listId); 064 for (String number : listNumber.keySet()) 065 { 066 subscribers.add(this.subscriberToJSON(number, listNumber)); 067 } 068 069 Map<String, Object> result = new HashMap<>(); 070 result.put("subscribers", subscribers); 071 072 Request request = ObjectModelHelper.getRequest(objectModel); 073 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 074 075 return EMPTY_MAP; 076 } 077 078 /** 079 * Gets subscriber's properties to JSON format 080 * @param number the subscriber's numbers 081 * @param listNumber the mapping of numbers -> date 082 * @return The subscriber properties 083 */ 084 protected Map<String, Object> subscriberToJSON(String number, Map<String, Date> listNumber) 085 { 086 Map<String, Object> infos = new HashMap<>(); 087 088 infos.put("value", number); 089 Date date = listNumber.get(number); 090 091 if (date != null) 092 { 093 LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); 094 infos.put("date", localDate.toString()); 095 } 096 097 return infos; 098 } 099 100}