// Path: app/src/main/java/io/rapidpro/surveyor/Logger.java
// public class Logger {
// 
//     private static final String TAG = "Surveyor";
// 
//     public static void e(String message, Throwable t) {
//         Log.e(TAG, message, t);
//     }
// 
//     public static void w(String message) {
//         Log.w(TAG, message);
//     }
// 
//     public static void d(String message) {
//         Log.d(TAG, message);
//     }
// }
// 
// Path: app/src/main/java/io/rapidpro/surveyor/net/TembaException.java
// public class TembaException extends SurveyorException {
//     public TembaException(String message) {
//         super(message);
//     }
// 
//     public TembaException(String message, Exception e) {
//         super(message, e);
//     }
// }


import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import io.rapidpro.surveyor.Logger;
import io.rapidpro.surveyor.net.TembaException;
package io.rapidpro.surveyor.data;



/**
 * Directory based service for org configurations
 */
public class OrgService {

    private File rootDir;

    private Map<String, Org> cache = new HashMap<>();

    public OrgService(File rootDir) {
        this.rootDir = rootDir;

        Logger.d("OrgService created for directory " + this.rootDir.getAbsolutePath());
    }

    public Org get(String uuid) throws IOException {
        if (cache.containsKey(uuid)) {
            Logger.d("Returning cached org " + uuid);
            return cache.get(uuid);
        }

        File directory = new File(rootDir, uuid);
        Org org = Org.load(directory);
        Logger.d("Loaded org " + uuid);
        cache.put(uuid, org);
        return org;
    }

    /**
     * Fetches an org using the given API token and saves it to the org storage
     *
     * @param uuid  the UUID of the org
     * @param name  the name of the org
     * @param token the API token
     */
// Path: app/src/main/java/io/rapidpro/surveyor/Logger.java
// public class Logger {
// 
//     private static final String TAG = "Surveyor";
// 
//     public static void e(String message, Throwable t) {
//         Log.e(TAG, message, t);
//     }
// 
//     public static void w(String message) {
//         Log.w(TAG, message);
//     }
// 
//     public static void d(String message) {
//         Log.d(TAG, message);
//     }
// }
// 
// Path: app/src/main/java/io/rapidpro/surveyor/net/TembaException.java
// public class TembaException extends SurveyorException {
//     public TembaException(String message) {
//         super(message);
//     }
// 
//     public TembaException(String message, Exception e) {
//         super(message, e);
//     }
// }


// Path: app/src/main/java/io/rapidpro/surveyor/data/OrgService.java
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import io.rapidpro.surveyor.Logger;
import io.rapidpro.surveyor.net.TembaException;

package io.rapidpro.surveyor.data;



/**
 * Directory based service for org configurations
 */
public class OrgService {

    private File rootDir;

    private Map<String, Org> cache = new HashMap<>();

    public OrgService(File rootDir) {
        this.rootDir = rootDir;

        Logger.d("OrgService created for directory " + this.rootDir.getAbsolutePath());
    }

    public Org get(String uuid) throws IOException {
        if (cache.containsKey(uuid)) {
            Logger.d("Returning cached org " + uuid);
            return cache.get(uuid);
        }

        File directory = new File(rootDir, uuid);
        Org org = Org.load(directory);
        Logger.d("Loaded org " + uuid);
        cache.put(uuid, org);
        return org;
    }

    /**
     * Fetches an org using the given API token and saves it to the org storage
     *
     * @param uuid  the UUID of the org
     * @param name  the name of the org
     * @param token the API token
     */
    public Org getOrFetch(String uuid, String name, String token) throws TembaException, IOException {