0

我得到前三列的值,但不是最后一列的值。如何将列表转换为字符串,以便获取组织列的值?

这是我的代码:

String appname = "abc";
String path = "//home/exportfile//";
String filename = path+"ApplicationExport-"+appname+".txt";
String ret = "false";

        QueryOptions ops = new QueryOptions();
    Filter [] filters = new Filter[1];
    filters[0] = Filter.eq("application.name", appname);
    ops.add(filters);

    List props = new ArrayList();
    props.add("identity.name");

    //Do search
    Iterator it = context.search(Link.class, ops, props);

    //Build file and export header row
    BufferedWriter out = new BufferedWriter(new FileWriter(filename));
    out.write("IdentityName,UserName,WorkforceID,Organization");
    out.newLine();          

    //Iterate Search Results
    if (it!=null)
    {                               
            while ( it.hasNext() ) {

                    //Get link and create object
                    Object [] record = it.next();
                    String identityName = (String) record[0];
                    Identity user = (Identity) context.getObject(Identity.class, identityName);

                    //Get Identity attributes for export
                    String workforceid = (String) user.getAttribute("workforceID");                 

                    //Get application attributes for export
                    String userid="";

                    List links = user.getLinks();
                    if (links!=null)
                    {
                            Iterator lit = links.iterator();
                            while (lit.hasNext())
                            {
                                    Link l = lit.next();
                                    String lname = l.getApplicationName();
                                    if (lname.equalsIgnoreCase(appname))
                                    {
                                              userid = (String) l.getAttribute("User Name");
                                              List organizations = l.getAttribute("Organization");

                                              StringBuilder sb = new StringBuilder();
                                              String listItemsSeparator = ","; // this you can change to anything you want, it separates items from list

                                                                                                            for (Object organization : organizations)
                                                                                                                    {
                                                                                                                            sb.append(organization.toString());
                                                                                                                            sb.append(listItemsSeparator);
                                                                                                                    }

                                                                                                                    org = sb.toString().trim();

                                    }
                            }
                    }                   

                    //Output file
                    out.write(identityName+","+userid+","+workforceid+","+org);                             
                    out.newLine();                                                                          
                    out.flush();
            }

                     ret="true";
    }

    //Close file and return
    out.close();
    return ret;

此代码为 Organization 列的值写入 Void。我怎样才能解决这个问题?

4

3 回答 3

1

您确实没有提供太多关于您的应用程序中发生了什么,或者您为什么要为组织获取列表的信息。但是试试这个:

out.write(identityName+","+userid+","+workforceid+","+(String)orgList.get(0));   

你的列表也没有参数化,所以我们不知道列表存储的是什么类型的对象。

于 2012-08-02T18:53:59.663 回答
0

您需要覆盖列表中对象的默认 toString 方法。对列表调用 toString 将导致调用其中所有对象的 toString 方法。您还可以遍历列表并打印每个值。

于 2012-08-02T18:52:21.117 回答
0

编辑:你的代码真的很乱,但我想我知道你在问什么:

    List links = user.getLinks();
    if (links != null) {
        Iterator lit = links.iterator();
        while (lit.hasNext()) {
            Link l = lit.next();
            String lname = l.getApplicationName();
            if (lname.equalsIgnoreCase(appname)) {
                userid = (String) l.getAttribute("User Name");
                List organizations = l.getAttribute("Organization");

                StringBuilder sb = new StringBuilder();
                String listItemsSeparator = " "; // this you can change to anything you want, it separates items from list

                //iterating over list, to convert it to single String 
                for (Object organization : organizations) {
                    sb.append(organization.toString());
                    sb.append(listItemsSeparator);
                }

                orgList = sb.toString().trim();

            }
        }
    }

编辑2:

while (it.hasNext()) {

    //Get link and create object
    Object [] record = it.next();
    String identityName = (String) record[0];
    Identity user = (Identity) context.getObject(Identity.class, identityName);

    //Get Identity attributes for export
    String workforceid = (String) user.getAttribute("workforceID");

    //Get application attributes for export
    String userid="";

    List links = user.getLinks();
    if (links!=null)
    {
        Iterator lit = links.iterator();
        while (lit.hasNext())
        {
            Link l = lit.next();
            String lname = l.getApplicationName();
            if (lname.equalsIgnoreCase(appname))
            {
                userid = (String) l.getAttribute("User Name");
                List orgList = l.getAttribute("Organization");
                if(organization < orgList.size()) {
                    String singleOrganization = orgList.get(organization);
                    organization++;


                    //Output file
                    out.write(identityName+","+userid+","+workforceid+","+orgList);
                    out.newLine();
                    out.flush();

                }

            }
        }
    }
于 2012-08-02T18:56:05.580 回答