package wenrgise.workflow.utility;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import javax.ejb.CreateException;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.ServiceLocator;
import wenrgise.common.utility.UserInfo;
import wenrgise.workflow.bean.WflDocMstAttrDtlBean;
import wenrgise.workflow.core.WflDocumentInfo;
import wenrgise.workflow.ejb.facade.WorkFlowFacade;
import wenrgise.workflow.ejb.facade.WorkFlowFacadeHome;

public class AttributesHandler {
  public boolean checkAttributes(WflDocumentInfo wflDocInfo, UserInfo oUserInfo) throws RemoteException, CreateException, EnrgiseSystemException, EnrgiseApplicationException {
    WorkFlowFacadeHome oHome = (WorkFlowFacadeHome)ServiceLocator.getLocator().getService("WorkFlowFacade");
    WorkFlowFacade oWorkFlowFacade = oHome.create();
    long attrCount = oWorkFlowFacade.getWflDocTypeAttrDtlMetaInfo(wflDocInfo.getDocumentTypeId(), oUserInfo).getRecordCount();
    if (attrCount > 0L) {
      Set oSet = wflDocInfo.getDocumentAttributes().keySet();
      ArrayList docAttributes = oWorkFlowFacade.getWflDocTypeAttrDtlInfo(wflDocInfo.getDocumentTypeId(), 1L, attrCount, oUserInfo);
      Iterator oIt = docAttributes.iterator();
      while (oIt.hasNext()) {
        WflDocMstAttrDtlBean oWflDocMstAttrDtlBean = oIt.next();
        if (!oSet.contains(oWflDocMstAttrDtlBean.getTxtAttrName()))
          return false; 
      } 
    } 
    return true;
  }
}
