Sync.swift 68 KB

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