1

I need to make a class that get the list of folders stored in a database and creates them on the local machine in the correct hierarchy.

The folders are arranged on the database like so:

id name parent_id

1 documents 0
2 movies 0
3 videos 0
4 my files 1
5 desktop 0
6 other 4

So documents, movies, videos and desktop are in the root. 'my files' goes in the folder with the id of 1(documents) and 'other' goes in the folder with the id of 4(my files)

I have been trying to do it by using a whyle loop but dont know how to get them to go into the correct folders.

try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        while( i < 50 )
        {   
            try {            

            Statement st = con.createStatement();
            ResultSet result = st.executeQuery("SELECT name, id, parent_id FROM categories WHERE parent_id = '"+PID+"' AND repository_id = '"+RepoID+"'");



            while (result.next ())
            {
                String FolderName = result.getString ("name");
                String FolderId = result.getString ("id");
                String FolderId = result.getString ("parent_id");
make the folder name here
                System.out.println( FolderName+" "+FolderId );
            }

            System.out.println( " ");
                    i++ ;
            PID++;
            } catch (SQLException ex) {
                 System.out.println(ex.getMessage());
             }
            }
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
WookooUK
  • 156
  • 9

3 Answers3

2

Create a Folder object to store the data, then build these objects as you read from the database. Once you have built all the Folder objects, do a final loop to bind each Folder to its parent class. Perhaps something like this:

class Folder {
    private String name;
    private int id;
    private int parentId;
    private List<Folder> children = new ArrayList<Folder>();

    public Folder(String name, int id, int parentId) {
        this.name = name;
        this.id = id;
        this.parentId = parentId;
    }

    public void addChildFolder(Folder folder) {
        this.children.add(folder);
    }

    public List<Folder> getChildren() {
        return Collections.unmodifiableList(children);
    }

    public int getParentFolderId() {
        parentId;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}

Now as you read the data from the database, you create these Folder objects (with no children) and add them to the map with:

Map<Integer, Folder> data = new HashMap<Integer, Folder>();

... loop through your result set getting folder data...
Folder newFolder = new Folder(nameString, id, parentId);
data.put(newFolder.getId(), newFolder);

Use Integer.valueOf(String) to convert String to int.

Once you have the created all the Folders, you can make one final loop to connect the parent folders to the children, like this:

for(Folder folder : data.values()) {
    int parentId = folder.getParentFolderId();
    Folder parentFolder = data.get(parentId);
    if(parentFolder != null)
        parentFolder.addChildFolder(folder);
}

Finally, just grab the folder with id 0 and start building your Files on the disk, using folder.getChildren() as a convenient way to move down the tree. Check out the javadoc on the File object, you will particularly want to use the mkdirs() method.

Hope that helps.

Eric Lindauer
  • 1,813
  • 13
  • 19
  • This all works great for me (never new about objects before) except that in "data.put(newFolder.getId(), newFolder);" i get: "Type mismatch: cannot convert from element type Integer to Folder" and eclipse wants to convert 'folder' to an interger. Any idea what I did wrong? – WookooUK Sep 23 '11 at 20:33
  • The map generic type was wrong, I've edited the code for creating the Map to reflect this. It should be Map data = new HashMap(); – Eric Lindauer Sep 24 '11 at 00:43
  • How can I go about the last part, grapping the folder with id=0. I am also not sure where to put the code, currently it is all inside the pid loop – WookooUK Sep 25 '11 at 16:57
  • You have a Map from id to folder, so data.get(0) will give you the Folder with id 0 (if any). Good luck. – Eric Lindauer Sep 26 '11 at 20:05
  • Thankyou Eric, not just to figure out the last part and im away. Thanks again – WookooUK Sep 27 '11 at 09:33
  • Eric, I have been trying to get a print out of the higherarchy but cant seam to write it correctly, can you see where I am going wrong?: `for(Folder folder : data.values()) { int parentId = folder.getParentFolderId(); Folder parentFolder = data.get(parentId); if(parentFolder != null) parentFolder.addChildFolder(folder); } Folder rootFolder = data.get(1); String rootFolderName = rootFolder.getName(); rootFolder.getChildren(); System.out.print("/"+rootFolderName); }` – WookooUK Sep 27 '11 at 09:55
  • I am still not having any luck moving down the tree and making the dirs. I have been trying to 2 days with no luck :( – WookooUK Sep 29 '11 at 09:12
1

once you get all the values out of the database, i would suggest creating a Map<Integer,Folder> which maps each folder id to its Folder information (where Folder holds the id, name, and parent id). then, you can loop through this map, and for each folder, build up the full path by recursing up the parent ids until you reach the root and (e.g. "foo/bar/baz") and then call File.mkdirs() with this path.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Thank for revising my question, I am very new to Java and stack over flow but am VERY excited to learn, I have build a few basic java apps and some android apps but this is all a bit advanced for me. I do not quite follow how the mapping and recursive loop would work, could you show me an example? Thank you – WookooUK Sep 23 '11 at 17:05
  • @user961574 - the best way to get help on this site is post targeted questions. "how do i write this code" is not very targeted. instead, i'd suggest taking a crack at it and posting what you create and asking questions about what isn't working. (you can edit your question to add new code) – jtahlborn Sep 23 '11 at 18:23
  • Thank you, I have not used this site for asking questions before, only found it when googleing and usually find the fix i need on a result that point to this site. I will write up the new revision of the code and post again if I have no luck. THanks again – WookooUK Sep 23 '11 at 18:25
0

I see your comment on Sep 29 asking about printing folders. Your code there seems ok, except that you are only printing out the name of the parent folder. If you want to print them all you need to dig into the children and print them too, perhaps something like this:

...
for (Folder folder : data.values()) {
    int parentId = folder.getParentFolderId();
    Folder parentFolder = data.get(parentId);
    if (parentFolder != null)
        parentFolder.addChildFolder(folder);
}

printFolderRecursively(data.get(1));

and then write a method that prints a folder name along with it's children:

public void printFolderRecursively(Folder folder) {
    System.out.print("/" + rootFolderName);
    for(Folder child : folder.getChildren())
        printFolderRecursively(child);
}

hope that helps.

Eric Lindauer
  • 1,813
  • 13
  • 19