nl.strohalm.cyclos.controls.admins.mailPreferences.MailPreferencesAction.java Source code

Java tutorial

Introduction

Here is the source code for nl.strohalm.cyclos.controls.admins.mailPreferences.MailPreferencesAction.java

Source

/*
   This file is part of Cyclos.
    
   Cyclos is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
    
   Cyclos is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with Cyclos; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    
 */

package nl.strohalm.cyclos.controls.admins.mailPreferences;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import nl.strohalm.cyclos.annotations.Inject;
import nl.strohalm.cyclos.controls.ActionContext;
import nl.strohalm.cyclos.controls.BaseFormAction;
import nl.strohalm.cyclos.dao.exceptions.EntityNotFoundException;
import nl.strohalm.cyclos.entities.accounts.guarantees.GuaranteeType;
import nl.strohalm.cyclos.entities.accounts.guarantees.GuaranteeTypeQuery;
import nl.strohalm.cyclos.entities.accounts.transactions.TransferType;
import nl.strohalm.cyclos.entities.accounts.transactions.TransferTypeQuery;
import nl.strohalm.cyclos.entities.accounts.transactions.TransferType.Context;
import nl.strohalm.cyclos.entities.alerts.MemberAlert;
import nl.strohalm.cyclos.entities.alerts.SystemAlert;
import nl.strohalm.cyclos.entities.groups.AdminGroup;
import nl.strohalm.cyclos.entities.groups.Group;
import nl.strohalm.cyclos.entities.groups.MemberGroup;
import nl.strohalm.cyclos.entities.members.Administrator;
import nl.strohalm.cyclos.entities.members.messages.MessageCategory;
import nl.strohalm.cyclos.entities.members.preferences.AdminNotificationPreference;
import nl.strohalm.cyclos.entities.members.preferences.MemberAlertNotificationPreference;
import nl.strohalm.cyclos.entities.members.preferences.SystemAlertNotificationPreference;
import nl.strohalm.cyclos.services.accounts.guarantees.GuaranteeTypeService;
import nl.strohalm.cyclos.services.preferences.PreferenceService;
import nl.strohalm.cyclos.services.transfertypes.TransferTypeService;
import nl.strohalm.cyclos.utils.RequestHelper;
import nl.strohalm.cyclos.utils.binding.BeanBinder;
import nl.strohalm.cyclos.utils.binding.BeanCollectionBinder;
import nl.strohalm.cyclos.utils.binding.DataBinder;
import nl.strohalm.cyclos.utils.binding.PropertyBinder;
import nl.strohalm.cyclos.utils.binding.SimpleCollectionBinder;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;

/**
 * Action used to save the mail preferences
 * @author Lucas Geiss
 */
public class MailPreferencesAction extends BaseFormAction {

    /**
     * Transforms an AlertNotificationPreference in it's type
     * @author luis
     */
    private static class AlertTypeTransformer implements Transformer {

        private static final AlertTypeTransformer INSTANCE = new AlertTypeTransformer();

        public Object transform(final Object object) {
            if (object instanceof SystemAlertNotificationPreference) {
                return ((SystemAlertNotificationPreference) object).getType();
            } else if (object instanceof MemberAlertNotificationPreference) {
                return ((MemberAlertNotificationPreference) object).getType();
            }
            return null;
        }

    }

    private GuaranteeTypeService guaranteeTypeService;
    private PreferenceService preferenceService;
    private TransferTypeService transferTypeService;
    private DataBinder<AdminNotificationPreference> dataBinder;

    @Inject
    public void setGuaranteeTypeService(final GuaranteeTypeService guaranteeTypeService) {
        this.guaranteeTypeService = guaranteeTypeService;
    }

    @Inject
    public void setPreferenceService(final PreferenceService preferenceService) {
        this.preferenceService = preferenceService;
    }

    @Inject
    public void setTransferTypeService(final TransferTypeService transferTypeService) {
        this.transferTypeService = transferTypeService;
    }

    @Override
    protected void formAction(final ActionContext context) throws Exception {
        final MailPreferencesForm form = context.getForm();
        AdminNotificationPreference notificationPreference = getDataBinder()
                .readFromString(form.getAdminNotificationPreference());
        notificationPreference = preferenceService.save(notificationPreference);
        context.sendMessage("mailPreferences.saved");
    }

    @Override
    protected void prepareForm(final ActionContext context) throws Exception {
        final HttpServletRequest request = context.getRequest();
        final MailPreferencesForm form = context.getForm();

        final Administrator admin = context.getElement();
        AdminGroup group = admin.getAdminGroup();
        group = getFetchService().fetch(group, AdminGroup.Relationships.VIEW_INFORMATION_OF,
                Group.Relationships.MESSAGE_CATEGORIES);

        // Find the initial groups this admin manages
        final List<MemberGroup> initialGroups = new ArrayList<MemberGroup>();
        for (final MemberGroup memberGroup : getFetchService().fetch(group, AdminGroup.Relationships.MANAGES_GROUPS)
                .getManagesGroups()) {
            if (memberGroup.isInitialGroup()) {
                initialGroups.add(memberGroup);
            }
        }

        final TransferTypeQuery transferTypeQuery = new TransferTypeQuery();
        transferTypeQuery.setFromOrToAccountTypes(group.getViewInformationOf());
        final List<TransferType> transferTypes = transferTypeService.search(transferTypeQuery);
        for (final Iterator<TransferType> iterator = transferTypes.iterator(); iterator.hasNext();) {
            // Remove those transfer types which does not allow direct payment or self payment
            final TransferType tt = iterator.next();
            final Context ttContext = tt.getContext();
            if (!ttContext.isPayment() && !ttContext.isSelfPayment()) {
                iterator.remove();
            }
        }

        final TransferTypeQuery newPendingPaymentsQuery = new TransferTypeQuery();
        newPendingPaymentsQuery.setAuthorizable(true);
        final List<TransferType> newPendingPayments = transferTypeService.search(newPendingPaymentsQuery);

        List<GuaranteeType> guaranteeTypes = Collections.emptyList();
        if (getPermissionService().checkPermission("systemGuaranteeTypes", "view")) {
            final GuaranteeTypeQuery guaranteeTypeQuery = new GuaranteeTypeQuery();
            guaranteeTypeQuery.setEnabled(true);
            guaranteeTypes = guaranteeTypeService.search(guaranteeTypeQuery);
        }

        final List<MessageCategory> messageCategories = new ArrayList<MessageCategory>(
                group.getMessageCategories());
        Collections.sort(messageCategories);

        AdminNotificationPreference notificationPreference = null;
        try {
            notificationPreference = preferenceService.load(admin,
                    AdminNotificationPreference.Relationships.TRANSFER_TYPES,
                    AdminNotificationPreference.Relationships.MESSAGE_CATEGORIES,
                    AdminNotificationPreference.Relationships.MEMBER_ALERTS,
                    AdminNotificationPreference.Relationships.SYSTEM_ALERTS);
            form.setAdminNotificationPreference("applicationErrors", notificationPreference.isApplicationErrors());
            form.setAdminNotificationPreference("systemInvoices", notificationPreference.isSystemInvoices());
            request.setAttribute("selectedTransferTypes", notificationPreference.getTransferTypes());
            request.setAttribute("selectedNewPendingPayments", notificationPreference.getNewPendingPayments());
            request.setAttribute("selectedGuaranteeTypes", notificationPreference.getGuaranteeTypes());
            request.setAttribute("selectedMessageCategories", notificationPreference.getMessageCategories());
            request.setAttribute("selectedNewMembers", notificationPreference.getNewMembers());
            request.setAttribute("selectedSystemAlerts", CollectionUtils
                    .collect(notificationPreference.getSystemAlerts(), AlertTypeTransformer.INSTANCE));
            request.setAttribute("selectedMemberAlerts", CollectionUtils
                    .collect(notificationPreference.getMemberAlerts(), AlertTypeTransformer.INSTANCE));
        } catch (final EntityNotFoundException e) {
            // Ignore - no current preference
        }

        RequestHelper.storeEnum(request, MemberAlert.Alerts.class, "memberAlerts");
        RequestHelper.storeEnum(request, SystemAlert.Alerts.class, "systemAlerts");

        request.setAttribute("transferTypes", transferTypes);
        request.setAttribute("newPendingPayments", newPendingPayments);
        request.setAttribute("guaranteeTypes", guaranteeTypes);
        request.setAttribute("messageCategories", messageCategories);
        request.setAttribute("initialGroups", initialGroups);
        request.setAttribute("notificationPreference", notificationPreference);
    }

    private DataBinder<AdminNotificationPreference> getDataBinder() {
        if (dataBinder == null) {
            final BeanBinder<SystemAlertNotificationPreference> systemAlertBinder = BeanBinder
                    .instance(SystemAlertNotificationPreference.class);
            systemAlertBinder.registerBinder("type", PropertyBinder.instance(SystemAlert.Alerts.class, "type"));
            final BeanBinder<MemberAlertNotificationPreference> memberAlertBinder = BeanBinder
                    .instance(MemberAlertNotificationPreference.class);
            memberAlertBinder.registerBinder("type", PropertyBinder.instance(MemberAlert.Alerts.class, "type"));
            final BeanBinder<AdminNotificationPreference> binder = BeanBinder
                    .instance(AdminNotificationPreference.class);

            binder.registerBinder("transferTypes",
                    SimpleCollectionBinder.instance(TransferType.class, Set.class, "transferTypes"));
            binder.registerBinder("newPendingPayments",
                    SimpleCollectionBinder.instance(TransferType.class, Set.class, "newPendingPayments"));
            binder.registerBinder("messageCategories",
                    SimpleCollectionBinder.instance(MessageCategory.class, Set.class, "messageCategories"));
            binder.registerBinder("guaranteeTypes",
                    SimpleCollectionBinder.instance(GuaranteeType.class, Set.class, "guaranteeTypes"));
            binder.registerBinder("newMembers",
                    SimpleCollectionBinder.instance(MemberGroup.class, Set.class, "newMembers"));
            binder.registerBinder("systemAlerts", BeanCollectionBinder.instance(systemAlertBinder, "systemAlerts"));
            binder.registerBinder("memberAlerts", BeanCollectionBinder.instance(memberAlertBinder, "memberAlerts"));
            binder.registerBinder("applicationErrors", PropertyBinder.instance(Boolean.TYPE, "applicationErrors"));
            binder.registerBinder("systemInvoices", PropertyBinder.instance(Boolean.TYPE, "systemInvoices"));
            dataBinder = binder;
        }
        return dataBinder;
    }

}