Skip to content

Commit

Permalink
Aula 06
Browse files Browse the repository at this point in the history
Aula 06
  • Loading branch information
estevandias authored Nov 5, 2018
1 parent e40d4b6 commit 7a481e0
Show file tree
Hide file tree
Showing 20 changed files with 214 additions and 80 deletions.
23 changes: 12 additions & 11 deletions HoraDaPipocaUpSource/WebContent/WEB-INF/spring-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="br.usjt.ads.arqdes" />
<context:component-scan
base-package="br.usjt.ads.arqdes" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:interceptors>
<bean class="br.usjt.ads.sixteam.interceptor.Interceptor" />
</mvc:interceptors>

<mvc:resources mapping="/resources/**" location="/resources/" />

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mysqlDataSource" />
Expand All @@ -33,18 +32,20 @@
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

<bean id="mysqlDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/pipocadb"></property>
<property name="username" value="Alunos"></property>
<property name="password" value="alunos"></property>
<property name="username" value="morone"></property>
<property name="password" value="morone123"></property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />

</beans>
39 changes: 17 additions & 22 deletions HoraDaPipocaUpSource/WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>pipocad2</display-name>
<display-name>pipoca_aula06</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
Expand All @@ -25,25 +25,20 @@
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<session-config>
<session-timeout>5</session-timeout>
</session-config>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5 changes: 3 additions & 2 deletions HoraDaPipocaUpSource/build/classes/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="pipoca_aula05">
<persistence-unit name="pipoca_aula06">
<!-- provedor/implementacao do JPA -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- entidade mapeada -->
<class>br.usjt.ads.arqdes.model.entity.Filme</class>
<class>br.usjt.ads.arqdes.model.entity.Genero</class>
<class>br.usjt.ads.arqdes.model.entity.Usuario</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 3 additions & 2 deletions HoraDaPipocaUpSource/src/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="pipoca_aula05">
<persistence-unit name="pipoca_aula06">
<!-- provedor/implementacao do JPA -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- entidade mapeada -->
<class>br.usjt.ads.arqdes.model.entity.Filme</class>
<class>br.usjt.ads.arqdes.model.entity.Genero</class>
<class>br.usjt.ads.arqdes.model.entity.Usuario</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package br.usjt.ads.arqdes.controller;

import java.io.IOException;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import br.usjt.ads.arqdes.model.entity.Filme;
import br.usjt.ads.arqdes.model.service.FilmeService;

@RestController
public class FilmesRest {
@Autowired
private FilmeService fService;

@Transactional
@RequestMapping(method=RequestMethod.POST, value="rest/inserir_filme")
public ResponseEntity<Filme> criarFilme(@RequestBody Filme filme) {
try {
fService.inserirFilme(filme);
return new ResponseEntity<Filme>(filme, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<Filme>(filme, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@Transactional
@RequestMapping(method=RequestMethod.GET, value="rest/buscar_filme")
public @ResponseBody Filme buscarFilme(int id) {
Filme filme = null;
try {
filme = fService.buscarFilme(id);
return filme;
} catch(IOException e) {
e.printStackTrace();
}
return filme;
}

@Transactional
@RequestMapping(method=RequestMethod.PUT, value="rest/atualizar_filme")
public ResponseEntity<Filme> atualizarFilme(@RequestBody Filme filme) {
try {
fService.atualizarFilme(filme);
return new ResponseEntity<Filme>(filme, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<Filme>(filme, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@Transactional
@RequestMapping(method=RequestMethod.DELETE, value="rest/deletar_filme")
public ResponseEntity<Filme> deletarFilme(@RequestBody Filme filme) {
try {
fService.excluirFilme(filme.getId());
return new ResponseEntity<Filme>(filme, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<Filme>(filme, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@Transactional
@RequestMapping(method=RequestMethod.GET, value="rest/filmes")
public @ResponseBody List<Filme> listagem(String chave) {
List<Filme> lista = null;
try{
if(chave == null || chave.equals("")){
lista = fService.listarFilmes();
} else {
lista = fService.listarFilmes(chave);
}
} catch(IOException e){
e.printStackTrace();
}
return lista;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@

import br.usjt.ads.arqdes.model.entity.Filme;
import br.usjt.ads.arqdes.model.entity.Genero;
import br.usjt.ads.arqdes.model.entity.Usuario;
import br.usjt.ads.arqdes.model.service.FilmeService;
import br.usjt.ads.arqdes.model.service.GeneroService;
import br.usjt.ads.arqdes.model.service.UsuarioService;

@Controller
public class ManterFilmesController {
@Autowired
private FilmeService fService;
@Autowired
private GeneroService gService;
@Autowired
private UsuarioService uService;


@RequestMapping("/")
Expand Down Expand Up @@ -94,23 +90,6 @@ public String buscarFilmes(HttpSession session, @RequestParam String chave){
return "Erro";
}
}

@RequestMapping("/tela_login")
public String telaLogin() {
return "TelaLogin";
}

@RequestMapping("/fazer_login")
public String fazerLogin(Usuario usuario, HttpSession session) {
try {
usuario = uService.fazerLogin(usuario);
session.setAttribute("usuario", usuario);
return "redirect:/inicio";
} catch (IOException e) {
e.printStackTrace();
}
return "erro";
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import br.usjt.ads.arqdes.model.entity.Filme;
@Repository
public class FilmeDAO {

@PersistenceContext
EntityManager manager;

Expand All @@ -24,18 +23,41 @@ public int inserirFilme(Filme filme) throws IOException {
public Filme buscarFilme(int id) throws IOException{
return manager.find(Filme.class, id);
}

public void atualizarFilme(Filme filme) throws IOException {
manager.merge(filme);
}

public void excluirFilme(int id) throws IOException {
manager.remove(manager.find(Filme.class, id));
}

@SuppressWarnings("unchecked")
public List<Filme> listarFilmes(String chave) throws IOException {
Query query = manager.createQuery("select f from Filme f where f.nome like :chave");
query.setParameter("chave", "%"+chave+"%");
return query.getResultList();
Query query = manager.createQuery("select f from Filme f where f.titulo like :chave");
query.setParameter("query", "%"+chave+"%");
return query.getResultList();
}

@SuppressWarnings("unchecked")
public List<Filme> listarFilmes() throws IOException {
Query query = manager.createQuery("select f from Filme f");
return query.getResultList();
return manager.createQuery("select f from Filme f").getResultList();
}

}
















Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import br.usjt.ads.arqdes.model.entity.Genero;

@Repository
public class GeneroDAO {

@PersistenceContext
EntityManager manager;

Expand All @@ -23,7 +21,22 @@ public Genero buscarGenero(int id) throws IOException {

@SuppressWarnings("unchecked")
public List<Genero> listarGeneros() throws IOException {
Query query = manager.createQuery("select g from Genero");
return query.getResultList();
return manager.createQuery("select g from Genero g").getResultList();
}
}
















Loading

0 comments on commit 7a481e0

Please sign in to comment.