views:

328

answers:

1

Hi,

I'm currently trying to construct a list of bean classes in Java from a flat description file formatted in csv. Concretely :

Here is the structure of the csv file :

MES_ID;GRP_PARENT_ID;GRP_ID;ATTR_ID
M1    ;             ;G1    ;A1
M1    ;             ;G1    ;A2
M1    ;G1           ;G2    ;A3
M1    ;G1           ;G2    ;A4
M1    ;G2           ;G3    ;A5
M1    ;             ;G4    ;A6
M1    ;             ;G4    ;A7
M1    ;             ;G4    ;A8
M2    ;             ;G1    ;A1
M2    ;             ;G1    ;A2
M2    ;             ;G2    ;A3
M2    ;             ;G2    ;A4

It corresponds to the hierarchical data structure :

M1
---G1
------A1
------A2
------G2
---------A3
---------A4
---------G3
------------A5
---G4
------A7
------A8
M2
---G1
------A1
------A2
---G2
------A3
------A4

Remarks :

  • A message M can have an infinite number of groups G and attributes A

  • A group G can have an infinite number of attributes and an infinite number of under-groups each of them having under-groups too

That beeing said, I'm trying to read this flat csv decription to store it in this structure of beans :

Map<String, MBean> messages = new HashMap<String, Mbean>();

==

public class MBean {
   private String mes_id;
   private Map<String, GBean> groups;
}

public class GBean {
   private String grp_id;
   private Map<String, ABean> attributes;
   private Map<String, GBean> underGroups;
}

public class ABean {
   private String attr_id;
}

Reading the csv file sequentially is ok and I've been investigating how to use recursion to store the description data, but couldn't find a way.

Thanks in advance for any of your algorithmic ideas.

I hope it will put you in the mood of thinking about this ... I've to admit that I'm out of ideas :s

+2  A: 

Here is a solution, adding comments is left as an exercise...

public class Test {

public static void main(String[] args) {
    List<Record> records = new ArrayList<Record>() {
        {
            add(new Record("M1", "", "G1", "A1"));
            add(new Record("M1", "", "G1", "A2"));

            add(new Record("M1", "G1", "G2", "A3"));
            add(new Record("M1", "G1", "G2", "A4"));
        }
    };

    MessageContainer messageContainer = new MessageContainer();
    for (Record record : records) {
        messageContainer.addOrUpdateMessage(record);
    }

}

private static class Record {

    final String messageId;
    final String parentGroupId;
    final String groupId;
    final String attributeId;

    public Record(String messageId, String parentGroupId, String groupId, String attributeId) {
        super();
        this.messageId = messageId;
        this.parentGroupId = parentGroupId;
        this.groupId = groupId;
        this.attributeId = attributeId;
    }

}

private static class MessageContainer {

    Map<String, MBean> messages = new HashMap<String, MBean>();

    public void addOrUpdateMessage(Record record) {
        MBean mBean = messages.get(record.messageId);
        if (mBean == null) {
            mBean = new MBean(record.messageId);
            messages.put(record.messageId, mBean);
        }
        mBean.addOrUpdateGroup(record);
    }
}

private static class MBean {
    private final String mes_id;
    private final Map<String, GBean> groups = new HashMap<String, GBean>();

    public MBean(String mesId) {
        super();
        mes_id = mesId;
    }

    public void addOrUpdateGroup(Record record) {
        String groupToHandle = (record.parentGroupId != "" ? record.parentGroupId : record.groupId);
        GBean gBean = groups.get(groupToHandle);
        if (gBean == null) {
            gBean = new GBean(groupToHandle);
            groups.put(groupToHandle, gBean);
        }
        gBean.addOrUpdateGroup(record);
    }
}

private static class GBean {
    private final String groupId;
    private final Map<String, ABean> attributes = new HashMap<String, ABean>();
    private final Map<String, GBean> underGroups = new HashMap<String, GBean>();

    public GBean(String groupId) {
        super();
        this.groupId = groupId;
    }

    public void addOrUpdateGroup(Record record) {
        if (groupId == record.parentGroupId) {

            GBean child = underGroups.get(record.groupId);
            if (child == null) {
                child = new GBean(record.groupId);
                underGroups.put(record.groupId, child);
            }
            child.addOrUpdateGroup(record);

        } else if (groupId == record.groupId) {
            attributes.put(record.attributeId, new ABean(record.attributeId));
        } else {
            throw new RuntimeException("Unexpected state [recordParentGroupId="+record.parentGroupId+", recordGroupId="+record.groupId+", groupId="+groupId+"]");

        }

    }

    private static class ABean {
        private final String attr_id;
        public ABean(String attrId) {
            super();
            attr_id = attrId;
        }

    }
}
}
pgras
Hi pgras,What else could say except THANKS A LOT !!You just managed the recursion I was having troubles to code.I integrated a code snippet to print the hierarchy and it works the perfect wayregards,Clem
Clem
Great to know it works, because I hadn't tested it :)
pgras