Commit b2e75f59 authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

Query Parameter in GET Methode von StudyResource

Verwendung von Interfaces statt abstrakten Klassen
parent 77549b5f
package university.at.jku.ce.dao; package university.at.jku.ce.dao;
import java.util.List;
import java.util.Map; import java.util.Map;
import university.at.jku.ce.model.Study; import university.at.jku.ce.model.Study;
public abstract class AbstractStudyDao { public interface StudyDao {
public abstract Map<Long,Study> getAllStudies(String name);
public abstract Map<Long,Study> getAllStudies();
public abstract Study getStudy(Long id); public abstract Study getStudy(Long id);
......
package university.at.jku.ce.dao; package university.at.jku.ce.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import university.at.jku.ce.model.Subject; import university.at.jku.ce.model.Subject;
public abstract class AbstractSubjectDao { public interface SubjectDao {
public abstract Map<Long,Subject> getAllSubjects(Long studyId); public abstract Map<Long,Subject> getAllSubjects(Long studyId);
public abstract Subject getSubject(long studyId, long id); public abstract Subject getSubject(long studyId, long id);
......
...@@ -7,15 +7,14 @@ import java.io.FileWriter; ...@@ -7,15 +7,14 @@ import java.io.FileWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import university.at.jku.ce.model.Study; import university.at.jku.ce.model.Study;
public class TextStudyDao extends AbstractStudyDao { public class TextStudyDao implements StudyDao {
Map <Long,Study> studies; Map <Long,Study> studies;
@Override @Override
public Map<Long,Study> getAllStudies() { public Map<Long,Study> getAllStudies(String name) {
ArrayList <String> lineList=new ArrayList<String>(); ArrayList <String> lineList=new ArrayList<String>();
studies=new HashMap<Long,Study>(); studies=new HashMap<Long,Study>();
try { try {
...@@ -50,7 +49,14 @@ public class TextStudyDao extends AbstractStudyDao { ...@@ -50,7 +49,14 @@ public class TextStudyDao extends AbstractStudyDao {
i++; i++;
} }
studyName=b.toString(); studyName=b.toString();
studies.put(studyId, new Study(studyId,studyName)); if (name==null||name.equals("")) {
studies.put(studyId, new Study(studyId,studyName));
}
else {
if(studyName.contains(name)) {
studies.put(studyId, new Study(studyId,studyName));
}
}
} }
} }
return studies; return studies;
...@@ -58,13 +64,13 @@ public class TextStudyDao extends AbstractStudyDao { ...@@ -58,13 +64,13 @@ public class TextStudyDao extends AbstractStudyDao {
@Override @Override
public Study getStudy(Long id) { public Study getStudy(Long id) {
studies=getAllStudies(); studies=getAllStudies(null);
return studies.get(id); return studies.get(id);
} }
@Override @Override
public Study addStudy(Study study) { public Study addStudy(Study study) {
studies=getAllStudies(); studies=getAllStudies(null);
study.setStudyId(getNextId()); study.setStudyId(getNextId());
studies.put(study.getStudyId(), study); studies.put(study.getStudyId(), study);
write(); write();
...@@ -73,7 +79,7 @@ public class TextStudyDao extends AbstractStudyDao { ...@@ -73,7 +79,7 @@ public class TextStudyDao extends AbstractStudyDao {
@Override @Override
public Study updateStudy(Study study) { public Study updateStudy(Study study) {
studies=getAllStudies(); studies=getAllStudies(null);
if (studies.containsKey(study.getStudyId())) { if (studies.containsKey(study.getStudyId())) {
studies.put(study.getStudyId(), study); studies.put(study.getStudyId(), study);
} }
...@@ -84,7 +90,7 @@ public class TextStudyDao extends AbstractStudyDao { ...@@ -84,7 +90,7 @@ public class TextStudyDao extends AbstractStudyDao {
@Override @Override
public void removeStudy(long studyId) { public void removeStudy(long studyId) {
studies=getAllStudies(); studies=getAllStudies(null);
studies.remove(studyId); studies.remove(studyId);
write(); write();
} }
......
...@@ -13,7 +13,7 @@ import java.util.Map.Entry; ...@@ -13,7 +13,7 @@ import java.util.Map.Entry;
import university.at.jku.ce.model.Study; import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject; import university.at.jku.ce.model.Subject;
public class TextSubjectDao extends AbstractSubjectDao { public class TextSubjectDao implements SubjectDao {
Map <Long,Subject> subjects; Map <Long,Subject> subjects;
Map <SubjectId,Subject> allSubjects; Map <SubjectId,Subject> allSubjects;
......
...@@ -13,13 +13,10 @@ public class Study { ...@@ -13,13 +13,10 @@ public class Study {
private long studyId; private long studyId;
private String name; private String name;
private Map <Long, Subject> subjects=new HashMap<Long,Subject>();
public Study() {} public Study() {}
public Study(String name, Map<Long, Subject> subjects) { public Study(String name) {
this.name=name; this.name=name;
this.subjects=subjects;
} }
public Study (long studyId, String name) { public Study (long studyId, String name) {
...@@ -27,12 +24,6 @@ public class Study { ...@@ -27,12 +24,6 @@ public class Study {
this.name=name; this.name=name;
} }
public Study(long studyId, String name, Map<Long, Subject> subjects) {
this.name=name;
this.studyId=studyId;
this.subjects=subjects;
}
public long getStudyId() { public long getStudyId() {
return studyId; return studyId;
} }
...@@ -48,13 +39,4 @@ public class Study { ...@@ -48,13 +39,4 @@ public class Study {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@JsonbTransient
public Map<Long, Subject> getSubjects() {
return subjects;
}
public void setSubjects(Map<Long, Subject> subjects) {
this.subjects = subjects;
}
} }
...@@ -15,87 +15,88 @@ import javax.ws.rs.core.MediaType; ...@@ -15,87 +15,88 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import university.at.jku.ce.model.Student; import university.at.jku.ce.model.Student;
import university.at.jku.ce.service.StudentService; import university.at.jku.ce.service.Service;
import university.at.jku.ce.service.ServiceImpl;
@Path("/students") @Path("/students")
public class StudentResource { public class StudentResource {
StudentService studentService=new StudentService(); Service service=new ServiceImpl();
@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 // @GET
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public List <Student> getStudents() { // public Response getStudents() {
// return studentService.getAllStudents(); // List<Student>list= studentService.getAllStudents();
// GenericEntity<List<Student>> entity=new GenericEntity<List<Student>>(list) {};
// return Response.ok(entity).build();
// } // }
//
@POST //// @GET
@Consumes(MediaType.APPLICATION_JSON) //// @Produces(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON) //// public List <Student> getStudents() {
public Student addStudent(Student student) { //// return studentService.getAllStudents();
return studentService.addStudent(student); //// }
} //
// @POST // @POST
// @Consumes(MediaType.APPLICATION_XML) // @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public Student addMessage(Student student) { // public Student addStudent(Student student) {
// return studentService.addStudent(student); // return studentService.addStudent(student);
// } // }
//
@GET //// @POST
@Path("/{studentMatrNr}") //// @Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON) //// @Produces(MediaType.APPLICATION_XML)
public Student getStudent(@PathParam("studentMatrNr")long matrNr) { //// public Student addMessage(Student student) {
return studentService.getStudent(matrNr); //// return studentService.addStudent(student);
} //// }
//
// @GET // @GET
// @Path("/{studentMatrNr}") // @Path("/{studentMatrNr}")
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public Student getStudent(@PathParam("studentMatrNr")long matrNr) { // public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
// return studentService.getStudent(matrNr); // return studentService.getStudent(matrNr);
// } // }
//
@PUT //// @GET
@Path("/{studentMatrNr}") //// @Path("/{studentMatrNr}")
@Consumes(MediaType.APPLICATION_JSON) //// @Produces(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON) //// public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) { //// return studentService.getStudent(matrNr);
student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL //// }
return studentService.updateStudent(student); //
}
// @PUT // @PUT
// @Path("/{studentMatrNr}") // @Path("/{studentMatrNr}")
// @Consumes(MediaType.APPLICATION_XML) // @Consumes(MediaType.APPLICATION_JSON)
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) { // 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 // student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
// return studentService.updateStudent(student); // return studentService.updateStudent(student);
// } // }
//
@DELETE //// @PUT
@Path("/{studentMatrNr}") //// @Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON) //// @Consumes(MediaType.APPLICATION_XML)
public void deleteStudent(@PathParam("studentMatrNr")long matrNr) { //// @Produces(MediaType.APPLICATION_XML)
studentService.removeStudent(matrNr); //// 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 // @DELETE
// @Path("/{studentMatrNr}") // @Path("/{studentMatrNr}")
// @Produces(MediaType.APPLICATION_XML) // @Produces(MediaType.APPLICATION_JSON)
// public void deleteStudent(@PathParam("studentMatrNr")long matrNr) { // public void deleteStudent(@PathParam("studentMatrNr")long matrNr) {
// studentService.removeStudent(matrNr); // studentService.removeStudent(matrNr);
// } // }
//
//// @DELETE
//// @Path("/{studentMatrNr}")
//// @Produces(MediaType.APPLICATION_XML)
//// public void deleteStudent(@PathParam("studentMatrNr")long matrNr) {
//// studentService.removeStudent(matrNr);
//// }
//
} }
...@@ -11,27 +11,29 @@ import javax.ws.rs.PUT; ...@@ -11,27 +11,29 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; 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.model.Study;
import university.at.jku.ce.service.StudentService; import university.at.jku.ce.service.Service;
import university.at.jku.ce.service.StudyService; import university.at.jku.ce.service.ServiceImpl;
@Path("/studies") @Path("/studies")
public class StudyResource { public class StudyResource {
StudyService studyService=new StudyService(); Service service=new ServiceImpl();
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getStudies() { public Response getStudies(@QueryParam("name") String name) {
List<Study>list= studyService.getAllStudies();
List<Study>list= service.getAllStudies(name);
GenericEntity<List<Study>> entity=new GenericEntity<List<Study>>(list) {}; GenericEntity<List<Study>> entity=new GenericEntity<List<Study>>(list) {};
return Response.ok(entity).build(); return Response.ok(entity).build();
} }
...@@ -39,7 +41,7 @@ public class StudyResource { ...@@ -39,7 +41,7 @@ public class StudyResource {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Study addStudy(Study study) { public Study addStudy(Study study) {
return studyService.addStudy(study); return service.addStudy(study);
} }
...@@ -47,7 +49,7 @@ public class StudyResource { ...@@ -47,7 +49,7 @@ public class StudyResource {
@Path("/{studyId}") @Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Study getStudy(@PathParam("studyId")long studyId) { public Study getStudy(@PathParam("studyId")long studyId) {
return studyService.getStudy(studyId); return service.getStudy(studyId);
} }
@PUT @PUT
...@@ -56,14 +58,14 @@ public class StudyResource { ...@@ -56,14 +58,14 @@ public class StudyResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Study updateStudy(@PathParam("studyId")long studyId,Study study) { 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 study.setStudyId(studyId); //if User does not send correct id it is still used correct id from URL
return studyService.updateStudy(study); return service.updateStudy(study);
} }
@DELETE @DELETE
@Path("/{studyId}") @Path("/{studyId}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public void deleteStudent(@PathParam("studyId")long studyId) { public void deleteStudent(@PathParam("studyId")long studyId) {
studyService.removeStudy(studyId); service.removeStudy(studyId);
} }
@Path("/{studyId}/subjects") @Path("/{studyId}/subjects")
......
...@@ -11,42 +11,42 @@ import javax.ws.rs.Path; ...@@ -11,42 +11,42 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import university.at.jku.ce.model.Subject; import university.at.jku.ce.model.Subject;
import university.at.jku.ce.service.SubjectService; import university.at.jku.ce.service.Service;
import university.at.jku.ce.service.ServiceImpl;
@Path("/") @Path("/")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public class SubjectResource { public class SubjectResource {
private SubjectService subjectService=new SubjectService(); private Service service=new ServiceImpl();
@GET @GET
public List getAllSubjects(@PathParam("studyId")long studyId) { public List getAllSubjects(@PathParam("studyId")long studyId) {
return subjectService.getAllSubjects(studyId); return service.getAllSubjects(studyId);
} }
@POST @POST
public Subject addSubject(@PathParam("studyId")long studyId, Subject subject) { public Subject addSubject(@PathParam("studyId")long studyId, Subject subject) {
return subjectService.addSubject(studyId, subject); return service.addSubject(studyId, subject);
} }
@PUT @PUT
@Path("/{id}") @Path("/{id}")
public Subject updateSubject(@PathParam("studyId")long studyId,@PathParam("id")long id, Subject subject) { public Subject updateSubject(@PathParam("studyId")long studyId,@PathParam("id")long id, Subject subject) {
subject.setId(id); subject.setId(id);
return subjectService.updateSubject(studyId, subject); return service.updateSubject(studyId, subject);
} }
@DELETE @DELETE
@Path("/{id}") @Path("/{id}")
public void removeSubject(@PathParam("studyId")long studyId,@PathParam("id")long id) { public void removeSubject(@PathParam("studyId")long studyId,@PathParam("id")long id) {
subjectService.removeSubject(studyId, id); service.removeSubject(studyId, id);
} }
@GET @GET
@Path("/{id}") @Path("/{id}")
public Subject getSubject(@PathParam("studyId")long studyId,@PathParam("id")long id) { public Subject getSubject(@PathParam("studyId")long studyId,@PathParam("id")long id) {
return subjectService.getSubject(studyId, id); return service.getSubject(studyId, id);
} }
} }
package university.at.jku.ce.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
public interface Service {
public List<Study> getAllStudies(String name);
public Study getStudy(Long id);
public Study addStudy(Study study);
public Study updateStudy(Study study);
public void removeStudy(long studyId);
public List<Subject> getAllSubjects(long studyId);
public Subject getSubject(long studyId,long id);
public Subject addSubject (long studyId, Subject subject);
public Subject updateSubject (long studyId, Subject subject);
public void removeSubject (long studyId, long id);