Sync.swift 81 KB

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