Sync.swift 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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. var vBlog: Int = 0
  29. var vActivities: Int = 0
  30. var vGallery: Int = 0
  31. var vBlanca: Int = 0
  32. var vAll: Int = 0
  33. /**
  34. Starts the sync process.
  35. Always asynchronously.
  36. */
  37. init(){
  38. let url = buildUrl();
  39. sync(url: url)
  40. }
  41. /**
  42. Starts the sync process, synchronously or asynchronously.
  43. :param: synchronous True for synchronous sync, false for asynchronously
  44. */
  45. init(synchronous: Bool){
  46. if synchronous == true{
  47. NSLog(":SYNC:LOG: Synchronous sync started.")
  48. self.initial = true
  49. }
  50. let url = buildUrl();
  51. sync(url: url)
  52. }
  53. /**
  54. Builds the URL to perform the sync against.
  55. :returns: The URL.
  56. */
  57. func buildUrl() -> URL{
  58. let defaults = UserDefaults.standard
  59. var uId: String = "unknown"
  60. if defaults.value(forKey: "versionAll") != nil{
  61. self.vAll = defaults.value(forKey: "versionAll") as! Int
  62. }
  63. if defaults.value(forKey: "versionBlog") != nil{
  64. self.vAll = defaults.value(forKey: "versionBlog") as! Int
  65. }
  66. if defaults.value(forKey: "versionActivities") != nil{
  67. self.vActivities = defaults.value(forKey: "versionActivities") as! Int
  68. }
  69. if defaults.value(forKey: "versionGallery") != nil{
  70. self.vGallery = defaults.value(forKey: "versionGallery") as! Int
  71. }
  72. if defaults.value(forKey: "versionBlanca") != nil{
  73. self.vBlanca = defaults.value(forKey: "versionBlanca") as! Int
  74. }
  75. if defaults.value(forKey: "userId") != nil{
  76. uId = defaults.value(forKey: "userId") as! String
  77. }
  78. let url = URL(string: "https://margolariak.com/API/v1/sync.php?client=com.margolariak.app&user=\(uId)&action=sync&version=\(self.vAll)")
  79. return url!
  80. }
  81. /**
  82. Performs an asynchronous sync.
  83. It fetches the info from the server and stores as Core Data
  84. :param: url The url to sync.
  85. */
  86. func sync(url: URL){
  87. NSLog(":SYNC:LOG: Sync started.")
  88. //Synchronously get data
  89. let task = URLSession.shared.dataTask(with: url) { data, response, error in
  90. guard error == nil else {
  91. NSLog(":SYNC:ERROR: Unknown error.")
  92. return
  93. }
  94. guard let data = data else {
  95. NSLog(":SYNC:ERROR: Data is empty.")
  96. return
  97. }
  98. NSLog(":SYNC:LOG: Data received.")
  99. let defaults = UserDefaults.standard
  100. var strData = String(data:data, encoding: String.Encoding.utf8)
  101. var strVersions = ""
  102. if strData != nil{
  103. strVersions = (strData?.subStr(start : (strData?.indexOf(target : "\"version\":")!)! + 11, end : (strData?.indexOf(target : "}]}")!)! + 1))!
  104. }
  105. var vrAll: Int = 0
  106. var vrBlog: Int = 0
  107. var vrActivities: Int = 0
  108. var vrGallery: Int = 0
  109. var vrBlanca: Int = 0
  110. vrAll = Int(strVersions.subStr(start : strVersions.indexOf(target : "\"all\":")! + 7, end : strVersions.indexOf(target : "}")!-2))!
  111. strVersions = strVersions.subStr(start : strVersions.indexOf(target : ",\"")! + 1, end : strVersions.length - 1)
  112. vrBlog = Int(strVersions.subStr(start : strVersions.indexOf(target : "\"blog\":")! + 7, end : strVersions.indexOf(target : "}")!-2))!
  113. strVersions = strVersions.subStr(start : strVersions.indexOf(target : ",\"")! + 1, end : strVersions.length - 1)
  114. vrActivities = Int(strVersions.subStr(start : strVersions.indexOf(target : "\"activities\":")! + 13, end : strVersions.indexOf(target : "}")!-2))!
  115. strVersions = strVersions.subStr(start : strVersions.indexOf(target : ",\"")! + 1, end : strVersions.length - 1)
  116. vrGallery = Int(strVersions.subStr(start : strVersions.indexOf(target : "\"gallery\":")! + 10, end : strVersions.indexOf(target : "}")!-2))!
  117. strVersions = strVersions.subStr(start : strVersions.indexOf(target : ",\"")! + 1, end : strVersions.length - 1)
  118. vrBlanca = Int(strVersions.subStr(start : strVersions.indexOf(target : "\"lablanca\":")! + 11, end : strVersions.indexOf(target : "}")!-2))!
  119. let dataIdx = strData?.indexOf(target: "{\"data\"")
  120. strData = strData!.subStr(start: dataIdx!, end: strData!.length - 1)
  121. //Get tables and save them
  122. if vrActivities > self.vActivities || vrBlanca > self.vBlanca{
  123. let dataPlace : [String] = self.getTable(data: strData!, table: "place")
  124. self.saveTablePlace(entries: dataPlace)
  125. }
  126. if vrActivities > self.vActivities {
  127. let dataActivity : [String] = self.getTable(data: strData!, table: "activity")
  128. self.saveTableActivity(entries : dataActivity)
  129. let dataActivityComment : [String] = self.getTable(data: strData!, table: "activity_comment")
  130. self.saveTableActivityComment(entries : dataActivityComment)
  131. let dataActivityImage : [String] = self.getTable(data: strData!, table: "activity_image")
  132. self.saveTableActivityImage(entries : dataActivityImage)
  133. let dataActivityTag : [String] = self.getTable(data: strData!, table: "activity_tag")
  134. self.saveTableActivityTag(entries : dataActivityTag)
  135. let dataActivityItinerary : [String] = self.getTable(data: strData!, table: "activity_itinerary")
  136. self.saveTableActivityItinerary(entries : dataActivityItinerary)
  137. defaults.set(vrActivities, forKey: "versionActivities")
  138. }
  139. if vrBlanca > self.vBlanca {
  140. let dataPeople : [String] = self.getTable(data: strData!, table: "people")
  141. self.saveTablePeople(entries: dataPeople)
  142. let dataFestival : [String] = self.getTable(data: strData!, table: "festival")
  143. self.saveTableFestival(entries: dataFestival)
  144. let dataFestivalDay : [String] = self.getTable(data: strData!, table: "festival_day")
  145. self.saveTableFestivalDay(entries: dataFestivalDay)
  146. let dataFestivalEvent : [String] = self.getTable(data: strData!, table: "festival_event")
  147. self.saveTableFestivalEvent(entries: dataFestivalEvent)
  148. //let dataFestivalEventImage : [String] = self.getTable(data: strData!, table: "festival_event_image")
  149. //
  150. let dataFestivalOffer : [String] = self.getTable(data: strData!, table: "festival_offer")
  151. self.saveTableFestivalOffer(entries: dataFestivalOffer)
  152. defaults.set(vrBlanca, forKey: "versionBlanca")
  153. }
  154. if vrGallery > self.vGallery {
  155. let dataAlbum : [String] = self.getTable(data: strData!, table: "album")
  156. self.saveTableAlbum(entries: dataAlbum)
  157. let dataPhoto : [String] = self.getTable(data: strData!, table: "photo")
  158. self.saveTablePhoto(entries: dataPhoto)
  159. let dataPhotoAlbum : [String] = self.getTable(data: strData!, table: "photo_album")
  160. self.saveTablePhotoAlbum(entries: dataPhotoAlbum)
  161. //let dataPhotoComment : [String] = self.getTable(data: strData!, table: "photo_comment")
  162. //
  163. defaults.set(vrGallery, forKey: "versionGallery")
  164. }
  165. if vrBlog > self.vBlog {
  166. let dataPost : [String] = self.getTable(data: strData!, table: "post")
  167. self.saveTablePost(entries: dataPost)
  168. let dataPostComment : [String] = self.getTable(data: strData!, table: "post_comment")
  169. self.saveTablePostComment(entries: dataPostComment)
  170. let dataPostImage : [String] = self.getTable(data: strData!, table: "post_image")
  171. self.saveTablePostImage(entries: dataPostImage)
  172. let dataPostTag : [String] = self.getTable(data: strData!, table: "post_tag")
  173. self.saveTablePostTag(entries: dataPostTag)
  174. defaults.set(vrBlog, forKey: "versionBlog")
  175. }
  176. let dataSponsor : [String] = self.getTable(data: strData!, table: "sponsor")
  177. self.saveTableSponsor(entries: dataSponsor)
  178. let dataSettings: [String] = self.getTable(data: strData!, table: "settings")
  179. self.saveSettings(entries: dataSettings)
  180. defaults.set(vrAll, forKey: "versionAll")
  181. // If it's the initial sync, hide the segue
  182. if self.initial == true{
  183. NSLog(":SYNC:LOG: Finishing synchronous sync.")
  184. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  185. delegate.controller?.initialSync(showScreen: false)
  186. }
  187. }
  188. task.resume()
  189. }
  190. /**
  191. Extracts table rows from the raw data received.
  192. :param: param Received data in JSON format.
  193. :param: table The name of the table to look for.
  194. :return: Array of strings containing the rows of the table, in JSON format.
  195. */
  196. func getTable(data: String, table: String) -> [String]{
  197. //Does the table exists?
  198. if data.indexOf(target: "\"\(table)\":[") == nil{
  199. NSLog(":SYNC:LOG: Empty table: \(table).")
  200. return [String]()
  201. }
  202. let iS : Int? = data.indexOf(target: "\"\(table)\":[")
  203. var str = data.subStr(start: iS!, end: data.length - 1)
  204. let iE : Int? = str.indexOf(target: "}]}")!
  205. str = str.subStr(start: 0, end: iE!)
  206. str = str.subStr(start: table.length + 3,end: str.length - 1)
  207. //Separate the entries
  208. var entries : [String] = str.components(separatedBy: "},{")
  209. //Modify the first and the last one to remove bracers
  210. entries[0] = entries[0].subStr(start: 1, end: entries[0].length - 1)
  211. entries[entries.count - 1] = entries[entries.count - 1].subStr(start: 0, end: entries[entries.count - 1].length - 2)
  212. NSLog(":SYNC:LOG: Table \(table) has \(entries.count) rows.")
  213. //Return the array
  214. return entries
  215. }
  216. /**
  217. Saves the data in the table.
  218. :param: entries Array of strings containing the rows of the table, in JSON format.
  219. */
  220. func saveTablePlace(entries : [String]){
  221. NSLog(":SYNC:LOG: Saving table Place.")
  222. //Set up context
  223. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  224. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  225. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  226. let entity = NSEntityDescription.entity(forEntityName: "Place", in: context)
  227. var row: NSManagedObject
  228. //Delete all previous entries
  229. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Place")
  230. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  231. do {
  232. try context.execute(request)
  233. } catch let error as NSError {
  234. NSLog(":SYNC:ERROR: Could not clean up Places entity: \(error), \(error.userInfo).")
  235. } catch {
  236. NSLog(":SYNC:ERROR: Could not clean up Places entity.")
  237. }
  238. //Loop new entries
  239. for entry in entries{
  240. //Get id
  241. var str = entry
  242. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  243. //Get name_es
  244. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  245. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  246. //Get name_en
  247. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  248. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  249. //Get name_eu
  250. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  251. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  252. //Get address_es
  253. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  254. let address_es : String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  255. //Get address_en
  256. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  257. let address_en : String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  258. //Get address_eu
  259. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  260. let address_eu : String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  261. //Get cp
  262. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  263. let cp : String = str.subStr(start : str.indexOf(target : "\"cp\":")! + 6, end : str.indexOf(target : ",\"")! - 2)
  264. //Get lat
  265. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  266. let lat : Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  267. //Get lon
  268. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  269. let lon : Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  270. //Save CoreData
  271. row = NSManagedObject(entity: entity!, insertInto: context)
  272. row.setValue(id, forKey: "id")
  273. row.setValue(name_es, forKey: "name_es")
  274. row.setValue(name_en, forKey: "name_en")
  275. row.setValue(name_eu, forKey: "name_eu")
  276. row.setValue(address_es, forKey: "address_es")
  277. row.setValue(address_en, forKey: "address_en")
  278. row.setValue(address_eu, forKey: "address_eu")
  279. row.setValue(cp, forKey: "cp")
  280. row.setValue(lat, forKey: "lat")
  281. row.setValue(lon, forKey: "lon")
  282. do {
  283. try context.save()
  284. } catch let error as NSError {
  285. NSLog(":SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  286. } catch {
  287. NSLog(":SYNC:ERROR: Could not store Place with id \(id).")
  288. }
  289. }
  290. }
  291. /**
  292. Saves the data in the table.
  293. :param: entries Array of strings containing the rows of the table, in JSON format.
  294. */
  295. func saveTablePeople(entries : [String]){
  296. NSLog(":SYNC:LOG: Saving table People.")
  297. //Set up context
  298. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  299. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  300. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  301. let entity = NSEntityDescription.entity(forEntityName: "People", in: context)
  302. var row: NSManagedObject
  303. //Delete all previous entries
  304. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "People")
  305. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  306. do {
  307. try context.execute(request)
  308. } catch let error as NSError {
  309. NSLog(":SYNC:ERROR: Could not clean up People entity: \(error), \(error.userInfo).")
  310. } catch {
  311. NSLog(":SYNC:ERROR: Could not clean up People entity.")
  312. }
  313. //Loop new entries
  314. for entry in entries{
  315. row = NSManagedObject(entity: entity!, insertInto: context)
  316. //Get id
  317. var str = entry
  318. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  319. row.setValue(id, forKey: "id")
  320. //Get name_es
  321. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  322. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  323. row.setValue(name_es, forKey: "name_es")
  324. //Get name_en
  325. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  326. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  327. row.setValue(name_en, forKey: "name_en")
  328. //Get name_eu
  329. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  330. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  331. row.setValue(name_eu, forKey: "name_eu")
  332. //Get link
  333. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  334. let link : String = str.subStr(start : str.indexOf(target : "\"link\":")! + 8, end : str.length - 2)
  335. row.setValue(link, forKey: "link")
  336. //Save CoreData
  337. do {
  338. try context.save()
  339. } catch let error as NSError {
  340. NSLog(":SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  341. } catch {
  342. NSLog(":SYNC:ERROR: Could not store Place with id \(id).")
  343. }
  344. }
  345. }
  346. /**
  347. Saves the data in the table.
  348. :param: entries Array of strings containing the rows of the table, in JSON format.
  349. */
  350. func saveTablePost(entries : [String]){
  351. NSLog(":SYNC:LOG: Saving table Post.")
  352. let dateFormatter = DateFormatter()
  353. dateFormatter.timeZone = TimeZone.ReferenceType.local
  354. //Set up context
  355. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  356. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  357. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  358. let entity = NSEntityDescription.entity(forEntityName: "Post", in: context)
  359. var row: NSManagedObject
  360. //Delete all previous entries
  361. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
  362. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  363. do {
  364. try context.execute(request)
  365. } catch let error as NSError {
  366. NSLog(":SYNC:ERROR: Could not clean up Post entity: \(error), \(error.userInfo).")
  367. } catch {
  368. NSLog(":SYNC:ERROR: Could not clean up Post entity.")
  369. }
  370. //Loop new entries
  371. for entry in entries{
  372. //Get id
  373. var str = entry
  374. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  375. //Get permalink
  376. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  377. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  378. //Get title_es
  379. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  380. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  381. //Get title_en
  382. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  383. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  384. //Get title_eu
  385. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  386. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  387. //Get text_es
  388. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  389. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  390. //Get text_en
  391. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  392. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  393. //Get text_eu
  394. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  395. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  396. //Get comments
  397. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  398. let comments : Int = Int(str.subStr(start : str.indexOf(target : "\"comments\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  399. //Get username
  400. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  401. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  402. //Get dtime
  403. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  404. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  405. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.length - 2))!
  406. //Save CoreData
  407. row = NSManagedObject(entity: entity!, insertInto: context)
  408. //set the entity values
  409. row.setValue(id, forKey: "id")
  410. row.setValue(permalink, forKey: "permalink")
  411. row.setValue(dtime, forKey: "dtime")
  412. row.setValue(username, forKey: "username")
  413. row.setValue(comments, forKey: "comments")
  414. row.setValue(title_es, forKey: "title_es")
  415. row.setValue(title_en, forKey: "title_en")
  416. row.setValue(title_eu, forKey: "title_eu")
  417. row.setValue(text_es, forKey: "text_es")
  418. row.setValue(text_en, forKey: "text_en")
  419. row.setValue(text_eu, forKey: "text_eu")
  420. do {
  421. try context.save()
  422. } catch let error as NSError {
  423. NSLog(":SYNC:ERROR: Could not store Post with id \(id): \(error), \(error.userInfo).")
  424. } catch {
  425. NSLog(":SYNC:ERROR: Could not store Post with id \(id).")
  426. }
  427. }
  428. }
  429. /**
  430. Saves the data in the table.
  431. :param: entries Array of strings containing the rows of the table, in JSON format.
  432. */
  433. func saveTablePostComment(entries : [String]){
  434. NSLog(":SYNC:LOG: Saving table Post_comment.")
  435. let dateFormatter = DateFormatter()
  436. dateFormatter.timeZone = TimeZone.ReferenceType.local
  437. //Set up context
  438. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  439. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  440. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  441. let entity = NSEntityDescription.entity(forEntityName: "Post_comment", in: context)
  442. var row: NSManagedObject
  443. //Delete all previous entries
  444. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_comment")
  445. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  446. do {
  447. try context.execute(request)
  448. } catch let error as NSError {
  449. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  450. } catch {
  451. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity.")
  452. }
  453. //Loop new entries
  454. for entry in entries{
  455. //Get id
  456. var str = entry
  457. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  458. //Get activity
  459. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  460. let post : String = str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  461. //Get text
  462. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  463. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  464. //Get dtime
  465. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  466. dateFormatter.dateFormat = "yyyy-MM-dd"
  467. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  468. //Get username
  469. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  470. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  471. //Get lang
  472. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  473. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  474. //Save CoreData
  475. row = NSManagedObject(entity: entity!, insertInto: context)
  476. row.setValue(id, forKey: "id")
  477. row.setValue(post, forKey: "post")
  478. row.setValue(text, forKey: "text")
  479. row.setValue(dtime, forKey: "dtime")
  480. row.setValue(username, forKey: "username")
  481. row.setValue(lang, forKey: "lang")
  482. do {
  483. try context.save()
  484. } catch let error as NSError {
  485. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  486. } catch {
  487. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id).")
  488. }
  489. }
  490. }
  491. /**
  492. Saves the data in the table.
  493. :param: entries Array of strings containing the rows of the table, in JSON format.
  494. */
  495. func saveTablePostImage(entries : [String]){
  496. NSLog(":SYNC:LOG: Saving table Post_image.")
  497. //Set up context
  498. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  499. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  500. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  501. let entity = NSEntityDescription.entity(forEntityName: "Post_image", in: context)
  502. var row: NSManagedObject
  503. //Delete all previous entries
  504. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_image")
  505. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  506. do {
  507. try context.execute(request)
  508. } catch let error as NSError {
  509. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  510. } catch {
  511. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity.")
  512. }
  513. //Loop new entries
  514. for entry in entries{
  515. //Get id
  516. var str = entry
  517. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  518. //Get activity
  519. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  520. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  521. //Get image
  522. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  523. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  524. //Get idx
  525. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  526. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  527. //Save CoreData
  528. row = NSManagedObject(entity: entity!, insertInto: context)
  529. row.setValue(id, forKey: "id")
  530. row.setValue(post, forKey: "post")
  531. row.setValue(image, forKey: "image")
  532. row.setValue(idx, forKey: "idx")
  533. do {
  534. try context.save()
  535. } catch let error as NSError {
  536. NSLog(":SYNC:ERROR: Could not store Post_image with id \(id): \(error), \(error.userInfo).")
  537. } catch {
  538. NSLog(":SYNC:ERROR: Could not store Post_image with id \(id).")
  539. }
  540. }
  541. }
  542. /**
  543. Saves the data in the table.
  544. :param: entries Array of strings containing the rows of the table, in JSON format.
  545. */
  546. func saveTablePostTag(entries : [String]){
  547. NSLog(":SYNC:LOG: Saving table Post_tag.")
  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: "Post_tag", in: context)
  553. var row: NSManagedObject
  554. //Delete all previous entries
  555. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_tag")
  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 Post_tag entity: \(error), \(error.userInfo).")
  561. } catch {
  562. NSLog(":SYNC:ERROR: Could not clean up Post_tag entity.")
  563. }
  564. //Loop new entries
  565. for entry in entries{
  566. //Get activity
  567. var str = entry
  568. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  569. //Get tag
  570. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  571. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  572. //Save CoreData
  573. row = NSManagedObject(entity: entity!, insertInto: context)
  574. row.setValue(post, forKey: "post")
  575. row.setValue(tag, forKey: "tag")
  576. do {
  577. try context.save()
  578. } catch let error as NSError {
  579. NSLog(":SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag): \(error), \(error.userInfo).")
  580. } catch {
  581. NSLog(":SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag).")
  582. }
  583. }
  584. }
  585. /**
  586. Saves the data in the table.
  587. :param: entries Array of strings containing the rows of the table, in JSON format.
  588. */
  589. func saveTableActivity(entries : [String]){
  590. NSLog(":SYNC:LOG: Saving table Activity.")
  591. let dateFormatter = DateFormatter()
  592. dateFormatter.timeZone = TimeZone.ReferenceType.local
  593. //Set up context
  594. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  595. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  596. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  597. let entity = NSEntityDescription.entity(forEntityName: "Activity", in: context)
  598. var row: NSManagedObject
  599. //Delete all previous entries
  600. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity")
  601. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  602. do {
  603. try context.execute(request)
  604. } catch let error as NSError {
  605. NSLog(":SYNC:ERROR: Could not clean up Activity entity: \(error), \(error.userInfo).")
  606. } catch {
  607. NSLog(":SYNC:ERROR: Could not clean up Activity entity.")
  608. }
  609. //Loop new entries
  610. for entry in entries{
  611. //Get id
  612. var str = entry
  613. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  614. //Get permalink
  615. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  616. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  617. //Get date
  618. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  619. dateFormatter.dateFormat = "yyyy-MM-dd"
  620. let date = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  621. //Get city
  622. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  623. let city : String = str.subStr(start : str.indexOf(target : "\"city\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  624. //Get title_es
  625. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  626. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  627. //Get title_en
  628. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  629. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  630. //Get title_eu
  631. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  632. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  633. //Get text_es
  634. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  635. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  636. //Get text_eu
  637. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  638. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  639. //Get text_en
  640. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  641. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  642. //Get after_es
  643. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  644. var after_es : String = str.subStr(start : str.indexOf(target : "\"after_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  645. if (after_es == "ul"){ //From "null"
  646. after_es = ""
  647. }
  648. //Get after_en
  649. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  650. var after_en : String = str.subStr(start : str.indexOf(target : "\"after_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  651. if (after_en == "ul"){ //From "null"
  652. after_en = ""
  653. }
  654. //Get after_eu
  655. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  656. var after_eu : String = str.subStr(start : str.indexOf(target : "\"after_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  657. if (after_eu == "ul"){ //From "null"
  658. after_eu = ""
  659. }
  660. //Get price
  661. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  662. let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  663. //Get inscription
  664. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  665. let inscription : Int = Int(str.subStr(start : str.indexOf(target : "\"inscription\":")! + 15, end : str.indexOf(target : ",\"")! - 2))!
  666. //Get max_people
  667. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  668. let maxPeoplePre : String = str.subStr(start : str.indexOf(target : "\"max_people\":")! + 14, end : str.indexOf(target : ",")! - 2)
  669. var maxPeople = -1
  670. if (maxPeoplePre != "ul"){ //From "null"
  671. maxPeople = Int(maxPeoplePre)!
  672. }
  673. //Get album
  674. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  675. let albumPre = str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2)
  676. var album = -1
  677. if (albumPre != "ul"){ //From "null"
  678. album = Int(albumPre)!
  679. }
  680. //Skipping dtime
  681. //Skipping comments
  682. //Save CoreData
  683. row = NSManagedObject(entity: entity!, insertInto: context)
  684. //set the entity values
  685. row.setValue(id, forKey: "id")
  686. row.setValue(permalink, forKey: "permalink")
  687. row.setValue(date, forKey: "date")
  688. row.setValue(city, forKey: "city")
  689. row.setValue(title_es, forKey: "title_es")
  690. row.setValue(title_en, forKey: "title_en")
  691. row.setValue(title_eu, forKey: "title_eu")
  692. row.setValue(text_es, forKey: "text_es")
  693. row.setValue(text_en, forKey: "text_en")
  694. row.setValue(text_eu, forKey: "text_eu")
  695. row.setValue(after_es, forKey: "after_es")
  696. row.setValue(after_en, forKey: "after_en")
  697. row.setValue(after_eu, forKey: "after_eu")
  698. row.setValue(price, forKey: "price")
  699. row.setValue(inscription, forKey: "inscription")
  700. row.setValue(maxPeople, forKey: "max_people")
  701. if (album != -1){
  702. row.setValue(album, forKey: "album")
  703. }
  704. do {
  705. try context.save()
  706. } catch let error as NSError {
  707. NSLog(":SYNC:ERROR: Could not store Activity with id \(id): \(error), \(error.userInfo).")
  708. } catch {
  709. NSLog(":SYNC:ERROR: Could not store Activity with id \(id).")
  710. }
  711. }
  712. }
  713. /**
  714. Saves the data in the table.
  715. :param: entries Array of strings containing the rows of the table, in JSON format.
  716. */
  717. func saveTableActivityComment(entries : [String]){
  718. NSLog(":SYNC:LOG: Saving table Activity_comment.")
  719. let dateFormatter = DateFormatter()
  720. dateFormatter.timeZone = TimeZone.ReferenceType.local
  721. //Set up context
  722. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  723. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  724. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  725. let entity = NSEntityDescription.entity(forEntityName: "Activity_comment", in: context)
  726. var row: NSManagedObject
  727. //Delete all previous entries
  728. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_comment")
  729. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  730. do {
  731. try context.execute(request)
  732. } catch let error as NSError {
  733. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  734. } catch {
  735. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity.")
  736. }
  737. //Loop new entries
  738. for entry in entries{
  739. //Get id
  740. var str = entry
  741. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  742. //Get activity
  743. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  744. let activity : String = str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  745. //Get text
  746. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  747. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  748. //Get dtime
  749. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  750. dateFormatter.dateFormat = "yyyy-MM-dd"
  751. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  752. //Get username
  753. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  754. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  755. //Get lang
  756. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  757. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  758. //Save CoreData
  759. row = NSManagedObject(entity: entity!, insertInto: context)
  760. row.setValue(id, forKey: "id")
  761. row.setValue(activity, forKey: "activity")
  762. row.setValue(text, forKey: "text")
  763. row.setValue(dtime, forKey: "dtime")
  764. row.setValue(username, forKey: "username")
  765. row.setValue(lang, forKey: "lang")
  766. do {
  767. try context.save()
  768. } catch let error as NSError {
  769. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  770. } catch {
  771. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id).")
  772. }
  773. }
  774. }
  775. /**
  776. Saves the data in the table.
  777. :param: entries Array of strings containing the rows of the table, in JSON format.
  778. */
  779. func saveTableActivityImage(entries : [String]){
  780. NSLog(":SYNC:LOG: Saving table Activity_image.")
  781. //Set up context
  782. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  783. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  784. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  785. let entity = NSEntityDescription.entity(forEntityName: "Activity_image", in: context)
  786. var row: NSManagedObject
  787. //Delete all previous entries
  788. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_image")
  789. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  790. do {
  791. try context.execute(request)
  792. } catch let error as NSError {
  793. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  794. } catch {
  795. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity.")
  796. }
  797. //Loop new entries
  798. for entry in entries{
  799. //Get id
  800. var str = entry
  801. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  802. //Get activity
  803. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  804. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  805. //Get image
  806. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  807. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  808. //Get idx
  809. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  810. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  811. //Save CoreData
  812. row = NSManagedObject(entity: entity!, insertInto: context)
  813. row.setValue(id, forKey: "id")
  814. row.setValue(activity, forKey: "activity")
  815. row.setValue(image, forKey: "image")
  816. row.setValue(idx, forKey: "idx")
  817. do {
  818. try context.save()
  819. } catch let error as NSError {
  820. NSLog(":SYNC:ERROR: Could not store Activity_image with id \(id): \(error), \(error.userInfo).")
  821. } catch {
  822. NSLog(":SYNC:ERROR: Could not store Activity_image with id \(id).")
  823. }
  824. }
  825. }
  826. /**
  827. Saves the data in the table.
  828. :param: entries Array of strings containing the rows of the table, in JSON format.
  829. */
  830. func saveTableActivityTag(entries : [String]){
  831. NSLog(":SYNC:LOG: Saving table Activity_tag.")
  832. //Set up context
  833. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  834. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  835. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  836. let entity = NSEntityDescription.entity(forEntityName: "Activity_tag", in: context)
  837. var row: NSManagedObject
  838. //Delete all previous entries
  839. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_tag")
  840. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  841. do {
  842. try context.execute(request)
  843. } catch let error as NSError {
  844. NSLog(":SYNC:ERROR: Could not clean up Activity_tag entity: \(error), \(error.userInfo).")
  845. } catch {
  846. NSLog(":SYNC:ERROR: Could not clean up Activity_tag entity.")
  847. }
  848. //Loop new entries
  849. for entry in entries{
  850. //Get activity
  851. var str = entry
  852. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  853. //Get tag
  854. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  855. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  856. //Save CoreData
  857. row = NSManagedObject(entity: entity!, insertInto: context)
  858. row.setValue(activity, forKey: "activity")
  859. row.setValue(tag, forKey: "tag")
  860. do {
  861. try context.save()
  862. } catch let error as NSError {
  863. NSLog(":SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag): Error: \(error), \(error.userInfo).")
  864. } catch {
  865. NSLog(":SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag).")
  866. }
  867. }
  868. }
  869. /**
  870. Saves the data in the table.
  871. :param: entries Array of strings containing the rows of the table, in JSON format.
  872. */
  873. //CHECK: New (and probably better format than the others.)
  874. func saveTableActivityItinerary(entries : [String]){
  875. NSLog(":SYNC:LOG: Saving table Activity_itinerary.")
  876. let dateFormatter = DateFormatter()
  877. dateFormatter.timeZone = TimeZone.ReferenceType.local
  878. //Set up context
  879. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  880. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  881. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  882. let entity = NSEntityDescription.entity(forEntityName: "Activity_itinerary", in: context)
  883. var row: NSManagedObject
  884. //Delete all previous entries
  885. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_itinerary")
  886. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  887. do {
  888. try context.execute(request)
  889. } catch let error as NSError {
  890. NSLog(":SYNC:ERROR: Could not clean up Activity_itinerary entity: \(error), \(error.userInfo).")
  891. } catch {
  892. NSLog(":SYNC:ERROR: Could not clean up Activity_itinerary entity.")
  893. }
  894. //Loop new entries
  895. for entry in entries{
  896. row = NSManagedObject(entity: entity!, insertInto: context)
  897. //Get id
  898. var str = entry
  899. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  900. row.setValue(id, forKey: "id")
  901. //Get activity
  902. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  903. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  904. row.setValue(activity, forKey: "activity")
  905. //Get name_es
  906. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  907. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  908. row.setValue(name_es, forKey: "name_es")
  909. //Get name_en
  910. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  911. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  912. row.setValue(name_en, forKey: "name_en")
  913. //Get name_eu
  914. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  915. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  916. row.setValue(name_eu, forKey: "name_eu")
  917. //Get description_es
  918. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  919. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  920. if (description_es != "ul"){ //From "null"
  921. row.setValue(description_es, forKey: "description_es")
  922. }
  923. //Get description_en
  924. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  925. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  926. if (description_en != "ul"){ //From "null"
  927. row.setValue(description_en, forKey: "description_en")
  928. }
  929. //Get description_eu
  930. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  931. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  932. if (description_eu != "ul"){ //From "null"
  933. row.setValue(description_eu, forKey: "description_eu")
  934. }
  935. //Get start
  936. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  937. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  938. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  939. row.setValue(start, forKey: "start")
  940. //Get end
  941. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  942. let end = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  943. if (end != "ul"){ //From "null"
  944. row.setValue(dateFormatter.date(from: end), forKey: "end")
  945. }
  946. //Get place
  947. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  948. let place : Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.length - 2))!
  949. row.setValue(place, forKey: "place")
  950. //Save CoreData
  951. do {
  952. try context.save()
  953. } catch let error as NSError {
  954. NSLog(":SYNC:ERROR: Could not store Activity_itinerary with id \(id): \(error), \(error.userInfo).")
  955. } catch {
  956. NSLog(":SYNC:ERROR: Could not store Activity_itinerary with id \(id).")
  957. }
  958. }
  959. }
  960. /**
  961. Saves the data in the table.
  962. :param: entries Array of strings containing the rows of the table, in JSON format.
  963. */
  964. func saveTableAlbum(entries : [String]){
  965. NSLog(":SYNC:LOG: Saving table Album.")
  966. //Set up context
  967. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  968. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  969. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  970. let entity = NSEntityDescription.entity(forEntityName: "Album", in: context)
  971. var row: NSManagedObject
  972. //Delete all previous entries
  973. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Album")
  974. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  975. do {
  976. try context.execute(request)
  977. } catch let error as NSError {
  978. NSLog(":SYNC:ERROR: Could not clean up Album: \(error), \(error.userInfo).")
  979. } catch {
  980. NSLog(":SYNC:ERROR: Could not clean up Album entity.")
  981. }
  982. //Loop new entries
  983. for entry in entries{
  984. row = NSManagedObject(entity: entity!, insertInto: context)
  985. //Get id
  986. var str = entry
  987. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  988. row.setValue(id, forKey: "id")
  989. //Get permalink
  990. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  991. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  992. row.setValue(permalink, forKey: "permalink")
  993. //Get title_es
  994. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  995. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  996. row.setValue(title_es, forKey: "title_es")
  997. //Get title_en
  998. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  999. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1000. row.setValue(title_en, forKey: "title_en")
  1001. //Get title_eu
  1002. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1003. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1004. row.setValue(title_eu, forKey: "title_eu")
  1005. //Get description_es
  1006. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1007. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1008. if (description_es != "ul"){ //From "null"
  1009. row.setValue(description_es, forKey: "description_es")
  1010. }
  1011. //Get description_en
  1012. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1013. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1014. if (description_en != "ul"){ //From "null"
  1015. row.setValue(description_en, forKey: "description_en")
  1016. }
  1017. //Get description_eu
  1018. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1019. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1020. if (description_eu != "ul"){ //From "null"
  1021. row.setValue(description_eu, forKey: "description_eu")
  1022. }
  1023. //Get open
  1024. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1025. let open : Int = Int(str.subStr(start : str.indexOf(target : "\"open\":")! + 8, end : str.length - 2))!
  1026. row.setValue(open, forKey: "open")
  1027. //Save CoreData
  1028. do {
  1029. try context.save()
  1030. } catch let error as NSError {
  1031. NSLog(":SYNC:ERROR: Could not store Album with id \(id): \(error), \(error.userInfo).")
  1032. } catch {
  1033. NSLog(":SYNC:ERROR: Could not store Album with id \(id).")
  1034. }
  1035. }
  1036. }
  1037. /**
  1038. Saves the data in the table.
  1039. :param: entries Array of strings containing the rows of the table, in JSON format.
  1040. */
  1041. func saveTablePhoto(entries : [String]){
  1042. NSLog(":SYNC:LOG: Saving table Photo.")
  1043. let dateFormatter = DateFormatter()
  1044. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1045. //Set up context
  1046. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1047. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1048. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1049. let entity = NSEntityDescription.entity(forEntityName: "Photo", in: context)
  1050. var row: NSManagedObject
  1051. //Delete all previous entries
  1052. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo")
  1053. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1054. do {
  1055. try context.execute(request)
  1056. } catch let error as NSError {
  1057. NSLog(":SYNC:ERROR: Could not clean up Photo: \(error), \(error.userInfo).")
  1058. } catch {
  1059. NSLog(":SYNC:ERROR: Could not clean up Photo entity.")
  1060. }
  1061. //Loop new entries
  1062. for entry in entries{
  1063. row = NSManagedObject(entity: entity!, insertInto: context)
  1064. //Get id
  1065. var str = entry
  1066. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1067. row.setValue(id, forKey: "id")
  1068. //Get file
  1069. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1070. let file : String = str.subStr(start : str.indexOf(target : "\"file\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1071. row.setValue(file, forKey: "file")
  1072. //Get permalink
  1073. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1074. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  1075. row.setValue(permalink, forKey: "permalink")
  1076. //Get title_es
  1077. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1078. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1079. if (title_es != "ul"){ //From "null"
  1080. row.setValue(title_es, forKey: "title_es")
  1081. }
  1082. //Get title_en
  1083. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1084. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1085. if (title_en != "ul"){ //From "null"
  1086. row.setValue(title_en, forKey: "title_en")
  1087. }
  1088. //Get title_eu
  1089. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1090. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1091. if (title_eu != "ul"){ //From "null"
  1092. row.setValue(title_eu, forKey: "title_eu")
  1093. }
  1094. //Get description_es
  1095. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1096. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1097. if (description_es != "ul"){ //From "null"
  1098. row.setValue(description_es, forKey: "description_es")
  1099. }
  1100. //Get description_en
  1101. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1102. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1103. if (description_en != "ul"){ //From "null"
  1104. row.setValue(description_en, forKey: "description_en")
  1105. }
  1106. //Get description_eu
  1107. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1108. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1109. if (description_eu != "ul"){ //From "null"
  1110. row.setValue(description_eu, forKey: "description_eu")
  1111. }
  1112. //Get uploaded
  1113. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1114. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1115. let uploaded = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"uploaded\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  1116. row.setValue(uploaded, forKey: "uploaded")
  1117. //TODO: Ignoting place
  1118. //TODO: Ignoring width
  1119. //TODO: Ignoring height
  1120. //TODO: Ignoring size
  1121. //TODO: Ignoring username
  1122. //Save CoreData
  1123. do {
  1124. try context.save()
  1125. } catch let error as NSError {
  1126. NSLog(":SYNC:ERROR: Could not store Photo with id \(id): \(error), \(error.userInfo).")
  1127. } catch {
  1128. NSLog(":SYNC:ERROR: Could not store Photo with id \(id).")
  1129. }
  1130. }
  1131. }
  1132. /**
  1133. Saves the data in the table.
  1134. :param: entries Array of strings containing the rows of the table, in JSON format.
  1135. */
  1136. func saveTablePhotoAlbum(entries : [String]){
  1137. NSLog(":SYNC:LOG: Saving table Photo_album.")
  1138. let dateFormatter = DateFormatter()
  1139. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1140. //Set up context
  1141. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1142. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1143. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1144. let entity = NSEntityDescription.entity(forEntityName: "Photo_album", in: context)
  1145. var row: NSManagedObject
  1146. //Delete all previous entries
  1147. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo_album")
  1148. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1149. do {
  1150. try context.execute(request)
  1151. } catch let error as NSError {
  1152. NSLog(":SYNC:ERROR: Could not clean up Photo_album: \(error), \(error.userInfo).")
  1153. } catch {
  1154. NSLog(":SYNC:ERROR: Could not clean up Photo_album entity.")
  1155. }
  1156. //Loop new entries
  1157. for entry in entries{
  1158. row = NSManagedObject(entity: entity!, insertInto: context)
  1159. //Get photo
  1160. var str = entry
  1161. let photo : Int = Int(str.subStr(start : str.indexOf(target : "\"photo\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1162. row.setValue(photo, forKey: "photo")
  1163. //Get album
  1164. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1165. let album : Int = Int(str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2))!
  1166. row.setValue(album, forKey: "album")
  1167. //Save CoreData
  1168. do {
  1169. try context.save()
  1170. } catch let error as NSError {
  1171. NSLog(":SYNC:ERROR: Could not store Photo_album entry \(error.userInfo).")
  1172. } catch {
  1173. NSLog(":SYNC:ERROR: Could not store Photo_album entry.")
  1174. }
  1175. }
  1176. }
  1177. /**
  1178. Saves the data in the table.
  1179. :param: entries Array of strings containing the rows of the table, in JSON format.
  1180. */
  1181. func saveTableFestival(entries : [String]){
  1182. NSLog(":SYNC:LOG: Saving table Festival.")
  1183. //Set up context
  1184. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1185. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1186. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1187. let entity = NSEntityDescription.entity(forEntityName: "Festival", in: context)
  1188. var row: NSManagedObject
  1189. //Delete all previous entries
  1190. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival")
  1191. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1192. do {
  1193. try context.execute(request)
  1194. } catch let error as NSError {
  1195. NSLog(":SYNC:ERROR: Could not clean up Festival: \(error), \(error.userInfo).")
  1196. } catch {
  1197. NSLog(":SYNC:ERROR: Could not clean up Festival entity.")
  1198. }
  1199. //Loop new entries
  1200. for entry in entries{
  1201. row = NSManagedObject(entity: entity!, insertInto: context)
  1202. //Get id
  1203. var str = entry
  1204. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1205. row.setValue(id, forKey: "id")
  1206. //Get year
  1207. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1208. let file: Int = Int(str.subStr(start : str.indexOf(target : "\"year\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1209. row.setValue(file, forKey: "year")
  1210. //Get text_es
  1211. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1212. let text_es: String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1213. row.setValue(text_es, forKey: "text_es")
  1214. //Get text_en
  1215. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1216. let text_en: String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1217. row.setValue(text_en, forKey: "text_en")
  1218. //Get text_eu
  1219. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1220. let text_eu: String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1221. row.setValue(text_eu, forKey: "text_eu")
  1222. //Get summary_es
  1223. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1224. let summary_es: String = str.subStr(start : str.indexOf(target : "\"summary_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1225. row.setValue(summary_es, forKey: "summary_es")
  1226. //Get summary_en
  1227. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1228. let summary_en: String = str.subStr(start : str.indexOf(target : "\"summary_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1229. row.setValue(summary_en, forKey: "summary_en")
  1230. //Get summary_eu
  1231. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1232. let summary_eu: String = str.subStr(start : str.indexOf(target : "\"summary_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1233. row.setValue(summary_eu, forKey: "summary_eu")
  1234. //Get img
  1235. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1236. let img: String = str.subStr(start : str.indexOf(target : "\"img\":")! + 7, end : str.length - 2)
  1237. row.setValue(img, forKey: "img")
  1238. //Save CoreData
  1239. do {
  1240. try context.save()
  1241. } catch let error as NSError {
  1242. NSLog(":SYNC:ERROR: Could not store Festival entry \(error.userInfo).")
  1243. } catch {
  1244. NSLog(":SYNC:ERROR: Could not store Festival entry.")
  1245. }
  1246. }
  1247. }
  1248. /**
  1249. Saves the data in the table.
  1250. :param: entries Array of strings containing the rows of the table, in JSON format.
  1251. */
  1252. func saveTableFestivalDay(entries : [String]){
  1253. NSLog(":SYNC:LOG: Saving table FestivalDay.")
  1254. let dateFormatter = DateFormatter()
  1255. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1256. //Set up context
  1257. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1258. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1259. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1260. let entity = NSEntityDescription.entity(forEntityName: "Festival_day", in: context)
  1261. var row: NSManagedObject
  1262. //Delete all previous entries
  1263. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_day")
  1264. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1265. do {
  1266. try context.execute(request)
  1267. } catch let error as NSError {
  1268. NSLog(":SYNC:ERROR: Could not clean up Festival_day: \(error), \(error.userInfo).")
  1269. } catch {
  1270. NSLog(":SYNC:ERROR: Could not clean up Festival_day entity.")
  1271. }
  1272. //Loop new entries
  1273. for entry in entries{
  1274. row = NSManagedObject(entity: entity!, insertInto: context)
  1275. //Get id
  1276. var str = entry
  1277. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1278. row.setValue(id, forKey: "id")
  1279. //Get date
  1280. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1281. dateFormatter.dateFormat = "yyyy-MM-dd"
  1282. let date = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1283. row.setValue(date, forKey: "date")
  1284. //Get name_es
  1285. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1286. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1287. row.setValue(name_es, forKey: "name_es")
  1288. //Get name_en
  1289. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1290. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1291. row.setValue(name_en, forKey: "name_en")
  1292. //Get name_eu
  1293. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1294. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1295. row.setValue(name_eu, forKey: "name_eu")
  1296. //Get price
  1297. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1298. let price: Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.length - 2))!
  1299. row.setValue(price, forKey: "price")
  1300. //Save CoreData
  1301. do {
  1302. try context.save()
  1303. } catch let error as NSError {
  1304. NSLog(":SYNC:ERROR: Could not store Festival_day entry \(error.userInfo).")
  1305. } catch {
  1306. NSLog(":SYNC:ERROR: Could not store Festival_day entry.")
  1307. }
  1308. }
  1309. }
  1310. /**
  1311. Saves the data in the table.
  1312. :param: entries Array of strings containing the rows of the table, in JSON format.
  1313. */
  1314. func saveTableFestivalOffer(entries : [String]){
  1315. NSLog(":SYNC:LOG: Saving table FestivalOffer.")
  1316. //Set up context
  1317. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1318. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1319. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1320. let entity = NSEntityDescription.entity(forEntityName: "Festival_offer", in: context)
  1321. var row: NSManagedObject
  1322. //Delete all previous entries
  1323. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_offer")
  1324. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1325. do {
  1326. try context.execute(request)
  1327. } catch let error as NSError {
  1328. NSLog(":SYNC:ERROR: Could not clean up Festival_offer: \(error), \(error.userInfo).")
  1329. } catch {
  1330. NSLog(":SYNC:ERROR: Could not clean up Festival_offer entity.")
  1331. }
  1332. //Loop new entries
  1333. for entry in entries{
  1334. row = NSManagedObject(entity: entity!, insertInto: context)
  1335. // Get id
  1336. var str = entry
  1337. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1338. row.setValue(id, forKey: "id")
  1339. // Get year
  1340. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1341. let year: Int = Int(str.subStr(start : str.indexOf(target : "\"year\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1342. row.setValue(year, forKey: "year")
  1343. // Get name_es
  1344. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1345. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1346. row.setValue(name_es, forKey: "name_es")
  1347. // Get name_en
  1348. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1349. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1350. row.setValue(name_en, forKey: "name_en")
  1351. // Get name_eu
  1352. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1353. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1354. row.setValue(name_eu, forKey: "name_eu")
  1355. // Get description_es
  1356. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1357. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1358. if (description_es != "ul"){ //From "null"
  1359. row.setValue(description_es, forKey: "description_es")
  1360. }
  1361. // Get description_en
  1362. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1363. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1364. if (description_en != "ul"){ //From "null"
  1365. row.setValue(description_en, forKey: "description_en")
  1366. }
  1367. // Get description_eu
  1368. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1369. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1370. if (description_eu != "ul"){ //From "null"
  1371. row.setValue(description_eu, forKey: "description_eu")
  1372. }
  1373. // Get days
  1374. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1375. let days: Int = Int(str.subStr(start : str.indexOf(target : "\"days\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1376. row.setValue(days, forKey: "days")
  1377. //Get price
  1378. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1379. let price: Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.length - 2))!
  1380. row.setValue(price, forKey: "price")
  1381. //Save CoreData
  1382. do {
  1383. try context.save()
  1384. } catch let error as NSError {
  1385. NSLog(":SYNC:ERROR: Could not store Festival_offer entry \(error.userInfo).")
  1386. } catch {
  1387. NSLog(":SYNC:ERROR: Could not store Festival_offer entry.")
  1388. }
  1389. }
  1390. }
  1391. /**
  1392. Saves the data in the table.
  1393. :param: entries Array of strings containing the rows of the table, in JSON format.
  1394. */
  1395. func saveTableFestivalEvent(entries : [String]){
  1396. NSLog(":SYNC:LOG: Saving table FestivalEvent.")
  1397. let dateFormatter = DateFormatter()
  1398. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1399. //Set up context
  1400. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1401. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1402. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1403. let entity = NSEntityDescription.entity(forEntityName: "Festival_event", in: context)
  1404. var row: NSManagedObject
  1405. //Delete all previous entries
  1406. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_event")
  1407. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1408. do {
  1409. try context.execute(request)
  1410. } catch let error as NSError {
  1411. NSLog(":SYNC:ERROR: Could not clean up Festival_event: \(error), \(error.userInfo).")
  1412. } catch {
  1413. NSLog(":SYNC:ERROR: Could not clean up Festival_event entity.")
  1414. }
  1415. //Loop new entries
  1416. for entry in entries{
  1417. row = NSManagedObject(entity: entity!, insertInto: context)
  1418. //Get id
  1419. var str = entry
  1420. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1421. row.setValue(id, forKey: "id")
  1422. //Get gm
  1423. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1424. let gm: Int = Int(str.subStr(start : str.indexOf(target : "\"gm\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1425. row.setValue(gm, forKey: "gm")
  1426. //Get title_es
  1427. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1428. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1429. row.setValue(title_es, forKey: "title_es")
  1430. //Get title_en
  1431. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1432. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1433. row.setValue(title_en, forKey: "title_en")
  1434. //Get title_eu
  1435. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1436. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1437. row.setValue(title_eu, forKey: "title_eu")
  1438. //Get description_es
  1439. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1440. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1441. if (description_es != "ul"){ //From "null"
  1442. row.setValue(description_es, forKey: "description_es")
  1443. }
  1444. //Get description_en
  1445. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1446. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1447. if (description_en != "ul"){ //From "null"
  1448. row.setValue(description_en, forKey: "description_en")
  1449. }
  1450. //Get description_eu
  1451. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1452. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1453. if (description_eu != "ul"){ //From "null"
  1454. row.setValue(description_eu, forKey: "description_eu")
  1455. }
  1456. //Get host
  1457. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1458. let strHost: String = str.subStr(start : str.indexOf(target : "\"host\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1459. if (strHost != "ul"){ //From "null"
  1460. let host: Int = Int(strHost)!
  1461. row.setValue(host, forKey: "host")
  1462. }
  1463. //Get place
  1464. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1465. let place: Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1466. row.setValue(place, forKey: "place")
  1467. //Get start
  1468. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1469. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1470. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1471. row.setValue(start, forKey: "start")
  1472. // Get day
  1473. // Special item, not in the sync content
  1474. dateFormatter.dateFormat = "yyyy-MM-dd"
  1475. var day = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 11))!
  1476. // If on the first hours of the next day...
  1477. let calendar = Calendar.current
  1478. let hours = calendar.component(.hour, from: day )
  1479. // ... the event belongs to the previous day.
  1480. if hours < 6{
  1481. day = Calendar.current.date(byAdding: .day, value: -1, to: day)!
  1482. }
  1483. row.setValue(day, forKey: "day")
  1484. //Get end
  1485. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1486. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1487. let strEnd: String = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.length - 2)
  1488. if (strEnd != "ul"){ //From "null"
  1489. let end = dateFormatter.date(from: strEnd)!
  1490. row.setValue(end, forKey: "end")
  1491. }
  1492. //Save CoreData
  1493. do {
  1494. try context.save()
  1495. } catch let error as NSError {
  1496. NSLog(":SYNC:ERROR: Could not store Festival_event entry \(error.userInfo).")
  1497. } catch {
  1498. NSLog(":SYNC:ERROR: Could not store Festival_event entry.")
  1499. }
  1500. }
  1501. }
  1502. /**
  1503. Saves the data in the table.
  1504. :param: entries Array of strings containing the rows of the table, in JSON format.
  1505. */
  1506. func saveTableSponsor(entries : [String]){
  1507. NSLog(":SYNC:LOG: Saving table Sponsor.")
  1508. //Set up context
  1509. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1510. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1511. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1512. let entity = NSEntityDescription.entity(forEntityName: "Sponsor", in: context)
  1513. var row: NSManagedObject
  1514. //Delete all previous entries
  1515. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Sponsor")
  1516. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1517. do {
  1518. try context.execute(request)
  1519. } catch let error as NSError {
  1520. NSLog(":SYNC:ERROR: Could not clean up Sponsor: \(error), \(error.userInfo).")
  1521. } catch {
  1522. NSLog(":SYNC:ERROR: Could not clean up Sponsor entity.")
  1523. }
  1524. //Loop new entries
  1525. for entry in entries{
  1526. row = NSManagedObject(entity: entity!, insertInto: context)
  1527. //Get id
  1528. var str = entry
  1529. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1530. row.setValue(id, forKey: "id")
  1531. // Get name_es
  1532. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1533. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1534. row.setValue(name_es, forKey: "name_es")
  1535. // Get name_en
  1536. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1537. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1538. row.setValue(name_en, forKey: "name_en")
  1539. // Get name_eu
  1540. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1541. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1542. row.setValue(name_eu, forKey: "name_eu")
  1543. // Get text_es
  1544. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1545. let text_es: String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1546. row.setValue(text_es, forKey: "text_es")
  1547. // Get text_en
  1548. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1549. let text_en: String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1550. row.setValue(text_en, forKey: "text_en")
  1551. // Get text_eu
  1552. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1553. let text_eu: String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1554. row.setValue(text_eu, forKey: "text_eu")
  1555. // Get image
  1556. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1557. let image: String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  1558. if (image != "ul"){ //From "null"
  1559. row.setValue(image, forKey: "image")
  1560. }
  1561. // Get address_es
  1562. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1563. let address_es: String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1564. if (address_es != "ul"){ //From "null"
  1565. row.setValue(address_es, forKey: "address_es")
  1566. }
  1567. // Get address_en
  1568. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1569. let address_en: String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1570. if (address_en != "ul"){ //From "null"
  1571. row.setValue(address_en, forKey: "address_en")
  1572. }
  1573. // Get address_eu
  1574. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1575. let address_eu: String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1576. if (address_eu != "ul"){ //From "null"
  1577. row.setValue(address_eu, forKey: "address_eu")
  1578. }
  1579. //Get link
  1580. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1581. let link: String = str.subStr(start : str.indexOf(target : "\"link\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1582. if (link != "ul"){ //From "null"
  1583. row.setValue(link, forKey: "link")
  1584. }
  1585. //Get lat
  1586. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1587. let strLat: String = str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  1588. if (strLat != "ul"){ //From "null"
  1589. let lat: Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  1590. row.setValue(lat, forKey: "lat")
  1591. }
  1592. //Get lon
  1593. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1594. let strLon: String = str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2)
  1595. if (strLon != "ul"){ //From "null"
  1596. let lon: Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  1597. row.setValue(lon, forKey: "lon")
  1598. }
  1599. //Save CoreData
  1600. do {
  1601. try context.save()
  1602. } catch let error as NSError {
  1603. NSLog(":SYNC:ERROR: Could not store Sponsor entry \(error.userInfo).")
  1604. } catch {
  1605. NSLog(":SYNC:ERROR: Could not store Sponsor entry.")
  1606. }
  1607. }
  1608. }
  1609. /**
  1610. Saves the received settings.
  1611. :param: entries Array of strings containing the rows of the table, in JSON format.
  1612. */
  1613. func saveSettings(entries : [String]){
  1614. NSLog(":SYNC:LOG: Saving settings.")
  1615. let defaults = UserDefaults.standard
  1616. // Loop new entries
  1617. for entry in entries{
  1618. // Get setting name
  1619. var str = entry
  1620. let name: String = str.subStr(start : str.indexOf(target : "\"name\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1621. // Get setting value
  1622. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1623. let value: Int = Int(str.subStr(start : str.indexOf(target : "\"value\":")! + 9, end : str.length - 2))!
  1624. // Save only the needed settings
  1625. if ["festivals", "comments", "photos"].contains(name){
  1626. NSLog(":SYNC:LOG: Setting \(name) \(value)")
  1627. defaults.set(value, forKey: name)
  1628. }
  1629. }
  1630. }
  1631. }