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