franta-hg@149
|
1 |
package cz.frantovo.nekurak.ejb;
|
franta-hg@149
|
2 |
|
franta-hg@149
|
3 |
import cz.frantovo.nekurak.chat.MistnostPripojena;
|
franta-hg@149
|
4 |
import cz.frantovo.nekurak.chat.Nastaveni;
|
franta-hg@149
|
5 |
import cz.frantovo.nekurak.chat.Spojeni;
|
franta-hg@149
|
6 |
import cz.frantovo.nekurak.chat.SpravceNastaveni;
|
franta-hg@149
|
7 |
import cz.frantovo.nekurak.chat.UcetRobota;
|
franta-hg@149
|
8 |
import cz.frantovo.nekurak.dto.ZpravaChatu;
|
franta-hg@149
|
9 |
import cz.frantovo.nekurak.vyjimky.NekurakVyjimka;
|
franta-hg@149
|
10 |
import java.util.ArrayList;
|
franta-hg@149
|
11 |
import java.util.Collection;
|
franta-hg@149
|
12 |
import java.util.logging.Level;
|
franta-hg@149
|
13 |
import java.util.logging.Logger;
|
franta-hg@149
|
14 |
import javax.annotation.PostConstruct;
|
franta-hg@149
|
15 |
import javax.annotation.PreDestroy;
|
franta-hg@149
|
16 |
import javax.ejb.Singleton;
|
franta-hg@149
|
17 |
import javax.ejb.Startup;
|
franta-hg@149
|
18 |
import javax.naming.NamingException;
|
franta-hg@149
|
19 |
|
franta-hg@149
|
20 |
/**
|
franta-hg@149
|
21 |
*
|
franta-hg@149
|
22 |
* @author fiki
|
franta-hg@149
|
23 |
*/
|
franta-hg@149
|
24 |
@Singleton
|
franta-hg@149
|
25 |
@Startup
|
franta-hg@149
|
26 |
public class ChatEJB implements ChatRemote {
|
franta-hg@149
|
27 |
|
franta-hg@149
|
28 |
private static final Logger log = Logger.getLogger(ChatRemote.class.getSimpleName());
|
franta-hg@149
|
29 |
private Nastaveni nastaveni;
|
franta-hg@149
|
30 |
private Collection<Spojeni> spojeni = new ArrayList<Spojeni>();
|
franta-hg@149
|
31 |
|
franta-hg@149
|
32 |
@Override
|
franta-hg@149
|
33 |
public void posliZpravu(String mistnost, String prezdivka, String zprava) throws NekurakVyjimka {
|
franta-hg@149
|
34 |
MistnostPripojena mp = najdiMistnost(mistnost);
|
franta-hg@149
|
35 |
if (mp == null) {
|
franta-hg@149
|
36 |
throw new NekurakVyjimka("Místnost s tímto názvem neexistuje", null);
|
franta-hg@149
|
37 |
} else {
|
franta-hg@149
|
38 |
try {
|
franta-hg@149
|
39 |
mp.posliZpravu(new ZpravaChatu(prezdivka, zprava));
|
franta-hg@149
|
40 |
} catch (Exception e) {
|
franta-hg@149
|
41 |
log.log(Level.SEVERE, "Selhalo odesílání zprávy", e);
|
franta-hg@149
|
42 |
throw new NekurakVyjimka("Zprávu se nepodařilo odeslat.", e);
|
franta-hg@149
|
43 |
}
|
franta-hg@149
|
44 |
}
|
franta-hg@149
|
45 |
}
|
franta-hg@149
|
46 |
|
franta-hg@149
|
47 |
/**
|
franta-hg@149
|
48 |
* @param mistnost název místnosti včetně zavináče a serveru
|
franta-hg@149
|
49 |
* @param poradoveCislo pořadové číslo poslední zprávy, kterou jsme dostali
|
franta-hg@149
|
50 |
* @return všechny novější zprávy než dané pořadové číslo
|
franta-hg@149
|
51 |
* @throws NekurakVyjimka
|
franta-hg@149
|
52 |
*/
|
franta-hg@149
|
53 |
@Override
|
franta-hg@149
|
54 |
public Collection<ZpravaChatu> getZpravy(String mistnost, int poradoveCislo) throws NekurakVyjimka {
|
franta-hg@149
|
55 |
MistnostPripojena mp = najdiMistnost(mistnost);
|
franta-hg@149
|
56 |
if (mp == null) {
|
franta-hg@149
|
57 |
throw new NekurakVyjimka("Místnost s tímto názvem neexistuje", null);
|
franta-hg@149
|
58 |
} else {
|
franta-hg@149
|
59 |
return mp.getZpravy(poradoveCislo);
|
franta-hg@149
|
60 |
}
|
franta-hg@149
|
61 |
}
|
franta-hg@149
|
62 |
|
franta-hg@149
|
63 |
public ChatEJB() throws NekurakVyjimka {
|
franta-hg@149
|
64 |
/** TODO: vyřešit lépe. */
|
franta-hg@149
|
65 |
nastaveni = new SpravceNastaveni().getNastaveni();
|
franta-hg@149
|
66 |
}
|
franta-hg@149
|
67 |
|
franta-hg@149
|
68 |
@PreDestroy
|
franta-hg@149
|
69 |
public void odpoj() {
|
franta-hg@149
|
70 |
for (Spojeni s : spojeni) {
|
franta-hg@149
|
71 |
s.odpoj();
|
franta-hg@149
|
72 |
}
|
franta-hg@149
|
73 |
}
|
franta-hg@149
|
74 |
|
franta-hg@149
|
75 |
@PostConstruct
|
franta-hg@149
|
76 |
public void inicializuj() throws NekurakVyjimka, NamingException {
|
franta-hg@149
|
77 |
pripojXMPP();
|
franta-hg@149
|
78 |
}
|
franta-hg@149
|
79 |
|
franta-hg@149
|
80 |
private void pripojXMPP() throws NekurakVyjimka {
|
franta-hg@149
|
81 |
try {
|
franta-hg@149
|
82 |
for (UcetRobota u : nastaveni.getUctyRobota()) {
|
franta-hg@149
|
83 |
Spojeni s = new Spojeni(u);
|
franta-hg@149
|
84 |
spojeni.add(s);
|
franta-hg@149
|
85 |
}
|
franta-hg@149
|
86 |
} catch (Exception e) {
|
franta-hg@149
|
87 |
throw new NekurakVyjimka("Chyba při připojování.", e);
|
franta-hg@149
|
88 |
}
|
franta-hg@149
|
89 |
}
|
franta-hg@149
|
90 |
|
franta-hg@149
|
91 |
/**
|
franta-hg@149
|
92 |
* @param nazev Název místnosti, kterou hledáme.
|
franta-hg@149
|
93 |
* @return nalezená místnost, nebo null, pokud místnost nebyla nalezena.
|
franta-hg@149
|
94 |
*/
|
franta-hg@149
|
95 |
private MistnostPripojena najdiMistnost(String nazev) {
|
franta-hg@149
|
96 |
for (Spojeni s : spojeni) {
|
franta-hg@149
|
97 |
for (MistnostPripojena mp : s.getMistnosti()) {
|
franta-hg@149
|
98 |
if (mp.porovnejNazev(nazev)) {
|
franta-hg@149
|
99 |
return mp;
|
franta-hg@149
|
100 |
}
|
franta-hg@149
|
101 |
}
|
franta-hg@149
|
102 |
}
|
franta-hg@149
|
103 |
return null;
|
franta-hg@149
|
104 |
}
|
franta-hg@149
|
105 |
}
|