Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. package src;
  2.  
  3. import beans.Korisnik;
  4. import java.io.Serializable;
  5. import javax.enterprise.context.SessionScoped;
  6. import javax.inject.Named;
  7. import org.hibernate.Query;
  8. import org.hibernate.Session;
  9. import org.hibernate.Transaction;
  10. import tools.HibernateUtil;
  11.  
  12. /*
  13. * To change this license header, choose License Headers in Project Properties.
  14. * To change this template file, choose Tools | Templates
  15. * and open the template in the editor.
  16. */
  17.  
  18. /**
  19. *
  20. * @author Nikola
  21. */
  22. @Named(value = "controllernovi")
  23. @SessionScoped
  24. public class Controller implements Serializable {
  25.  
  26. //private String ime;
  27. private Session session = null;
  28. //private List<Korisnik> korisnici = new ArrayList<>();
  29. private Korisnik korisnik;
  30. private String username;
  31. private String password;
  32. //za registraciju
  33. private String ime;
  34. private String prezime;
  35. private String korIme;
  36. private String lozinka;
  37. private String ponLozinka;
  38. private String telefon;
  39. private String eposta;
  40. //za promenu lozinke
  41. private String korImepromena;
  42. private String lozinkastara;
  43. private String novalozinka;
  44. private String ponnovalozinka;
  45.  
  46. public Controller() {
  47. this.session = HibernateUtil.getSessionFactory().getCurrentSession();
  48. }
  49.  
  50. public String login() {
  51. if (!username.isEmpty() && !password.isEmpty()) {
  52. Transaction tx = session.beginTransaction();
  53. Query q = session.createQuery("from Korisnik where KorisnickoIme='" + username + "'");
  54. if (q.list().iterator().hasNext()) {
  55. q = session.createQuery("from Korisnik where KorisnickoIme='" + username + "' and Lozinka='" + password + "'");
  56. if (q.list().iterator().hasNext()) {
  57. korisnik = (Korisnik) q.list().iterator().next();
  58. //ovde dodati admin stvari
  59. if (korisnik.getIsAdmin() == 1) {
  60. return "admin";
  61. } else {
  62. return "korisnik";
  63. }
  64. } else {
  65. return "index";
  66. }
  67. }
  68. }
  69. return "index";
  70. }
  71.  
  72. public String register() {
  73. Transaction tx = session.beginTransaction();
  74. Korisnik korisnik = new Korisnik(ime, prezime, korIme, lozinka, telefon, eposta, 0);
  75. session.save(korisnik);
  76. session.getTransaction().commit();
  77. session.close();
  78. return "index";
  79. }
  80.  
  81. public String promeniLozinku() {
  82. if (!korImepromena.isEmpty() && !lozinkastara.isEmpty() && !novalozinka.isEmpty()) {
  83. Transaction tx = session.beginTransaction();
  84. Query q = session.createQuery("from Korisnik where KorisnickoIme='" + korImepromena + "'");
  85. if (q.list().iterator().hasNext()) {
  86. q = session.createQuery("from Korisnik where KorisnickoIme='" + korImepromena + "' and Lozinka='" + lozinkastara + "'");
  87. if (q.list().iterator().hasNext()) {
  88. Korisnik korisnik = (Korisnik) q.list().iterator().next();
  89. //ovde dodati admin stvari
  90. korisnik.setLozinka(novalozinka);
  91. session.saveOrUpdate(korisnik);
  92. session.getTransaction().commit();
  93. }
  94. }
  95. }
  96. return "index";
  97. }
  98.  
  99. // public Korisnik test() {
  100. // org.hibernate.Transaction tx = session.beginTransaction();;
  101. // try {
  102. // Query q = session.createQuery("from Korisnik");
  103. // korisnici.clear();
  104. // korisnici = (List<Korisnik>) q.list();
  105. // dodaj();
  106. // return korisnici.iterator().next();
  107. // } catch (Exception e) {
  108. // e.printStackTrace();
  109. // }
  110. // return null;
  111. // }
  112. //
  113. // public void dodaj() {
  114. // org.hibernate.Transaction tx = session.beginTransaction();;
  115. // try {
  116. // Korisnik korisnik = new Korisnik("Milos", "Milosevic", "m12ilos", "123123", "16541644163", "admin@djerga.com");
  117. // session.save(korisnik);
  118. // session.getTransaction().commit();
  119. // } catch (Exception e) {
  120. // e.printStackTrace();
  121. // }
  122. // }
  123. public Session getSession() {
  124. return session;
  125. }
  126.  
  127. public void setSession(Session session) {
  128. this.session = session;
  129. }
  130.  
  131. public String getUsername() {
  132. return username;
  133. }
  134.  
  135. public void setUsername(String username) {
  136. this.username = username;
  137. }
  138.  
  139. public String getPassword() {
  140. return password;
  141. }
  142.  
  143. public void setPassword(String password) {
  144. this.password = password;
  145. }
  146.  
  147. public Korisnik getKorisnik() {
  148. return korisnik;
  149. }
  150.  
  151. public void setKorisnik(Korisnik korisnik) {
  152. this.korisnik = korisnik;
  153. }
  154.  
  155. public String getIme() {
  156. return ime;
  157. }
  158.  
  159. public void setIme(String ime) {
  160. this.ime = ime;
  161. }
  162.  
  163. public String getPrezime() {
  164. return prezime;
  165. }
  166.  
  167. public void setPrezime(String prezime) {
  168. this.prezime = prezime;
  169. }
  170.  
  171. public String getKorIme() {
  172. return korIme;
  173. }
  174.  
  175. public void setKorIme(String korIme) {
  176. this.korIme = korIme;
  177. }
  178.  
  179. public String getLozinka() {
  180. return lozinka;
  181. }
  182.  
  183. public void setLozinka(String lozinka) {
  184. this.lozinka = lozinka;
  185. }
  186.  
  187. public String getPonLozinka() {
  188. return ponLozinka;
  189. }
  190.  
  191. public void setPonLozinka(String ponLozinka) {
  192. this.ponLozinka = ponLozinka;
  193. }
  194.  
  195. public String getTelefon() {
  196. return telefon;
  197. }
  198.  
  199. public void setTelefon(String telefon) {
  200. this.telefon = telefon;
  201. }
  202.  
  203. public String getEposta() {
  204. return eposta;
  205. }
  206.  
  207. public void setEposta(String eposta) {
  208. this.eposta = eposta;
  209. }
  210.  
  211. public String getKorImepromena() {
  212. return korImepromena;
  213. }
  214.  
  215. public void setKorImepromena(String korImepromena) {
  216. this.korImepromena = korImepromena;
  217. }
  218.  
  219. public String getLozinkastara() {
  220. return lozinkastara;
  221. }
  222.  
  223. public void setLozinkastara(String lozinkastara) {
  224. this.lozinkastara = lozinkastara;
  225. }
  226.  
  227. public String getNovalozinka() {
  228. return novalozinka;
  229. }
  230.  
  231. public void setNovalozinka(String novalozinka) {
  232. this.novalozinka = novalozinka;
  233. }
  234.  
  235. public String getPonnovalozinka() {
  236. return ponnovalozinka;
  237. }
  238.  
  239. public void setPonnovalozinka(String ponnovalozinka) {
  240. this.ponnovalozinka = ponnovalozinka;
  241. }
  242.  
  243. //test
  244. //private Part image;
  245.  
  246. // public String uploadImage() throws IOException {
  247. // //InputStream input = image.getInputStream();
  248. // //Files.copy(input, new File("C:\\Users\\Nikola\\Desktop\\projekatPIA file.jpg").toPath());
  249. // String appPath = "/resources";
  250. // // constructs path of the directory to save uploaded file
  251. // String savePath = appPath + File.separator + "uploadFiles";
  252. //
  253. // // creates the save directory if it does not exists
  254. // File fileSaveDir = new File(savePath);
  255. // if (!fileSaveDir.exists()) {
  256. // fileSaveDir.mkdir();
  257. // }
  258. //
  259. // String fileName = "fajl.jpg";
  260. // // refines the fileName in case it is an absolute path
  261. // fileName = new File(fileName).getName();
  262. // image.write(savePath + File.separator + fileName);
  263. // return "korisnik";
  264. // }
  265. // private String destination = "D:\\tmp\\";
  266. //
  267. // public void upload(FileUploadEvent event) {
  268. // FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
  269. // FacesContext.getCurrentInstance().addMessage(null, msg);
  270. // // Do what you want with the file
  271. // try {
  272. // uploadImage(event.getFile().getFileName(), event.getFile().getInputstream());
  273. // } catch (IOException e) {
  274. // e.printStackTrace();
  275. // }
  276. //
  277. // }
  278. //
  279. // public void uploadImage(String fileName, InputStream in) throws IOException {
  280. // try {
  281. // // write the inputStream to a FileOutputStream
  282. // OutputStream out = new FileOutputStream(new File(destination + fileName));
  283. //
  284. // int read = 0;
  285. // byte[] bytes = new byte[1024];
  286. //
  287. // while ((read = in.read(bytes)) != -1) {
  288. // out.write(bytes, 0, read);
  289. // }
  290. //
  291. // in.close();
  292. // out.flush();
  293. // out.close();
  294. //
  295. // System.out.println("New file created!");
  296. // } catch (IOException e) {
  297. // System.out.println(e.getMessage());
  298. // }
  299. // }
  300.  
  301. // public Part getImage() {
  302. // return image;
  303. // }
  304. //
  305. // public void setImage(Part image) {
  306. // this.image = image;
  307. // }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement