FRAIdentityModel.m revision 6a2ae9c7fb4d2c40d75cab0edaf940f22c18224f
893N/A/*
2362N/A * The contents of this file are subject to the terms of the Common Development and
893N/A * Distribution License (the License). You may not use this file except in compliance with the
893N/A * License.
893N/A *
893N/A * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
893N/A * specific language governing permission and limitations under the License.
893N/A *
893N/A * When distributing Covered Software, include this CDDL Header Notice in each file and include
893N/A * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
893N/A * Header, with the fields enclosed by brackets [] replaced by your own identifying
893N/A * information: "Portions copyright [year] [name of copyright owner]".
893N/A *
893N/A * Copyright 2016 ForgeRock AS.
893N/A */
893N/A
893N/A#import "FRAFMDatabaseConnectionHelper.h"
893N/A#import "FRAIdentity.h"
2362N/A#import "FRAIdentityDatabase.h"
2362N/A#import "FRAIdentityDatabaseSQLiteOperations.h"
2362N/A#import "FRAIdentityModel.h"
893N/A#import "FRAMechanism.h"
893N/A#import "FRANotification.h"
893N/A#import "FRAModelsFromDatabase.h"
893N/A#import "FRAPushMechanism.h"
893N/A#import "FRAModelObjectProtected.h"
893N/A
893N/A/*!
893N/A * Private interface.
893N/A */
893N/A@interface FRAIdentityModel ()
893N/A
893N/A/*!
893N/A * The database to which this object graph is persisted.
893N/A */
893N/A@property (strong, nonatomic) FRAIdentityDatabase *database;
893N/A
893N/A@end
893N/A
893N/A
893N/A@implementation FRAIdentityModel {
893N/A
893N/A NSMutableArray<FRAIdentity*> *identitiesList;
893N/A
893N/A}
893N/A
893N/A#pragma mark -
893N/A#pragma mark Lifecyle
893N/A
893N/A- (instancetype)initWithDatabase:(FRAIdentityDatabase *)database sqlDatabase:(FRAFMDatabaseConnectionHelper *) sql {
893N/A if (self = [super init]) {
893N/A _database = database;
893N/A NSError *error;
893N/A NSArray<FRAIdentity*> *identities = [FRAModelsFromDatabase allIdentitiesWithDatabase:sql identityDatabase:database identityModel:self error:&error];
893N/A if (!identities) {
893N/A return nil;
893N/A }
893N/A identitiesList = [[NSMutableArray alloc] initWithArray:identities];
893N/A }
893N/A return self;
893N/A}
893N/A
893N/A#pragma mark -
893N/A#pragma mark Identity Functions
893N/A
893N/A- (NSArray *)identities {
893N/A return [[NSArray alloc] initWithArray:identitiesList];
893N/A}
893N/A
893N/A- (FRAIdentity *)identityWithIssuer:(NSString *)issuer accountName:(NSString *)accountName {
893N/A for (FRAIdentity *identity in identitiesList) {
893N/A if ([identity.issuer isEqualToString:issuer] && [identity.accountName isEqualToString:accountName]) {
893N/A return identity;
893N/A }
893N/A }
893N/A return nil;
}
- (BOOL)addIdentity:(FRAIdentity *)identity error:(NSError *__autoreleasing *)error {
[identitiesList addObject:identity];
return [self.database insertIdentity:identity error:error];
}
- (BOOL)removeIdentity:(FRAIdentity *)identity error:(NSError *__autoreleasing *)error {
[identitiesList removeObject:identity];
return [self.database deleteIdentity:identity error:error];
}
#pragma mark -
#pragma mark Mechanism Functions
- (FRAMechanism *)mechanismWithId:(NSString *)uid {
for (FRAIdentity *identity in identitiesList) {
for (FRAMechanism *mechanism in [identity mechanisms]) {
if ([mechanism isKindOfClass:[FRAPushMechanism class]] && [((FRAPushMechanism *)mechanism).mechanismUID isEqualToString: uid]) {
return mechanism;
}
}
}
return nil;
}
#pragma mark -
#pragma mark Notification Functions
- (NSInteger)pendingNotificationsCount {
NSInteger count = 0;
for (FRAIdentity *identity in self.identities) {
count += [identity pendingNotificationsCount];
}
return count;
}
@end