// Source File Name:   FldToolEquipment.java

package psdi.app.woreqck;
import java.rmi.*;

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

  public void validate()
      throws MXException, RemoteException
  {
      String sHdtraspart = getMboValue().getCurrentValue().asString();
      sHdtraspart =(sHdtraspart == null)?"":sHdtraspart.trim();
      String sWo8 = getMboValue("WO8").getCurrentValue().asString();
      sWo8 = (sWo8 == null) ?"":sWo8.trim();
      if(sWo8.indexOf(sHdtraspart,0) > -1){
        throw new MXApplicationException("woreq","RepeatTransferSp");
      }
  }

  public void action()
      throws MXException, RemoteException
  {
     String sWo8 = getMboValue("WO8").getCurrentValue().asString();
     String preDept = getMboValue("hdtraspart").getPreviousValue().asString();
     preDept = (preDept == null) ?"":preDept.trim();
     String taskDept = getMboValue().getCurrentValue().asString();
     if(preDept.length() > 0){
       getMboValue("WO8").setValue(sWo8 +","+ taskDept);
     }else{
       String preTaskDept = getMboValue("Wopm2").getCurrentValue().asString();
       getMboValue("WO8").setValue(preTaskDept +","+ taskDept);
     }
     String projDept = getMboValue("hdtraspart").getCurrentValue().asString();
     projDept =(projDept == null) ?"":projDept.trim();
     if(projDept.length() > 0){
       String tWopm1 = getWopm1(projDept);
       getMboValue("WOPM1").setValue(tWopm1);
       getMboValue("WOPM2").setValue(projDept);
       String projMng = getMboValue("supervisor").getCurrentValue().asString();
       String preProjMng = getMboValue("hdtrasproj").getCurrentValue().asString();
       preProjMng =(preProjMng == null) ?"":preProjMng.trim();
       if(projMng.length() > 0){
         projMng = (preProjMng.length() > 0)?preProjMng+","+projMng:projMng;
         getMboValue("hdtrasproj").setValue(projMng);
       }
       getMboValue("supervisor").setValue("");
     }
  }
  private  String getWopm1(String sDeptpm)
      throws MXException, RemoteException
  {
    String sWopm1 = "";
    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();
        int iStart = 0;
        int iStop = sWoreqSpStr.indexOf(",",iStart);
        if(iStop <= 0) iStop = sWoreqSpStr.length();
        sWopm1 = sWoreqSpStr.substring(iStart,iStop);
        sWopm1 =(sWopm1 == null)?"":sWopm1.trim();
      }
      return sWopm1;
    }
    catch(Exception exception) { }
    return "";
  }

}
