package kr.co.kihyun.beans.entity;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import kr.co.kihyun.beans.entity.ecross.IRemotePersistable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public abstract class AcceptanceLimitStrategy extends LongEntity implements IRemotePersistable {
    /**
     * 
     */
    private static final long serialVersionUID = -8462477835809004737L;

    @Persistent(nullValue = NullValue.EXCEPTION)
    private TotDocProcess process = TotDocProcess.REG;

    @Persistent(mappedBy = "acceptanceLimitStrategy")
    private TotDoc totDoc;

    @Persistent
    private Long limitCount;

    public AcceptanceLimitStrategy() {
    }

    public abstract List<TotReport> getCompReports(PersistenceManager pm, Map<String, Object> filterMap);

    public abstract TotDocProcess getProcess(PersistenceManager pm);

    public abstract String getDescription(PersistenceManager pm);

    public static AcceptanceLimitStrategy create(String strategyName, Long limitCount) throws IllegalArgumentException {
        
        //44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 : Update by YOUNGJUN,CHO
        /*
        try {
            return (AcceptanceLimitStrategy) Class.forName(
                    AcceptanceLimitStrategy.class.getPackage().getName() + "." + strategyName)
                    .getConstructor(Long.class).newInstance(limitCount);
        } catch (Exception e) {
            throw new IllegalArgumentException(strategyName+" does not exist.");
        }
        */
        
        AcceptanceLimitStrategy als = null;
                
        try {
            als = (AcceptanceLimitStrategy) Class.forName( AcceptanceLimitStrategy.class.getPackage().getName() + "." + strategyName )
                    .getConstructor(Long.class).newInstance(limitCount);
            
        } catch (ClassNotFoundException classNotFoundException) {
            classNotFoundException.printStackTrace();
        } catch (NoSuchMethodException noSuchMethodException) {
            noSuchMethodException.printStackTrace();
        } catch (SecurityException securityException) {
            securityException.printStackTrace();
        } catch (InstantiationException instantiationException) {
            instantiationException.printStackTrace();
        } catch (IllegalAccessException illegalAccessException) {
            illegalAccessException.printStackTrace();
        } catch (IllegalArgumentException illegalArgumentException) {
            illegalArgumentException.printStackTrace();
        } catch (InvocationTargetException invocationTargetException) {
            invocationTargetException.printStackTrace();
        }
        
        return als;
        //================================================
    }

    public void setTotDoc(TotDoc totDoc) {
        this.totDoc = totDoc;
    }

    public TotDoc getTotDoc() {
        return totDoc;
    }

    protected AcceptanceLimitStrategy(Long limitCount) {
        this.limitCount = limitCount;
    }

    public void setLimitCount(Long limitCount) {
        this.limitCount = limitCount;
    }

    public Long getLimitCount() {
        return limitCount;
    }

    public void setProcess(TotDocProcess process) {
        this.process = process;
    }

    public TotDocProcess getProcess() {
        return process;
    }

    public MDept getOwnerParty(PersistenceManager pm) {
        return this.getTotDoc().getDept(pm).getRootDept(pm);
    }

}
