<commit_before>//
//  ResourceTagger.swift
//  Caturlog
//
//  Created by <NAME> on 7/27/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import AppKit
import CoreData

class ResourceTagger: ResourceTaggingServiceProtocol {
 
    func addTag(tag: String, contentID: String, user: User) -> (Bool, NSError?) {
        var appDel = NSApplication.sharedApplication().delegate as AppDelegate
        var services = appDel.caturlogServices

        if let tagEntity = getTag(tag) {
            if let item = services.resourceLoader.resourceWithContentID(contentID) {
                addUserItemTag(user, item: item, tag: tagEntity)
                return (true, nil)
            }
        }
        
        return (false, nil)
    }
    
    func itemsForTag(tag: String, withUser: User) -> (NSSet?) {
        
        return nil
    }
    
    func getTag(name: String) -> (Tag?) {
        let predicate = NSPredicate(format: "name is %@", name)
        if let tag = fetchEntity("Tag", predicate: predicate) as? Tag {
            return tag
        }
        else if let tag = addTag(name) {
            return tag
        }
        return nil
        
    }

    func addTag(name: String) -> (Tag?) {
        let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
        if let moc = appDelegate.managedObjectContext {
            if let tag = NSEntityDescription
                .insertNewObjectForEntityForName("Tag", inManagedObjectContext: moc) 
                as? Tag 
            {
                tag.name = name
                moc.save(nil) // Fixme: do error checking
                return tag
            }
        }
        return nil
    }
    
    func addUserItemTag(user: User, item: Item, tag: Tag) {
        var appDel = NSApplication.sharedApplication().delegate as AppDelegate
        var services = appDel.caturlogServices

        if let moc = appDel.managedObjectContext {
            if let userItemTag = NSEntityDescription 
                .insertNewObjectForEntityForName("UserItemTag", inManagedObjectContext: moc)
                as? UserItemTag
            {
                userItemTag.user = user
                userItemTag.item = item
                userItemTag.tag = tag
                moc.save(nil) // Fixme: do error checking
            }
        }
    }<commit_msg>Oops, forgot to set the timestamp for the useritemtags.<commit_after>//
//  ResourceTagger.swift
//  Caturlog
//
//  Created by <NAME> on 7/27/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import AppKit
import CoreData

class ResourceTagger: ResourceTaggingServiceProtocol {
 
    func addTag(tag: String, contentID: String, user: User) -> (Bool, NSError?) {
        var appDel = NSApplication.sharedApplication().delegate as AppDelegate
        var services = appDel.caturlogServices

        if let tagEntity = getTag(tag) {
            if let item = services.resourceLoader.resourceWithContentID(contentID) {
                addUserItemTag(user, item: item, tag: tagEntity)
                return (true, nil)
            }
        }
        
        return (false, nil)
    }
    
    func itemsForTag(tag: String, withUser: User) -> (NSSet?) {
        
        return nil
    }
    
    func getTag(name: String) -> (Tag?) {
        let predicate = NSPredicate(format: "name is %@", name)
        if let tag = fetchEntity("Tag", predicate: predicate) as? Tag {
            return tag
        }
        else if let tag = addTag(name) {
            return tag
        }
        return nil
        
    }

    func addTag(name: String) -> (Tag?) {
        let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
        if let moc = appDelegate.managedObjectContext {
            if let tag = NSEntityDescription
                .insertNewObjectForEntityForName("Tag", inManagedObjectContext: moc) 
                as? Tag 
            {
                tag.name = name
                moc.save(nil) // Fixme: do error checking
                return tag
            }
        }
        return nil
    }
    
    func addUserItemTag(user: User, item: Item, tag: Tag) {
        var appDel = NSApplication.sharedApplication().delegate as AppDelegate
        var services = appDel.caturlogServices

        if let moc = appDel.managedObjectContext {
            if let userItemTag = NSEntityDescription 
                .insertNewObjectForEntityForName("UserItemTag", inManagedObjectContext: moc)
                as? UserItemTag
            {
                userItemTag.timestamp = NSDate()
                userItemTag.user = user
                userItemTag.item = item
                userItemTag.tag = tag
                moc.save(nil) // Fixme: do error checking
            }
        }
    }
{'fraction_non_alphanumeric': 0.08619612742036227, 'fraction_numerical': 0.0037476577139287947, 'mean_word_length': 1.7514318442153494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '3383697', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1283, 'n_words': 348}