星期五, 9月 07, 2018

[JavaScript] 公用函示庫xLibPub

function getDbSetClassDb(tkey: String): NotesDatabse {

    var view: NotesView = database.getView("(vwDbSetKey)");
    var setDoc: NotesDocument = view.getDocumentByKey(tkey, true);
    if (setDoc == null) {
        return null;
    } else {
        var DbSetDb: NotesDatabase = session.getDatabase(setDoc.getItemValueString("ServerName"), setDoc.getItemValueString("DataBaseName"));
        return DbSetDb;
    }
}


function setLockDoc(tDoc: NotesDocument, tUser: String): String {

    var LockDB: NotesDatabase = getDbSetClassDb("LockDB");
    var vDocLock: NotesView = LockDB.getView("(vDocLock)");
    var LockDoc: NotesDocument = vDocLock.getDocumentByKey(tDoc.getItemValueString("DocNo"), true);

    if (LockDoc == null) {
        var LockDoc: NotesDocument = vDocLock.getDocumentByKey(tDoc.getItemValueString("DocID"), true);
    }

    if (LockDoc == null) {
        var LockDoc: NotesDocument = LockDB.createDocument();

        LockDoc.replaceItemValue("Form", "fDocLock");
        LockDoc.replaceItemValue("DocUNID", tDoc.getItemValueString("DocID"));
        LockDoc.replaceItemValue("DocNo", tDoc.getItemValueString("DocNo"));
        LockDoc.replaceItemValue("LockBy", tUser);
        LockDoc.replaceItemValue("LockAt", session.createDateTime(@Now()));
        LockDoc.replaceItemValue("LockKey", @UserName());
        LockDoc.replaceItemValue("DocEditors", "*");
        LockDoc.replaceItemValue("NotesService", tDoc.getParentDatabase().getServer());
        LockDoc.replaceItemValue("NotesDB", tDoc.getParentDatabase().getFilePath());
        LockDoc.save(true);
        return "";

    } else {

        if (LockDoc.getItemValueString("LockKey") == @UserName()) {
            return "";
        } else {
            return "目前文件已被鎖定,鎖定人員:" + "\\n" + LockDoc.getItemValueString("LockBy") + "  " + LockDoc.getItemValueDateTimeArray("LockAt").toString();
        }
    }

}


function setReleaseDoc(tDoc: NotesDocument): String {

    var LockDB: NotesDatabase = getDbSetClassDb("LockDB");
    var vDocLock: NotesView = LockDB.getView("(vDocLock)");
    var DCLockDoc: NotesDocumentCollection = vDocLock.getAllDocumentsByKey(tDoc.getItemValueString("DocNo"), true);

    if (DCLockDoc.getCount() == 0) {
        var DCLockDoc: NotesDocumentCollection = vDocLock.getAllDocumentsByKey(tDoc.getItemValueString("DocID"), true);
    }

    if (DCLockDoc.getCount() != 0) {

        var LockDoc: NotesDocument = DCLockDoc.getFirstDocument();
        if (LockDoc.getItemValueString("LockKey") == @UserName()) {

            while (LockDoc != null) {
                var tmpdoc = DCLockDoc.getNextDocument(LockDoc);
                LockDoc.remove(true);
                LockDoc.recycle();
                LockDoc = tmpdoc;
            }
        }
    }
}


function setReleaseDoc02(): String {
//print("-------------------------------------------------");
//print(@UserName());

    var LockDB: NotesDatabase = getDbSetClassDb("LockDB");
print(LockDB.getCurrentAccessLevel());
if (LockDB!=null){
    var vDocLock: NotesView = LockDB.getView("(vDocLock02)");

    var DCLockDoc: NotesDocumentCollection = vDocLock.getAllDocumentsByKey(@UserName(), true);

    if (DCLockDoc.getCount() != 0) {

        var LockDoc: NotesDocument = DCLockDoc.getFirstDocument();

        while (LockDoc != null) {
            var tmpdoc = DCLockDoc.getNextDocument(LockDoc);
            LockDoc.remove(true);
            LockDoc.recycle();
            LockDoc = tmpdoc;
        }

    }
}
}

function CounterSign(){
print("CounterSign");
var tmpdoc = database.createDocument();   //建立暫存文件
tmpdoc.replaceItemValue("Form","fTempSign");
print(currdoc.getItemValueString("InstanceOS"));
tmpdoc.replaceItemValue("DocNo",currdoc.getItemValueString("InstanceOS"));
tmpdoc.replaceItemValue("SignUser",@Name("[CN]",session.getEffectiveUserName()));
//tmpdoc.replaceItemValue("APPROVALALT",getComponent("ChoiceOSValue").getValue());
tmpdoc.replaceItemValue("APPROVALALT","會簽請求");
//print(getComponent("Comment").getValue());  // For Debug
//tmpdoc.replaceItemValue("SignComment",currdoc.getItemValueString("SignComment"));
tmpdoc.replaceItemValue("SignComment",getComponent("Comment").getValue());
var agentName:String="XpageSign";
print(currdoc.getItemValueString("BinderDocIDOS"));
tmpdoc.replaceItemValue("MDocUnid",currdoc.getItemValueString("BinderDocIDOS"));
//tmpdoc.replaceItemValue("DeleteFlag","1");
tmpdoc.save();
print("tmpdoc.save");
var NoteID:string = tmpdoc.getNoteID();
tmpdoc.recycle();
print("doc.recycle()");
//Delete tmpdoc   '此行若不加的話,後續RunOnServer 後會無法再次取得tmpdoc
var agent:NotesAgent = database.getAgent(agentName);
if(agent.runOnServer(NoteID) == 0){
var tmpdoc:NotesDocument = database.getDocumentByID(NoteID);
if (tmpdoc!=null){
if (tmpdoc.getItemValueString('IsNoWrite')== "1"){
view.postScript("alert('沒有選擇任何決定');");
}else{
view.postScript("top.location.replace('HomePage.xsp');");
//context.redirectToHome();
print("WEB會簽完成");
return true;
  }
}
else {print("tmpdoc=null");}
}else{
view.postScript("alert(agent.Name + '程式無法執行完成,請檢查,謝謝!!');");
}
}

function ModelSelect(){
print(getComponent("CurDocID").getValue());
var doc:Notesdocument= viewPanel1.getDocument();
if (getComponent("CurDocID").getValue() != ""){  //有紀錄原始文件ID
var currdoc:Notesdocument=database.getDocumentByUNID(getComponent("CurDocID").getValue());
}
else{
var DocID:String = sessionScope.get("BinderDocIDOS");
print("get SessionScope:" + DocID);
var currdoc:Notesdocument=database.getDocumentByUNID(DocID);
}
if (currdoc == null) {
print("currdoc == null");
}
else{
currdoc.replaceItemValue("ModalID",doc.getItemValueString("ModalID"));
currdoc.replaceItemValue("ModalName",doc.getItemValueString("ModalName"));
currdoc.replaceItemValue("SystemCategory",doc.getItemValueString("SystemCategory"));
currdoc.replaceItemValue("ModalLeader",doc.getItemValueString("ModalLeader"));
currdoc.replaceItemValue("ModalLeaderID",doc.getItemValueString("ModalLeaderID"));
currdoc.replaceItemValue("SANotesID",doc.getItemValueString("ModelOwnerID"));
currdoc.replaceItemValue("SAEmpName",doc.getItemValueString("ModelOwner"));
currdoc.save();
print("currdoc.save()");
}
view.postScript("window.parent.$('.modal').modal('hide');");
view.postScript("parent.location.reload();");
}

function SASelect(){
print(getComponent("CurDocID").getValue());
var doc:Notesdocument= viewPanel1.getDocument();  //人員文件
if (getComponent("CurDocID").getValue() != ""){  //有紀錄原始文件ID
var currdoc:Notesdocument=database.getDocumentByUNID(getComponent("CurDocID").getValue());
}
else{
var DocID:String = sessionScope.get("BinderDocIDOS");
print("get SessionScope:" + DocID);
var currdoc:Notesdocument=database.getDocumentByUNID(DocID);
}
if (currdoc == null) {
print("currdoc == null");
}
else{
currdoc.replaceItemValue("SAEmpCode",doc.getItemValueString("EmpCode"));
currdoc.replaceItemValue("SAEmpName",doc.getItemValueString("EmpCName"));
currdoc.replaceItemValue("SANotesID",doc.getItemValueString("EmpNotesID"));
currdoc.save();
print("currdoc.save()");
}
view.postScript("window.parent.$('.modal').modal('hide');");
view.postScript("parent.location.reload();")
}

function WindowSign(){
//回寫 類別 模組 SA
var errMsg = "";
print("Model: " + getComponent("comboBoxModel").getValue());
//print("SA: " + getComponent("comboBoxSA").getValue());
try {
//var doc:Notesdocument=database.getDocumentByID(docIDArray[0]);
print(getComponent("CurDocID").getValue());
var EmpName = @Right(getComponent("comboBoxSA2").getValue(),"\\");  //中文姓名
print("SA: " + EmpName);
if (getComponent("CurDocID").getValue() != ""){  //有紀錄原始文件ID
var currdoc:Notesdocument=database.getDocumentByUNID(getComponent("CurDocID").getValue());
if (currdoc == null) {
print("currdoc == null");
return false;
}
else{
var mView:NotesView = database.getView("vMISModalbyName");
if (mView == null){
print("mView == null");
return false;
}
var doc:NotesDocument = mView.getDocumentByKey(getComponent("comboBoxModel").getValue());
if (doc == null){
print("Model Doc == null");
return false;
}
currdoc.replaceItemValue("ModalID",doc.getItemValueString("ModalID"));
//currdoc.replaceItemValue("ModalName",doc.getItemValueString("ModalName"));
currdoc.replaceItemValue("SystemCategory",doc.getItemValueString("SystemCategory"));
currdoc.replaceItemValue("ModalLeader",doc.getItemValueString("ModalLeader"));
currdoc.replaceItemValue("ModalLeaderID",doc.getItemValueString("ModalLeaderID"));

currdoc.replaceItemValue("RequestType",getComponent("comboBoxType").getValue());
currdoc.replaceItemValue("ModalName",getComponent("comboBoxModel").getValue());
currdoc.replaceItemValue("SAEmpName",EmpName);
var empDB:NotesDatabase = session.getDatabase("","GroupSystem\\GroupEmpHR.nsf");
    if (empDB == null){
        print("empDB == null");
       return false;
   
    //var macro = "Select Form ='fmEmp' & EmpCName ='" + getComponent("comboBoxSA").getValue() + "'";
    var macro = "Select Form ='fmEmp' & EmpCName ='" + EmpName + "'";
    print(macro);
    var empDC:NotesDocumentCollection = empDB.search(macro);
    //var empdoc:Notesdocument = empDB.getDocumentByID(docIDArray[0]);  // :Notesdocument
if (empDC.getCount() > 0){
var empdoc:Notesdocument = empDC.getFirstDocument();
currdoc.replaceItemValue("SAEmpCode",empdoc.getItemValueString("EmpCode"));
//currdoc.replaceItemValue("SAEmpName",empdoc.getItemValueString("EmpCName"));
currdoc.replaceItemValue("SANotesID",empdoc.getItemValueString("EmpNotesID"));
}
else{
print("empDOC == null");
    return false;
}
currdoc.save();
print("currdoc.save()");
//*/ 以下為簽閱的CODE
var tmpdoc = database.createDocument();   //建立暫存文件
tmpdoc.replaceItemValue("Form","fTempSign");
print(currdoc.getItemValueString("InstanceOS"));
tmpdoc.replaceItemValue("DocNo",currdoc.getItemValueString("InstanceOS"));
tmpdoc.replaceItemValue("SignUser",@Name("[CN]",session.getEffectiveUserName()));
tmpdoc.replaceItemValue("APPROVALALT","簽閱"); tmpdoc.replaceItemValue("SignComment",getComponent("SignComment").getValue());
var agentName:String="XpageSign";
tmpdoc.replaceItemValue("MDocUnid",currdoc.getItemValueString("BinderDocIDOS"));
//tmpdoc.replaceItemValue("DeleteFlag","1");
tmpdoc.save();
var NoteID:string = tmpdoc.getNoteID();
tmpdoc.recycle();
//Delete tmpdoc   '此行若不加的話,後續RunOnServer 後會無法再次取得tmpdoc
var agent:NotesAgent = database.getAgent(agentName);
if(agent.runOnServer(NoteID) == 0){
var tmpdoc:NotesDocument = database.getDocumentByID(NoteID);
if (tmpdoc!=null){
if (tmpdoc.getItemValueString('IsNoWrite')== "1"){
view.postScript("alert('沒有選擇任何決定');");
}else{
//view.postScript("top.location.replace('HomePage.xsp');");
//context.redirectToHome();
  }
}
else {print("tmpdoc=null");}
}else{
view.postScript("alert(agent.Name + '程式無法執行完成,請檢查,謝謝!!');");
}
//*/
}

}
    //view.postScript("window.parent.$('.modal').modal('hide');");
//view.postScript("parent.location.reload();");
view.postScript("top.location.replace('HomePage.xsp');");
} catch (e) {
print("Error:" + e.toString());
getComponent("inputText1").setValue(e.toString());
 
}
}

function DbSetClass(strDbName) {
  this.DbName = strDbName;
  this.GetSetDb = function(){
  var viewSS: NotesView = database.getView("(vwDbSetKey)");
  var setDoc: NotesDocument = viewSS.getDocumentByKey(this.DbName, true);
  if (setDoc == null) {
  view.postScript("alert('資料庫設定文件 ("+  this.DbName  +") 不存在');")
  }else{
  if (setDoc.getItemValueString("ServerName")==""){
  var strServer = database.getServer();
  }else{
  var strServer = setDoc.getItemValueString("ServerName");
  }
 
  if (setDoc.getItemValueString("DatabaseName")==""){
  var strDatabase = database.getFilePath();
  }else{
  var strDatabase = setDoc.getItemValueString("DatabaseName");
  }
  var DbSetDb: NotesDatabase = session.getDatabase(strServer, strDatabase);
  return DbSetDb;
  }
  };
}

沒有留言: