Sync.swift 53 KB

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