001/*
002 *  Copyright 2011 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;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.core.ui.Callable;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029
030
031/**
032 * DAO for manipulating newsletter
033 *
034 */
035public class NewsletterDAO implements Serviceable, Component
036{
037    /** The Avalon role */
038    public static final String ROLE = NewsletterDAO.class.getName();
039    
040    private AmetysObjectResolver _resolver;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    /**
049     * Determines if the newsletter was already sent
050     * @param newsletterId the id of newsletter
051     * @return true if the newsletter was already sent
052     */
053    @Callable
054    public boolean isSent (String newsletterId)
055    {
056        Content content = _resolver.resolveById(newsletterId);
057        return content.getMetadataHolder().getBoolean("sent", false);
058    }
059    
060    /**
061     * Gets newsletter's properties to JSON format
062     * @param newsletter The newsletter
063     * @return The newsletter's properties
064     */
065    @Callable
066    public Map<String, Object> getNewsletterProperties(Content newsletter)
067    {
068        Map<String, Object> infos = new HashMap<>();
069        
070        infos.put("id", newsletter.getId());
071        infos.put("title", newsletter.getTitle());
072        infos.put("name", newsletter.getName());
073        infos.put("automatic", newsletter.getMetadataHolder().getBoolean("automatic", false));
074        
075        return infos;
076    }
077}