Commit 4703bb1c authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

StudyResource, StudyService, Study generated

merge conflicts aufgehoben

Merge branch 'master' into test

Conflicts:
	src/main/java/university/at/jku/ce/Database.java
	src/main/java/university/at/jku/ce/Student.java
	src/main/java/university/at/jku/ce/StudentResource.java
	src/main/java/university/at/jku/ce/StudentService.java
parents d879795e 5c37dd54
package university.at.jku.ce;
import java.util.HashMap;
import java.util.Map;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
public class Database {
private static Map<Long ,Student> students=new HashMap<Long,Student>();
private static Map<Long ,Subject> subjects=new HashMap<Long,Subject>();
private static Map<Long ,Study> studies=new HashMap<Long,Study>();
public static Map<Long, Student> getStudents() {
return students;
}
public static Map<Long, Subject> getSubjects() {
return subjects;
}
public static Map<Long, Study> getStudies() {
return studies;
}
}
package university.at.jku.ce;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
package university.at.jku.ce;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("/students")
public class StudentResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
StudentService studentService=new StudentService();
@GET
@Produces(MediaType.APPLICATION_XML)
public List <Student> getStudents() {
System.out.println("Start: getStudents");
return studentService.getAllStudents();
}
}
package university.at.jku.ce;
import java.util.ArrayList;
import java.util.List;
public class StudentService {
public List<Student> getAllStudents(){
List<Subject> subjects=new ArrayList<Subject>();
subjects.add(new Subject("Softwareenwicklung", "Grundlegendes zu Java"));
subjects.add(new Subject("Datenmodellierung","Grundlegendes zu SQL und Relationalen Datenbanken"));
Student s2=new Student(1156101, "Michael", "Schimpelsberger", subjects);
Student s1=new Student(1234567,"Franz", "Reschl", null);
Student s3=new Student(2424247,"Susanne", "Zaunmayr", subjects);
List<Student> students=new ArrayList<Student>();
students.add(s1);
students.add(s2);
students.add(s3);
return students;
}
}
package university.at.jku.ce;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String profession;
public User(){}
public User(int id, String name, String profession){
this.id = id;
this.name = name;
this.profession = profession;
}
public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getProfession() {
return profession;
}
@XmlElement
public void setProfession(String profession) {
this.profession = profession;
}
}
\ No newline at end of file
package university.at.jku.ce;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public List<User> getAllUsers(){
List<User> userList = null;
try {
File file = new File("Users.dat");
if (!file.exists()) {
User user = new User(1, "Mahesh", "Teacher");
userList = new ArrayList<User>();
userList.add(user);
saveUserList(userList);
}
else{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
userList = (List<User>) ois.readObject();
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return userList;
}
private void saveUserList(List<User> userList){
try {
File file = new File("Users.dat");
FileOutputStream fos;
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
package university.at.jku.ce;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
UserDao userDao = new UserDao();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
}
\ No newline at end of file
package university.at.jku.ce; package university.at.jku.ce.model;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
// Comment added for testing purposes. ;)
@XmlRootElement @XmlRootElement
public class Student { public class Student {
private int matrNr; private long matrNr;
private String firstName; private String firstName;
private String lastName; private String lastName;
List <Subject> subjects;
public Student () {}; public Student () {};
public Student(int matrNr, String firstName, String lastName, List<Subject> subjects) { public Student(int matrNr, String firstName, String lastName) {
this.matrNr=matrNr; this.matrNr=matrNr;
this.firstName=firstName; this.firstName=firstName;
this.lastName=lastName; this.lastName=lastName;
this.subjects=subjects;
} }
public String getFirstName() { public String getFirstName() {
...@@ -40,24 +40,12 @@ public class Student { ...@@ -40,24 +40,12 @@ public class Student {
this.lastName = lastName; this.lastName = lastName;
} }
public int getMatrNr() { public long getMatrNr() {
return matrNr; return matrNr;
} }
public void setMatrNr(int matrNr) { public void setMatrNr(long matrNr) {
this.matrNr = matrNr; this.matrNr = matrNr;
} }
public List<Subject> getSubjects() {
return subjects;
}
public void setSubjects(List<Subject> subjects) {
this.subjects = subjects;
}
} }
package university.at.jku.ce.model;
import java.util.HashMap;
import java.util.Map;
import javax.json.bind.annotation.JsonbTransient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
public class Study {
private long studyId;
private String name;
private Map <Long, Subject> subjects=new HashMap<Long,Subject>();
public Study() {}
public Study(long studyId, String name, Map<Long, Subject> subjects) {
this.name=name;
this.studyId=studyId;
this.subjects=subjects;
}
public long getStudyId() {
return studyId;
}
public void setStudyId(long studyId) {
this.studyId = studyId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonbTransient
public Map<Long, Subject> getSubjects() {
return subjects;
}
public void setSubjects(Map<Long, Subject> subjects) {
this.subjects = subjects;
}
}
package university.at.jku.ce; package university.at.jku.ce.model;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement @XmlRootElement
public class Subject { public class Subject {
String name; private long id;
String info; private String name;
private String info;
public Subject() {}; public Subject() {};
public Subject(String name, String info) { public Subject(long id, String name, String info) {
this.id=id;
this.name=name; this.name=name;
this.info=info; this.info=info;
} }
...@@ -29,7 +31,12 @@ public class Subject { ...@@ -29,7 +31,12 @@ public class Subject {
public void setInfo(String info) { public void setInfo(String info) {
this.info = info; this.info = info;
} }
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
} }
package university.at.jku.ce.resource;
import javax.ws.rs.core.GenericEntity;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.service.StudentService;
@Path("/students")
public class StudentResource {
StudentService studentService=new StudentService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudents() {
List<Student>list= studentService.getAllStudents();
GenericEntity<List<Student>> entity=new GenericEntity<List<Student>>(list) {};
return Response.ok(entity).build();
}
// @GET
// @Produces(MediaType.APPLICATION_XML)
// public List <Student> getStudents() {
// return studentService.getAllStudents();
// }
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student addStudent(Student student) {
return studentService.addStudent(student);
}
// @POST
// @Consumes(MediaType.APPLICATION_XML)
// @Produces(MediaType.APPLICATION_XML)
// public Student addMessage(Student student) {
// return studentService.addStudent(student);
// }
@GET
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
return studentService.getStudent(matrNr);
}
// @GET
// @Path("/{studentMatrNr}")
// @Produces(MediaType.APPLICATION_XML)
// public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
// return studentService.getStudent(matrNr);
// }
@PUT
@Path("/{studentMatrNr}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) {
student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
return studentService.updateStudent(student);
}
// @PUT
// @Path("/{studentMatrNr}")
// @Consumes(MediaType.APPLICATION_XML)
// @Produces(MediaType.APPLICATION_XML)
// public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) {
// student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
// return studentService.updateStudent(student);
// }
@DELETE
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteStudent(@PathParam("studentMatrNr")long matrNr) {
studentService.removeStudent(matrNr);
}
// @DELETE
// @Path("/{studentMatrNr}")
// @Produces(MediaType.APPLICATION_XML)
// public void deleteStudent(@PathParam("studentMatrNr")long matrNr) {
// studentService.removeStudent(matrNr);
// }
}
package university.at.jku.ce.resource;
import javax.ws.rs.core.GenericEntity;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import university.at.jku.ce.model.Student;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.service.StudentService;
import university.at.jku.ce.service.StudyService;
@Path("/studies")
public class StudyResource {
StudyService studyService=new StudyService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudies() {
List<Study>list= studyService.getAllStudies();
GenericEntity<List<Study>> entity=new GenericEntity<List<Study>>(list) {};
return Response.ok(entity).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Study addStudy(Study study) {
return studyService.addStudy(study);
}
@GET
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Study getStudy(@PathParam("studyId")long studyId) {
return studyService.getStudy(studyId);
}
@PUT
@Path("/{studyId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Study updateStudy(@PathParam("studyId")long studyId,Study study) {
study.setStudyId(studyId); //if User does not send correct id it is still used correct id from URL
return studyService.updateStudy(study);
}
@DELETE
@Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteStudent(@PathParam("studyId")long studyId) {
studyService.removeStudy(studyId);
}
@Path("/{studyId}/subjects")
public SubjectResource getSubjectResource() {
return new SubjectResource();
}
}