Sync.swift 60 KB

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