Sync.swift 79 KB

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