Sync.swift 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. //DEBUG: Tester, Debug only
  100. // Test if a entity has entries
  101. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  102. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  103. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  104. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest() //Entity to debug
  105. do {
  106. //go get the results
  107. let searchResults = try context.fetch(fetchRequest)
  108. print("SYNC:DEBUG: Num of results = \(searchResults.count).")
  109. for r in searchResults as [NSManagedObject] {
  110. print("SYNC:DEBUG: id: \(r.value(forKey: "id")).")
  111. }
  112. } catch {
  113. print("SYNC:DEBUG: Error with request: \(error).")
  114. }
  115. //End tester
  116. }
  117. task.resume()
  118. }
  119. /**
  120. Extracts table rows from the raw data received.
  121. :param: param Received data in JSON format.
  122. :param: table The name of the table to look for.
  123. :return: Array of strings containing the rows of the table, in JSON format.
  124. */
  125. func getTable(data: String, table: String) -> [String]{
  126. //Does the table exists?
  127. if data.indexOf(target: "\"\(table)\":[") == nil{
  128. print("SYNC:LOG: Empty table: \(table).")
  129. return [String]()
  130. }
  131. let iS : Int? = data.indexOf(target: "\"\(table)\":[")
  132. var str = data.subStr(start: iS!, end: data.length - 1)
  133. let iE : Int? = str.indexOf(target: "}]}")!
  134. str = str.subStr(start: 0, end: iE!)
  135. str = str.subStr(start: table.length + 3,end: str.length - 1)
  136. //Separate the entries
  137. var entries : [String] = str.components(separatedBy: "},{")
  138. //Modify the first and the last one to remove bracers
  139. entries[0] = entries[0].subStr(start: 1, end: entries[0].length - 1)
  140. entries[entries.count - 1] = entries[entries.count - 1].subStr(start: 0, end: entries[entries.count - 1].length - 2)
  141. print("SYNC:LOG: Table \(table) has \(entries.count) rows.")
  142. //Return the array
  143. return entries
  144. }
  145. // FIX: ERROR in API.
  146. /**
  147. Saves the data in the table.
  148. :param: entries Array of strings containing the rows of the table, in JSON format.
  149. */
  150. func saveTablePeople(entries : [String]){
  151. print("SYNC:LOG: Saving table People.")
  152. //Set up context
  153. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  154. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  155. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  156. let entity = NSEntityDescription.entity(forEntityName: "People", in: context)
  157. var row: NSManagedObject
  158. //Delete all previous entries
  159. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "People")
  160. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  161. do {
  162. try context.execute(request)
  163. } catch let error as NSError {
  164. print("SYNC:ERROR: Could not clean up People entity: \(error), \(error.userInfo).")
  165. } catch {
  166. print("SYNC:ERROR: Could not clean up People entity")
  167. }
  168. //Loop new entries
  169. for entry in entries{
  170. //Get id
  171. var str = entry
  172. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  173. //Get name_es
  174. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  175. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  176. //Get name_en
  177. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  178. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  179. //Get name_eu
  180. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  181. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  182. //Get address_es
  183. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  184. let address_es : String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  185. //Get address_en
  186. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  187. let address_en : String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  188. //Get address_eu
  189. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  190. let address_eu : String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  191. //Get cp
  192. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  193. let cp : String = str.subStr(start : str.indexOf(target : "\"cp\":")! + 6, end : str.indexOf(target : ",\"")! - 2)
  194. //Get lat
  195. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  196. let lat : Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  197. //Get lon
  198. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  199. let lon : Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  200. //Save CoreData
  201. row = NSManagedObject(entity: entity!, insertInto: context)
  202. row.setValue(id, forKey: "id")
  203. row.setValue(name_es, forKey: "name_es")
  204. row.setValue(name_en, forKey: "name_en")
  205. row.setValue(name_eu, forKey: "name_eu")
  206. row.setValue(address_es, forKey: "address_es")
  207. row.setValue(address_en, forKey: "address_en")
  208. row.setValue(address_eu, forKey: "address_eu")
  209. row.setValue(cp, forKey: "cp")
  210. row.setValue(lat, forKey: "lat")
  211. row.setValue(lon, forKey: "lon")
  212. do {
  213. try context.save()
  214. } catch let error as NSError {
  215. print("SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  216. } catch {
  217. print("SYNC:ERROR: Could not store Place with id \(id).")
  218. }
  219. }
  220. }
  221. /**
  222. Saves the data in the table.
  223. :param: entries Array of strings containing the rows of the table, in JSON format.
  224. */
  225. func saveTablePlace(entries : [String]){
  226. print("SYNC:LOG: Saving table Place.")
  227. //Set up context
  228. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  229. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  230. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  231. let entity = NSEntityDescription.entity(forEntityName: "Place", in: context)
  232. var row: NSManagedObject
  233. //Delete all previous entries
  234. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Place")
  235. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  236. do {
  237. try context.execute(request)
  238. } catch let error as NSError {
  239. print("SYNC:ERROR: Could not clean up Places entity: \(error), \(error.userInfo).")
  240. } catch {
  241. print("SYNC:ERROR: Could not clean up Places entity.")
  242. }
  243. //Loop new entries
  244. for entry in entries{
  245. //Get id
  246. var str = entry
  247. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  248. //Get name_es
  249. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  250. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  251. //Get name_en
  252. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  253. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  254. //Get name_eu
  255. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  256. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  257. //Get address_es
  258. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  259. let address_es : String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  260. //Get address_en
  261. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  262. let address_en : String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  263. //Get address_eu
  264. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  265. let address_eu : String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  266. //Get cp
  267. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  268. let cp : String = str.subStr(start : str.indexOf(target : "\"cp\":")! + 6, end : str.indexOf(target : ",\"")! - 2)
  269. //Get lat
  270. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  271. let lat : Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  272. //Get lon
  273. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  274. let lon : Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  275. //Save CoreData
  276. row = NSManagedObject(entity: entity!, insertInto: context)
  277. row.setValue(id, forKey: "id")
  278. row.setValue(name_es, forKey: "name_es")
  279. row.setValue(name_en, forKey: "name_en")
  280. row.setValue(name_eu, forKey: "name_eu")
  281. row.setValue(address_es, forKey: "address_es")
  282. row.setValue(address_en, forKey: "address_en")
  283. row.setValue(address_eu, forKey: "address_eu")
  284. row.setValue(cp, forKey: "cp")
  285. row.setValue(lat, forKey: "lat")
  286. row.setValue(lon, forKey: "lon")
  287. do {
  288. try context.save()
  289. } catch let error as NSError {
  290. print("SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  291. } catch {
  292. print("SYNC:ERROR: Could not store Place with id \(id).")
  293. }
  294. }
  295. }
  296. /**
  297. Saves the data in the table.
  298. :param: entries Array of strings containing the rows of the table, in JSON format.
  299. */
  300. func saveTablePost(entries : [String]){
  301. print("SYNC:LOG: Saving table Post.")
  302. let dateFormatter = DateFormatter()
  303. dateFormatter.timeZone = TimeZone.ReferenceType.local
  304. //Set up context
  305. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  306. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  307. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  308. let entity = NSEntityDescription.entity(forEntityName: "Post", in: context)
  309. var row: NSManagedObject
  310. //Delete all previous entries
  311. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
  312. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  313. do {
  314. try context.execute(request)
  315. } catch let error as NSError {
  316. print("SYNC:ERROR: Could not clean up Post entity: \(error), \(error.userInfo).")
  317. } catch {
  318. print("SYNC:ERROR: Could not clean up Post entity.")
  319. }
  320. //Loop new entries
  321. for entry in entries{
  322. //Get id
  323. var str = entry
  324. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  325. //Get permalink
  326. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  327. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  328. //Get title_es
  329. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  330. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  331. //Get title_en
  332. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  333. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  334. //Get title_eu
  335. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  336. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  337. //Get text_es
  338. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  339. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  340. //Get text_en
  341. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  342. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  343. //Get text_eu
  344. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  345. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  346. //Get comments
  347. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  348. let comments : Int = Int(str.subStr(start : str.indexOf(target : "\"comments\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  349. //Get username
  350. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  351. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  352. //Get dtime
  353. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  354. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  355. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.length - 2))!
  356. //Save CoreData
  357. row = NSManagedObject(entity: entity!, insertInto: context)
  358. //set the entity values
  359. row.setValue(id, forKey: "id")
  360. row.setValue(permalink, forKey: "permalink")
  361. row.setValue(dtime, forKey: "dtime")
  362. row.setValue(username, forKey: "username")
  363. row.setValue(comments, forKey: "comments")
  364. row.setValue(title_es, forKey: "title_es")
  365. row.setValue(title_en, forKey: "title_en")
  366. row.setValue(title_eu, forKey: "title_eu")
  367. row.setValue(text_es, forKey: "text_es")
  368. row.setValue(text_en, forKey: "text_en")
  369. row.setValue(text_eu, forKey: "text_eu")
  370. do {
  371. try context.save()
  372. } catch let error as NSError {
  373. print("SYNC:ERROR: Could not store Post with id \(id): \(error), \(error.userInfo).")
  374. } catch {
  375. print("SYNC:ERROR: Could not store Post with id \(id).")
  376. }
  377. }
  378. }
  379. /**
  380. Saves the data in the table.
  381. :param: entries Array of strings containing the rows of the table, in JSON format.
  382. */
  383. func saveTablePostComment(entries : [String]){
  384. print("SYNC:LOG: Saving table Post_comment.")
  385. let dateFormatter = DateFormatter()
  386. dateFormatter.timeZone = TimeZone.ReferenceType.local
  387. //Set up context
  388. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  389. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  390. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  391. let entity = NSEntityDescription.entity(forEntityName: "Post_comment", in: context)
  392. var row: NSManagedObject
  393. //Delete all previous entries
  394. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_comment")
  395. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  396. do {
  397. try context.execute(request)
  398. } catch let error as NSError {
  399. print("SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  400. } catch {
  401. print("SYNC:ERROR: Could not clean up Post_comment entity.")
  402. }
  403. //Loop new entries
  404. for entry in entries{
  405. //Get id
  406. var str = entry
  407. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  408. //Get activity
  409. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  410. let post : String = str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  411. //Get text
  412. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  413. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  414. //Get dtime
  415. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  416. dateFormatter.dateFormat = "yyyy-MM-dd"
  417. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  418. //Get username
  419. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  420. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  421. //Get lang
  422. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  423. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  424. //Save CoreData
  425. row = NSManagedObject(entity: entity!, insertInto: context)
  426. row.setValue(id, forKey: "id")
  427. row.setValue(post, forKey: "post")
  428. row.setValue(text, forKey: "text")
  429. row.setValue(dtime, forKey: "dtime")
  430. row.setValue(username, forKey: "username")
  431. row.setValue(lang, forKey: "lang")
  432. do {
  433. try context.save()
  434. } catch let error as NSError {
  435. print("SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  436. } catch {
  437. print("SYNC:ERROR: Could not store Activity_comment with id \(id).")
  438. }
  439. }
  440. }
  441. /**
  442. Saves the data in the table.
  443. :param: entries Array of strings containing the rows of the table, in JSON format.
  444. */
  445. func saveTablePostImage(entries : [String]){
  446. print("SYNC:LOG: Saving table Post_image.")
  447. //Set up context
  448. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  449. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  450. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  451. let entity = NSEntityDescription.entity(forEntityName: "Post_image", in: context)
  452. var row: NSManagedObject
  453. //Delete all previous entries
  454. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_image")
  455. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  456. do {
  457. try context.execute(request)
  458. } catch let error as NSError {
  459. print("SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  460. } catch {
  461. print("SYNC:ERROR: Could not clean up Post_comment entity.")
  462. }
  463. //Loop new entries
  464. for entry in entries{
  465. //Get id
  466. var str = entry
  467. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  468. //Get activity
  469. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  470. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  471. //Get image
  472. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  473. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  474. //Get idx
  475. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  476. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  477. //Save CoreData
  478. row = NSManagedObject(entity: entity!, insertInto: context)
  479. row.setValue(id, forKey: "id")
  480. row.setValue(post, forKey: "post")
  481. row.setValue(image, forKey: "image")
  482. row.setValue(idx, forKey: "idx")
  483. do {
  484. try context.save()
  485. } catch let error as NSError {
  486. print("SYNC:ERROR: Could not store Post_image with id \(id): \(error), \(error.userInfo).")
  487. } catch {
  488. print("SYNC:ERROR: Could not store Post_image with id \(id).")
  489. }
  490. }
  491. }
  492. /**
  493. Saves the data in the table.
  494. :param: entries Array of strings containing the rows of the table, in JSON format.
  495. */
  496. func saveTablePostTag(entries : [String]){
  497. print("SYNC:LOG: Saving table Post_tag.")
  498. //Set up context
  499. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  500. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  501. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  502. let entity = NSEntityDescription.entity(forEntityName: "Post_tag", in: context)
  503. var row: NSManagedObject
  504. //Delete all previous entries
  505. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_tag")
  506. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  507. do {
  508. try context.execute(request)
  509. } catch let error as NSError {
  510. print("SYNC:ERROR: Could not clean up Post_tag entity: \(error), \(error.userInfo).")
  511. } catch {
  512. print("SYNC:ERROR: Could not clean up Post_tag entity.")
  513. }
  514. //Loop new entries
  515. for entry in entries{
  516. //Get activity
  517. var str = entry
  518. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  519. //Get tag
  520. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  521. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  522. //Save CoreData
  523. row = NSManagedObject(entity: entity!, insertInto: context)
  524. row.setValue(post, forKey: "post")
  525. row.setValue(tag, forKey: "tag")
  526. do {
  527. try context.save()
  528. } catch let error as NSError {
  529. print("SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag): \(error), \(error.userInfo).")
  530. } catch {
  531. print("SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag).")
  532. }
  533. }
  534. }
  535. /**
  536. Saves the data in the table.
  537. :param: entries Array of strings containing the rows of the table, in JSON format.
  538. */
  539. func saveTableActivity(entries : [String]){
  540. print("SYNC:LOG: Saving table Activity.")
  541. let dateFormatter = DateFormatter()
  542. dateFormatter.timeZone = TimeZone.ReferenceType.local
  543. //Set up context
  544. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  545. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  546. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  547. let entity = NSEntityDescription.entity(forEntityName: "Activity", in: context)
  548. var row: NSManagedObject
  549. //Delete all previous entries
  550. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity")
  551. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  552. do {
  553. try context.execute(request)
  554. } catch let error as NSError {
  555. print("SYNC:ERROR: Could not clean up Activity entity: \(error), \(error.userInfo).")
  556. } catch {
  557. print("SYNC:ERROR: Could not clean up Activity entity.")
  558. }
  559. //Loop new entries
  560. for entry in entries{
  561. //Get id
  562. var str = entry
  563. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  564. //Get permalink
  565. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  566. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  567. //Get date
  568. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  569. dateFormatter.dateFormat = "yyyy-MM-dd"
  570. let date = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  571. //Get city
  572. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  573. let city : String = str.subStr(start : str.indexOf(target : "\"city\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  574. //Get title_es
  575. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  576. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  577. //Get title_en
  578. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  579. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  580. //Get title_eu
  581. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  582. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  583. //Get text_es
  584. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  585. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  586. //Get text_eu
  587. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  588. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  589. //Get text_en
  590. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  591. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  592. //Get after_es
  593. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  594. var after_es : String = str.subStr(start : str.indexOf(target : "\"after_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  595. if (after_es == "ul"){ //From "null"
  596. after_es = ""
  597. }
  598. //Get after_en
  599. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  600. var after_en : String = str.subStr(start : str.indexOf(target : "\"after_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  601. if (after_en == "ul"){ //From "null"
  602. after_en = ""
  603. }
  604. //Get after_eu
  605. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  606. var after_eu : String = str.subStr(start : str.indexOf(target : "\"after_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  607. if (after_eu == "ul"){ //From "null"
  608. after_eu = ""
  609. }
  610. //Get price
  611. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  612. let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  613. //Get inscription
  614. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  615. let inscription : Int = Int(str.subStr(start : str.indexOf(target : "\"inscription\":")! + 15, end : str.indexOf(target : ",\"")! - 2))!
  616. //Get max_people
  617. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  618. let maxPeoplePre : String = str.subStr(start : str.indexOf(target : "\"max_people\":")! + 14, end : str.indexOf(target : ",")! - 2)
  619. var maxPeople = -1
  620. if (maxPeoplePre != "ul"){ //From "null"
  621. maxPeople = Int(maxPeoplePre)!
  622. }
  623. //Get album
  624. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  625. let albumPre = str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2)
  626. var album = -1
  627. if (albumPre != "ul"){ //From "null"
  628. album = Int(albumPre)!
  629. }
  630. //Skipping dtime
  631. //Skipping comments
  632. //Save CoreData
  633. row = NSManagedObject(entity: entity!, insertInto: context)
  634. //set the entity values
  635. row.setValue(id, forKey: "id")
  636. row.setValue(permalink, forKey: "permalink")
  637. row.setValue(date, forKey: "date")
  638. row.setValue(city, forKey: "city")
  639. row.setValue(title_es, forKey: "title_es")
  640. row.setValue(title_en, forKey: "title_en")
  641. row.setValue(title_eu, forKey: "title_eu")
  642. row.setValue(text_es, forKey: "text_es")
  643. row.setValue(text_en, forKey: "text_en")
  644. row.setValue(text_eu, forKey: "text_eu")
  645. row.setValue(after_es, forKey: "after_es")
  646. row.setValue(after_en, forKey: "after_en")
  647. row.setValue(after_eu, forKey: "after_eu")
  648. row.setValue(price, forKey: "price")
  649. row.setValue(inscription, forKey: "inscription")
  650. row.setValue(maxPeople, forKey: "max_people")
  651. if (album != -1){
  652. row.setValue(album, forKey: "album")
  653. }
  654. do {
  655. try context.save()
  656. } catch let error as NSError {
  657. print("SYNC:ERROR: Could not store Activity with id \(id): \(error), \(error.userInfo).")
  658. } catch {
  659. print("SYNC:ERROR: Could not store Activity with id \(id).")
  660. }
  661. }
  662. }
  663. /**
  664. Saves the data in the table.
  665. :param: entries Array of strings containing the rows of the table, in JSON format.
  666. */
  667. func saveTableActivityComment(entries : [String]){
  668. print("SYNC:LOG: Saving table Activity_comment.")
  669. let dateFormatter = DateFormatter()
  670. dateFormatter.timeZone = TimeZone.ReferenceType.local
  671. //Set up context
  672. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  673. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  674. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  675. let entity = NSEntityDescription.entity(forEntityName: "Activity_comment", in: context)
  676. var row: NSManagedObject
  677. //Delete all previous entries
  678. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_comment")
  679. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  680. do {
  681. try context.execute(request)
  682. } catch let error as NSError {
  683. print("SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  684. } catch {
  685. print("SYNC:ERROR: Could not clean up Activity_comment entity.")
  686. }
  687. //Loop new entries
  688. for entry in entries{
  689. //Get id
  690. var str = entry
  691. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  692. //Get activity
  693. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  694. let activity : String = str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  695. //Get text
  696. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  697. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  698. //Get dtime
  699. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  700. dateFormatter.dateFormat = "yyyy-MM-dd"
  701. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  702. //Get username
  703. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  704. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  705. //Get lang
  706. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  707. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  708. //Save CoreData
  709. row = NSManagedObject(entity: entity!, insertInto: context)
  710. row.setValue(id, forKey: "id")
  711. row.setValue(activity, forKey: "activity")
  712. row.setValue(text, forKey: "text")
  713. row.setValue(dtime, forKey: "dtime")
  714. row.setValue(username, forKey: "username")
  715. row.setValue(lang, forKey: "lang")
  716. do {
  717. try context.save()
  718. } catch let error as NSError {
  719. print("SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  720. } catch {
  721. print("SYNC:ERROR: Could not store Activity_comment with id \(id).")
  722. }
  723. }
  724. }
  725. /**
  726. Saves the data in the table.
  727. :param: entries Array of strings containing the rows of the table, in JSON format.
  728. */
  729. func saveTableActivityImage(entries : [String]){
  730. print("SYNC:LOG: Saving table Activity_image.")
  731. //Set up context
  732. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  733. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  734. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  735. let entity = NSEntityDescription.entity(forEntityName: "Activity_image", in: context)
  736. var row: NSManagedObject
  737. //Delete all previous entries
  738. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_image")
  739. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  740. do {
  741. try context.execute(request)
  742. } catch let error as NSError {
  743. print("SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  744. } catch {
  745. print("SYNC:ERROR: Could not clean up Activity_comment entity.")
  746. }
  747. //Loop new entries
  748. for entry in entries{
  749. //Get id
  750. var str = entry
  751. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  752. //Get activity
  753. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  754. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  755. //Get image
  756. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  757. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  758. //Get idx
  759. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  760. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  761. //Save CoreData
  762. row = NSManagedObject(entity: entity!, insertInto: context)
  763. row.setValue(id, forKey: "id")
  764. row.setValue(activity, forKey: "activity")
  765. row.setValue(image, forKey: "image")
  766. row.setValue(idx, forKey: "idx")
  767. do {
  768. try context.save()
  769. } catch let error as NSError {
  770. print("SYNC:ERROR: Could not store Activity_image with id \(id): \(error), \(error.userInfo).")
  771. } catch {
  772. print("SYNC:ERROR: Could not store Activity_image with id \(id).")
  773. }
  774. }
  775. }
  776. /**
  777. Saves the data in the table.
  778. :param: entries Array of strings containing the rows of the table, in JSON format.
  779. */
  780. func saveTableActivityTag(entries : [String]){
  781. print("SYNC:LOG: Saving table Activity_tag.")
  782. //Set up context
  783. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  784. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  785. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  786. let entity = NSEntityDescription.entity(forEntityName: "Activity_tag", in: context)
  787. var row: NSManagedObject
  788. //Delete all previous entries
  789. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_tag")
  790. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  791. do {
  792. try context.execute(request)
  793. } catch let error as NSError {
  794. print("SYNC:ERROR: Could not clean up Activity_tag entity: \(error), \(error.userInfo).")
  795. } catch {
  796. print("SYNC:ERROR: Could not clean up Activity_tag entity.")
  797. }
  798. //Loop new entries
  799. for entry in entries{
  800. //Get activity
  801. var str = entry
  802. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  803. //Get tag
  804. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  805. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  806. //Save CoreData
  807. row = NSManagedObject(entity: entity!, insertInto: context)
  808. row.setValue(activity, forKey: "activity")
  809. row.setValue(tag, forKey: "tag")
  810. do {
  811. try context.save()
  812. } catch let error as NSError {
  813. print("SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag): Error: \(error), \(error.userInfo).")
  814. } catch {
  815. print("SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag).")
  816. }
  817. }
  818. }
  819. /**
  820. Saves the data in the table.
  821. :param: entries Array of strings containing the rows of the table, in JSON format.
  822. */
  823. //CHECK: New (and probably better format than the others.)
  824. func saveTableActivityItinerary(entries : [String]){
  825. print("SYNC:LOG: Saving table Activity_itinerary.")
  826. let dateFormatter = DateFormatter()
  827. dateFormatter.timeZone = TimeZone.ReferenceType.local
  828. //Set up context
  829. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  830. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  831. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  832. let entity = NSEntityDescription.entity(forEntityName: "Activity_itinerary", in: context)
  833. var row: NSManagedObject
  834. //Delete all previous entries
  835. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_itinerary")
  836. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  837. do {
  838. try context.execute(request)
  839. } catch let error as NSError {
  840. print("SYNC:ERROR: Could not clean up Activity_itinerary entity: \(error), \(error.userInfo).")
  841. } catch {
  842. print("SYNC:ERROR: Could not clean up Activity_itinerary entity.")
  843. }
  844. //Loop new entries
  845. for entry in entries{
  846. row = NSManagedObject(entity: entity!, insertInto: context)
  847. //Get id
  848. var str = entry
  849. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  850. row.setValue(id, forKey: "id")
  851. //Get activity
  852. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  853. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  854. row.setValue(activity, forKey: "activity")
  855. //Get name_es
  856. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  857. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  858. row.setValue(name_es, forKey: "name_es")
  859. //Get name_en
  860. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  861. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  862. row.setValue(name_en, forKey: "name_en")
  863. //Get name_eu
  864. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  865. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  866. row.setValue(name_eu, forKey: "name_eu")
  867. //Get description_es
  868. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  869. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  870. if (description_es != "ul"){ //From "null"
  871. row.setValue(description_es, forKey: "description_es")
  872. }
  873. //Get description_en
  874. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  875. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  876. if (description_en != "ul"){ //From "null"
  877. row.setValue(description_en, forKey: "description_en")
  878. }
  879. //Get description_eu
  880. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  881. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  882. if (description_eu != "ul"){ //From "null"
  883. row.setValue(description_eu, forKey: "description_eu")
  884. }
  885. //Get start
  886. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  887. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  888. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  889. row.setValue(start, forKey: "start")
  890. //Get end
  891. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  892. let end = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  893. if (end != "ul"){ //From "null"
  894. row.setValue(dateFormatter.date(from: end), forKey: "end")
  895. }
  896. //Get place
  897. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  898. let place : Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.length - 2))!
  899. row.setValue(place, forKey: "place")
  900. //Save CoreData
  901. do {
  902. try context.save()
  903. } catch let error as NSError {
  904. print("SYNC:ERROR: Could not store Activity_itinerary with id \(id): \(error), \(error.userInfo).")
  905. } catch {
  906. print("SYNC:ERROR: Could not store Activity_itinerary with id \(id).")
  907. }
  908. }
  909. }
  910. /**
  911. Saves the data in the table.
  912. :param: entries Array of strings containing the rows of the table, in JSON format.
  913. */
  914. //CHECK: New (and probably better format than the others.)
  915. func saveTableAlbum(entries : [String]){
  916. print("SYNC:LOG: Saving table Album.")
  917. //Set up context
  918. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  919. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  920. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  921. let entity = NSEntityDescription.entity(forEntityName: "Album", in: context)
  922. var row: NSManagedObject
  923. //Delete all previous entries
  924. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Album")
  925. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  926. do {
  927. try context.execute(request)
  928. } catch let error as NSError {
  929. print("SYNC:ERROR: Could not clean up Album: \(error), \(error.userInfo).")
  930. } catch {
  931. print("SYNC:ERROR: Could not clean up Album entity.")
  932. }
  933. //Loop new entries
  934. for entry in entries{
  935. row = NSManagedObject(entity: entity!, insertInto: context)
  936. //Get id
  937. var str = entry
  938. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  939. row.setValue(id, forKey: "id")
  940. //Get permalink
  941. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  942. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  943. row.setValue(permalink, forKey: "permalink")
  944. //Get title_es
  945. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  946. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  947. row.setValue(title_es, forKey: "title_es")
  948. //Get title_en
  949. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  950. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  951. row.setValue(title_es, forKey: "title_es")
  952. //Get title_eu
  953. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  954. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  955. row.setValue(title_es, forKey: "title_es")
  956. //Get description_es
  957. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  958. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  959. if (description_es != "ul"){ //From "null"
  960. row.setValue(description_es, forKey: "description_es")
  961. }
  962. //Get description_en
  963. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  964. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  965. if (description_en != "ul"){ //From "null"
  966. row.setValue(description_en, forKey: "description_en")
  967. }
  968. //Get description_eu
  969. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  970. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  971. if (description_eu != "ul"){ //From "null"
  972. row.setValue(description_eu, forKey: "description_eu")
  973. }
  974. //Get open
  975. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  976. let open : Int = Int(str.subStr(start : str.indexOf(target : "\"open\":")! + 8, end : str.length - 2))!
  977. row.setValue(open, forKey: "open")
  978. //Save CoreData
  979. do {
  980. try context.save()
  981. } catch let error as NSError {
  982. print("SYNC:ERROR: Could not store Album with id \(id): \(error), \(error.userInfo).")
  983. } catch {
  984. print("SYNC:ERROR: Could not store Album with id \(id).")
  985. }
  986. }
  987. }
  988. }