<commit_before>//
//  SharingGroupRepository.swift
//  Server
//
//  Created by <NAME> on 6/23/18.
//

// A sharing group is a group of users who are sharing a collection of files.

import Foundation
import LoggerAPI
import SyncServerShared

class SharingGroup : NSObject, Model {
    static let sharingGroupUUIDKey = "sharingGroupUUID"
    var sharingGroupUUID: String!
    
    static let sharingGroupNameKey = "sharingGroupName"
    var sharingGroupName: String!
    
    static let deletedKey = "deleted"
    var deleted:Bool!
    
    // Not a part of this table, but a convenience for doing joins with the MasterVersion table.
    static let masterVersionKey = "masterVersion"
    var masterVersion: MasterVersionInt!

    // Similarly, not part of this table. For doing joins.
    public static let permissionKey = "permission"
    public var permission:Permission?
    
    // Also not part of this table. For doing fetches of sharing group users for the sharing group.
    public var sharingGroupUsers:[SyncServerShared.SharingGroupUser]!

    static let accountTypeKey = "accountType"
    var accountType: String!

    static let owningUserIdKey = "owningUserId"
    var owningUserId:UserId?

    subscript(key:String) -> Any? {
        set {
            switch key {
            case SharingGroup.sharingGroupUUIDKey:
                sharingGroupUUID = newValue as! String?

            case SharingGroup.sharingGroupNameKey:
                sharingGroupName = newValue as! String?
            
            case SharingGroup.deletedKey:
                deleted = newValue as! Bool?

            case SharingGroup.masterVersionKey:
                masterVersion = newValue as! MasterVersionInt?
                
            case SharingGroup.permissionKey:
                permission = newValue as! Permission?
                
            case SharingGroup.accountTypeKey:
                accountType = newValue as! String?

            case SharingGroup.owningUserIdKey:
                owningUserId = newValue as! UserId?

            default:
                assert(false)
            }
        }
        
        get {
            return getValue(forKey: key)
        }
    }
    
    override init() {
        super.init()
    }
    
    func typeConvertersToModel(propertyName:String) -> ((_ propertyValue:Any) -> Any?)? {
        switch propertyName {
            case SharingGroup.deletedKey:
                return {(x:Any) -> Any? in
                    return (x as! Int8) == 1
                }
            case SharingGroupUser.permissionKey:
                return {(x:Any) -> Any? in
                    return Permission(rawValue: x as! String)
                }
            default:
                return nil
        }
    }
    
    func toClient() -> SyncServerShared.SharingGroup  {
        let clientGroup = SyncServerShared.SharingGroup()!
        clientGroup.sharingGroupUUID = sharingGroupUUID
        clientGroup.sharingGroupName = sharingGroupName
        clientGroup.deleted = deleted
        clientGroup.masterVersion = masterVersion
        clientGroup.permission = permission
        clientGroup.sharingGroupUsers = sharingGroupUsers
        
        Log.debug("accountType: \(String(describing: accountType))")
        
        if let accountType = accountType {
            clientGroup.cloudStorageType = AccountType(rawValue: accountType)?.cloudStorageType?.rawValue
        }

        return clientGroup
    }
}

class SharingGroupRepository: Repository, RepositoryLookup {<commit_msg>Added comment.<commit_after>//
//  SharingGroupRepository.swift
//  Server
//
//  Created by <NAME> on 6/23/18.
//

// A sharing group is a group of users who are sharing a collection of files.

import Foundation
import LoggerAPI
import SyncServerShared

class SharingGroup : NSObject, Model {
    static let sharingGroupUUIDKey = "sharingGroupUUID"
    var sharingGroupUUID: String!
    
    static let sharingGroupNameKey = "sharingGroupName"
    var sharingGroupName: String!
    
    static let deletedKey = "deleted"
    var deleted:Bool!
    
    // Not a part of this table, but a convenience for doing joins with the MasterVersion table.
    static let masterVersionKey = "masterVersion"
    var masterVersion: MasterVersionInt!

    // Similarly, not part of this table. For doing joins.
    public static let permissionKey = "permission"
    public var permission:Permission?
    
    // Also not part of this table. For doing fetches of sharing group users for the sharing group.
    public var sharingGroupUsers:[SyncServerShared.SharingGroupUser]!

    static let accountTypeKey = "accountType"
    var accountType: String!

    static let owningUserIdKey = "owningUserId"
    var owningUserId:UserId?

    subscript(key:String) -> Any? {
        set {
            switch key {
            case SharingGroup.sharingGroupUUIDKey:
                sharingGroupUUID = newValue as! String?

            case SharingGroup.sharingGroupNameKey:
                sharingGroupName = newValue as! String?
            
            case SharingGroup.deletedKey:
                deleted = newValue as! Bool?

            case SharingGroup.masterVersionKey:
                masterVersion = newValue as! MasterVersionInt?
                
            case SharingGroup.permissionKey:
                permission = newValue as! Permission?
                
            case SharingGroup.accountTypeKey:
                accountType = newValue as! String?

            case SharingGroup.owningUserIdKey:
                owningUserId = newValue as! UserId?

            default:
                assert(false)
            }
        }
        
        get {
            return getValue(forKey: key)
        }
    }
    
    override init() {
        super.init()
    }
    
    func typeConvertersToModel(propertyName:String) -> ((_ propertyValue:Any) -> Any?)? {
        switch propertyName {
            case SharingGroup.deletedKey:
                return {(x:Any) -> Any? in
                    return (x as! Int8) == 1
                }
            case SharingGroupUser.permissionKey:
                return {(x:Any) -> Any? in
                    return Permission(rawValue: x as! String)
                }
            default:
                return nil
        }
    }
    
    func toClient() -> SyncServerShared.SharingGroup  {
        let clientGroup = SyncServerShared.SharingGroup()!
        clientGroup.sharingGroupUUID = sharingGroupUUID
        clientGroup.sharingGroupName = sharingGroupName
        clientGroup.deleted = deleted
        clientGroup.masterVersion = masterVersion
        clientGroup.permission = permission
        clientGroup.sharingGroupUsers = sharingGroupUsers
        
        Log.debug("accountType: \(String(describing: accountType)) (expected to be nil for an owning user)")
        
        if let accountType = accountType {
            clientGroup.cloudStorageType = AccountType(rawValue: accountType)?.cloudStorageType?.rawValue
        }

        return clientGroup
    }
}

class SharingGroupRepository: Repository, RepositoryLookup {
{'fraction_non_alphanumeric': 0.06856257919189075, 'fraction_numerical': 0.0019709981697874136, 'mean_word_length': 2.0165605095541403, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '5010181', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1771, 'n_words': 503}