Sync.swift 63 KB

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