/*******************************************************************************
 *  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.mahnlauf;

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.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 MahnlaufService wird vom MahnlaufExecuteAdapter 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 MahnlaufService {

	
	public static final int TASK_MAHNLAUF_START = 5100;
	public static final int TASK_MAHNLAUF_END = 5299;
	public static final int EVENT_MAHNLAUF_EXECUTE = 300;
	
	public static final String ITEM_DBTR_NUMBER = "dbtr.number";
	public static final String ITEM_DBTR_NAME = "dbtr.name";
	public static final String ITEM_DBTR_EMAIL = "dbtr.mail";
	public static final String ERROR_CONFIG = "CONFIG_ERROR";
	public static final String ERROR_WORKFLOW = "ERROR_WORKFLOW";

  
    @Inject
    WorkflowService workflowService; 

  

    /**
     * * 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_WORKFLOW,
                    "Die Rechnung konnte nicht verarbeitet werden - " + e.getMessage(), e);
        }
    }

}
