Skip to content
Snippets Groups Projects
Commit 1599e78c authored by Thomas Carrara's avatar Thomas Carrara
Browse files

Deja vu, I've just been in this place before, Higher on the street, And I know...

Deja vu, I've just been in this place before, Higher on the street, And I know it's my time to go, Calling you and the search is mystery, Standing on my feet, It's so hard when I try to be me uoooh!
parent 48070255
No related branches found
No related tags found
No related merge requests found
Showing with 23 additions and 181 deletions
# TP Kotlin # TP Kotlin
--- ---
## [Diapo disponible ici !](https://docs.google.com/presentation/d/1oz5HcHxAeiFB5n_BwYRH1tPDPVsgJ5tOLuE_J7y7YS4/edit?usp=sharing)
---
**PS :** Vous pouvez skip l'intro et aller à __l'Étape 1__ si vous n'aimez pas les thrillers de science-fiction autobiographiques. **PS :** Vous pouvez skip l'intro et aller à __l'Étape 1__ si vous n'aimez pas les thrillers de science-fiction autobiographiques.
### Précédemment, dans _la vie_ ### Précédemment, dans _la vie_
...@@ -36,8 +38,9 @@ Alternativement, les plus chanceux d'entre vous qui utilisent IntelliJ (pas Ecli ...@@ -36,8 +38,9 @@ Alternativement, les plus chanceux d'entre vous qui utilisent IntelliJ (pas Ecli
##### TODO : ##### TODO :
- [ ] Créer les mêmes classes que dans l'étape 1, mais en _Kotlin_ (donc `Meme` et `FavDir`). - [ ] Créer les mêmes classes que dans l'étape 1, mais en _Kotlin_ (donc `Meme` et `FavDir`).
- [ ] Lui (`FavDIr`) ajouter un bloc _init_ qui print un truc dans la console (histoire de voir qu'il est bien initialisé une seule fois) - [ ] Créer une méthode _affiche()_ dans `FavDIr` pour l'afficher en front (tout bête, on fait du back ici)
- [ ] Lui rajouter la méthode `addMeme(meme: Meme)`, pour ajouter un meme - [ ] Ajouter un bloc _init_ qui print un truc dans la console dans le `FavDIr` (histoire de voir qu'il est bien initialisé une seule fois)
- [ ] Rajouter la méthode `addMeme(meme: Meme)`, pour ajouter un meme (toujours dans `FavDIr`)
- [ ] Et aussi `getAll()` qui (vous vous en doutez, bilingue que vous êtes) retourne à l'appelant la liste des memes du _FavDir_ - [ ] Et aussi `getAll()` qui (vous vous en doutez, bilingue que vous êtes) retourne à l'appelant la liste des memes du _FavDir_
## Étape 2 : Ajouter des memes a ses favoris ## Étape 2 : Ajouter des memes a ses favoris
......
package com.tpKotlin.glhf.tpkotlin.controller; package com.tpKotlin.glhf.tpkotlin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import com.tpKotlin.glhf.tpkotlin.entity.FavDir; import com.tpKotlin.glhf.tpkotlin.entity.FavDir;
import com.tpKotlin.glhf.tpkotlin.entity.Meme; import com.tpKotlin.glhf.tpkotlin.entity.Meme;
import com.tpKotlin.glhf.tpkotlin.service.DossierFavService;
/**
* Controller pour les jsp (oui on sait c'est vieux les jsp, mais ça marche
* facilement)
*/
@Controller @Controller
public class SimpleController { public class SimpleController {
@Autowired // @Autowired
private DossierFavService dfs; // private DossierFavService dfs;
@RequestMapping({ "/", "/index" }) @RequestMapping({ "/", "/index" })
public ModelAndView index() { public ModelAndView index() {
...@@ -22,9 +24,8 @@ public class SimpleController { ...@@ -22,9 +24,8 @@ public class SimpleController {
@RequestMapping("/ex1") @RequestMapping("/ex1")
public ModelAndView exo1() { public ModelAndView exo1() {
String s = FavDir.affiche(); final ModelAndView mav = new ModelAndView("ex1");
ModelAndView mav = new ModelAndView("ex1"); mav.addObject("dirfav", FavDir.affiche());
mav.addObject("dirfav", s );
return mav; return mav;
} }
...@@ -40,9 +41,8 @@ public class SimpleController { ...@@ -40,9 +41,8 @@ public class SimpleController {
@RequestMapping("/ex4") @RequestMapping("/ex4")
public ModelAndView exo4() { public ModelAndView exo4() {
String s = dfs.getAllImagesFromFavFASTER(); final ModelAndView mav = new ModelAndView("ex4");
ModelAndView mav = new ModelAndView("ex4"); mav.addObject("dirfav", "il faut charger depuis le back"/* this.dfs.getAllImagesFromFav() */);
mav.addObject("dirfav", s );
return mav; return mav;
} }
......
package com.tpKotlin.glhf.tpkotlin.controller
import com.tpKotlin.glhf.tpkotlin.entity.Meme
import com.tpKotlin.glhf.tpkotlin.service.DossierFavService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.servlet.ModelAndView
@Controller
@RequestMapping("meme")
class MemeController {
@Autowired
lateinit var favService: DossierFavService
@PostMapping("/add")
fun addMeme(@ModelAttribute("formMeme") formMeme: Meme): ModelAndView {
favService.insertMeme(formMeme)
var mav = ModelAndView("ex2", "succeed", "true")
mav.addObject("formMeme", Meme("", ""))
return mav
}
}
\ No newline at end of file
package com.tpKotlin.glhf.tpkotlin.dao package com.tpKotlin.glhf.tpkotlin.dao
import com.tpKotlin.glhf.tpkotlin.entity.Meme
interface ImageAccessorDao { interface ImageAccessorDao {
fun getMeme(nom: String): Boolean fun getMeme(nom: String): Boolean
fun saveMeme(meme: Meme) fun saveMeme(/*meme: Meme*/)
} }
\ No newline at end of file
package com.tpKotlin.glhf.tpkotlin.dao package com.tpKotlin.glhf.tpkotlin.dao
import com.tpKotlin.glhf.tpkotlin.entity.FavDir
import com.tpKotlin.glhf.tpkotlin.entity.Meme
import com.tpKotlin.glhf.tpkotlin.tools.FileManager import com.tpKotlin.glhf.tpkotlin.tools.FileManager
import kotlinx.coroutines.* import kotlinx.coroutines.*
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
...@@ -14,14 +11,12 @@ class ImageAccessorDaoImpl : ImageAccessorDao { ...@@ -14,14 +11,12 @@ class ImageAccessorDaoImpl : ImageAccessorDao {
override fun getMeme(nom: String): Boolean { override fun getMeme(nom: String): Boolean {
//celui qui retire çeci sera maudit pendant 3 generations (la sienne incluse) //celui qui retire çeci sera maudit pendant 3 generations (la sienne incluse)
runBlocking { runBlocking {
println("" + LocalDateTime.now() + ">> on va attendre")
//delay(1000L)
Thread.sleep(1000L) Thread.sleep(1000L)
} }
return true return true
} }
override fun saveMeme(meme: Meme) { override fun saveMeme(/*meme: Meme*/) {
FileManager.saveMemeInFile(meme.nom, meme.url) //FileManager.saveMemeInFile(meme.nom, meme.url)
} }
} }
\ No newline at end of file
package com.tpKotlin.glhf.tpkotlin.entity
import java.time.LocalDateTime
import com.tpKotlin.glhf.tpkotlin.tools.FileManager
object FavDir {
val list: MutableList<Meme> = mutableListOf()
init {
println("Dossier de favoris crée")
if (FileManager.thereIsSomethingInTheFile()) {
FileManager.loadAllFromFile()
}
}
fun addMeme(meme: Meme) {
list.add(meme)
FileManager.saveMemeInFile(meme.nom, meme.url)
}
fun loadMeme(meme: Meme) {
list.add(meme)
}
@JvmStatic
fun affiche(): String {
var s: String = ""
for (i in 0 until list.size) {
s = list.get(i).nom + " " + list.get(i).url + "&&&"+ s
}
return s
}
fun getAll(): MutableList<Meme> {
return list
}
}
data class Meme(var nom: String, var url: String)
package com.tpKotlin.glhf.tpkotlin.service package com.tpKotlin.glhf.tpkotlin.service
import com.tpKotlin.glhf.tpkotlin.entity.Meme
interface DossierFavService { interface DossierFavService {
fun insertMeme(meme: Meme) fun insertMeme(/*meme: Meme*/)
fun getAllImagesFromFav(): String fun getAllImagesFromFav(): String
fun getAllImagesFromFavFASTER(): String fun getAllImagesFromFavFASTER(): String
} }
\ No newline at end of file
package com.tpKotlin.glhf.tpkotlin.service
import com.tpKotlin.glhf.tpkotlin.dao.ImageAccessorDao
import kotlinx.coroutines.*
import com.tpKotlin.glhf.tpkotlin.entity.FavDir
import com.tpKotlin.glhf.tpkotlin.entity.Meme
import org.springframework.stereotype.Service
import com.tpKotlin.glhf.tpkotlin.dao.ImageAccessorDaoImpl
import org.springframework.beans.factory.annotation.Autowired
import java.time.LocalDateTime
@Service
class DossierFavServiceImpl : DossierFavService {
@Autowired
lateinit var imgDao: ImageAccessorDao
//méthode check + ajout exo 2
override fun insertMeme(meme: Meme) {
FavDir.addMeme(meme)
}
//Truc lent pour l'exo 3
override fun getAllImagesFromFav(): String {
var liste = FavDir.getAll()
var s: String = ""
var res: Boolean = false
for (i in 0 until liste.size) {
s = liste.get(i).url + "&&&" + s
}
for (img in liste) {
println("> here we go")
res = imgDao.getMeme(img.nom)
}
return if (res) s else "fail"
}
//Truc rapide pour le 4
override fun getAllImagesFromFavFASTER(): String {
var liste = FavDir.getAll()
var jobList: MutableList<Job> = mutableListOf()
var s: String = ""
for (i in 0 until liste.size) {
s = liste.get(i).url + "&&&" + s
}
runBlocking {
for (img in liste) {
println("> service lance l'appel DAO")
jobList.add(GlobalScope.launch { imgDao.getMeme(img.nom) })
}
for (myJob in jobList) {
myJob.join()
println("" + LocalDateTime.now() + " >> affichage ? " + myJob.isActive)
}
}
return s
}
}
\ No newline at end of file
package com.tpKotlin.glhf.tpkotlin.tools package com.tpKotlin.glhf.tpkotlin.tools
import java.io.File import java.io.File
import com.tpKotlin.glhf.tpkotlin.entity.FavDir
import com.tpKotlin.glhf.tpkotlin.entity.Meme
import java.io.FileOutputStream import java.io.FileOutputStream
object FileManager { object FileManager {
...@@ -24,30 +22,10 @@ object FileManager { ...@@ -24,30 +22,10 @@ object FileManager {
} }
fun loadAllFromFile() { fun loadAllFromFile() {
lateinit var tab: List<String> //charger les images depuis le fichier vers le singleton
var nom: String = ""
File(PATH_TO_FILE + FILE_NAME).forEachLine {
if (!it.startsWith('#')) {
tab = it.split(" ")
if (tab.size == 2) {
FavDir.loadMeme(Meme(tab[0], tab[1]))
} else if(tab.size > 2) {
for(i in 0 until tab.size-1) {
nom += tab[i] + " "
}
FavDir.loadMeme(Meme(nom, tab[tab.size-1]))
}
}
}
} }
fun saveMemeInFile(name: String, url: String) { fun saveMemeInFile(name: String, url: String) {
FileOutputStream(File(PATH_TO_FILE + FILE_NAME), true) //sauvegarder une image dans le fichier
.bufferedWriter()
.use {
it.newLine()
it.write(name.trim() + " " + url.trim())
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment