0

我想从"FolderPath"列下的 excel 表中读取和获取我的文件夹路径,并在名为"Documentum"的父文件夹下创建一个文件夹结构列表。我已附上示例 excel 数据,如下所示。我可以通过硬编码要创建的文件夹路径从下面的代码创建一个文件夹。例如,在这种情况下它是:/documentum。

文件夹路径

/documentum/folder1

/documentum/folder2

/documentum/folder3

/documentum/folder8/folder9

**

请建议,如何在下面的代码中添加此功能。我尝试使用 excel POI 但没有运气,因为我在 POI 中工作不多,对以下代码的任何示例修改或建议都会有所帮助。

package com.documentum;

import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;
import com.documentum.fc.common.IDfLoginInfo;

import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;

import java.util.StringTokenizer;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;

public class AutoFolderStructure
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String username = "user";
        String password = "docu";
        String repoName = "documentum";
        String folderPath = "/documentum"; //folder path to be created

        IDfSessionManager sessMgr = null;
        IDfSession sess = null;
        try
        {
            sessMgr = createSessionManager();
            addIdentity(sessMgr,username,password,repoName);
            sess = sessMgr.getSession(repoName);
            IDfId newId = createFolder(sess,folderPath,null,null,null);
            System.out.println("Created folder: " + newId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if((sessMgr != null) && (sess != null))
            {
                sessMgr.release(sess);
            }
        }

    }



    public static IDfId createFolder(
       IDfSession sess,
       String path,
       String pathSep,
       String type,
       String basePath)
       throws DfException
    {
       boolean cabinetStillToBeProcessed = true;

       if (pathSep == null || (pathSep.length() == 0))
       {
          pathSep = "/";
       }

       if ((type == null) || (type.length() == 0))
       {
          type = "dm_folder";
       }

       IDfId currentId = null;

       StringBuffer bufFldrPath = new StringBuffer(48);
       if ((basePath != null) && (basePath.length() > 1))
       {
          currentId = getIdByPath(sess, basePath);
          if (!currentId.isNull())
          {
             //base path actually exists.
             bufFldrPath.append(basePath);
             cabinetStillToBeProcessed = false; //cabinet already processed due to base path.

             int basePathLen = basePath.length();            
             if(basePathLen < path.length())
             {
                path = path.substring(basePath.length());
             }            
          }

       }

       StringTokenizer tokFldrNames = new StringTokenizer(path, pathSep);

       if (cabinetStillToBeProcessed)
       {
          //Execution will come here only if basePath was not specified or
          //if specified basePath was not valid.
          String cabinetName = tokFldrNames.nextToken();
          StringBuffer cabQual = new StringBuffer(32);
          cabQual.append("dm_cabinet where object_name='").append(
             cabinetName).append(
             "'");

          currentId = sess.getIdByQualification(cabQual.toString());
          if (currentId.isNull())
          {
             //need to create cabinet.
             IDfFolder cab = (IDfFolder) sess.newObject("dm_cabinet");
             cab.setObjectName(cabinetName);
             cab.save();
             currentId = cab.getObjectId();            
          }
          bufFldrPath.append(pathSep).append(cabinetName);
       }
       //By this point the bufFldrPath will either have the cabinet path
       //or it will have the basePath in it. 

       //now create all folders beyond the cabinet or basePath.
       while(tokFldrNames.hasMoreTokens())
       {         
          String parentPath = bufFldrPath.toString();

          String fldrName = tokFldrNames.nextToken();
          bufFldrPath.append(pathSep).append(fldrName);
          //by this point the buffer should contain the new expected path

          currentId = getIdByPath(sess,bufFldrPath.toString());
          if(currentId.isNull())
          {
             //looks like the new folder in the path does not exist.
             IDfFolder newFldr = (IDfFolder) sess.newObject(type);
             newFldr.setObjectName(fldrName);
             newFldr.link(parentPath);
             newFldr.save();
             currentId = newFldr.getObjectId();            
          }
          //by this point currentId should point to next folder in path                           
       }//while(all folder names)

       return currentId;
    }


    public static IDfId getIdByPath(IDfSession sess, String path)
       throws DfException
    {       

       int pathSepIndex = path.lastIndexOf('/');
       if (pathSepIndex == -1)
       {
          return new DfId("000");
       }

       StringBuffer bufQual = new StringBuffer(32);
       if (pathSepIndex == 0)
       {
          //its a cabinet path 
          bufQual.append(" dm_cabinet where object_name='");
          bufQual.append(path.substring(1));
          bufQual.append("'");
       }
       else
       {
          bufQual.append(" dm_sysobject where FOLDER('");
          bufQual.append(path.substring(0, pathSepIndex));
          bufQual.append("') ");
          bufQual.append(" and object_name='");
          bufQual.append(path.substring(pathSepIndex + 1));
          bufQual.append("'");
       }

       String strQual = bufQual.toString();
       IDfId id = sess.getIdByQualification(strQual);
       return id;
    }

    private static IDfSessionManager createSessionManager() throws DfException
    {
        IDfClientX clientX = new DfClientX();
        IDfClient localClient = clientX.getLocalClient();
        IDfSessionManager sessMgr = localClient.newSessionManager();
        return sessMgr;
    }


    private static void addIdentity(IDfSessionManager sm, String username,
            String password, String repoName) throws DfException
    {
        IDfClientX clientX = new DfClientX();

        IDfLoginInfo li = clientX.getLoginInfo();
        li.setUser(username);
        li.setPassword(password);

        // check if session manager already has an identity.
        // if yes, remove it.
        if (sm.hasIdentity(repoName))
        {
            sm.clearIdentity(repoName);
        }

        sm.setIdentity(repoName, li);
    }

}

谢谢和问候德布

4

2 回答 2

0

您可以使用 Apache POI 库。如果您正在运行自己的脚本,则需要将其添加到类路径,如果您正在执行 documentum 作业(由 documentum JMS 执行),则需要将其添加到 java_methods 目录

于 2014-05-14T22:56:34.407 回答
0

我将在这里发布我使用 POI 的代码:

首先:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

您可以从我的示例中提取我是如何使用它的:

我用了:

列名 1 | 列名 2 | 列名 3 | 列名 4

数据在这里

代码:rowStart = 1;意味着我从数据行开始逐行处理。最后一列 = 4; 意味着我有 4 列。

修复你发现的代码。

// Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook;
        try
        {
            workbook = new XSSFWorkbook(file);

            // Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            int rowStart = 1;
            int rowEnd = sheet.getLastRowNum();

            for (int rowNum = rowStart; rowNum < rowEnd; rowNum++)
            {
                Row r = sheet.getRow(rowNum);

                int lastColumn = 4;
                int inputtype = 0;
                table_no = "";
                dql = "";
                description = "";
                table_code = "";
                code = "";

                for (int cn = 0; cn < lastColumn; cn++)
                {
                    Cell cell = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);

                    if (cell == null)
                    {
                        switch (inputtype)
                        {
                        case 0:
                            inputtype++;
                            code = "";
                            break;
                        case 1:
                            inputtype++;
                            table_no = "";
                            break;

                        case 2:
                            inputtype++;
                            table_code = "";
                            break;
                        case 3:
                            inputtype++;
                            description = "";
                            break;

                        default:
                            inputtype = 0;
                            break;
                        }
                    }
                    else
                    {
                        switch (inputtype)
                        {
                        case 0:
                            inputtype++;
                            if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                            {
                                code = cell.getStringCellValue();
                            }
                            else
                            {
                                tmp = cell.getNumericCellValue();
                                code = tmp.intValue() + "";
                            }
                            break;

                        case 1:
                            inputtype++;
                            if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                            {
                                table_no = cell.getStringCellValue();
                            }
                            else
                            {
                                tmp = cell.getNumericCellValue();
                                table_no = tmp.intValue() + "";
                            }
                            break;

                        case 2:
                            inputtype++;
                            if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                            {
                                table_code = cell.getStringCellValue().trim();
                            }
                            else
                            {
                                tmp = cell.getNumericCellValue();

                                table_code = tmp.intValue() + "";
                            }
                            break;
                        case 3:
                            inputtype++;
                            if (cell.getCellType() == Cell.CELL_TYPE_STRING)
                            {
                                description = cell.getStringCellValue();
                            }
                            else
                            {
                                tmp = cell.getNumericCellValue();

                                description = tmp.intValue() + "";
                            }
                            break;
                        default:
                            inputtype = 0;
                            break;
                        }
                    }
                }
于 2014-10-07T08:38:37.467 回答