iT邦幫忙

2021 iThome 鐵人賽

DAY 26
0
自我挑戰組

從Agile PLM 移轉 Aras PLM大小事系列 第 26

26.移轉Aras PLM 大小事-打包壓縮檔案並下載

如果需要將相關的檔案壓縮Zip檔
可以利用外部元件先打包好,然後儲存到資料夾或Aras的Vault
1.儲存到其他資料夾,就要回傳給Javascript檔案路徑
2.儲存到Vault,就要給Vault URL

我這裡選用第一種方式
先在前端按鈕Click執行下段程式碼

var inn = new Innovator();

var body = "<id>"+document.thisItem.getProperty("id")+"</id>";
var result = inn.applyMethod("JPC_Zip_RFQ_AllFile",body);

if(result.isError()==false){
    // var fileId = itmFile.getProperty("fileId","");
    // var fileItem = inn.newItem("File","get");
    // fileItem.setProperty("id",fileId);
    // fileItem = fileItem.apply();
    // if (fileItem.isError()) {
    // top.aras.AlertError("File Not Found: " +fileItem.getErrorString());
    // return;
    // }
    // var fileName=fileItem.getProperty("filename");
    // // Get the URL to the file and download it
    // var fileURL = top.aras.getFileURLEx(fileItem.node);
    // var results = top.aras.vault.downloadFile(fileURL);
    // if (!results) {
    // top.aras.AlertError("Unable to retrieve the file ("+fileName+") from the vault");
    // return;
    // }   
    //開啟連結
    window.open(result.getResult());
}

後端打包JPC_Zip_RFQ_AllFile

    inn = this.getInnovator();
    return main(this);
}
private void Log(string msg)
{
    Item log = inn.newItem("JPC_Method_Log", "add");
    log.setProperty("jpc_run_method", "JPC_Zip_RFQ_AllFile");
    log.setProperty("jpc_method_event", "action");
    log.setProperty("jpc_log", msg);
    log = log.apply();
}
private Item GetAllFileData(string rfw_id){
    string aml = @"<AML>
                    <Item action='get' type='JPC RFQ Flow' select='id,item_number'>
                        <id>{0}</id>
                        <Relationships>
                        <Item action='get' type='JPC RFQ Flow RFQ' select='related_id(id,item_number)'>
                            <related_id>
                            <Item type='JPC RFQ' action='get' select='id,item_number,jpc_rfq_note'>
                                <Relationships>
                                <Item action='get' type='JPC RFQ Work' select='jpc_part(id,name),jpc_cad(id,name),jpc_doc(id,filename)'>
                                    <jpc_part>
                                    <Item type='Part' action='get' select='id,item_number'>
                                        <Relationships>
                                        <Item action='get' type='Part File' select='related_id(id,filename)'>
                                        </Item>
                                        <Item action='get' type='Part CAD' select='related_id(id,item_number,name)'>
                                            <related_id>
                                            <Item type='CAD' action='get' select='id'>
                                                <Relationships>
                                                <Item action='get' type='cad files' select='related_id(id,filename)'>
                                                </Item>
                                                </Relationships>
                                            </Item>
                                            </related_id>
                                        </Item>
                                        <Item action='get' type='Part Document' select='related_id(id,item_number,name)'>
                                            <related_id>
                                            <Item type='Document' action='get' select='id'>
                                                <Relationships>
                                                <Item action='get' type='Document File' select='related_id(id,filename)'>
                                                </Item>
                                                </Relationships>
                                            </Item>
                                            </related_id>
                                        </Item>
                                        </Relationships>
                                    </Item>
                                    </jpc_part>
                                    <jpc_cad>
                                    <Item type='CAD' action='get' select='id,item_number'>
                                        <Relationships>
                                        <Item action='get' type='cad files' select='related_id(id,filename)'>
                                        </Item>
                                        </Relationships>
                                    </Item>
                                    </jpc_cad>
                                    <jpc_doc>
                                    <Item type='File' action='get' select='id,filename'>
                                    </Item>
                                    </jpc_doc>
                                </Item>
                                </Relationships>
                            </Item>
                            </related_id>
                        </Item>
                        </Relationships>
                    </Item>
                    </AML>";
    aml = string.Format(aml,rfw_id);
    return inn.applyAML(aml);
}
private void CreateDirectoryFolder(string dir,string folder_name){
    if (!Directory.Exists(dir+folder_name))
        Directory.CreateDirectory(dir+folder_name);
}
private string CreateZip(string dir,string folder_name,string comment){
    using (Ionic.Zip.ZipFile zip2 = new Ionic.Zip.ZipFile())
    {
        zip2.AddDirectory(dir+@"/"+folder_name, System.IO.Path.GetFileName(dir+@"/"+folder_name));
        zip2.Comment = comment;
        zip2.Save( dir+"/"+folder_name+".zip");               
    }
    return folder_name+".zip";
}
private Innovator inn;
private string error_log="";
private string baseFolder = @"C:\Program Files (x86)\Aras\Innovator\Innovator\Client\Files\RFQ\";
public Item main(Item thisItem){
    string rfw_id = thisItem.getProperty("id","");
    //rfw_id = "75401095B8EC4F78BB016C4B02BDF823";
    if(rfw_id=="") return inn.newError("RFW ID = null");

    Item itmRFW = GetAllFileData(rfw_id);
    string rfw_number = itmRFW.getProperty("item_number","");
    CreateDirectoryFolder(baseFolder,rfw_number);
    Item itmRFWRs = itmRFW.getRelationships("JPC RFQ Flow RFQ");
    for(int rfw_r = 0; rfw_r < itmRFWRs.getItemCount();rfw_r++)
    {
        Item itmRFQ = itmRFWRs.getItemByIndex(rfw_r).getRelatedItem();
        //return inn.newResult(itmRFQ.getProperty("item_number"));
        string item_number_RFQ = itmRFQ.getProperty("item_number","");
        string jpc_rfq_note = itmRFQ.getProperty("jpc_rfq_note","");
        string item_number_RFQ_Dir = rfw_number +@"/"+item_number_RFQ+"_"+jpc_rfq_note;
        CreateDirectoryFolder(baseFolder,item_number_RFQ_Dir);

        Item itmRFQWorks = itmRFQ.getRelationships("JPC RFQ Work");
        for(int rfq_w = 0; rfq_w < itmRFQWorks.getItemCount();rfq_w++)
        {
            Item itmWork = itmRFQWorks.getItemByIndex(rfq_w);
            string jpc_part = itmWork.getProperty("jpc_part","");
            string jpc_doc = itmWork.getProperty("jpc_doc","");
            string jpc_cad = itmWork.getProperty("jpc_cad","");
            if(jpc_part!=""){
                Item itmPart = itmWork.getPropertyItem("jpc_part");
                string part_number = itmPart.getProperty("item_number","");
                string part_Dir = item_number_RFQ_Dir + @"\" + part_number;
                CreateDirectoryFolder(baseFolder,part_Dir);
                
                Item itmPartFiles = itmPart.getRelationships("Part File");
                if(itmPartFiles.getItemCount()>0){
                    for(int indexPF=0;indexPF<itmPartFiles.getItemCount();indexPF++)
                    {
                        Item itmPartFile = itmPartFiles.getItemByIndex(indexPF).getRelatedItem();
                        itmPartFile.checkout(baseFolder+part_Dir);
                    }
                }

                Item itmPartDocument = itmPart.getRelationships("Part Document");
                if(itmPartDocument.getItemCount()>0){
                    for(int indexPF=0;indexPF<itmPartDocument.getItemCount();indexPF++)
                    {
                        Item itmDoc = itmPartDocument.getItemByIndex(indexPF).getRelatedItem();
                        Item itmDocFiles = itmDoc.getRelationships("Document File");
                        for(int indexDF=0;indexDF<itmDocFiles.getItemCount();indexDF++){
                            Item itmFile = itmDocFiles.getItemByIndex(indexDF).getRelatedItem();
                            itmFile.checkout(baseFolder+part_Dir);
                        }
                    }
                }
                Item itmPartCAD = itmPart.getRelationships("Part CAD");
                if(itmPartCAD.getItemCount()>0){
                    for(int indexPF=0;indexPF<itmPartCAD.getItemCount();indexPF++)
                    {
                        Item itmCAD = itmPartCAD.getItemByIndex(indexPF).getRelatedItem();
                        Item itmCADFiles = itmCAD.getRelationships("cad files");
                        for(int indexDF=0;indexDF<itmCADFiles.getItemCount();indexDF++){
                            Item itmFile = itmCADFiles.getItemByIndex(indexDF).getRelatedItem();
                            itmFile.checkout(baseFolder+part_Dir);
                        }
                    }
                }
            }
            if(jpc_doc!=""){
                string other_Dir = item_number_RFQ_Dir + @"\" + "Cost Analytics";
                CreateDirectoryFolder(baseFolder,other_Dir);
                Item itmFile = inn.newItem("File","get");
                itmFile.setID(jpc_doc);
                itmFile = itmFile.apply();
                itmFile.checkout(baseFolder+other_Dir);
            }
            if(jpc_cad!=""){
                 Item itmCAD = itmWork.getPropertyItem("jpc_cad");
                string cad_number = itmCAD.getProperty("item_number","");
                string cad_dir = item_number_RFQ_Dir + @"\" + cad_number;
                CreateDirectoryFolder(baseFolder,cad_dir);

                Item itmCADFiles = itmCAD.getRelationships("cad files");
                for(int indexDF=0;indexDF<itmCADFiles.getItemCount();indexDF++){
                    Item itmFile = itmCADFiles.getItemByIndex(indexDF).getRelatedItem();
                    itmFile.checkout(baseFolder+cad_dir);
                }
            }
        }
    }
    Item itmUser = inn.getItemById("User",inn.getUserID());
    Item itmHistory = inn.newItem("JPC RFQ Flow Download History","add");
    itmHistory.setProperty("source_id",rfw_id);
    itmHistory = itmHistory.apply();
    string fileZip = CreateZip(baseFolder,rfw_number,itmUser.getProperty("keyed_name",""));
    System.IO.Directory.Delete(baseFolder+@"/"+rfw_number, true);

    string url = @"http://10.1.6.62/plm/Client/X-salt=8_11.0.0.6920-X/Files/RFQ/"+fileZip;
    return inn.newResult(url);

上一篇
25.移轉 Aras PLM大小事-流程簽核動態指派(4)
下一篇
27. 移轉 Aras PLM大小事 - 額外編碼取號
系列文
從Agile PLM 移轉 Aras PLM大小事30

尚未有邦友留言

立即登入留言