package com.lagopusempire.multihomes.homeIO.database;

import com.lagopusempire.multihomes.home.Home;
import com.lagopusempire.multihomes.homeIO.HomeIO;
import java.util.Map;
import java.util.UUID;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author MrZoraman
 */
public class DBHomeIO implements HomeIO
{
    private final JavaPlugin plugin;
    
    public DBHomeIO(JavaPlugin plugin)
    {
        this.plugin = plugin;
    }
    
    @Override
    public void saveHome(Home home)
    {
        DBHome dbHome = plugin.getDatabase().find(DBHome.class)
                .where()
                .ieq("owner", home.getOwner().toString())
                .ieq("home_name", home.getName())
                .findUnique();
        
        if(dbHome == null)
        {
            dbHome = new DBHome();
        }
        
        dbHome.writeData(home);
        
        plugin.getDatabase().save(home);
    }

    @Override
    public Map<String, Home> loadHomes(UUID uuid)
    {
        return null;
    }

    @Override
    public Home loadHome(UUID uuid, String homeName)
    {
        return null;
    }
}

@@ -163,19 +163,94 @@
 va.util.
-Map
+HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0Aimport
@@ -1058,35 +1058,495 @@
   %7B%0A        
-return null
+final Set%3CDBHome%3E dbHomes = plugin.getDatabase().find(DBHome.class)%0A                .where()%0A                .ieq(%22owner%22, uuid.toString())%0A                .findSet();%0A        %0A        final Map%3CString, Home%3E homes = new HashMap%3C%3E();%0A        %0A        final Iterator%3CDBHome%3E it = dbHomes.iterator();%0A        while(it.hasNext())%0A        %7B%0A            final Home home = it.next().toHome();%0A            homes.put(home.getName(), home);%0A        %7D%0A        %0A        return homes
 ;%0A    %7D%0A%0A   

load homes added