从Jabberd2迁移到Openfire(四)


用户的所有数据准备好了之后,我们按照规则开始创建XML文,这块调试花了不少时间,先是因为在查询authreg和vcard表的时候没有使用左连接,导致部分用户丢失,然后是roster-item和roster-group表的数据没有使用右连接,导致联系人列表丢失……一次次调试,终于成功。

创建XML文的思路是这样的,用用户基本信息作为外层循环,每个用户都首先创建<Username><password>等标签,
然后使用用户列表信息循环查找属于当前用户的用户列表。
第一次进入循环的时候创建一个<Item>标签及其包含的<Group>标签,然后从用户列表信息中删除已经取出来的这条数据,
再次判断,如果还有属于这个用户的用户列表信息,则在再次入循环,第二次进入循环后判断当前取出来的列表中的用户是否和上次的相同(查询的时候已经做排序,保证相同的jid相邻),
如果相同,则只增加<Group>标签,否则增加上一个<Item>的结束标记,并且创建当前这条数据的<Item>标签及其包含的<Group>标签,
创建完了之后删除已经取出来的这条数据。

CreateImportXmlFile.java

public static void main(String[] args) {
    GetUserInfo getUserInfo = new GetUserInfo();
    List<User> userList = getUserInfo.getUser();
    List<Group> groupList = getUserInfo.getGroup();
    Writer w = null;
    try {
        w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                "openspark.xml"), "UTF-8"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        w.write("<?xml version="1.0" encoding="UTF-8"?>rnrn<Openfire>rn");
        for (User user : userList) {
            String str = "  <User>rn";
            str += "    <Username>" + user.getUsername() + "</Username>rn";
            str += "    <Password>" + user.getPassword() + "</Password>rn";
            str += "    <Email></Email>rn";
            str += "    <Name>" + ("".equals(user.getNickname())
                                    ? user.getN_given() + " " + user.getN_family()
                                            : user.getNickname()) + "</Name>rn";
            str += "    <CreationDate>1252398257423</CreationDate>rn";
            str += "    <ModifiedDate>1252398257423</ModifiedDate>rn";
            str += "    <Roster>rn";
            int i = -1;
            String preJid = "";
            boolean falg = true;
            while((i = groupList.indexOf(user)) != -1) {
                Group group = groupList.get(i);
                if(falg) {
                    str += "      <Item jid=""
                        + group.getJid()
                        + "" askstatus="-1" recvstatus="-1" substatus="3" name=""
                        + ("".equals(group.getName())
                                ? group.getJid().substring(0, group.getJid().indexOf("@"))
                                        : group.getName()) + "">rn";
                    str += "        <Group>" + group.getGroup() + "</Group>rn";
                    preJid = group.getJid();
                    falg = false;
                } else {
                    if(preJid.equals(group.getJid())) {
                        str += "        <Group>" + group.getGroup() + "</Group>rn";
                    } else {
                        str += "      </Item>rn";
                        str += "      <Item jid=""
                            + group.getJid()
                            + "" askstatus="-1" recvstatus="-1" substatus="3" name=""
                            + ("".equals(group.getName())
                                    ? group.getJid().substring(0, group.getJid().indexOf("@"))
                                            : group.getName()) + "">rn";
                        str += "        <Group>" + group.getGroup() + "</Group>rn";
                        preJid = group.getJid();
                    }
                }
                groupList.remove(i);
            }
            if(!str.endsWith("</Item>rn") && !str.endsWith("<Roster>rn")) {
                str += "      </Item>rn";
            }
            str += "    </Roster>rn";
            str += "  </User>rn";
            w.write(str);
        }
        w.write("</Openfire>rn");
        w.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s