<commit_before>//
//  IDFYDataManager.swift
//  iDecide4U
//
//  Created by <NAME> on 01/05/2015.
//  Copyright (c) 2015 <EMAIL>. All rights reserved.
//

import Foundation
import CoreData

class IDFYDataManager : IDFYDataManagerInterface {
  
  private let managedObjectContext = IDFYCoreDataStack.sharedCoreDataStack().managedObjectContext
  private let managedOptionListEntityName : String = NSStringFromClass(IDFYManagedOptionList).componentsSeparatedByString(".").last!
  
  
  // MARK: - IDFYDataManagerInterface
  
  func getCurrentList() -> IDFYOptionList {
    
    var optionList : IDFYOptionList = IDFYOptionList()
    
    if let lastUsedListName = IDFYUserDefaultsUtility.getLastUsedListName() {
      let listOfLastUsedManagedOptionLists = fetchManagedOptionListWithName(lastUsedListName)
      if 0 < listOfLastUsedManagedOptionLists.count {
        let managedOptionList : IDFYManagedOptionList = listOfLastUsedManagedOptionLists[0]
        optionList = IDFYOptionList(name: managedOptionList.name, options: managedOptionList.options)
      }
    } else {
      let entityDescriptionForNewList = NSEntityDescription.entityForName(managedOptionListEntityName, inManagedObjectContext: managedObjectContext!)
      var managedOptionList = IDFYManagedOptionList(entity: entityDescriptionForNewList!, insertIntoManagedObjectContext: managedObjectContext)
      managedOptionList.name = ""
      managedOptionList.options = [String]()
      // A new list need to be returned, which was already set at the beginnig of this function. But it's name needs to be saved still.
      IDFYUserDefaultsUtility.setLastUsedListName("")
    }
    
    return optionList
  }
  
  func getAllLists() -> [IDFYOptionList] {
    let listOfManagedOptionLists = fetchManagedOptionListWithPredicate(NSPredicate(format: "name != ''"))
    var listOfOptionLists = [IDFYOptionList]()
    for managedOptionList : IDFYManagedOptionList in listOfManagedOptionLists {
      listOfOptionLists.append(IDFYOptionList(name: managedOptionList.name, options: managedOptionList.options))
    }
    return listOfOptionLists
  }
  
  func updateCurrentList(list: IDFYOptionList) {
    let fetchResult : [IDFYManagedOptionList] = fetchManagedOptionListWithName(IDFYUserDefaultsUtility.getLastUsedListName()!)
    if 0 < fetchResult.count {
      let managedOptionList = fetchResult[0]
      managedOptionList.name = list.name
      managedOptionList.options = list.options
      IDFYCoreDataStack.sharedCoreDataStack().saveContext()
      IDFYUserDefaultsUtility.setLastUsedListName(list.name)
    } else {
      IDFYLoggingUtilities.error("Error while updating the list! Must not happen!")
    }
  }
  
  func startNewList() {
    var fetchResult : [IDFYManagedOptionList] = fetchManagedOptionListWithName("")
    var managedOptionList : IDFYManagedOptionList
    if 0 < fetchResult.count {
      managedOptionList = fetchResult[0]
    } else {
      managedOptionList = NSEntityDescription.insertNewObjectForEntityForName(managedOptionListEntityName, inManagedObjectContext:managedObjectContext!) as! IDFYManagedOptionList
      managedOptionList.name = ""
    }
    managedOptionList.options = [String]()
    IDFYUserDefaultsUtility.setLastUsedListName("")
  }
  
  func loadListWithName(listName: String) {
    IDFYUserDefaultsUtility.setLastUsedListName(listName)
  }
  
  func deleteListWithName(listName: String) {
    let managedOptionLists = fetchManagedOptionListWithName(listName)
    if 0 < managedOptionLists.count {
      managedObjectContext?.deleteObject(managedOptionLists[0])
    } else {
      println("Error while loading the list, which should be deleted.")
    }
  }
  
  
  // MARK: - Private methods
  
  private func fetchManagedOptionListWithName(listName: String) -> [IDFYManagedOptionList] {
    return fetchManagedOptionListWithPredicate(NSPredicate(format: "name == '" + listName + "'"))
  }
  
  private func fetchManagedOptionListWithPredicate(predicate: NSPredicate?) -> [IDFYManagedOptionList] {
    return fetchEntity(IDFYCoreDataStack.sharedCoreDataStack().optionListEntityName, fromManagedObjectContext: managedObjectContext!, withPredicate: predicate) as! [IDFYManagedOptionList]
  }
  
  private func fetchEntity(entityName: String, fromManagedObjectContext managedObjectContext: NSManagedObjectContext, withPredicate predicate: NSPredicate?) -> [AnyObject] {
    
    let fetchRequest = NSFetchRequest(entityName: entityName)
    fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
    var error: NSError?
    let fetchResult = managedObjectContext.executeFetchRequest(fetchRequest, error: &error)
    
    if let error = error {
      IDFYLoggingUtilities.error("Error loading option list from data base: " + error.description)
    }
    
    return fetchResult!
  }
  
}<commit_msg>IDFY-133: When updating a list in the data manager, check if the last used list had an empty name. In that case the fetch request will not find a list and therefore throw an error. To avoid this, a new list has to be generated when the last used list name is empty.<commit_after>//
//  IDFYDataManager.swift
//  iDecide4U
//
//  Created by <NAME> on 01/05/2015.
//  Copyright (c) 2015 dominic<EMAIL>i.<EMAIL>. All rights reserved.
//

import Foundation
import CoreData

class IDFYDataManager : IDFYDataManagerInterface {
  
  private let managedObjectContext = IDFYCoreDataStack.sharedCoreDataStack().managedObjectContext
  private let managedOptionListEntityName : String = NSStringFromClass(IDFYManagedOptionList).componentsSeparatedByString(".").last!
  
  
  // MARK: - IDFYDataManagerInterface
  
  func getCurrentList() -> IDFYOptionList {
    
    var optionList : IDFYOptionList = IDFYOptionList()
    
    if let lastUsedListName = IDFYUserDefaultsUtility.getLastUsedListName() {
      let listOfLastUsedManagedOptionLists = fetchManagedOptionListWithName(lastUsedListName)
      if 0 < listOfLastUsedManagedOptionLists.count {
        let managedOptionList : IDFYManagedOptionList = listOfLastUsedManagedOptionLists[0]
        optionList = IDFYOptionList(name: managedOptionList.name, options: managedOptionList.options)
      }
    } else {
      let entityDescriptionForNewList = NSEntityDescription.entityForName(managedOptionListEntityName, inManagedObjectContext: managedObjectContext!)
      var managedOptionList = IDFYManagedOptionList(entity: entityDescriptionForNewList!, insertIntoManagedObjectContext: managedObjectContext)
      managedOptionList.name = ""
      managedOptionList.options = [String]()
      // A new list need to be returned, which was already set at the beginnig of this function. But it's name needs to be saved still.
      IDFYUserDefaultsUtility.setLastUsedListName("")
    }
    
    return optionList
  }
  
  func getAllLists() -> [IDFYOptionList] {
    let listOfManagedOptionLists = fetchManagedOptionListWithPredicate(NSPredicate(format: "name != ''"))
    var listOfOptionLists = [IDFYOptionList]()
    for managedOptionList : IDFYManagedOptionList in listOfManagedOptionLists {
      listOfOptionLists.append(IDFYOptionList(name: managedOptionList.name, options: managedOptionList.options))
    }
    return listOfOptionLists
  }
  
  func updateCurrentList(optionList: IDFYOptionList) {
    let fetchResult : [IDFYManagedOptionList] = fetchManagedOptionListWithName(IDFYUserDefaultsUtility.getLastUsedListName()!)
    if 0 < fetchResult.count {
      let managedOptionList = fetchResult[0]
      transferDataOfOptionList(optionList, toManagedOptionList: managedOptionList)
    } else if "" == IDFYUserDefaultsUtility.getLastUsedListName()! {
      let managedOptionList = NSEntityDescription.insertNewObjectForEntityForName(managedOptionListEntityName, inManagedObjectContext:managedObjectContext!) as! IDFYManagedOptionList
      transferDataOfOptionList(optionList, toManagedOptionList: managedOptionList)
    } else {
      IDFYLoggingUtilities.error("Error while updating the list! Must not happen!")
    }
  }
  
  func startNewList() {
    var fetchResult : [IDFYManagedOptionList] = fetchManagedOptionListWithName("")
    var managedOptionList : IDFYManagedOptionList
    if 0 < fetchResult.count {
      managedOptionList = fetchResult[0]
    } else {
      managedOptionList = NSEntityDescription.insertNewObjectForEntityForName(managedOptionListEntityName, inManagedObjectContext:managedObjectContext!) as! IDFYManagedOptionList
      managedOptionList.name = ""
    }
    managedOptionList.options = [String]()
    IDFYUserDefaultsUtility.setLastUsedListName("")
  }
  
  func loadListWithName(listName: String) {
    IDFYUserDefaultsUtility.setLastUsedListName(listName)
  }
  
  func deleteListWithName(listName: String) {
    let managedOptionLists = fetchManagedOptionListWithName(listName)
    if 0 < managedOptionLists.count {
      managedObjectContext?.deleteObject(managedOptionLists[0])
    } else {
      println("Error while loading the list, which should be deleted.")
    }
  }
  
  
  // MARK: - Private methods
  
  private func fetchManagedOptionListWithName(listName: String) -> [IDFYManagedOptionList] {
    return fetchManagedOptionListWithPredicate(NSPredicate(format: "name == '" + listName + "'"))
  }
  
  private func fetchManagedOptionListWithPredicate(predicate: NSPredicate?) -> [IDFYManagedOptionList] {
    return fetchEntity(IDFYCoreDataStack.sharedCoreDataStack().optionListEntityName, fromManagedObjectContext: managedObjectContext!, withPredicate: predicate) as! [IDFYManagedOptionList]
  }
  
  private func fetchEntity(entityName: String, fromManagedObjectContext managedObjectContext: NSManagedObjectContext, withPredicate predicate: NSPredicate?) -> [AnyObject] {
    
    let fetchRequest = NSFetchRequest(entityName: entityName)
    fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
    var error: NSError?
    let fetchResult = managedObjectContext.executeFetchRequest(fetchRequest, error: &error)
    
    if let error = error {
      IDFYLoggingUtilities.error("Error loading option list from data base: " + error.description)
    }
    
    return fetchResult!
  }
  
  func transferDataOfOptionList(optionList: IDFYOptionList, toManagedOptionList managedOptionList: IDFYManagedOptionList) {
    managedOptionList.name = optionList.name
    managedOptionList.options = optionList.options
    IDFYCoreDataStack.sharedCoreDataStack().saveContext()
    IDFYUserDefaultsUtility.setLastUsedListName(optionList.name)
  }
  
}
{'fraction_non_alphanumeric': 0.07849635451188335, 'fraction_numerical': 0.004260960136350724, 'mean_word_length': 4.706104808211777, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '644486', 'n_tokens_mistral': 3061, 'n_tokens_neox': 2908, 'n_words': 609}