Sync.swift 56 KB

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