Sync.swift 80 KB

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