001/*
002 *  Copyright 2010 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.daos;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.thread.ThreadSafe;
024import org.apache.ibatis.session.RowBounds;
025import org.apache.ibatis.session.SqlSession;
026
027import org.ametys.core.datasource.AbstractMyBatisDAO;
028
029/**
030 * DAO for accessing newsletters subscribers.
031 */
032public class SubscribersDAO extends AbstractMyBatisDAO implements ThreadSafe
033{
034    /** The Avalon role name. */
035    public static final String ROLE = SubscribersDAO.class.getName();
036    
037    /**
038     * Get the whole list for subscribers
039     * @return The list for subscribers
040     */
041    public List<Subscriber> getSubscribers()
042    {
043        try (SqlSession session = getSession())
044        {
045            return session.selectList("Subscribers.getSubscribers");
046        }
047    }
048    
049    /**
050     * Get the subscribers to a newsletter category
051     * @param siteName The site name
052     * @param categoryId The newsletter category's id
053     * @return the subscribers
054     */
055    public List<Subscriber> getSubscribers (String siteName, String categoryId)
056    {
057        Map<String, Object> params = new HashMap<>();
058        params.put("siteName", siteName);
059        params.put("category", categoryId);
060        
061        try (SqlSession session = getSession())
062        {
063            return session.selectList("Subscribers.getSubscribersByCategory", params);
064        }
065    }
066    
067    /**
068     * Get the subscribers to a newsletter category
069     * @param siteName The site name
070     * @param categoryId The newsletter category's id
071     * @param offset The number of results to ignore.
072     * @param limit The maximum number of results to return.
073     * @return the subscribers
074     */
075    public List<Subscriber> getSubscribers (String siteName, String categoryId, int offset, int limit)
076    {
077        Map<String, Object> params = new HashMap<>();
078        params.put("siteName", siteName);
079        params.put("category", categoryId);
080        
081        try (SqlSession session = getSession())
082        {
083            return session.selectList("Subscribers.getSubscribersByCategory", params, new RowBounds(offset, limit));
084        }
085    }
086    
087    /**
088     * Get the subscribers count for a newsletter category
089     * @param siteName The site name
090     * @param categoryId The newsletter category's id
091     * @return the subscribers count
092     */
093    public int getSubscribersCount (String siteName, String categoryId)
094    {
095        Map<String, Object> params = new HashMap<>();
096        params.put("siteName", siteName);
097        params.put("category", categoryId);
098        
099        try (SqlSession session = getSession())
100        {
101            return (Integer) session.selectOne("Subscribers.getSubscribersCount", params);
102        }
103    }
104    
105    /**
106     * Get a subscriber to a newsletter category
107     * @param email The subscriber email
108     * @param siteName The site name
109     * @param categoryId The newsletter category's id
110     * @return the subscribers
111     */
112    public Subscriber getSubscriber (String email, String siteName, String categoryId)
113    {
114        Map<String, Object> params = new HashMap<>();
115        params.put("email", email);
116        params.put("siteName", siteName);
117        params.put("category", categoryId);
118        
119        try (SqlSession session = getSession())
120        {
121            return (Subscriber) session.selectOne("Subscribers.getSubscriber", params);
122        }
123    }
124    
125    /**
126     * Get a subscriber by his token
127     * @param token The user token
128     * @return the subscribers
129     */
130    public Subscriber getSubscriberByToken (String token)
131    {
132        Map<String, Object> params = new HashMap<>();
133        params.put("token", token);
134        
135        try (SqlSession session = getSession())
136        {
137            return (Subscriber) session.selectOne("Subscribers.getSubscriberByToken", params);
138        }
139    }
140    
141    /**
142     * Get the list of subscriptions for a given email and site name.
143     * @param email the email.
144     * @param siteName the site name.
145     * @return the list of subscriptions.
146     */
147    public List<Subscriber> getSubscriptions(String email, String siteName)
148    {
149        Map<String, Object> params = new HashMap<>();
150        params.put("email", email);
151        params.put("siteName", siteName);
152        
153        try (SqlSession session = getSession())
154        {
155            return session.selectList("Subscribers.getSubscriptionsByEmail", params);
156        }
157    }
158    
159    /**
160     * Subscribes to the newsletter
161     * @param subscriber The subscriber
162     */
163    public void subscribe (Subscriber subscriber)
164    {
165        try (SqlSession session = getSession(true))
166        {
167            session.insert("Subscribers.subscribe", subscriber);
168        }
169    }
170    
171    /**
172     * Insert several subscriptions to newsletters.
173     * @param subscribers a list of subscribers.
174     */
175    public void subscribe(Collection<Subscriber> subscribers)
176    {
177        try (SqlSession session = getSession())
178        {
179            for (Subscriber subscriber : subscribers)
180            {
181                session.insert("Subscribers.subscribe", subscriber);
182            }
183            
184            session.commit();
185        }
186    }
187    
188    
189    /**
190     * Insert several subscriptions to newsletters.
191     * @param newSubscribers the collection of subscribers to insert.
192     * @param removeSubscriptions the collection of subscription tokens to remove.
193     */
194    public void modifySubscriptions(Collection<Subscriber> newSubscribers, Collection<String> removeSubscriptions)
195    {
196        try (SqlSession session = getSession())
197        {
198            for (Subscriber subscriber : newSubscribers)
199            {
200                session.insert("Subscribers.subscribe", subscriber);
201            }
202            
203            for (String tokenToRemove : removeSubscriptions)
204            {
205                session.update("Subscribers.unsubscribe", tokenToRemove);
206            }
207            
208            session.commit();
209        }
210    }
211    
212    /**
213     * Unsubscribes to the newsletter
214     * @param token The unique token
215     */
216    public void unsubscribe (String token)
217    {
218        try (SqlSession session = getSession(true))
219        {
220            session.update("Subscribers.unsubscribe", token);
221        }
222    }
223    
224    /**
225     * Empty a category's subscribers.
226     * @param categoryId the category to empty.
227     * @param siteName the site name.
228     */
229    public void empty(String categoryId, String siteName)
230    {
231        Map<String, Object> params = new HashMap<>();
232        params.put("categoryId", categoryId);
233        params.put("siteName", siteName);
234        
235        try (SqlSession session = getSession(true))
236        {
237            session.delete("Subscribers.empty", params);
238        }
239    }
240    
241    /**
242     * Remove all subscriptions for a subscriber in a given site.
243     * @param email the category to empty.
244     * @param siteName the site name.
245     */
246    public void unsubscribe(String email, String siteName)
247    {
248        Map<String, Object> params = new HashMap<>();
249        params.put("email", email);
250        params.put("siteName", siteName);
251        
252        try (SqlSession session = getSession(true))
253        {
254            session.delete("Subscribers.removeSubscriptionsByEmail", params);
255        }
256    }
257}