Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Franz Reischl
tomcat-miniREST
Commits
b2e75f59
Commit
b2e75f59
authored
Jan 02, 2019
by
Michael Schimpelsberger
Browse files
Query Parameter in GET Methode von StudyResource
Verwendung von Interfaces statt abstrakten Klassen
parent
77549b5f
Changes
13
Show whitespace changes
Inline
Side-by-side
src/main/java/university/at/jku/ce/dao/
Abstract
StudyDao.java
→
src/main/java/university/at/jku/ce/dao/StudyDao.java
View file @
b2e75f59
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
);
...
...
src/main/java/university/at/jku/ce/dao/
Abstract
SubjectDao.java
→
src/main/java/university/at/jku/ce/dao/SubjectDao.java
View file @
b2e75f59
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
Abstract
SubjectDao
{
public
interface
SubjectDao
{
public
abstract
Map
<
Long
,
Subject
>
getAllSubjects
(
Long
studyId
);
public
abstract
Subject
getSubject
(
long
studyId
,
long
id
);
...
...
src/main/java/university/at/jku/ce/dao/TextStudyDao.java
View file @
b2e75f59
...
...
@@ -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
ext
en
d
s
Abstract
StudyDao
{
public
class
TextStudyDao
implem
en
t
s
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,21 +49,28 @@ public class TextStudyDao extends AbstractStudyDao {
i
++;
}
studyName
=
b
.
toString
();
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
;
}
@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
();
}
...
...
src/main/java/university/at/jku/ce/dao/TextSubjectDao.java
View file @
b2e75f59
...
...
@@ -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
ext
en
d
s
Abstract
SubjectDao
{
public
class
TextSubjectDao
implem
en
t
s
SubjectDao
{
Map
<
Long
,
Subject
>
subjects
;
Map
<
SubjectId
,
Subject
>
allSubjects
;
...
...
src/main/java/university/at/jku/ce/model/Study.java
View file @
b2e75f59
...
...
@@ -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
;
}
}
src/main/java/university/at/jku/ce/resource/StudentResource.java
View file @
b2e75f59
...
...
@@ -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
();
@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
();
}
Service
service
=
new
ServiceImpl
();
// @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 add
Message
(Student student) {
// @Consumes(MediaType.APPLICATION_
JSON
)
// @Produces(MediaType.APPLICATION_
JSON
)
// public Student add
Student
(Student student) {
// return studentService.addStudent(student);
// }
@GE
T
@Path
(
"/{studentMatrNr}"
)
@Produces
(
MediaType
.
APPLICATION_
JSON
)
public
Student
ge
t
Student
(
@PathParam
(
"studentMatrNr"
)
long
matrNr
)
{
return
studentService
.
get
Student
(
matrNr
);
}
//
//// @POS
T
//// @Consumes(MediaType.APPLICATION_XML
)
////
@Produces(MediaType.APPLICATION_
XML
)
////
public Student
addMessa
ge
(
Student
student
) {
////
return studentService.
add
Student(
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);
//// }
//
}
src/main/java/university/at/jku/ce/resource/StudyResource.java
View file @
b2e75f59
...
...
@@ -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
{
Study
Service
s
tudyS
ervice
=
new
Study
Service
();
Service
service
=
new
Service
Impl
();
@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
s
tudyS
ervice
.
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
s
tudyS
ervice
.
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
s
tudyS
ervice
.
updateStudy
(
study
);
return
service
.
updateStudy
(
study
);
}
@DELETE
@Path
(
"/{studyId}"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
void
deleteStudent
(
@PathParam
(
"studyId"
)
long
studyId
)
{
s
tudyS
ervice
.
removeStudy
(
studyId
);
service
.
removeStudy
(
studyId
);
}
@Path
(
"/{studyId}/subjects"
)
...
...
src/main/java/university/at/jku/ce/resource/SubjectResource.java
View file @
b2e75f59
...
...
@@ -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
Subject
Service
s
ubjectS
ervice
=
new
Subject
Service
();
private
Service
service
=
new
Service
Impl
();
@GET
public
List
getAllSubjects
(
@PathParam
(
"studyId"
)
long
studyId
)
{
return
s
ubjectS
ervice
.
getAllSubjects
(
studyId
);
return
service
.
getAllSubjects
(
studyId
);
}
@POST
public
Subject
addSubject
(
@PathParam
(
"studyId"
)
long
studyId
,
Subject
subject
)
{
return
s
ubjectS
ervice
.
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
s
ubjectS
ervice
.
updateSubject
(
studyId
,
subject
);
return
service
.
updateSubject
(
studyId
,
subject
);
}
@DELETE
@Path
(
"/{id}"
)
public
void
removeSubject
(
@PathParam
(
"studyId"
)
long
studyId
,
@PathParam
(
"id"
)
long
id
)
{
s
ubjectS
ervice
.
removeSubject
(
studyId
,
id
);
service
.
removeSubject
(
studyId
,
id
);
}
@GET
@Path
(
"/{id}"
)
public
Subject
getSubject
(
@PathParam
(
"studyId"
)
long
studyId
,
@PathParam
(
"id"
)
long
id
)
{
return
s
ubjectS
ervice
.
getSubject
(
studyId
,
id
);
return
service
.
getSubject
(
studyId
,
id
);
}
}
src/main/java/university/at/jku/ce/service/Service.java
0 → 100644
View file @
b2e75f59
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
);
}
src/main/java/university/at/jku/ce/service/
Subject
Service.java
→
src/main/java/university/at/jku/ce/service/Service
Impl
.java
View file @
b2e75f59
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
{
private
StudyDao
studyDao
=
new
TextStudyDao
();
private
SubjectDao
subjectDao
=
new
TextSubjectDao
();
public
ServiceImpl
()
{
}
public
class
SubjectService
{
public
List
<
Study
>
getAllStudies
(
String
name
){
return
new
ArrayList
<
Study
>(
studyDao
.
getAllStudies
(
name
).
values
());
}
private
AbstractSubjectDao
subjectDao
=
new
TextSubjectDao
();
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
);
}
}
src/main/java/university/at/jku/ce/service/StudentService.java
deleted
100644 → 0
View file @
77549b5f
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
;
}
}
src/main/java/university/at/jku/ce/service/StudyService.java
deleted
100644 → 0
View file @
77549b5f
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
);
}
}
src/main/resources/Studies.txt
View file @
b2e75f59