Hi Bart,
On 8 Feb 2016, at 12:24 am, jazzsalsa <[email protected]> wrote:
> The data looks like a tree (e.g. the AjaxTreeModel in the Ajax framework).
> Each node of the tree has a title (like a folder name for example). Each
> folder contains files, and can contain a sub-folder as well.
>
> I rewrote the code to avoid dictionaries as Hugi suggested. Like this I can
> access the folders and the files in the folder:
>
> String content = new String(Files.readAllBytes(Paths.get(inputFile)));
> Node rootNode;
> rootNode = objectMapper.readValue(content, Node.class); // Jackson
> List<Node> children = rootNode.getChildren().get(2).getChildren();
> children.forEach(e -> log.info(e.getTitle()));
>
> Now the problem:
> Depending on how many subfolders there are I should use either children1 or
> children2:
> List<Node> children1 =
> rootNode.getChildren().get(2).getChildren().get(0).getChildren();
> List<Node> children2 = rootNode.getChildren().get(2).getChildren();
>
> Is there a way to get all the files (children.getTitle) in all the subfolders?
It’s still not clear to me how much you know about this data structure before
it’s read in. It sounds like it’s completely arbitrary ("Each folder contains
files, and can contain a sub-folder as well”), but then you’re accessing parts
of it with magic numbers (get(2), get(0)). If it’s arbitrary (by which I mean
you don’t know the shape of it before you build it from the input), then I’d
just walk the tree processing each node as you originally proposed. I don’t
think there’s any shortcut here.
> Is should only find the file names, not the name of the subfolder. Both are
> retreived from the rootNode object with rootNode.getChildren().getTitle(),
> but one is a folder name and the other a file name.
>
> Last question: how to add a list (called children) into the editing context?
> I can only think of this:
>
> List<Node> children =
> rootNode.getChildren().get(2).getChildren();
> ERXEC ec = new ERXEC();
> ChildrenEO child = new ChildrenEO();
>
> for (Node node : children) {
> child.setTitle(node.getTitle());
> child.setDateAdded(node.getDateAdded());
> [...many more...]
> }
>
> ec.insertObject(child);
> ec.saveChanges();
>
> Is there a better way to convert a list to an EO automatically for all the
> items in the list?
I wouldn’t have thought so, no, but the code above needs fixing:
> ERXEC ec = new ERXEC();
> ChildrenEO child = new ChildrenEO();
You need to immediately insert that EO into the EC. Use:
ChildrenEO child = ChildrenEO.createChildrenEO(ec, …);
if you’re using templates that create such a method, otherwise:
ChildrenEO child = (ChildrenEO) EOUtilities.createAndInsertInstance(ec,
“ChildrenEO”);
> for (Node node : children) {
> child.setTitle(node.getTitle());
> child.setDateAdded(node.getDateAdded());
> [...many more...]
> }
You have many Nodes, but you’ve only created a single ChildrenEO, so this needs
to be re-thought.
> ec.insertObject(child);
Too late, see above.
> ec.saveChanges();
This line looks good. :)
--
Paul Hoadley
http://logicsquad.net/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]