Sync.swift 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CoreData
  22. import UIKit
  23. /**
  24. Class to handle server sync.
  25. */
  26. class Sync{
  27. /**
  28. Starts the sync process.
  29. */
  30. init(){
  31. let url = buildUrl();
  32. sync(url: url)
  33. }
  34. /**
  35. Builds the URL to perform the sync against.
  36. :returns: The URL.
  37. */
  38. func buildUrl() -> URL{
  39. let url = URL(string: "https://margolariak.com/API/v1/sync.php?client=com.margolariak.app")
  40. //TODO add parameters
  41. return url!
  42. }
  43. /**
  44. Performs an asynchronous sync.
  45. It fethes the info from the server and stores as Core Data
  46. :param: url The url to sync.
  47. */
  48. func sync(url: URL){
  49. print("SYNC:LOG: Sync started.")
  50. //Synchronously get data
  51. let task = URLSession.shared.dataTask(with: url) { data, response, error in
  52. guard error == nil else {
  53. print("SYNC:ERROR: Unknown error.")
  54. return
  55. }
  56. guard let data = data else {
  57. print("SYNC:ERROR: Data is empty.")
  58. return
  59. }
  60. print("SYNC:LOG: Data received.")
  61. var strData = String(data:data, encoding: String.Encoding.utf8)
  62. let dataIdx = strData?.indexOf(target: "{\"data\"")
  63. strData = strData!.subStr(start: dataIdx!, end: strData!.length - 1)
  64. //TODO: do something with versions
  65. //Get tables
  66. let dataActivity : [String] = self.getTable(data: strData!, table: "activity")
  67. let dataActivityComment : [String] = self.getTable(data: strData!, table: "activity_comment")
  68. let dataActivityImage : [String] = self.getTable(data: strData!, table: "activity_image")
  69. let dataActivityTag : [String] = self.getTable(data: strData!, table: "activity_tag")
  70. let dataActivityItinerary : [String] = self.getTable(data: strData!, table: "activity_itinerary")
  71. let dataAlbum : [String] = self.getTable(data: strData!, table: "album")
  72. //let dataFestival : [String] = self.getTable(data: strData!, table: "festival")
  73. //let dataFestivalDay : [String] = self.getTable(data: strData!, table: "festival_day")
  74. //let dataFestivalEvent : [String] = self.getTable(data: strData!, table: "festival_event")
  75. //let dataFestivalEventImage : [String] = self.getTable(data: strData!, table: "festival_event_image")
  76. //let dataFestivalOffer : [String] = self.getTable(data: strData!, table: "festival_offer")
  77. //let dataPeople : [String] = self.getTable(data: strData!, table: "people")
  78. let dataPhoto : [String] = self.getTable(data: strData!, table: "photo")
  79. let dataPhotoAlbum : [String] = self.getTable(data: strData!, table: "photo_album")
  80. //let dataPhotoComment : [String] = self.getTable(data: strData!, table: "photo_comment")
  81. let dataPlace : [String] = self.getTable(data: strData!, table: "place")
  82. let dataPost : [String] = self.getTable(data: strData!, table: "post")
  83. let dataPostComment : [String] = self.getTable(data: strData!, table: "post_comment")
  84. let dataPostImage : [String] = self.getTable(data: strData!, table: "post_image")
  85. let dataPostTag : [String] = self.getTable(data: strData!, table: "post_tag")
  86. //let dataSponsor : [String] = self.getTable(data: strData!, table: "sponsor")
  87. //One by one, save the received data
  88. self.saveTableActivity(entries : dataActivity)
  89. self.saveTableActivityComment(entries : dataActivityComment)
  90. self.saveTableActivityImage(entries : dataActivityImage)
  91. self.saveTableActivityTag(entries : dataActivityTag)
  92. self.saveTableActivityItinerary(entries : dataActivityItinerary)
  93. self.saveTablePost(entries: dataPost)
  94. self.saveTablePostComment(entries: dataPostComment)
  95. self.saveTablePostImage(entries: dataPostImage)
  96. self.saveTablePostTag(entries: dataPostTag)
  97. self.saveTablePlace(entries: dataPlace)
  98. self.saveTableAlbum(entries: dataAlbum)
  99. self.saveTablePhoto(entries: dataPhoto)
  100. self.saveTablePhotoAlbum(entries: dataPhotoAlbum)
  101. //DEBUG: Tester, Debug only
  102. // Test if a entity has entries
  103. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  104. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  105. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  106. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest() //Entity to debug
  107. do {
  108. //go get the results
  109. let searchResults = try context.fetch(fetchRequest)
  110. print("SYNC:DEBUG: Num of results = \(searchResults.count).")
  111. for r in searchResults as [NSManagedObject] {
  112. print("SYNC:DEBUG: id: \(r.value(forKey: "id")).")
  113. }
  114. } catch {
  115. print("SYNC:DEBUG: Error with request: \(error).")
  116. }
  117. //End tester
  118. }
  119. task.resume()
  120. }
  121. /**
  122. Extracts table rows from the raw data received.
  123. :param: param Received data in JSON format.
  124. :param: table The name of the table to look for.
  125. :return: Array of strings containing the rows of the table, in JSON format.
  126. */
  127. func getTable(data: String, table: String) -> [String]{
  128. //Does the table exists?
  129. if data.indexOf(target: "\"\(table)\":[") == nil{
  130. print("SYNC:LOG: Empty table: \(table).")
  131. return [String]()
  132. }
  133. let iS : Int? = data.indexOf(target: "\"\(table)\":[")
  134. var str = data.subStr(start: iS!, end: data.length - 1)
  135. let iE : Int? = str.indexOf(target: "}]}")!
  136. str = str.subStr(start: 0, end: iE!)
  137. str = str.subStr(start: table.length + 3,end: str.length - 1)
  138. //Separate the entries
  139. var entries : [String] = str.components(separatedBy: "},{")
  140. //Modify the first and the last one to remove bracers
  141. entries[0] = entries[0].subStr(start: 1, end: entries[0].length - 1)
  142. entries[entries.count - 1] = entries[entries.count - 1].subStr(start: 0, end: entries[entries.count - 1].length - 2)
  143. print("SYNC:LOG: Table \(table) has \(entries.count) rows.")
  144. //Return the array
  145. return entries
  146. }
  147. // FIX: ERROR in API.
  148. /**
  149. Saves the data in the table.
  150. :param: entries Array of strings containing the rows of the table, in JSON format.
  151. */
  152. func saveTablePeople(entries : [String]){
  153. print("SYNC:LOG: Saving table People.")
  154. //Set up context
  155. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  156. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  157. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  158. let entity = NSEntityDescription.entity(forEntityName: "People", in: context)
  159. var row: NSManagedObject
  160. //Delete all previous entries
  161. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "People")
  162. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  163. do {
  164. try context.execute(request)
  165. } catch let error as NSError {
  166. print("SYNC:ERROR: Could not clean up People entity: \(error), \(error.userInfo).")
  167. } catch {
  168. print("SYNC:ERROR: Could not clean up People entity")
  169. }
  170. //Loop new entries
  171. for entry in entries{
  172. //Get id
  173. var str = entry
  174. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  175. //Get name_es
  176. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  177. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  178. //Get name_en
  179. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  180. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  181. //Get name_eu
  182. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  183. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  184. //Get address_es
  185. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  186. let address_es : String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  187. //Get address_en
  188. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  189. let address_en : String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  190. //Get address_eu
  191. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  192. let address_eu : String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  193. //Get cp
  194. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  195. let cp : String = str.subStr(start : str.indexOf(target : "\"cp\":")! + 6, end : str.indexOf(target : ",\"")! - 2)
  196. //Get lat
  197. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  198. let lat : Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  199. //Get lon
  200. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  201. let lon : Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  202. //Save CoreData
  203. row = NSManagedObject(entity: entity!, insertInto: context)
  204. row.setValue(id, forKey: "id")
  205. row.setValue(name_es, forKey: "name_es")
  206. row.setValue(name_en, forKey: "name_en")
  207. row.setValue(name_eu, forKey: "name_eu")
  208. row.setValue(address_es, forKey: "address_es")
  209. row.setValue(address_en, forKey: "address_en")
  210. row.setValue(address_eu, forKey: "address_eu")
  211. row.setValue(cp, forKey: "cp")
  212. row.setValue(lat, forKey: "lat")
  213. row.setValue(lon, forKey: "lon")
  214. do {
  215. try context.save()
  216. } catch let error as NSError {
  217. print("SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  218. } catch {
  219. print("SYNC:ERROR: Could not store Place with id \(id).")
  220. }
  221. }
  222. }
  223. /**
  224. Saves the data in the table.
  225. :param: entries Array of strings containing the rows of the table, in JSON format.
  226. */
  227. func saveTablePlace(entries : [String]){
  228. print("SYNC:LOG: Saving table Place.")
  229. //Set up context
  230. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  231. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  232. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  233. let entity = NSEntityDescription.entity(forEntityName: "Place", in: context)
  234. var row: NSManagedObject
  235. //Delete all previous entries
  236. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Place")
  237. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  238. do {
  239. try context.execute(request)
  240. } catch let error as NSError {
  241. print("SYNC:ERROR: Could not clean up Places entity: \(error), \(error.userInfo).")
  242. } catch {
  243. print("SYNC:ERROR: Could not clean up Places entity.")
  244. }
  245. //Loop new entries
  246. for entry in entries{
  247. //Get id
  248. var str = entry
  249. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  250. //Get name_es
  251. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  252. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  253. //Get name_en
  254. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  255. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  256. //Get name_eu
  257. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  258. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  259. //Get address_es
  260. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  261. let address_es : String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  262. //Get address_en
  263. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  264. let address_en : String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  265. //Get address_eu
  266. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  267. let address_eu : String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  268. //Get cp
  269. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  270. let cp : String = str.subStr(start : str.indexOf(target : "\"cp\":")! + 6, end : str.indexOf(target : ",\"")! - 2)
  271. //Get lat
  272. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  273. let lat : Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  274. //Get lon
  275. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  276. let lon : Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  277. //Save CoreData
  278. row = NSManagedObject(entity: entity!, insertInto: context)
  279. row.setValue(id, forKey: "id")
  280. row.setValue(name_es, forKey: "name_es")
  281. row.setValue(name_en, forKey: "name_en")
  282. row.setValue(name_eu, forKey: "name_eu")
  283. row.setValue(address_es, forKey: "address_es")
  284. row.setValue(address_en, forKey: "address_en")
  285. row.setValue(address_eu, forKey: "address_eu")
  286. row.setValue(cp, forKey: "cp")
  287. row.setValue(lat, forKey: "lat")
  288. row.setValue(lon, forKey: "lon")
  289. do {
  290. try context.save()
  291. } catch let error as NSError {
  292. print("SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  293. } catch {
  294. print("SYNC:ERROR: Could not store Place with id \(id).")
  295. }
  296. }
  297. }
  298. /**
  299. Saves the data in the table.
  300. :param: entries Array of strings containing the rows of the table, in JSON format.
  301. */
  302. func saveTablePost(entries : [String]){
  303. print("SYNC:LOG: Saving table Post.")
  304. let dateFormatter = DateFormatter()
  305. dateFormatter.timeZone = TimeZone.ReferenceType.local
  306. //Set up context
  307. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  308. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  309. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  310. let entity = NSEntityDescription.entity(forEntityName: "Post", in: context)
  311. var row: NSManagedObject
  312. //Delete all previous entries
  313. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
  314. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  315. do {
  316. try context.execute(request)
  317. } catch let error as NSError {
  318. print("SYNC:ERROR: Could not clean up Post entity: \(error), \(error.userInfo).")
  319. } catch {
  320. print("SYNC:ERROR: Could not clean up Post entity.")
  321. }
  322. //Loop new entries
  323. for entry in entries{
  324. //Get id
  325. var str = entry
  326. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  327. //Get permalink
  328. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  329. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  330. //Get title_es
  331. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  332. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  333. //Get title_en
  334. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  335. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  336. //Get title_eu
  337. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  338. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  339. //Get text_es
  340. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  341. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  342. //Get text_en
  343. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  344. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  345. //Get text_eu
  346. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  347. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  348. //Get comments
  349. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  350. let comments : Int = Int(str.subStr(start : str.indexOf(target : "\"comments\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  351. //Get username
  352. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  353. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  354. //Get dtime
  355. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  356. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  357. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.length - 2))!
  358. //Save CoreData
  359. row = NSManagedObject(entity: entity!, insertInto: context)
  360. //set the entity values
  361. row.setValue(id, forKey: "id")
  362. row.setValue(permalink, forKey: "permalink")
  363. row.setValue(dtime, forKey: "dtime")
  364. row.setValue(username, forKey: "username")
  365. row.setValue(comments, forKey: "comments")
  366. row.setValue(title_es, forKey: "title_es")
  367. row.setValue(title_en, forKey: "title_en")
  368. row.setValue(title_eu, forKey: "title_eu")
  369. row.setValue(text_es, forKey: "text_es")
  370. row.setValue(text_en, forKey: "text_en")
  371. row.setValue(text_eu, forKey: "text_eu")
  372. do {
  373. try context.save()
  374. } catch let error as NSError {
  375. print("SYNC:ERROR: Could not store Post with id \(id): \(error), \(error.userInfo).")
  376. } catch {
  377. print("SYNC:ERROR: Could not store Post with id \(id).")
  378. }
  379. }
  380. }
  381. /**
  382. Saves the data in the table.
  383. :param: entries Array of strings containing the rows of the table, in JSON format.
  384. */
  385. func saveTablePostComment(entries : [String]){
  386. print("SYNC:LOG: Saving table Post_comment.")
  387. let dateFormatter = DateFormatter()
  388. dateFormatter.timeZone = TimeZone.ReferenceType.local
  389. //Set up context
  390. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  391. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  392. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  393. let entity = NSEntityDescription.entity(forEntityName: "Post_comment", in: context)
  394. var row: NSManagedObject
  395. //Delete all previous entries
  396. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_comment")
  397. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  398. do {
  399. try context.execute(request)
  400. } catch let error as NSError {
  401. print("SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  402. } catch {
  403. print("SYNC:ERROR: Could not clean up Post_comment entity.")
  404. }
  405. //Loop new entries
  406. for entry in entries{
  407. //Get id
  408. var str = entry
  409. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  410. //Get activity
  411. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  412. let post : String = str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  413. //Get text
  414. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  415. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  416. //Get dtime
  417. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  418. dateFormatter.dateFormat = "yyyy-MM-dd"
  419. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  420. //Get username
  421. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  422. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  423. //Get lang
  424. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  425. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  426. //Save CoreData
  427. row = NSManagedObject(entity: entity!, insertInto: context)
  428. row.setValue(id, forKey: "id")
  429. row.setValue(post, forKey: "post")
  430. row.setValue(text, forKey: "text")
  431. row.setValue(dtime, forKey: "dtime")
  432. row.setValue(username, forKey: "username")
  433. row.setValue(lang, forKey: "lang")
  434. do {
  435. try context.save()
  436. } catch let error as NSError {
  437. print("SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  438. } catch {
  439. print("SYNC:ERROR: Could not store Activity_comment with id \(id).")
  440. }
  441. }
  442. }
  443. /**
  444. Saves the data in the table.
  445. :param: entries Array of strings containing the rows of the table, in JSON format.
  446. */
  447. func saveTablePostImage(entries : [String]){
  448. print("SYNC:LOG: Saving table Post_image.")
  449. //Set up context
  450. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  451. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  452. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  453. let entity = NSEntityDescription.entity(forEntityName: "Post_image", in: context)
  454. var row: NSManagedObject
  455. //Delete all previous entries
  456. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_image")
  457. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  458. do {
  459. try context.execute(request)
  460. } catch let error as NSError {
  461. print("SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  462. } catch {
  463. print("SYNC:ERROR: Could not clean up Post_comment entity.")
  464. }
  465. //Loop new entries
  466. for entry in entries{
  467. //Get id
  468. var str = entry
  469. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  470. //Get activity
  471. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  472. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  473. //Get image
  474. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  475. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  476. //Get idx
  477. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  478. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  479. //Save CoreData
  480. row = NSManagedObject(entity: entity!, insertInto: context)
  481. row.setValue(id, forKey: "id")
  482. row.setValue(post, forKey: "post")
  483. row.setValue(image, forKey: "image")
  484. row.setValue(idx, forKey: "idx")
  485. do {
  486. try context.save()
  487. } catch let error as NSError {
  488. print("SYNC:ERROR: Could not store Post_image with id \(id): \(error), \(error.userInfo).")
  489. } catch {
  490. print("SYNC:ERROR: Could not store Post_image with id \(id).")
  491. }
  492. }
  493. }
  494. /**
  495. Saves the data in the table.
  496. :param: entries Array of strings containing the rows of the table, in JSON format.
  497. */
  498. func saveTablePostTag(entries : [String]){
  499. print("SYNC:LOG: Saving table Post_tag.")
  500. //Set up context
  501. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  502. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  503. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  504. let entity = NSEntityDescription.entity(forEntityName: "Post_tag", in: context)
  505. var row: NSManagedObject
  506. //Delete all previous entries
  507. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_tag")
  508. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  509. do {
  510. try context.execute(request)
  511. } catch let error as NSError {
  512. print("SYNC:ERROR: Could not clean up Post_tag entity: \(error), \(error.userInfo).")
  513. } catch {
  514. print("SYNC:ERROR: Could not clean up Post_tag entity.")
  515. }
  516. //Loop new entries
  517. for entry in entries{
  518. //Get activity
  519. var str = entry
  520. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  521. //Get tag
  522. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  523. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  524. //Save CoreData
  525. row = NSManagedObject(entity: entity!, insertInto: context)
  526. row.setValue(post, forKey: "post")
  527. row.setValue(tag, forKey: "tag")
  528. do {
  529. try context.save()
  530. } catch let error as NSError {
  531. print("SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag): \(error), \(error.userInfo).")
  532. } catch {
  533. print("SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag).")
  534. }
  535. }
  536. }
  537. /**
  538. Saves the data in the table.
  539. :param: entries Array of strings containing the rows of the table, in JSON format.
  540. */
  541. func saveTableActivity(entries : [String]){
  542. print("SYNC:LOG: Saving table Activity.")
  543. let dateFormatter = DateFormatter()
  544. dateFormatter.timeZone = TimeZone.ReferenceType.local
  545. //Set up context
  546. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  547. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  548. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  549. let entity = NSEntityDescription.entity(forEntityName: "Activity", in: context)
  550. var row: NSManagedObject
  551. //Delete all previous entries
  552. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity")
  553. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  554. do {
  555. try context.execute(request)
  556. } catch let error as NSError {
  557. print("SYNC:ERROR: Could not clean up Activity entity: \(error), \(error.userInfo).")
  558. } catch {
  559. print("SYNC:ERROR: Could not clean up Activity entity.")
  560. }
  561. //Loop new entries
  562. for entry in entries{
  563. //Get id
  564. var str = entry
  565. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  566. //Get permalink
  567. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  568. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  569. //Get date
  570. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  571. dateFormatter.dateFormat = "yyyy-MM-dd"
  572. let date = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  573. //Get city
  574. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  575. let city : String = str.subStr(start : str.indexOf(target : "\"city\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  576. //Get title_es
  577. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  578. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  579. //Get title_en
  580. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  581. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  582. //Get title_eu
  583. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  584. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  585. //Get text_es
  586. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  587. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  588. //Get text_eu
  589. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  590. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  591. //Get text_en
  592. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  593. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  594. //Get after_es
  595. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  596. var after_es : String = str.subStr(start : str.indexOf(target : "\"after_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  597. if (after_es == "ul"){ //From "null"
  598. after_es = ""
  599. }
  600. //Get after_en
  601. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  602. var after_en : String = str.subStr(start : str.indexOf(target : "\"after_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  603. if (after_en == "ul"){ //From "null"
  604. after_en = ""
  605. }
  606. //Get after_eu
  607. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  608. var after_eu : String = str.subStr(start : str.indexOf(target : "\"after_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  609. if (after_eu == "ul"){ //From "null"
  610. after_eu = ""
  611. }
  612. //Get price
  613. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  614. let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  615. //Get inscription
  616. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  617. let inscription : Int = Int(str.subStr(start : str.indexOf(target : "\"inscription\":")! + 15, end : str.indexOf(target : ",\"")! - 2))!
  618. //Get max_people
  619. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  620. let maxPeoplePre : String = str.subStr(start : str.indexOf(target : "\"max_people\":")! + 14, end : str.indexOf(target : ",")! - 2)
  621. var maxPeople = -1
  622. if (maxPeoplePre != "ul"){ //From "null"
  623. maxPeople = Int(maxPeoplePre)!
  624. }
  625. //Get album
  626. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  627. let albumPre = str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2)
  628. var album = -1
  629. if (albumPre != "ul"){ //From "null"
  630. album = Int(albumPre)!
  631. }
  632. //Skipping dtime
  633. //Skipping comments
  634. //Save CoreData
  635. row = NSManagedObject(entity: entity!, insertInto: context)
  636. //set the entity values
  637. row.setValue(id, forKey: "id")
  638. row.setValue(permalink, forKey: "permalink")
  639. row.setValue(date, forKey: "date")
  640. row.setValue(city, forKey: "city")
  641. row.setValue(title_es, forKey: "title_es")
  642. row.setValue(title_en, forKey: "title_en")
  643. row.setValue(title_eu, forKey: "title_eu")
  644. row.setValue(text_es, forKey: "text_es")
  645. row.setValue(text_en, forKey: "text_en")
  646. row.setValue(text_eu, forKey: "text_eu")
  647. row.setValue(after_es, forKey: "after_es")
  648. row.setValue(after_en, forKey: "after_en")
  649. row.setValue(after_eu, forKey: "after_eu")
  650. row.setValue(price, forKey: "price")
  651. row.setValue(inscription, forKey: "inscription")
  652. row.setValue(maxPeople, forKey: "max_people")
  653. if (album != -1){
  654. row.setValue(album, forKey: "album")
  655. }
  656. do {
  657. try context.save()
  658. } catch let error as NSError {
  659. print("SYNC:ERROR: Could not store Activity with id \(id): \(error), \(error.userInfo).")
  660. } catch {
  661. print("SYNC:ERROR: Could not store Activity with id \(id).")
  662. }
  663. }
  664. }
  665. /**
  666. Saves the data in the table.
  667. :param: entries Array of strings containing the rows of the table, in JSON format.
  668. */
  669. func saveTableActivityComment(entries : [String]){
  670. print("SYNC:LOG: Saving table Activity_comment.")
  671. let dateFormatter = DateFormatter()
  672. dateFormatter.timeZone = TimeZone.ReferenceType.local
  673. //Set up context
  674. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  675. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  676. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  677. let entity = NSEntityDescription.entity(forEntityName: "Activity_comment", in: context)
  678. var row: NSManagedObject
  679. //Delete all previous entries
  680. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_comment")
  681. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  682. do {
  683. try context.execute(request)
  684. } catch let error as NSError {
  685. print("SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  686. } catch {
  687. print("SYNC:ERROR: Could not clean up Activity_comment entity.")
  688. }
  689. //Loop new entries
  690. for entry in entries{
  691. //Get id
  692. var str = entry
  693. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  694. //Get activity
  695. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  696. let activity : String = str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  697. //Get text
  698. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  699. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  700. //Get dtime
  701. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  702. dateFormatter.dateFormat = "yyyy-MM-dd"
  703. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  704. //Get username
  705. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  706. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  707. //Get lang
  708. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  709. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  710. //Save CoreData
  711. row = NSManagedObject(entity: entity!, insertInto: context)
  712. row.setValue(id, forKey: "id")
  713. row.setValue(activity, forKey: "activity")
  714. row.setValue(text, forKey: "text")
  715. row.setValue(dtime, forKey: "dtime")
  716. row.setValue(username, forKey: "username")
  717. row.setValue(lang, forKey: "lang")
  718. do {
  719. try context.save()
  720. } catch let error as NSError {
  721. print("SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  722. } catch {
  723. print("SYNC:ERROR: Could not store Activity_comment with id \(id).")
  724. }
  725. }
  726. }
  727. /**
  728. Saves the data in the table.
  729. :param: entries Array of strings containing the rows of the table, in JSON format.
  730. */
  731. func saveTableActivityImage(entries : [String]){
  732. print("SYNC:LOG: Saving table Activity_image.")
  733. //Set up context
  734. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  735. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  736. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  737. let entity = NSEntityDescription.entity(forEntityName: "Activity_image", in: context)
  738. var row: NSManagedObject
  739. //Delete all previous entries
  740. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_image")
  741. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  742. do {
  743. try context.execute(request)
  744. } catch let error as NSError {
  745. print("SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  746. } catch {
  747. print("SYNC:ERROR: Could not clean up Activity_comment entity.")
  748. }
  749. //Loop new entries
  750. for entry in entries{
  751. //Get id
  752. var str = entry
  753. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  754. //Get activity
  755. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  756. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  757. //Get image
  758. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  759. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  760. //Get idx
  761. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  762. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  763. //Save CoreData
  764. row = NSManagedObject(entity: entity!, insertInto: context)
  765. row.setValue(id, forKey: "id")
  766. row.setValue(activity, forKey: "activity")
  767. row.setValue(image, forKey: "image")
  768. row.setValue(idx, forKey: "idx")
  769. do {
  770. try context.save()
  771. } catch let error as NSError {
  772. print("SYNC:ERROR: Could not store Activity_image with id \(id): \(error), \(error.userInfo).")
  773. } catch {
  774. print("SYNC:ERROR: Could not store Activity_image with id \(id).")
  775. }
  776. }
  777. }
  778. /**
  779. Saves the data in the table.
  780. :param: entries Array of strings containing the rows of the table, in JSON format.
  781. */
  782. func saveTableActivityTag(entries : [String]){
  783. print("SYNC:LOG: Saving table Activity_tag.")
  784. //Set up context
  785. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  786. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  787. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  788. let entity = NSEntityDescription.entity(forEntityName: "Activity_tag", in: context)
  789. var row: NSManagedObject
  790. //Delete all previous entries
  791. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_tag")
  792. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  793. do {
  794. try context.execute(request)
  795. } catch let error as NSError {
  796. print("SYNC:ERROR: Could not clean up Activity_tag entity: \(error), \(error.userInfo).")
  797. } catch {
  798. print("SYNC:ERROR: Could not clean up Activity_tag entity.")
  799. }
  800. //Loop new entries
  801. for entry in entries{
  802. //Get activity
  803. var str = entry
  804. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  805. //Get tag
  806. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  807. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  808. //Save CoreData
  809. row = NSManagedObject(entity: entity!, insertInto: context)
  810. row.setValue(activity, forKey: "activity")
  811. row.setValue(tag, forKey: "tag")
  812. do {
  813. try context.save()
  814. } catch let error as NSError {
  815. print("SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag): Error: \(error), \(error.userInfo).")
  816. } catch {
  817. print("SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag).")
  818. }
  819. }
  820. }
  821. /**
  822. Saves the data in the table.
  823. :param: entries Array of strings containing the rows of the table, in JSON format.
  824. */
  825. //CHECK: New (and probably better format than the others.)
  826. func saveTableActivityItinerary(entries : [String]){
  827. print("SYNC:LOG: Saving table Activity_itinerary.")
  828. let dateFormatter = DateFormatter()
  829. dateFormatter.timeZone = TimeZone.ReferenceType.local
  830. //Set up context
  831. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  832. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  833. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  834. let entity = NSEntityDescription.entity(forEntityName: "Activity_itinerary", in: context)
  835. var row: NSManagedObject
  836. //Delete all previous entries
  837. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_itinerary")
  838. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  839. do {
  840. try context.execute(request)
  841. } catch let error as NSError {
  842. print("SYNC:ERROR: Could not clean up Activity_itinerary entity: \(error), \(error.userInfo).")
  843. } catch {
  844. print("SYNC:ERROR: Could not clean up Activity_itinerary entity.")
  845. }
  846. //Loop new entries
  847. for entry in entries{
  848. row = NSManagedObject(entity: entity!, insertInto: context)
  849. //Get id
  850. var str = entry
  851. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  852. row.setValue(id, forKey: "id")
  853. //Get activity
  854. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  855. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  856. row.setValue(activity, forKey: "activity")
  857. //Get name_es
  858. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  859. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  860. row.setValue(name_es, forKey: "name_es")
  861. //Get name_en
  862. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  863. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  864. row.setValue(name_en, forKey: "name_en")
  865. //Get name_eu
  866. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  867. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  868. row.setValue(name_eu, forKey: "name_eu")
  869. //Get description_es
  870. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  871. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  872. if (description_es != "ul"){ //From "null"
  873. row.setValue(description_es, forKey: "description_es")
  874. }
  875. //Get description_en
  876. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  877. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  878. if (description_en != "ul"){ //From "null"
  879. row.setValue(description_en, forKey: "description_en")
  880. }
  881. //Get description_eu
  882. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  883. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  884. if (description_eu != "ul"){ //From "null"
  885. row.setValue(description_eu, forKey: "description_eu")
  886. }
  887. //Get start
  888. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  889. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  890. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  891. row.setValue(start, forKey: "start")
  892. //Get end
  893. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  894. let end = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  895. if (end != "ul"){ //From "null"
  896. row.setValue(dateFormatter.date(from: end), forKey: "end")
  897. }
  898. //Get place
  899. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  900. let place : Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.length - 2))!
  901. row.setValue(place, forKey: "place")
  902. //Save CoreData
  903. do {
  904. try context.save()
  905. } catch let error as NSError {
  906. print("SYNC:ERROR: Could not store Activity_itinerary with id \(id): \(error), \(error.userInfo).")
  907. } catch {
  908. print("SYNC:ERROR: Could not store Activity_itinerary with id \(id).")
  909. }
  910. }
  911. }
  912. /**
  913. Saves the data in the table.
  914. :param: entries Array of strings containing the rows of the table, in JSON format.
  915. */
  916. func saveTableAlbum(entries : [String]){
  917. print("SYNC:LOG: Saving table Album.")
  918. //Set up context
  919. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  920. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  921. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  922. let entity = NSEntityDescription.entity(forEntityName: "Album", in: context)
  923. var row: NSManagedObject
  924. //Delete all previous entries
  925. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Album")
  926. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  927. do {
  928. try context.execute(request)
  929. } catch let error as NSError {
  930. print("SYNC:ERROR: Could not clean up Album: \(error), \(error.userInfo).")
  931. } catch {
  932. print("SYNC:ERROR: Could not clean up Album entity.")
  933. }
  934. //Loop new entries
  935. for entry in entries{
  936. row = NSManagedObject(entity: entity!, insertInto: context)
  937. //Get id
  938. var str = entry
  939. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  940. row.setValue(id, forKey: "id")
  941. //Get permalink
  942. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  943. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  944. row.setValue(permalink, forKey: "permalink")
  945. //Get title_es
  946. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  947. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  948. row.setValue(title_es, forKey: "title_es")
  949. //Get title_en
  950. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  951. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  952. row.setValue(title_en, forKey: "title_en")
  953. //Get title_eu
  954. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  955. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  956. row.setValue(title_eu, forKey: "title_eu")
  957. //Get description_es
  958. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  959. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  960. if (description_es != "ul"){ //From "null"
  961. row.setValue(description_es, forKey: "description_es")
  962. }
  963. //Get description_en
  964. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  965. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  966. if (description_en != "ul"){ //From "null"
  967. row.setValue(description_en, forKey: "description_en")
  968. }
  969. //Get description_eu
  970. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  971. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  972. if (description_eu != "ul"){ //From "null"
  973. row.setValue(description_eu, forKey: "description_eu")
  974. }
  975. //Get open
  976. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  977. let open : Int = Int(str.subStr(start : str.indexOf(target : "\"open\":")! + 8, end : str.length - 2))!
  978. row.setValue(open, forKey: "open")
  979. //Save CoreData
  980. do {
  981. try context.save()
  982. } catch let error as NSError {
  983. print("SYNC:ERROR: Could not store Album with id \(id): \(error), \(error.userInfo).")
  984. } catch {
  985. print("SYNC:ERROR: Could not store Album with id \(id).")
  986. }
  987. }
  988. }
  989. /**
  990. Saves the data in the table.
  991. :param: entries Array of strings containing the rows of the table, in JSON format.
  992. */
  993. func saveTablePhoto(entries : [String]){
  994. print("SYNC:LOG: Saving table Photo.")
  995. let dateFormatter = DateFormatter()
  996. dateFormatter.timeZone = TimeZone.ReferenceType.local
  997. //Set up context
  998. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  999. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1000. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1001. let entity = NSEntityDescription.entity(forEntityName: "Photo", in: context)
  1002. var row: NSManagedObject
  1003. //Delete all previous entries
  1004. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo")
  1005. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1006. do {
  1007. try context.execute(request)
  1008. } catch let error as NSError {
  1009. print("SYNC:ERROR: Could not clean up Photo: \(error), \(error.userInfo).")
  1010. } catch {
  1011. print("SYNC:ERROR: Could not clean up Photo entity.")
  1012. }
  1013. //Loop new entries
  1014. for entry in entries{
  1015. row = NSManagedObject(entity: entity!, insertInto: context)
  1016. //Get id
  1017. var str = entry
  1018. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1019. row.setValue(id, forKey: "id")
  1020. //Get file
  1021. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1022. let file : String = str.subStr(start : str.indexOf(target : "\"file\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1023. row.setValue(file, forKey: "file")
  1024. //Get permalink
  1025. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1026. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  1027. row.setValue(permalink, forKey: "permalink")
  1028. //Get title_es
  1029. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1030. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1031. row.setValue(title_es, forKey: "title_es")
  1032. //Get title_en
  1033. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1034. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1035. row.setValue(title_en, forKey: "title_en")
  1036. //Get title_eu
  1037. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1038. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1039. row.setValue(title_eu, forKey: "title_eu")
  1040. //Get description_es
  1041. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1042. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1043. if (description_es != "ul"){ //From "null"
  1044. row.setValue(description_es, forKey: "description_es")
  1045. }
  1046. //Get description_en
  1047. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1048. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1049. if (description_en != "ul"){ //From "null"
  1050. row.setValue(description_en, forKey: "description_en")
  1051. }
  1052. //Get description_eu
  1053. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1054. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1055. if (description_eu != "ul"){ //From "null"
  1056. row.setValue(description_eu, forKey: "description_eu")
  1057. }
  1058. //Get uploaded
  1059. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1060. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1061. let uploaded = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"uploaded\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  1062. row.setValue(uploaded, forKey: "uploaded")
  1063. //TODO: Ignoting place
  1064. //TODO: Ignoring width
  1065. //TODO: Ignoring height
  1066. //TODO: Ignoring size
  1067. //TODO: Ignoring username
  1068. //Save CoreData
  1069. do {
  1070. try context.save()
  1071. } catch let error as NSError {
  1072. print("SYNC:ERROR: Could not store Photo with id \(id): \(error), \(error.userInfo).")
  1073. } catch {
  1074. print("SYNC:ERROR: Could not store Photo with id \(id).")
  1075. }
  1076. }
  1077. }
  1078. /**
  1079. Saves the data in the table.
  1080. :param: entries Array of strings containing the rows of the table, in JSON format.
  1081. */
  1082. func saveTablePhotoAlbum(entries : [String]){
  1083. print("SYNC:LOG: Saving table Photo_album.")
  1084. let dateFormatter = DateFormatter()
  1085. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1086. //Set up context
  1087. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1088. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1089. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1090. let entity = NSEntityDescription.entity(forEntityName: "Photo_album", in: context)
  1091. var row: NSManagedObject
  1092. //Delete all previous entries
  1093. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo_album")
  1094. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1095. do {
  1096. try context.execute(request)
  1097. } catch let error as NSError {
  1098. print("SYNC:ERROR: Could not clean up Photo_album: \(error), \(error.userInfo).")
  1099. } catch {
  1100. print("SYNC:ERROR: Could not clean up Photo_album entity.")
  1101. }
  1102. //Loop new entries
  1103. for entry in entries{
  1104. row = NSManagedObject(entity: entity!, insertInto: context)
  1105. //Get photo
  1106. var str = entry
  1107. let photo : Int = Int(str.subStr(start : str.indexOf(target : "\"photo\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1108. row.setValue(photo, forKey: "photo")
  1109. //Get album
  1110. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1111. let album : Int = Int(str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2))!
  1112. row.setValue(album, forKey: "album")
  1113. //Save CoreData
  1114. do {
  1115. try context.save()
  1116. } catch let error as NSError {
  1117. print("SYNC:ERROR: Could not store Photo_album entry \(error.userInfo).")
  1118. } catch {
  1119. print("SYNC:ERROR: Could not store Photo_album entry.")
  1120. }
  1121. }
  1122. }
  1123. }