// Source File Name:   FldToolEquipment.java

package psdi.app.workorder;
import java.rmi.*;
import java.sql.*;

import com.dbms.dbcon.*;
import psdi.mbo.*;
import psdi.security.*;
import psdi.util.*;
public class FldWopm2 extends MboValueAdapter
{
  psdi.mbo.MboValue test;
  public FldWopm2(MboValue mbovalue)
      throws MXException
  {
    super(mbovalue);
  }

  public void validate()
      throws MXException, RemoteException
  {
    String sEqnum = getMboValue("ASSETNUM").getCurrentValue().asString();
    sEqnum =(sEqnum == null)?"":sEqnum.trim();
    String sLocation = getMboValue("LOCATION").getCurrentValue().asString();
    sLocation =(sLocation == null)?"":sLocation.trim();
    String sDesc = getMboValue("description").getCurrentValue().asString();
    sDesc =(sDesc == null)?"":sDesc.trim();
    //String sHdunit = getMboValue("hdunit").getCurrentValue().asString();
    //sHdunit =(sHdunit == null)?"":sHdunit.trim();
    if(sEqnum.length() == 0 && sLocation.length() == 0){
      throw new MXApplicationException("woreq","LocaEqNotCanNull");
    }
    if(sDesc.length() == 0){
      throw new MXApplicationException("woreq","WoreqDescNotCanNull");
    }
    String sWopm2 = getMboValue("WOPM2").getCurrentValue().asString();
    sWopm2 =(sWopm2 == null)?"":sWopm2.trim();
    String sWopm1 = getMboValue("WOPM1").getCurrentValue().asString();
    sWopm1 =(sWopm1 == null)?"":sWopm1.trim();
    if(!isWoreqSp(sWopm2,sWopm1)){
      //throw new MXApplicationException("woreq","WoreqSpCanDeptPm");
    }
    //if(sHdunit.length() == 0){
    //  throw new MXApplicationException("woreq","WoreqHdunitNotCanNull");
    //}
  }
  public void action()
      throws MXException, RemoteException
  {
     getMboValue("WO17").setValue("WOWAIT");
  }
  public void init()
      throws MXException
  {
    /**
    try
    {
      super.init();
      String sCurrSupper = getMboValue("supervisor").getCurrentValue().asString();
      sCurrSupper =(sCurrSupper == null)?"":sCurrSupper.trim();
      if(sCurrSupper.length() == 0){
        String laborCodeStr = getMboValue().getMbo().getUserInfo().getLaborCode();
        laborCodeStr =(laborCodeStr == null) ?"":laborCodeStr.trim();
        //getMboValue("supervisor").setValue(laborCodeStr);
      }
    }
    catch(Exception exception) { }
    */
  }

  private  boolean isWoreqSp(String sDeptpm,String sWoreqSp)
      throws MXException, RemoteException
  {
    boolean reIs = false;
    try
    {
      //查找本年月自动编号的值
      StringBuffer sqllocal = new StringBuffer(300);
      sqllocal.append("select DEPT1 ").append(" from ").append("DEPT").append(" where UPPER(TRIM(DEPTNUM)) ="+"\'"+sDeptpm+"\'");
      //取出连接关键字
      ConnectionKey connKey = getMboValue().getMbo().getUserInfo().getConnectionKey();
      //取出连接--测试成功
      Connection conn = getMboValue().getMbo().getMboServer().getDBConnection(connKey);
      //执行查询，返回一个结果集合
      QueryResult result = DBCon.runQuery(sqllocal.toString(),conn);
      if(result.size() >0){
        ResultRow resultRow = result.getRow(0);
        String sWoreqSpStr = resultRow.getValue("DEPT1");
        sWoreqSpStr =(sWoreqSpStr==null)?"":sWoreqSpStr.trim();
        if(sWoreqSpStr.indexOf(sWoreqSp,0) > -1) reIs = true;
      }
      return reIs;
    }
    catch(Exception exception) { }
    return reIs;
  }
}

