package de.philipphager.disclosure.service;

import com.squareup.sqlbrite.BriteDatabase;
import de.philipphager.disclosure.database.DatabaseManager;
import de.philipphager.disclosure.database.app.model.App;
import de.philipphager.disclosure.database.library.model.Library;
import de.philipphager.disclosure.database.permission.model.AppLibraryPermission;
import de.philipphager.disclosure.database.permission.model.AppPermission;
import de.philipphager.disclosure.database.permission.model.Permission;
import de.philipphager.disclosure.database.permission.repositories.AppLibraryPermissionRepository;
import de.philipphager.disclosure.database.permission.repositories.AppPermissionRepository;
import de.philipphager.disclosure.database.permission.repositories.PermissionRepository;
import java.util.List;
import javax.inject.Inject;
import rx.Observable;

public class PermissionService {
  private final DatabaseManager databaseManager;
  private final PermissionRepository permissionRepository;
  private final AppPermissionRepository appPermissionRepository;
  private final AppLibraryPermissionRepository appLibraryPermissionRepository;

  @Inject public PermissionService(DatabaseManager databaseManager,
      PermissionRepository permissionRepository,
      AppPermissionRepository appPermissionRepository,
      AppLibraryPermissionRepository appLibraryPermissionRepository) {
    this.databaseManager = databaseManager;
    this.permissionRepository = permissionRepository;
    this.appPermissionRepository = appPermissionRepository;
    this.appLibraryPermissionRepository = appLibraryPermissionRepository;
  }

  public void insertOrUpdate(List<Permission> permissions) {
    BriteDatabase db = databaseManager.get();
    try (BriteDatabase.Transaction transaction = db.newTransaction()) {
      for (Permission permission : permissions) {
        int updatedRows = permissionRepository.update(db, permission);

        if (updatedRows == 0) {
          permissionRepository.insert(db, permission);
        }
      }
      transaction.markSuccessful();
    }
  }

  public void insertForApp(List<AppPermission> appPermissions) {
    BriteDatabase db = databaseManager.get();
    try (BriteDatabase.Transaction transaction = db.newTransaction()) {

      for (AppPermission appPermission : appPermissions) {
        appPermissionRepository.insert(db, appPermission);
      }

      transaction.markSuccessful();
    }
  }

  public void insertForAppAndLibrary(List<AppLibraryPermission> appLibraryPermissions) {
    BriteDatabase db = databaseManager.get();
    try (BriteDatabase.Transaction transaction = db.newTransaction()) {

      for (AppLibraryPermission appLibraryPermission : appLibraryPermissions) {
        appLibraryPermissionRepository.insert(db, appLibraryPermission);
      }

      transaction.markSuccessful();
    }
  }

  public Observable<List<Permission>> all() {
    BriteDatabase db = databaseManager.get();
    return permissionRepository.all(db);
  }

  public Observable<List<Permission>> byApp(App app) {
    BriteDatabase db = databaseManager.get();
    return permissionRepository.byApp(db, app.id());
  }

  public Observable<List<Permission>> byAppAndLibrary(App app, Library library) {
    BriteDatabase db = databaseManager.get();
    return permissionRepository.byAppAndLibrary(db, app.id(), library.id());
  }
}

@@ -2467,23 +2467,39 @@
 ary(
-List%3CAppLibrary
+App app, Library library, List%3C
 Perm
@@ -2506,27 +2506,17 @@
 ission%3E 
-appLibraryP
+p
 ermissio
@@ -2651,16 +2651,63 @@
    for (
+Permission permission : permissions) %7B%0A        
 AppLibra
@@ -2740,19 +2740,31 @@
 mission 
-: a
+=%0A            A
 ppLibrar
@@ -2766,36 +2766,81 @@
 ibraryPermission
-s) %7B
+.create(app.id(), library.id(), permission.id());
 %0A        appLibr

Change signatures in PermissionService