站長留言

  • ✅ 本站維護及更新歷史紀錄,詳情請參考公告
  • ✅ 有任何意見、想法,歡迎留言給Spicy知道喔
  • ✅ 固定於每周一至周五更新Blogger文章,周末不定期
程式AlfrescoBonita BPM

【Bonita BPM】Document 檔案上傳到Alfresco,並取得URL

tags: Bonita BPM

COMMON 基本需求

  1. 使用方法:OpenCMIS
    • CMIS:Content Management Interoperability Services, Java 類庫、框架和工具
  2. 下載需要的Jar檔
  3. 程式碼分為2部分
    • Groovy Scripts

    • Connectors 的 Scripts

程式碼

Connectors 的 Scripts

  1. serialDocumentReference的設定
  2. Document 請參考:
  3. Local variables:equipmentId
  4. Parameters:alfrescoHost, alfrescoUsername, alfrescoPassword, alfrescoFolderPath
Document document = apiAccessor.getProcessAPI().getDocumentAtProcessInstantiation(processInstanceId, "serialDocumentReference")
String contentFilename = document.getContentFileName() //取得完整檔名(包含副檔名e.g. test.jpg)
String filename = contentFilename.substring(contentFilename.lastIndexOf(".")) //取得副檔名
String contentStorageId = document.getContentStorageId()
byte[] file = apiAccessor.getProcessAPI().getDocumentContent(contentStorageId)

UploadToAlfresco.uploadFile(file,equipmentId,filename,alfrescoHost,alfrescoUsername,alfrescoPassword,alfrescoFolderPath)

Groovy Scripts

1. OpenCMIS提供可以使用Path的方法取得指定的檔案

Path 根目錄
/ alfresco根目錄
/Shared 分享資料夾的根目錄
/User Homes/使用者名稱 各別使用者的根目錄
/Sites/網站名稱/documentLibrary 各別網站儲存庫根目錄

2. 上傳功能

static void uploadFile(byte[] file, String equipmentId, String filename, String alfrescoHost, String alfrescoUsername, String alfrescoPassword, String alfrescoFolderPath){
    String AlfrescoHostName = alfrescoHost;
    String USERNAME = alfrescoUsername; //Alfresco使用者帳密
    String PASSWORD = alfrescoPassword;

    //======== connection alfresco by openCMIS ========
    Map<String, String> sessionParameters = new HashMap<String, String>();
    sessionParameters.put(SessionParameter.USER, USERNAME);
    sessionParameters.put(SessionParameter.PASSWORD, PASSWORD);
    sessionParameters.put(SessionParameter.ATOMPUB_URL, AlfrescoHostName + "alfresco/api/-default-/public/cmis/versions/1.1/atom");
    sessionParameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
    SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
    Session lSession = sessionFactory.getRepositories(sessionParameters).get(0).createSession();

    //使用者根目錄(My files)
    Folder root = (Folder) lSession.getObjectByPath(alfrescoFolderPath);

    //== InputStream Get ==
    InputStream stream = new ByteArrayInputStream(file);

    //== properities ==
    Map<String, Object> lProperties = new HashMap<String, Object>();
    StringBuilder nameSB = new StringBuilder(); //拼出完整檔名
    nameSB.append("test");
    nameSB.append(equipmentId); //用id創造唯一檔名
    nameSB.append(".");
    nameSB.append(filename) //副檔名
    String name = nameSB.toString();
    String mimeType = "image/png, image/jpeg";
    lProperties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
    lProperties.put(PropertyIds.NAME, name);

    ContentStream contentStream = new ContentStreamImpl(name, new BigInteger("" + file.length), mimeType, stream);
    Document upFile =  root.createDocument(lProperties, contentStream, null);

    //下載code加入的位置
}

3. 取得URL:直接加在上傳的code

  • 回傳值型態:void要改成String
  • 要寫return
//回傳URL,void要改成String
StringBuilder sb = new StringBuilder()
sb.append(alfrescoHost)
sb.append("share/page/document-details?nodeRef=workspace://SpacesStore/")
sb.append(upFile.getId())
return sb.toString()

Reference 參考資料

  1. 官方文件:http://docs.alfresco.com/community/concepts/ws-overview.html
  2. 官方文件:http://docs.alfresco.com/community/tasks/ws-tutorials.html
  3. OpenCMIS:http://chemistry.apache.org
  4. MIME types:
    https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

沒有留言:

張貼留言

本網站建議使用電腦或平板瀏覽