/*******************************************************************************
 *  Imixs Workflow 
 *  Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,  
 *  http://www.imixs.com
 *  
 *  This program 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.
 *  
 *  This program 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 can receive a copy of the GNU General Public
 *  License at http://www.gnu.org/licenses/gpl.html
 *  
 *  Project: 
 *  	http://www.imixs.org
 *  	http://java.net/projects/imixs-workflow
 *  
 *  Contributors:  
 *  	Imixs Software Solutions GmbH - initial API and implementation
 *  	Ralph Soika - Software Developer
 *******************************************************************************/

package com.alexanderlogistics;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.annotation.security.RunAs;
import javax.ejb.Singleton;
import javax.inject.Inject;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.WorkflowService;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.exceptions.ModelException;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.exceptions.ProcessingErrorException;

/**
 * Der ZahlungseingangService wird vom ZahlungseingangSaldoAdapter verwendet um
 * Ausgangsrechnungen zu akutalisieren.
 * 
 * @author rsoika
 * 
 */

@DeclareRoles({ "org.imixs.ACCESSLEVEL.NOACCESS", "org.imixs.ACCESSLEVEL.READERACCESS",
        "org.imixs.ACCESSLEVEL.AUTHORACCESS", "org.imixs.ACCESSLEVEL.EDITORACCESS",
        "org.imixs.ACCESSLEVEL.MANAGERACCESS" })
@RolesAllowed({ "org.imixs.ACCESSLEVEL.NOACCESS", "org.imixs.ACCESSLEVEL.READERACCESS",
        "org.imixs.ACCESSLEVEL.AUTHORACCESS", "org.imixs.ACCESSLEVEL.EDITORACCESS",
        "org.imixs.ACCESSLEVEL.MANAGERACCESS" })
@Singleton
@RunAs("org.imixs.ACCESSLEVEL.MANAGERACCESS")
public class ZahlungseingangService {
    
    public static final int EVENT_UPDATE_INVOICE = 200;
    public static final String ERROR_PAYMENT_BOOKING_FAILED = "PAYMENT_BOOKING_FAILED";
    public static final String ITEM_PAYMENT_DETAILS = "payment.details";

    @SuppressWarnings("unused")
    private static Logger logger = Logger.getLogger(ZahlungseingangService.class.getName());

    @Inject
    DocumentService documentService;

    @Inject
    WorkflowService workflowService;

    /**
     * Diese Method sucht eine Invoice mit Manager Rechnten
     * 
     * @throws PluginException
     */
    public ItemCollection findInvoice(String id) throws PluginException {

        return documentService.load(id);
    }

    /**
     * * Diese Method processed eine Invoice mit Manager Rechnten
     * 
     * @throws PluginException
     * @throws ModelException
     * @throws ProcessingErrorException
     * @throws AccessDeniedException
     */
    public ItemCollection processInvoice(ItemCollection invoice) throws PluginException {
        try {
            return workflowService.processWorkItem(invoice);
        } catch (AccessDeniedException | ProcessingErrorException | PluginException | ModelException e) {
            throw new PluginException(PluginException.class.getName(), ERROR_PAYMENT_BOOKING_FAILED,
                    "Der Rechnungs Saldo konnte nicht korrekt verbucht werden - " + e.getMessage(), e);
        }
    }

    /**
     * The method returns a List of ItemCollection holding the payment details
     * 
     * 
     * Convert the List of ItemCollections back into a List of Map elements
     * 
     * @param invoice
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List<ItemCollection> loadPaymentDetails(ItemCollection payment) {
        ArrayList<ItemCollection> childItems = new ArrayList<ItemCollection>();

        List<Map> mapOrderItems = payment.getItemValue(ITEM_PAYMENT_DETAILS);
        int pos = 1;
        for (Object mapOderItem : mapOrderItems) {

            if (mapOderItem instanceof Map) {
                ItemCollection itemCol = new ItemCollection((Map) mapOderItem);
                itemCol.replaceItemValue("numPos", pos);

                childItems.add(itemCol);
                pos++;
            }
        }
        return childItems;
    }

}
