/*
 * $Id: VKCEventAuditManager.java,v 1.4 2005/07/19 02:19:27 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 30.07.2004
 *
 * $Log: VKCEventAuditManager.java,v $
 * Revision 1.4  2005/07/19 02:19:27  marbobo
 * + Bugfix
 *
 * Revision 1.3  2004/11/10 15:02:51  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.2  2004/09/20 10:26:14  koppy
 * +javadoc Dokumentation
 *
 * Revision 1.1  2004/08/30 14:49:58  koppy
 * Initiale Version der Workflow-Komponente
 *
 */
package vkc.workflow.shark;

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

import org.enhydra.shark.api.RootException;
import org.enhydra.shark.api.SharkTransaction;
import org.enhydra.shark.api.internal.eventaudit.AssignmentEventAuditPersistenceInterface;
import org.enhydra.shark.api.internal.eventaudit.EventAuditException;
import org.enhydra.shark.api.internal.working.CallbackUtilities;
import org.enhydra.shark.eventaudit.DODSEventAuditManager;

import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCGroupSerializer;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.view.WebShare;
import vkc.view.bean.GroupInfo;
import vkc.workflow.model.ProcessInstanceInfo;
import vkc.workflow.model.WorkItemInfo;
import vkc.workflow.util.SharkUtils;
import vkc.workflow.util.WorkflowUtils;

/**
 * This class represents a custom event audit manager
 * for shark.
 * <p>
 * It is used for sending emails to the assignees of new workitems.
 * 
 * @author Andreas Koop
 */
public class VKCEventAuditManager extends DODSEventAuditManager{

    private CallbackUtilities cus = null;
    
    public void configure(CallbackUtilities cus) throws RootException {
        super.configure(cus);
        this.cus = cus;
        SharkUtils.initVKC(cus);
    }
    
    /**
     * This method is called every time an assigment is made
     * to a user or group. 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.eventaudit.EventAuditManagerInterface#persist(org.enhydra.shark.api.internal.eventaudit.AssignmentEventAuditPersistenceInterface, org.enhydra.shark.api.SharkTransaction)
     */
    public void persist(AssignmentEventAuditPersistenceInterface a,
            SharkTransaction t) throws EventAuditException {
        
        super.persist(a, t);
        
        final AssignmentEventAuditPersistenceInterface ai = a;
        try {
            // Is the send mail feature activated?
            VKCSysParameterSerializer.clearCache();
            if ( VKCSysParameterSerializer.retrieveValue("WORKFLOW", "SendMail", "0").equals("0")) {
                return;
            }
        } catch (DBException e) {
            cus.error( e.getMessage(), new RootException(e));
            return;
        }
        
        if( ai.getIsAccepted() )
            return;
        
            
        //Send an email
        
        try {
            final VKCUser user = VKCUserSerializer.retrieveUser(ai.getNewResourceUsername() );
            
            final List users = new ArrayList();
            if(user == null){
                GroupInfo group = VKCGroupSerializer.retrieveGroupInfo( ai.getNewResourceUsername() );
                Set userIds = VKCGroupSerializer.getGroupUserIds(group.getGroupId());
                for (Iterator iter = userIds.iterator(); iter.hasNext();) {
                    String userId = (String) iter.next();
                    users.add( VKCUserSerializer.retrieveUser( userId ));
                }
            }else{
                users.add(user);
            }
                
            
            Runnable r = new Runnable(){
                
                public void run(){
                    WorkItemInfo workitem = new WorkItemInfo();
                    workitem.setId(ai.getActivityId());
                    workitem.setName(ai.getActivityName());
                    ProcessInstanceInfo c= new ProcessInstanceInfo();
                    c.setName(ai.getProcessName());
                    workitem.setContainer( c );
                    
                    try {
                        WorkflowUtils.sendMail(users,workitem);
                    } catch (Exception e) {
                        WebShare.getTraceLogger().logStackTrace(e);
                    }
                }
            };
            
            new Thread(r).start();
            
        } catch (DBException e1) {
            cus.error( e1.getMessage(), new RootException(e1));
        }
        
    }
}
