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