Java tutorial
/* * Copyright 2016 FilipeRosa. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package algorithmi.models; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * * @author FilipeRosa */ public class Course { private int codCourse; private String name; private int school; public Course(String data) { //Transforma a string recebida pelo pedido http para json JsonParser jsonParser = new JsonParser(); JsonObject Course = (JsonObject) jsonParser.parse(data); //Exibe os dados, em formato json System.out.println(Course.entrySet()); /** * * Revalidar TUDO, formatos, campos vazios, TUDO!! * */ validateData(); //Associa os dados ao objecto Course this.codCourse = Course.getAsInt(); //ir buscar o max id da bd + 1 this.name = Course.get("name").getAsString(); this.school = Course.get("school").getAsInt(); } public void regist() { //Insere na BD } public Course(int CodCurse, String name, int scholl) { this.codCourse = codCourse; this.name = name; this.school = scholl; } public int getCodCourse() { return codCourse; } public void setCodCourse(int codCourse) { this.codCourse = codCourse; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSchool() { return school; } public void setSchool(int school) { this.school = school; } // converts a java object to JSON format, // and returned as JSON formatted string @Override public String toString() { Gson gson = new Gson(); String json = gson.toJson(this); System.out.println("json \n" + json); return json; } private void validateData() { /** * Se estiver tudo OK, inserer na BD, */ regist(); /**Seno Devolve um erro (mas dos * amigveis :D) */ } }