Sync.swift 63 KB

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