Pergunta

I need help extracting all the file names within a folder in Business Objects. With the code I have now I can get the name of a single file within a folder. I want to get all the file names in that folder. The variable temp is where I enter the name of the file that I want. The variable doc is where the file name is retrieved, and the variable reportname is where I store doc to write to an external spreadsheet.

public class variable {

    private static String expressionEx;
    private static String nameEx;
    private static String nameXE[] = new String[11];
    private static String expressionXE[] = new String[11];
    private static String query;
    public static String reportName;
    private static String reportPath;



    /** This method writes data to new excel file * */
    public static void writeDataToExcelFile(String fileName) {
        String[][] excelData = preapreDataToWriteToExcel();// Creates first
                                                            // sheet in Excel
        String[][] excelData1 = preapreDataToWriteToExcel1();// Creates
                                                                // second
                                                                // sheet in
                                                                // Excel
        String[][] excelData2 = preapreDataToWriteToExcel2();// Creates third
                                                                // sheet in
                                                                // Excel
        HSSFWorkbook myReports = new HSSFWorkbook();

        HSSFSheet mySheet = myReports.createSheet("Variables");
        HSSFSheet mySheet1 = myReports.createSheet("SQL Statement");
        HSSFSheet mySheet2 = myReports.createSheet("File Info");
        // edits first sheet
        mySheet.setFitToPage(true);
        mySheet.setHorizontallyCenter(true);
        mySheet.setColumnWidth(0, 800 * 6);
        mySheet.setColumnWidth(1, 7000 * 6);
        // edits second sheet
        mySheet1.setFitToPage(true);
        mySheet1.setHorizontallyCenter(true);
        mySheet1.setColumnWidth(0, 800 * 6);
        mySheet1.setColumnWidth(1, 7000 * 6);
        // edits third sheet
        mySheet2.setFitToPage(true);
        mySheet2.setHorizontallyCenter(true);
        mySheet2.setColumnWidth(0, 800 * 6);
        mySheet2.setColumnWidth(1, 2000 * 6);

        HSSFRow myRow = null;
        HSSFCell myCell = null;
        // first sheet
        for (int rowNum = 0; rowNum < excelData[0].length; rowNum++) {
            myRow = mySheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 4; cellNum++) {
                myCell = myRow.createCell(cellNum);
                myCell.setCellValue(excelData[rowNum][cellNum]);
            }
        }
        try {
            FileOutputStream out = new FileOutputStream(fileName);
            myReports.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // second sheet
        for (int rowNum = 0; rowNum < excelData1[0].length; rowNum++) {
            myRow = mySheet1.createRow(rowNum);
            for (int cellNum = 0; cellNum < 4; cellNum++) {
                myCell = myRow.createCell(cellNum);
                myCell.setCellValue(excelData1[rowNum][cellNum]);
            }
        }
        try {
            FileOutputStream out = new FileOutputStream(fileName);
            myReports.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // third sheet
        for (int rowNum = 0; rowNum < excelData2[0].length; rowNum++) {
            myRow = mySheet2.createRow(rowNum);
            for (int cellNum = 0; cellNum < 4; cellNum++) {
                myCell = myRow.createCell(cellNum);
                myCell.setCellValue(excelData2[rowNum][cellNum]);
            }
        }
        try {
            FileOutputStream out = new FileOutputStream(fileName);
            myReports.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static String password;
    public static String username;
    public static String temp;

    public variable() throws FileNotFoundException {
        // TODO Auto-generated method stub

        IEnterpriseSession oEnterpriseSession = null;

        ReportEngines reportEngines = null;
        try {

            // String cmsname = "det0190bpmsdev3";
            // String authenticationType = "secEnterprise";
            String cmsname = "";
            String authenticationType = "";

            // Log in.

            oEnterpriseSession = CrystalEnterprise.getSessionMgr().logon(
                    username, password, cmsname, authenticationType);



            if (oEnterpriseSession == null) {

            } else {

                // Process Document
                reportEngines = (ReportEngines) oEnterpriseSession
                        .getService("ReportEngines");
                ReportEngine wiRepEngine = (ReportEngine) reportEngines
                        .getService(ReportEngines.ReportEngineType.WI_REPORT_ENGINE);

                IInfoStore infoStore = (IInfoStore) oEnterpriseSession
                        .getService("InfoStore");
                String query = "select SI_NAME, SI_ID from CI_INFOOBJECTS "
                        + "where SI_KIND = 'Webi' and SI_INSTANCE=0 and SI_NAME ='"
                        + temp + "'";
                IInfoObjects infoObjects = (IInfoObjects) infoStore
                        .query(query);
                for (Object object : infoObjects) {
                    IInfoObject infoObject = (IInfoObject) object;
                    String path = getInfoObjectPath(infoObject);
                    if (path.startsWith("/")) {
                        DocumentInstance widoc = wiRepEngine
                                .openDocument(infoObject.getID());
                        String doc = infoObject.getTitle();
                    reportPath = path;// this is the path of document
            $$$$$$$$$  reportName = doc;// this is the document name

                        printDocumentVariables(widoc);
                        purgeQueries(widoc);
                        widoc.closeDocument();
                    }
                }
                // End processing
            }
        } catch (SDKException sdkEx) {

        } finally {
            if (reportEngines != null)
                reportEngines.close();
            if (oEnterpriseSession != null)
                oEnterpriseSession.logoff();
        }

    }

    public static void printDocumentVariables(DocumentInstance widoc) {
        int i = 0;
        // this is the report documents variables
        ReportDictionary dic = widoc.getDictionary();
        VariableExpression[] variables = dic.getVariables();

        for (VariableExpression e : variables) {

            nameEx = e.getFormulaLanguageID();
            expressionEx = e.getFormula().getValue();
            // stores variables in array
            nameXE[i] = nameEx;
            expressionXE[i] = expressionEx;

            i++;
        }
    }

    public static String getInfoObjectPath(IInfoObject infoObject)
            throws SDKException {
        String path = "";
        while (infoObject.getParentID() != 0) {
            infoObject = infoObject.getParent();
            path = "/" + infoObject.getTitle() + path;
        }
        return path;
    }

    @SuppressWarnings("deprecation")
    public static void purgeQueries(DocumentInstance widoc) {
        DataProviders dps = widoc.getDataProviders();
        for (int i = 0; i < dps.getCount(); ++i) {
            DataProvider dp = (DataProvider) dps.getItem(i);
            if (dp instanceof SQLDataProvider) {

                SQLDataProvider sdp = (SQLDataProvider) dp;
                sdp.purge(true);
                query = sdp.getQuery().getSQL();
            }
        }
Foi útil?

Solução

If Temp is going to hold the name of a unique folder, then you can use this code. Replace the String query = line with:

IInfoObjects oParents = infoStore.query("select si_id from ci_infoobjects where si_kind = 'folder' and si_name = '" + temp + "'");
if(oParents.size()==0)
    return; // folder name not found

IInfoObject oParent = (IInfoObject)oParents.get(0);

String query = "select SI_NAME, SI_ID from CI_INFOOBJECTS "
        + "where SI_KIND = 'Webi' and si_parentid = " + oParent.getID();

I don't see in the code where you're populating the temp, username, or password variables, but I assume you just left that out of the listing intentionally. Obviously they will need to be defined.

Couple of other things I noticed in your code:

  • You are purging the query in each report, but not saving the report afterwards.
  • You are storing the report's SQL in an instance variable named query, but you also have a local variable named query in the variable constructor. This could very likely cause a conflict if you need the SQL value for something.
  • You have an "empty catch" for SDKException. SDKExceptions can happen for all kinds of reasons, and as it is you will not know why the program failed. You should at least be printing out the exception, or just let it bubble up.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top