Created
April 4, 2018 00:47
-
-
Save Felipe00/bedfa0b07bf31a877ce3e0299dffcfa8 to your computer and use it in GitHub Desktop.
Ajustando servidor do Juan (Projeto Freelancer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.com.sistema.escola.modelo; | |
| import com.xpert.utils.DateUtils; | |
| import java.io.Serializable; | |
| import java.text.ParseException; | |
| import java.util.Date; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import javax.persistence.CascadeType; | |
| import javax.persistence.Column; | |
| import javax.persistence.Entity; | |
| import javax.persistence.GeneratedValue; | |
| import javax.persistence.GenerationType; | |
| import javax.persistence.Id; | |
| import javax.persistence.JoinColumn; | |
| import javax.persistence.ManyToOne; | |
| import javax.persistence.SequenceGenerator; | |
| import javax.persistence.Temporal; | |
| import javax.persistence.TemporalType; | |
| import javax.xml.bind.annotation.XmlRootElement; | |
| import org.json.JSONObject; | |
| /** | |
| * | |
| * @author juan | |
| * @author Felipe00 @{github.com/Felipe00} | |
| */ | |
| @Entity | |
| @XmlRootElement | |
| @SequenceGenerator(name = "aluno_id_seq", sequenceName = "aluno_id_seq", allocationSize = 1) | |
| public class Aluno implements Serializable { | |
| private static final String DATE_PATTERN = "dd/MM/yyyy"; | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = 1L; | |
| @Id | |
| @GeneratedValue(generator = "aluno_id_seq", strategy = GenerationType.SEQUENCE) | |
| private Long id; | |
| private String nome; | |
| private String matricula; | |
| @ManyToOne | |
| @JoinColumn(name = "id_turma", referencedColumnName = "id") | |
| private Turma turma; | |
| @ManyToOne(cascade = CascadeType.ALL) | |
| @JoinColumn(name = "id_foto") | |
| private Foto foto; | |
| @Column(name = "data_nascimento") | |
| @Temporal(TemporalType.DATE) | |
| private Date dataNascimento; | |
| public Aluno(String nome, String matricula, Turma turma) { | |
| this.nome = nome; | |
| this.matricula = matricula; | |
| this.turma = turma; | |
| } | |
| public Aluno() { | |
| } | |
| Aluno(JSONObject obj) { | |
| this.id = obj.has("id") ? obj.getLong("id") : null; | |
| this.nome = obj.has("nome") ? obj.getString("nome") : null; | |
| this.matricula = obj.has("matricula") ? obj.getString("matricula") : null; | |
| this.turma = obj.has("turma") ? new Turma(obj.getJSONObject("turma")) : null; | |
| try { | |
| this.dataNascimento = obj.has("dataNascimento") ? DateUtils.stringToDate(obj.getString("dataNascimento"), DATE_PATTERN) : null; | |
| // TODO fazer cast para foto | |
| } catch (ParseException ex) { | |
| this.dataNascimento = null; | |
| Logger.getLogger(Aluno.class.getName(), "Data incorreta em Aluno: " + ex.getMessage()).log(Level.SEVERE, null, ex); | |
| } | |
| } | |
| public Long getId() { | |
| return id; | |
| } | |
| public void setId(Long id) { | |
| this.id = id; | |
| } | |
| public String getNome() { | |
| return nome; | |
| } | |
| public void setNome(String nome) { | |
| this.nome = nome; | |
| } | |
| public String getMatricula() { | |
| return matricula; | |
| } | |
| public void setMatricula(String matricula) { | |
| this.matricula = matricula; | |
| } | |
| public Turma getTurma() { | |
| return turma; | |
| } | |
| public void setTurma(Turma turma) { | |
| this.turma = turma; | |
| } | |
| public Foto getFoto() { | |
| return foto; | |
| } | |
| public void setFoto(Foto foto) { | |
| this.foto = foto; | |
| } | |
| public Date getDataNascimento() { | |
| return dataNascimento; | |
| } | |
| public void setDataNascimento(Date dataNascimento) { | |
| this.dataNascimento = dataNascimento; | |
| } | |
| @Override | |
| public int hashCode() { | |
| int hash = 5; | |
| hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0); | |
| return hash; | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (this == obj) { | |
| return true; | |
| } | |
| if (obj == null) { | |
| return false; | |
| } | |
| if (getClass() != obj.getClass()) { | |
| return false; | |
| } | |
| final Aluno other = (Aluno) obj; | |
| if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| @Override | |
| public String toString() { | |
| return nome; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>br.com.sistema</groupId> | |
| <artifactId>turma</artifactId> | |
| <version>1.0-SNAPSHOT</version> | |
| <packaging>war</packaging> | |
| <name>turma</name> | |
| <properties> | |
| <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> | |
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server> | |
| </properties> | |
| <dependencies> | |
| <!-- web service dependencies --> | |
| <dependency> | |
| <groupId>asm</groupId> | |
| <artifactId>asm</artifactId> | |
| <version>3.3.1</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.sun.jersey</groupId> | |
| <artifactId>jersey-bundle</artifactId> | |
| <version>1.19.4</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.json</groupId> | |
| <artifactId>json</artifactId> | |
| <version>20170516</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.sun.jersey</groupId> | |
| <artifactId>jersey-server</artifactId> | |
| <version>1.19.4</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.sun.jersey</groupId> | |
| <artifactId>jersey-core</artifactId> | |
| <version>1.19.4</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.primefaces.themes</groupId> | |
| <artifactId>bootstrap</artifactId> | |
| <version>1.0.10</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.xpert</groupId> | |
| <artifactId>xpert-framework</artifactId> | |
| <version>1.8.3</version> | |
| </dependency> | |
| <!-- web service dependencies - FIM --> | |
| <dependency> | |
| <groupId>org.primefaces.themes</groupId> | |
| <artifactId>bootstrap</artifactId> | |
| <version>1.0.10</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.primefaces.themes</groupId> | |
| <artifactId>start</artifactId> | |
| <version>1.0.10</version> | |
| </dependency> | |
| <!-- <dependency> | |
| <groupId>org.primefaces.themes</groupId> | |
| <artifactId>delta</artifactId> | |
| <version>1.0.10</version> | |
| </dependency> --> | |
| <dependency> | |
| <groupId>junit</groupId> | |
| <artifactId>junit</artifactId> | |
| <version>3.8.1</version> | |
| <scope>test</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>commons-io</groupId> | |
| <artifactId>commons-io</artifactId> | |
| <version>2.4</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>commons-fileupload</groupId> | |
| <artifactId>commons-fileupload</artifactId> | |
| <version>1.2.2</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>commons-beanutils</groupId> | |
| <artifactId>commons-beanutils</artifactId> | |
| <version>1.8.3</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.hibernate</groupId> | |
| <artifactId>hibernate-core</artifactId> | |
| <!--<version>4.0.0.Final</version>--> | |
| <version>4.3.5.Final</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.hibernate</groupId> | |
| <artifactId>hibernate-entitymanager</artifactId> | |
| <!--<version>4.0.0.Final</version>--> | |
| <version>4.3.5.Final</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.hibernate</groupId> | |
| <artifactId>hibernate-validator</artifactId> | |
| <version>5.1.3.Final</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.xpert</groupId> | |
| <artifactId>xpert-framework</artifactId> | |
| <version>1.8.3</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.primefaces</groupId> | |
| <artifactId>primefaces</artifactId> | |
| <version>5.2</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.primefaces.extensions</groupId> | |
| <artifactId>primefaces-extensions</artifactId> | |
| <version>3.1.0</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>joda-time</groupId> | |
| <artifactId>joda-time</artifactId> | |
| <version>2.4</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.jbehave</groupId> | |
| <artifactId>jbehave-core</artifactId> | |
| <version>2.4-rc2</version> | |
| <type>jar</type> | |
| </dependency> | |
| <dependency> | |
| <groupId>javax.ws.rs</groupId> | |
| <artifactId>javax.ws.rs-api</artifactId> | |
| <version>2.0.1</version> | |
| <scope>provided</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>javax</groupId> | |
| <artifactId>javaee-web-api</artifactId> | |
| <version>6.0</version> | |
| <scope>provided</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>freemarker</groupId> | |
| <artifactId>freemarker</artifactId> | |
| <version>2.3.8</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.omnifaces</groupId> | |
| <artifactId>omnifaces</artifactId> | |
| <version>1.10</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>commons-lang</groupId> | |
| <artifactId>commons-lang</artifactId> | |
| <version>2.6</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.commons</groupId> | |
| <artifactId>commons-email</artifactId> | |
| <version>1.4</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>net.sf.jasperreports</groupId> | |
| <artifactId>jasperreports</artifactId> | |
| <version>4.1.3</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>net.sf.jasperreports</groupId> | |
| <artifactId>jasperreports-fonts</artifactId> | |
| <version>4.0.0</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.codeartisans</groupId> | |
| <artifactId>org.json</artifactId> | |
| <version>20161124</version> | |
| </dependency> | |
| </dependencies> | |
| <repositories> | |
| <repository> | |
| <id>prime-repo</id> | |
| <name>PrimeFaces Maven Repository</name> | |
| <url>http://repository.primefaces.org</url> | |
| <layout>default</layout> | |
| </repository> | |
| <repository> | |
| <id>xpert-framework</id> | |
| <name>xpert-framework</name> | |
| <url>http://xpert-framework.github.io/maven/</url> | |
| </repository> | |
| </repositories> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-compiler-plugin</artifactId> | |
| <version>2.3.2</version> | |
| <configuration> | |
| <source>1.6</source> | |
| <target>1.6</target> | |
| <compilerArguments> | |
| <endorseddirs>${endorsed.dir}</endorseddirs> | |
| </compilerArguments> | |
| </configuration> | |
| </plugin> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-war-plugin</artifactId> | |
| <version>2.1.1</version> | |
| <configuration> | |
| <failOnMissingWebXml>false</failOnMissingWebXml> | |
| </configuration> | |
| </plugin> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-dependency-plugin</artifactId> | |
| <version>2.1</version> | |
| <executions> | |
| <execution> | |
| <phase>validate</phase> | |
| <goals> | |
| <goal>copy</goal> | |
| </goals> | |
| <configuration> | |
| <outputDirectory>${endorsed.dir}</outputDirectory> | |
| <silent>true</silent> | |
| <artifactItems> | |
| <artifactItem> | |
| <groupId>javax</groupId> | |
| <artifactId>javaee-endorsed-api</artifactId> | |
| <version>6.0</version> | |
| <type>jar</type> | |
| </artifactItem> | |
| </artifactItems> | |
| </configuration> | |
| </execution> | |
| </executions> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.com.sistema.escola.modelo; | |
| import java.io.Serializable; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import javax.persistence.CascadeType; | |
| import javax.persistence.Entity; | |
| import javax.persistence.GeneratedValue; | |
| import javax.persistence.GenerationType; | |
| import javax.persistence.Id; | |
| import javax.persistence.OneToMany; | |
| import javax.persistence.SequenceGenerator; | |
| import javax.xml.bind.annotation.XmlRootElement; | |
| import org.json.JSONArray; | |
| import org.json.JSONObject; | |
| /** | |
| * | |
| * @author juan | |
| * @author Felipe00 @{github.com/Felipe00} | |
| */ | |
| @Entity | |
| @XmlRootElement | |
| @SequenceGenerator(name = "turma_id_seq", sequenceName = "turma_id_seq", allocationSize = 1) | |
| public class Turma implements Serializable { | |
| private static final long serialVersionUID = 1L; | |
| @Id | |
| @GeneratedValue(generator = "turma_id_seq", strategy = GenerationType.SEQUENCE) | |
| private Long id; | |
| private String nome; | |
| @OneToMany(mappedBy = "turma", cascade = CascadeType.ALL) | |
| private List<Aluno> alunos; | |
| public Turma(String nome) { | |
| this.nome = nome; | |
| } | |
| public Turma(JSONObject turmaJson) { | |
| this.id = turmaJson.has("id") ? turmaJson.getLong("id") : null; | |
| this.nome = turmaJson.has("nome") ? turmaJson.getString("nome") : null; | |
| if (turmaJson.has("alunos")) { | |
| JSONArray array = turmaJson.getJSONArray("alunos"); | |
| this.alunos = new ArrayList<Aluno>(); | |
| for (int i = 0; i < array.length(); i++) { | |
| this.alunos.add(new Aluno(array.getJSONObject(i))); | |
| } | |
| } | |
| } | |
| public void setListaDeAlunos(List<Aluno> alunos) { | |
| this.alunos = alunos; | |
| } | |
| public List<Aluno> getListaDeAlunos() { | |
| return this.alunos; | |
| } | |
| public Turma() { | |
| } | |
| public Long getId() { | |
| return id; | |
| } | |
| public void setId(Long id) { | |
| this.id = id; | |
| } | |
| public String getNome() { | |
| return nome; | |
| } | |
| public void setNome(String nome) { | |
| this.nome = nome; | |
| } | |
| @Override | |
| public int hashCode() { | |
| int hash = 7; | |
| hash = 41 * hash + (this.id != null ? this.id.hashCode() : 0); | |
| return hash; | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (this == obj) { | |
| return true; | |
| } | |
| if (obj == null) { | |
| return false; | |
| } | |
| if (getClass() != obj.getClass()) { | |
| return false; | |
| } | |
| final Turma other = (Turma) obj; | |
| if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| @Override | |
| public String toString() { | |
| return nome; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package br.com.sistema.escola.ws.restfull; | |
| import br.com.sistema.escola.modelo.Turma; | |
| import javax.ejb.Stateless; | |
| import javax.ws.rs.Consumes; | |
| import javax.ws.rs.GET; | |
| import javax.ws.rs.POST; | |
| import javax.ws.rs.Path; | |
| import javax.ws.rs.Produces; | |
| import org.json.JSONObject; | |
| /** | |
| * | |
| * @author Felipe00 @{github.com/Felipe00} | |
| */ | |
| @Path("/service") | |
| @Stateless | |
| public class TurmaService { | |
| @GET | |
| @Path("listartodasturmas") | |
| @Produces("application/json") | |
| public String listarTurmas() { | |
| //TODO fazer uma query no banco e retornar em formato Json | |
| return fakeDatabase(); | |
| } | |
| /** | |
| * Grava um objeto turma com base no json | |
| * | |
| * @param json String contendo dados de uma turma (objeto turma) | |
| * @return Um objeto turma em formato json | |
| */ | |
| @POST // Método HTTP | |
| @Path("cadastrarturma") // Url da ação | |
| @Consumes("application/json") // Recebe uma requisição JSON | |
| @Produces("application/json") // Envia uma resposta JSON | |
| public String saveTurma(String json) { | |
| System.out.println("\n\nJSON> " + json + "\n\n"); | |
| JSONObject turmaJson = new JSONObject(json); | |
| Turma turma = new Turma(turmaJson); | |
| try { | |
| System.out.println("turma: " + turma.getNome() + "\nquantidade de alunos: " + turma.getListaDeAlunos() != null? turma.getListaDeAlunos().size() : "0"); | |
| } catch (NullPointerException e) { | |
| System.out.println("ERRO: " + e.getCause()); | |
| } | |
| return json; | |
| } | |
| private String fakeDatabase() { | |
| return "[{\"nome\":\"Avengers!\",\"alunos\":[{\"nome\":\"Felipe Stark\"},{\"nome\":\"Juan Strange\"}]},{\"nome\":\"Liga da justiça\",\"alunos\":[{\"nome\":\"Felipe Wayne\"},{\"nome\":\"Juan Kent\"}]}]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | |
| <session-config> | |
| <session-timeout> | |
| 30 | |
| </session-timeout> | |
| </session-config> | |
| <welcome-file-list> | |
| <welcome-file>index.jsf</welcome-file> | |
| </welcome-file-list> | |
| <servlet> | |
| <servlet-name>Faces Servlet</servlet-name> | |
| <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> | |
| <load-on-startup>1</load-on-startup> | |
| </servlet> | |
| <servlet-mapping> | |
| <servlet-name>Faces Servlet</servlet-name> | |
| <url-pattern>*.jsf</url-pattern> | |
| </servlet-mapping> | |
| <servlet> | |
| <servlet-name>Jersey Web Application</servlet-name> | |
| <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> | |
| <init-param> | |
| <param-name>jersey.config.server.provider.packages</param-name> | |
| <param-value>br.com.sistema.escola.ws.restfull</param-value> | |
| </init-param> | |
| <load-on-startup>1</load-on-startup> | |
| </servlet> | |
| <servlet-mapping> | |
| <servlet-name>Jersey Web Application</servlet-name> | |
| <url-pattern>/ws/*</url-pattern> | |
| </servlet-mapping> | |
| <context-param> | |
| <param-name>primefaces.THEME</param-name> | |
| <param-value>start</param-value> | |
| </context-param> | |
| <persistence-context-ref> | |
| <persistence-context-ref-name>persistence/entityManager</persistence-context-ref-name> | |
| <persistence-unit-name>turmaPU</persistence-unit-name> | |
| </persistence-context-ref> | |
| <context-param> | |
| <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name> | |
| <param-value>true</param-value> | |
| </context-param> | |
| <filter> | |
| <filter-name>CharacterEncodingFilter</filter-name> | |
| <filter-class>com.xpert.core.filter.CharacterEncodingFilter</filter-class> | |
| </filter> | |
| <filter-mapping> | |
| <filter-name>CharacterEncodingFilter</filter-name> | |
| <servlet-name>Faces Servlet</servlet-name> | |
| </filter-mapping> | |
| <filter> | |
| <filter-name>PrimeFaces FileUpload Filter</filter-name> | |
| <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> | |
| </filter> | |
| <filter-mapping> | |
| <filter-name>PrimeFaces FileUpload Filter</filter-name> | |
| <servlet-name>Faces Servlet</servlet-name> | |
| </filter-mapping> | |
| <security-constraint> | |
| <web-resource-collection> | |
| <web-resource-name>XHTML</web-resource-name> | |
| <url-pattern>*.xhtml</url-pattern> | |
| </web-resource-collection> | |
| <auth-constraint/> | |
| </security-constraint> | |
| <!-- Referencias a EJBs --> | |
| <ejb-local-ref> | |
| <ejb-ref-name>ejb/ErroSistemaBO</ejb-ref-name> | |
| <ejb-ref-type>Session</ejb-ref-type> | |
| <local-home>br.com.sistema.xpert.bo.configuracao.ErroSistemaBO</local-home> | |
| <local>br.com.sistema.xpert.bo.configuracao.ErroSistemaBO</local> | |
| </ejb-local-ref> | |
| </web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment