0

我正在尝试使用以下代码导出 4 列。最后一列组织是一个列表。

    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 = ",";

                        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;

上述代码的输出将是。例如:

IdentityName,UserName,WorkforceID,Organization

dthomas,dthomas001,12345,Finance,HR

我如何以以下方式获得输出

IdentityName,UserName,WorkforceID,Organization

dthomas,dthomas001,12345,Finance

dthomas,dthomas001,12345,HR 

我需要在哪里更改代码?

4

2 回答 2

0

删除这个最里面for的块和相关的变量:

StringBuilder sb = new StringBuilder();

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

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

声明移到块organizations if (it != null) {

// Get application attributes for export
String userid = "";
List organizations = null;
List links = user.getLinks();

if (it != 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");
            organizations = l.getAttribute("Organization");

然后更改此文件输出代码:

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

对此:

// Output file
for (Object organization : organizations)
{
    out.write(identityName + "," + userid + "," + workforceid + "," + organization.toString());
    out.newLine();
}
于 2012-08-03T19:56:24.667 回答
0

您必须为每个组织在文件中写入一行。因此,基本上,不要将用户的所有组织与字符串生成器连接起来,并将输出语句移动到遍历组织的 for 循环中。

但是很难提供一个工作示例,因为您显示的代码尚未编译...


这应该使您更接近解决方案:

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");
      for (Object organization : organizations) {
        // Output file
        out.write(identityName + "," + userid + "," + workforceid + "," + organization);
        out.newLine();
        out.flush();
      }
    }
  }
}
于 2012-08-03T19:58:37.110 回答