Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.00 KB | None | 0 0
  1. -----------------------------------------------DAO---------------------------------------------------------------------------------
  2. package com.hackathon.dao;
  3.  
  4. import java.util.List;
  5.  
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.query.Query;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Repository;
  10.  
  11. import com.hackathon.model.Person;
  12. import org.hibernate.Session;
  13.  
  14. @Repository
  15. public class PersonDAOImpl implements PersonDAO {
  16.  
  17. @Autowired
  18. private SessionFactory sessionFactory;
  19.  
  20. @Override
  21. public Person fetchPerson(String email) {
  22. @SuppressWarnings("rawtypes")
  23. Query query = sessionFactory.getCurrentSession().createQuery("from Person where email =:code");
  24. query.setParameter("code", email);
  25. List<Person> l = query.list();
  26. return l.get(0);
  27. }
  28.  
  29. @Override
  30. public long insertPerson(Person person) {
  31. sessionFactory.getCurrentSession().save(person);
  32. return person.getId();
  33. }
  34.  
  35. @Override
  36. public void updatePerson(long id, Person person) {
  37. // TODO Auto-generated method stub
  38.  
  39. }
  40.  
  41. @Override
  42. public boolean checkIdExist(String email) {
  43. @SuppressWarnings("rawtypes")
  44. Query query = sessionFactory.getCurrentSession().createQuery("from Person where email =:code");
  45. query.setParameter("code", email);
  46. List<Person> l = query.list();
  47. if(l.isEmpty() || l==null) return false;
  48. else return true;
  49. }
  50.  
  51. @Override
  52. public List<Person> fetchPersonList() {
  53. List<Person> list = sessionFactory.getCurrentSession().createQuery("from Person").list();
  54. return list;
  55. }
  56.  
  57. @Override
  58. public boolean checkLogin(String email,String password) {
  59. @SuppressWarnings("rawtypes")
  60. Query query = sessionFactory.getCurrentSession().createQuery("from Person where email =:mail and password =:pass");
  61. query.setParameter("mail", email);
  62. query.setParameter("pass",password);
  63. List<Person> l = query.list();
  64. if(l.isEmpty() || l==null) return false;
  65. else return true;
  66. }
  67. }
  68. ------------------------------------------------------service------------------------------------------------------------------
  69. package com.hackathon.service;
  70.  
  71. import java.util.List;
  72.  
  73. import org.springframework.beans.factory.annotation.Autowired;
  74. import org.springframework.stereotype.Service;
  75. import org.springframework.transaction.annotation.Transactional;
  76.  
  77. import com.hackathon.dao.PersonDAO;
  78. import com.hackathon.model.Person;
  79.  
  80. @Service
  81. @Transactional(readOnly = true)
  82. public class PersonServiceImpl implements PersonService {
  83.  
  84. @Autowired
  85. private PersonDAO personDAO;
  86.  
  87. @Transactional
  88. @Override
  89. public List<Person> fetchPersonList() {
  90. return personDAO.fetchPersonList();
  91. }
  92.  
  93. @Transactional
  94. @Override
  95. public long insertPerson(Person person) {
  96. return personDAO.insertPerson(person);
  97. }
  98.  
  99. @Transactional
  100. @Override
  101. public void updatePerson(long id, Person person) {
  102. // TODO Auto-generated method stub
  103.  
  104. }
  105.  
  106. @Transactional
  107. @Override
  108. public boolean checkIdExist(String email) {
  109. return personDAO.checkIdExist(email);
  110. }
  111.  
  112. @Transactional
  113. @Override
  114. public Person fetchPerson(String email) {
  115. return personDAO.fetchPerson(email);
  116. }
  117.  
  118. @Transactional
  119. @Override
  120. public boolean checkLogin(String email,String password) {
  121. return personDAO.checkLogin(email, password);
  122. }
  123.  
  124. }
  125. ------------------------------------------------------------------------Controller-----------------------------------------------------
  126.  
  127.  
  128. package com.hackathon.controller;
  129.  
  130. import java.util.*;
  131. import org.springframework.beans.factory.annotation.Autowired;
  132. import org.springframework.http.ResponseEntity;
  133. import org.springframework.web.bind.annotation.CrossOrigin;
  134. import org.springframework.web.bind.annotation.GetMapping;
  135. import org.springframework.web.bind.annotation.PathVariable;
  136. import org.springframework.web.bind.annotation.PostMapping;
  137. import org.springframework.web.bind.annotation.PutMapping;
  138. import org.springframework.web.bind.annotation.RequestBody;
  139. import org.springframework.web.bind.annotation.RequestMapping;
  140. import org.springframework.web.bind.annotation.RestController;
  141.  
  142. import com.hackathon.model.Person;
  143. import com.hackathon.model.ResponseObject;
  144. import com.hackathon.service.PersonService;
  145.  
  146. @CrossOrigin(origins = "*")
  147. @RestController
  148. @RequestMapping("/api")
  149. public class PersonController {
  150.  
  151. @Autowired
  152. private PersonService personService;
  153.  
  154. @PostMapping("/insertPerson")
  155. public ResponseEntity<?> insertPerson(@RequestBody Person person) {
  156. long id = personService.insertPerson(person);
  157. return ResponseEntity.ok().body("id " + id);
  158. }
  159.  
  160. @GetMapping("/fetchPerson/{email}")
  161. public ResponseEntity<ResponseObject> fetchPerson(@PathVariable String email) {
  162. ResponseObject obj = new ResponseObject();
  163. obj.setFoodResponse(personService.fetchPerson(email));
  164. return ResponseEntity.ok().body(obj);
  165. }
  166.  
  167. @GetMapping("/fetchPersonList")
  168. public ResponseEntity<List<Person>> fetchPersonList(){
  169. List<Person> list = personService.fetchPersonList();
  170. return ResponseEntity.ok().body(list);
  171. }
  172.  
  173. @GetMapping("/checkIdExist/{email}")
  174. public ResponseEntity<Boolean> checkIdExist(@PathVariable("email") String email){
  175. return ResponseEntity.ok().body(personService.checkIdExist(email));
  176. }
  177.  
  178. @GetMapping("/checkLogin/{email}/{password}")
  179. public ResponseEntity<ResponseObject> checkLogin(@PathVariable("email") String email,@PathVariable("password") String password){
  180. ResponseObject obj = new ResponseObject();
  181. obj.setFoodResponse(personService.checkLogin(email,password));
  182. return ResponseEntity.ok().body(obj);
  183. }
  184. }
  185. -------------------------------------------ResponseObject.java-------------------------------------------------------------
  186.  
  187. package com.hackathon.model;
  188.  
  189. public class ResponseObject {
  190.  
  191. private Object foodResponse;
  192.  
  193. public Object getFoodResponse() {
  194. return foodResponse;
  195. }
  196.  
  197. public void setFoodResponse(Object foodResponse) {
  198. this.foodResponse = foodResponse;
  199. }
  200. }
  201. --------------------------------------Auth.service.ts---------------------------------------------------------------------
  202.  
  203. import { Router } from '@angular/router';
  204. import { Injectable } from '@angular/core';
  205. import * as firebase from 'firebase';
  206. import { Observable } from "rxjs/Observable";
  207. import { config } from './.configuration';
  208. import { Http,Response, Headers, RequestOptions } from '@angular/http';
  209. import 'rxjs/add/operator/map';
  210. import 'rxjs/add/operator/catch';
  211.  
  212. @Injectable()
  213. export class AuthService {
  214.  
  215. loggedIn: boolean = false;
  216. displayName: string;
  217. email: string;
  218. emailVerified: boolean;
  219. photoURL: string;
  220. isAnonymous: string;
  221. uid: string;
  222. providerData: string;
  223. errorMessage: string;
  224. error: boolean;
  225. a:{};
  226.  
  227. constructor(public router: Router, private _hhtpService:Http) {
  228. firebase.initializeApp(config);
  229. }
  230.  
  231.  
  232. createUser(email: string, password: string) {
  233. firebase.auth().createUserWithEmailAndPassword(email, password)
  234. .then(() => {
  235. this.loggedIn = true;
  236. this.router.navigate(['/recipes'])
  237. })
  238. .catch((error) => {
  239. this.error = true;
  240. this.errorMessage = error.message;
  241. console.log(this.errorMessage);
  242. });
  243. }
  244.  
  245.  
  246.  
  247. loginUser(email: string, password: string) {
  248.  
  249. this.a = this._hhtpService.get("http://localhost:8080/foodapp/api/checkLogin/bharat@gmail/pass")
  250. .map((response: Response) => response.json())
  251. .catch(this.handleError);
  252. console.log("check this : "+this.a['foodResponse'])
  253. this.error = false;
  254. this.displayName = 'Bharat';
  255. this.email = 'bharat@bosch.com';
  256. this.emailVerified = true;
  257. this.photoURL = 'https://avatars2.githubusercontent.com/u/17523670?s=400&v=4';
  258. this.isAnonymous = 'no';
  259. this.uid = '123';
  260. this.providerData = '456';
  261. this.router.navigate(['/recipes'])
  262. }
  263. // firebase.auth().signInWithEmailAndPassword(email, password)
  264. // .then(() => {
  265. // firebase.auth().onAuthStateChanged((user) => {
  266. // if (user) {
  267. // // User is signed in.
  268. // this.loggedIn = true;
  269. // this.error = false;
  270. // this.displayName = user.displayName;
  271. // this.email = user.email;
  272. // this.emailVerified = user.emailVerified;
  273. // this.photoURL = user.photoURL;
  274. // this.isAnonymous = user.isAnonymous;
  275. // this.uid = user.uid;
  276. // this.providerData = user.providerData;
  277. // this.router.navigate(['/recipes'])
  278. // } else {
  279. // this.loggedIn = false;
  280. // this.router.navigate(['/recipes'])
  281. // }
  282. // });
  283. // })
  284. // .catch((error) => {
  285. // this.error = true;
  286. // this.errorMessage = error.message;
  287. // });
  288.  
  289.  
  290. onLogout() {
  291. firebase.auth().signOut()
  292. .catch(function (error) {
  293. this.errorMessage = error.message;
  294. console.log(this.errorMessage);
  295. })
  296. }
  297.  
  298. isAuthenticated() {
  299. return this.loggedIn;
  300. }
  301.  
  302. private handleError(error: Response){
  303. return Observable.throw(error);
  304. }
  305.  
  306. test(email: string, password: string){
  307. return this._hhtpService.get("http://localhost:8080/foodapp/api/checkLogin/"+email+"/"+password)
  308. .map((response: Response) => response.json())
  309. }
  310.  
  311. fetchUser(email: string){
  312. return this._hhtpService.get("http://localhost:8080/foodapp/api/fetchPerson/"+email)
  313. .map((response: Response) => response.json())
  314. .catch(this.handleError);
  315. }
  316.  
  317.  
  318.  
  319. }
  320. -------------------------------------------------------login.Component------------------------------------------------------------
  321.  
  322. import { Router } from '@angular/router';
  323. import { AuthService } from '../../services/auth.service';
  324. import { NgForm } from '@angular/forms';
  325. import { Component } from '@angular/core';
  326. import { Observable } from "rxjs/Observable";
  327. import 'rxjs/add/observable/interval';
  328.  
  329. @Component({
  330. selector: 'app-login',
  331. templateUrl: './login.component.html',
  332. styleUrls: ['./login.component.css']
  333. })
  334.  
  335. export class LoginComponent {
  336.  
  337. testobj :any;
  338. sub:any;
  339. usersub:any;
  340. user :{
  341. id:number,
  342. password:string,
  343. name:string,
  344. email:string,
  345. phone:number,
  346. address:string,
  347. type:string,
  348. }
  349.  
  350. constructor(public auth: AuthService, public router: Router) { }
  351.  
  352.  
  353. onLogin(data: NgForm) {
  354. const email: string = data.value.email;
  355. const password: string = data.value.password;
  356. this.auth.email = email;
  357. this.testobj = this.auth.test(email,password).subscribe(res => {
  358. console.log(res['foodResponse']);
  359. if(res['foodResponse']){
  360. this.auth.loggedIn = true;
  361. this.auth.error = false;
  362.  
  363. // this.displayName = user.displayName;
  364. // this.email = user.email;
  365. // this.emailVerified = user.emailVerified;
  366. // this.photoURL = user.photoURL;
  367. // this.isAnonymous = user.isAnonymous;
  368. // this.uid = user.uid;
  369. // this.providerData = user.providerData;
  370. // this.router.navigate(['/recipes'])
  371. this.router.navigate(['/recipes'])
  372. }
  373. });
  374. data.reset();
  375. }
  376.  
  377. fetchUser(email:string){
  378. if(this.auth.loggedIn){
  379. this.usersub = this.auth.fetchUser(email).subscribe(res=>{
  380. console.log(res['foodResponse'])
  381. });
  382. }
  383. }
  384.  
  385.  
  386.  
  387. }
  388. -------------------------------------------------------------------------------------------------------------------=====---
  389.  
  390. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  391. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  392. <modelVersion>4.0.0</modelVersion>
  393. <groupId>com.bushansirgur</groupId>
  394. <artifactId>bookapi</artifactId>
  395. <version>0.0.1-SNAPSHOT</version>
  396. <packaging>war</packaging>
  397. <properties>
  398. <failOnMissingWebXml>false</failOnMissingWebXml>
  399. </properties>
  400.  
  401. <dependencies>
  402. <!-- Spring MVC Dependency -->
  403. <dependency>
  404. <groupId>org.springframework</groupId>
  405. <artifactId>spring-webmvc</artifactId>
  406. <version>4.3.10.RELEASE</version>
  407. </dependency>
  408. <!-- Spring ORM -->
  409. <dependency>
  410. <groupId>org.springframework</groupId>
  411. <artifactId>spring-orm</artifactId>
  412. <version>4.3.10.RELEASE</version>
  413. </dependency>
  414. <!-- Hibernate ORM -->
  415. <dependency>
  416. <groupId>org.hibernate</groupId>
  417. <artifactId>hibernate-core</artifactId>
  418. <version>5.2.11.Final</version>
  419. </dependency>
  420. <!-- Hibernate-C3P0 Integration -->
  421. <dependency>
  422. <groupId>org.hibernate</groupId>
  423. <artifactId>hibernate-c3p0</artifactId>
  424. <version>5.2.11.Final</version>
  425. </dependency>
  426. <!-- c3p0 -->
  427. <dependency>
  428. <groupId>com.mchange</groupId>
  429. <artifactId>c3p0</artifactId>
  430. <version>0.9.5.2</version>
  431. </dependency>
  432. <!-- Mysql Connector -->
  433. <dependency>
  434. <groupId>mysql</groupId>
  435. <artifactId>mysql-connector-java</artifactId>
  436. <version>6.0.5</version>
  437. </dependency>
  438. <!-- Jackson API for JSON -->
  439. <dependency>
  440. <groupId>com.fasterxml.jackson.core</groupId>
  441. <artifactId>jackson-databind</artifactId>
  442. <version>2.8.7</version>
  443. </dependency>
  444. <!-- Servlet Dependency -->
  445. <dependency>
  446. <groupId>javax.servlet</groupId>
  447. <artifactId>javax.servlet-api</artifactId>
  448. <version>3.1.0</version>
  449. <scope>provided</scope>
  450. </dependency>
  451.  
  452. </dependencies>
  453. <build>
  454. <plugins>
  455. <plugin>
  456. <artifactId>maven-compiler-plugin</artifactId>
  457. <version>3.5.1</version>
  458. <configuration>
  459. <source>1.8</source>
  460. <target>1.8</target>
  461. </configuration>
  462. </plugin>
  463.  
  464. <!-- Embedded Apache Tomcat required for testing web application -->
  465. <plugin>
  466. <groupId>org.apache.tomcat.maven</groupId>
  467. <artifactId>tomcat7-maven-plugin</artifactId>
  468. <version>2.2</version>
  469. <configuration>
  470. <path>/</path>
  471. </configuration>
  472. </plugin>
  473. </plugins>
  474. </build>
  475. </project>
  476.  
  477.  
  478. --------------------------------------------------------------AppConfig--------------------------------------------------------
  479.  
  480.  
  481. package com.bushansirgur.config;
  482.  
  483. import java.util.Properties;
  484.  
  485. import org.springframework.beans.factory.annotation.Autowired;
  486. import org.springframework.context.annotation.Bean;
  487. import org.springframework.context.annotation.ComponentScan;
  488. import org.springframework.context.annotation.ComponentScans;
  489. import org.springframework.context.annotation.Configuration;
  490. import org.springframework.context.annotation.PropertySource;
  491. import org.springframework.core.env.Environment;
  492. import org.springframework.orm.hibernate5.HibernateTransactionManager;
  493. import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  494. import org.springframework.transaction.annotation.EnableTransactionManagement;
  495. import static org.hibernate.cfg.Environment.*;
  496.  
  497. @Configuration
  498. @PropertySource("classpath:db.properties")
  499. @EnableTransactionManagement
  500. @ComponentScans(value = { @ComponentScan("com.bushansirgur.dao"),
  501. @ComponentScan("com.bushansirgur.service") })
  502. public class AppConfig {
  503.  
  504. @Autowired
  505. private Environment env;
  506.  
  507. @Bean
  508. public LocalSessionFactoryBean getSessionFactory() {
  509. LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
  510.  
  511. Properties props = new Properties();
  512. // Setting JDBC properties
  513. props.put(DRIVER, env.getProperty("mysql.driver"));
  514. props.put(URL, env.getProperty("mysql.url"));
  515. props.put(USER, env.getProperty("mysql.user"));
  516. props.put(PASS, env.getProperty("mysql.password"));
  517.  
  518. // Setting Hibernate properties
  519. props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
  520. props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
  521.  
  522. // Setting C3P0 properties
  523. props.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));
  524. props.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));
  525. props.put(C3P0_ACQUIRE_INCREMENT,
  526. env.getProperty("hibernate.c3p0.acquire_increment"));
  527. props.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));
  528. props.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));
  529.  
  530. factoryBean.setHibernateProperties(props);
  531. factoryBean.setPackagesToScan("com.bushansirgur.model");
  532.  
  533. return factoryBean;
  534. }
  535.  
  536. @Bean
  537. public HibernateTransactionManager getTransactionManager() {
  538. HibernateTransactionManager transactionManager = new HibernateTransactionManager();
  539. transactionManager.setSessionFactory(getSessionFactory().getObject());
  540. return transactionManager;
  541. }
  542. }
  543.  
  544. -----------------------------------------------------------MyWebAppInitializer---------------------------------------------------------
  545.  
  546. package com.bushansirgur.config;
  547.  
  548. import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  549.  
  550. public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  551.  
  552. @Override
  553. protected Class<?>[] getRootConfigClasses() {
  554. return new Class[] { AppConfig.class };
  555. }
  556.  
  557. @Override
  558. protected Class<?>[] getServletConfigClasses() {
  559. return new Class[] { WebConfig.class };
  560. }
  561.  
  562. @Override
  563. protected String[] getServletMappings() {
  564. return new String[] { "/" };
  565. }
  566. }
  567. --------------------------------------------------------WebConfig.java--------------------------------------------------------
  568.  
  569. package com.bushansirgur.config;
  570.  
  571. import org.springframework.context.annotation.ComponentScan;
  572. import org.springframework.context.annotation.Configuration;
  573. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  574. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  575.  
  576. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  577.  
  578. @Configuration
  579. @EnableWebMvc
  580. @ComponentScan(basePackages = { "com.bushansirgur.controller" })
  581. public class WebConfig extends WebMvcConfigurerAdapter {
  582.  
  583. }
  584.  
  585. ----------------------------------------------------------------------------------------------------------------------------------
  586.  
  587. # MySQL properties
  588. mysql.driver=com.mysql.cj.jdbc.Driver
  589. mysql.url=jdbc:mysql://localhost:3306/bookdb
  590. mysql.user=root
  591. mysql.password=root
  592.  
  593. # Hibernate properties
  594. hibernate.show_sql=true
  595. hibernate.hbm2ddl.auto=update
  596.  
  597. #C3P0 properties
  598. hibernate.c3p0.min_size=5
  599. hibernate.c3p0.max_size=20
  600. hibernate.c3p0.acquire_increment=1
  601. hibernate.c3p0.timeout=1800
  602. hibernate.c3p0.max_statements=150
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement