Today's Menu  Portugal
journal and plan nutrition
MenuItem+CoreDataClass.m
Go to the documentation of this file.
1 //
2 // MenuItem+CoreDataClass.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 6/7/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 //
9 
11 
12 @implementation MenuItem
13 
14 + (MenuItem *) findMenuItemWithRandomkey:(NSString *)randomkey context:(NSManagedObjectContext *)moc
15 {
16  NSArray *dupDoctorN = [self menuItemWithKey:randomkey context:moc];
17  MenuItem *result = [dupDoctorN lastObject];
18  if (result) {
19  // NSLog(@"MenuItem+CoreDataClass.findOrCreate: Reuse MenuItem with randomkey: %@ name: %@", randomkey, [result name]);
20  }
21  return result;
22 }
23 
24 + (MenuItem *) findOrCreateMenuItemWithRandomkey:(NSString *)key context:(NSManagedObjectContext *)moc
25 {
26  NSArray *dupDoctorN = [self menuItemWithKey:key context:moc];
27  MenuItem *result = [dupDoctorN lastObject];
28  if (result) {
29  // NSLog(@"MenuItem+CoreDataClass.findOrCreate: Reuse MenuItem with randomkey: %@ name: %@", key, [result name]);
30  } else {
31  // NSLog(@"MenuItem+CoreDataClass.findOrCreate: Create new MenuItem with randomkey: %@", key);
32  result = [NSEntityDescription insertNewObjectForEntityForName:@"MenuItem" inManagedObjectContext:moc];
33  NSAssert(result, @"MenuItem insert failed");
34  result.randomkey = key;
35  }
36  return result;
37 }
38 
39 + (MenuItem *) findMenuItemWithFileIdentification:(NSString *)googlefileid
40  context:(NSManagedObjectContext *) moc
41 {
42  NSArray *dupDoctorN = [self menuItemWithFileid:googlefileid context:moc];
43  MenuItem *result = [dupDoctorN lastObject];
44  // if (result) { NSLog(@"Reuse MenuItem with file-id: %@", googlefileid); }
45  return result;
46 }
47 
48 + (MenuItem *) findOrCreateMenuItemWithFileIdentification:(NSString *)googlefileid context:(NSManagedObjectContext *)moc
49 {
50  NSArray *dupDoctorN = [self menuItemWithFileid:googlefileid context:moc];
51  MenuItem *result = [dupDoctorN lastObject];
52  if (result) {
53  // NSLog(@"MenuItem+CoreDataClass.findOrCreate: Reuse MenuItem with file-id: %@ name: %@", googlefileid, [result name]);
54  } else {
55  // NSLog(@"MenuItem+CoreDataClass.findOrCreate: Create new MenuItem with file-id: %@", googlefileid);
56  result = [NSEntityDescription insertNewObjectForEntityForName:@"MenuItem" inManagedObjectContext:moc];
57  NSAssert(result, @"MenuItem insert failed");
58  result.remoteUniqueIID = googlefileid;
59  }
60  return result;
61 }
62 
63 + (NSArray *) menuItemWithUID:(NSString *)uidkey context:(NSManagedObjectContext *)moc
64 {
65  if (nil == uidkey) {
66  // NSLog(@"ERROR: nil randomkey at MenuItem.menuItemWithKey");
67  return nil;
68  }
69  NSManagedObjectModel *mom = moc.persistentStoreCoordinator.managedObjectModel;
70 
71  NSFetchRequest *menuItemKeyFetchRequest =
72  [mom fetchRequestFromTemplateWithName:@"MenuItemByUID" substitutionVariables:@{@"x" : uidkey} ];
73 
74  menuItemKeyFetchRequest.fetchBatchSize = 1;
75 
76  NSError *error = nil;
77  NSArray *results = [moc executeFetchRequest:menuItemKeyFetchRequest error:&error];
78  if (!results) {
79  // NSLog(@"MenuItem+CoreDataClass.menuItemWithKey: error.localizedDescription: %@", [error localizedDescription]);
80  }
81 
82  return results;
83 }
84 
85 + (NSArray<MenuItem *> *) findNextMenuItemForGallery: (NSNumber *) galleryNo afterPosition: (NSNumber *) positionInGallery context:(NSManagedObjectContext *)moc
86 {
87  NSManagedObjectModel *mom = moc.persistentStoreCoordinator.managedObjectModel;
88 
89  NSFetchRequest *menuItemKeyFetchRequest =
90  [mom fetchRequestFromTemplateWithName:@"MenuItemByGalleryAndPosition" substitutionVariables:@{@"x" : galleryNo, @"y" : positionInGallery} ];
91 
92  menuItemKeyFetchRequest.fetchBatchSize = 1;
93 
94  NSError *error = nil;
95  NSArray *results = [moc executeFetchRequest:menuItemKeyFetchRequest error:&error];
96  if (!results) {
97  NSLog(@"MenuItem+CoreDataClass.findNextMenuItemForGallery: error.localizedDescription: %@", [error localizedDescription]);
98  }
99 
100  return results;
101 
102 }
103 + (NSArray<MenuItem *> *) retrieveAllMenuItemsForGallery:(NSNumber *) galleryNo
104  context:(NSManagedObjectContext *)moc
105 {
106  // NSLog(@"MenuItem+CoreDataClass.retrieveAllMenuItemsForGallery: %@", galleryNo);
107  NSManagedObjectModel *mom = moc.persistentStoreCoordinator.managedObjectModel;
108 
109  NSFetchRequest *galleryFetchRequest =
110  [mom fetchRequestFromTemplateWithName:@"AllMenuItemsInGallery" substitutionVariables:@{@"x" : galleryNo} ];
111  NSSortDescriptor *modelDescriptor = [NSSortDescriptor
112  sortDescriptorWithKey:@"galleryPositionNo"
113  ascending:YES
114  selector:@selector(compare:)];
115  NSArray<NSSortDescriptor *> *descriptors = @[modelDescriptor];
116 
117  [galleryFetchRequest setSortDescriptors:descriptors];
118 
119  galleryFetchRequest.fetchBatchSize = 30;
120 
121  NSError *error = nil;
122  NSArray *results = [moc executeFetchRequest:galleryFetchRequest error:&error];
123  if (!results) {
124  NSLog(@"MenuItem+CoreDataClass.retrieveAllMenuItemsForGallery: error.localizedDescription: %@", [error localizedDescription]);
125  }
126 
127  return results;
128 
129 }
130 
131 + (NSArray *) menuItemWithKey:(NSString *)randomkey context:(NSManagedObjectContext *)moc
132 {
133  if (nil == randomkey) {
134  // NSLog(@"ERROR: nil randomkey at MenuItem.menuItemWithKey");
135  return nil;
136  }
137  NSManagedObjectModel *mom = moc.persistentStoreCoordinator.managedObjectModel;
138 
139  NSFetchRequest *menuItemKeyFetchRequest =
140  [mom fetchRequestFromTemplateWithName:@"MenuItemByKey" substitutionVariables:@{@"x" : randomkey} ];
141 
142  menuItemKeyFetchRequest.fetchBatchSize = 1;
143 
144  NSError *error = nil;
145  NSArray *results = [moc executeFetchRequest:menuItemKeyFetchRequest error:&error];
146  if (!results) {
147  NSLog(@"MenuItem+CoreDataClass.menuItemWithKey: error.localizedDescription: %@", [error localizedDescription]);
148  }
149 
150  return results;
151 }
152 
153 + (NSArray *) menuItemWithFileid:(NSString *)googlefileid context:(NSManagedObjectContext *)moc
154 {
155  if (nil == googlefileid) {
156  NSLog(@"MenuItem+CoreDataClass.menuItemWithKey: ERROR: nil googlefileid at MenuItem.menuItemWithFileid");
157  return nil;
158  }
159  NSManagedObjectModel *mom = moc.persistentStoreCoordinator.managedObjectModel;
160 
161  NSFetchRequest *menuItemKeyFetchRequest =
162  [mom fetchRequestFromTemplateWithName:@"MenuItemByFileId" substitutionVariables:@{@"x" : googlefileid} ];
163 
164  menuItemKeyFetchRequest.fetchBatchSize = 1;
165 
166  NSError *error = nil;
167  NSArray *results = [moc executeFetchRequest:menuItemKeyFetchRequest error:&error];
168  if (!results) {
169  NSLog(@"%@", error.localizedDescription);
170  }
171 
172  return results;
173 }
174 @end
NSString * remoteUniqueIID
NSArray * menuItemWithFileid:context:(NSString *googlefileid, [context] NSManagedObjectContext *moc)
NSArray * menuItemWithKey:context:(NSString *randomkey, [context] NSManagedObjectContext *moc)