Sync.swift 63 KB

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