Commit 77549b5f authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

Datenpersistierung über Textdateien

parent 86557fdf
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.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.model.Study;
public abstract class AbstractStudyDao {
abstract List<Study> getAllStudies();
public abstract Map<Long,Study> getAllStudies();
abstract Study getStudy(Long id);
public abstract Study getStudy(Long id);
abstract Study addStudy(Study study);
public abstract Study addStudy(Study study);
abstract Study updateStudy(Study study);
public abstract Study updateStudy(Study study);
abstract Study removeStudy(long studyId);
public abstract void removeStudy(long studyId);
}
......@@ -7,7 +7,7 @@ import java.util.Map;
import university.at.jku.ce.model.Subject;
public abstract class AbstractSubjectDao {
public abstract List<Subject> getAllSubjects(long studyId);
public abstract Map<Long,Subject> getAllSubjects(Long studyId);
public abstract Subject getSubject(long studyId, long id);
......@@ -15,5 +15,5 @@ public abstract class AbstractSubjectDao {
public abstract Subject updateSubject (long studyId, Subject subject);
public abstract Subject removeSubject (long studyId, long id);
public abstract void removeSubject (long studyId, long id);
}
package university.at.jku.ce.dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
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 {
Map <Long,Study> studies;
@Override
public Map<Long,Study> getAllStudies() {
ArrayList <String> lineList=new ArrayList<String>();
studies=new HashMap<Long,Study>();
try {
FileReader fr = new FileReader("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Studies.txt");
BufferedReader br = new BufferedReader(fr);
String line="";
do{
line = br.readLine();
lineList.add(line);
}
while (line != null);
br.close();
} catch (Throwable t) {t.printStackTrace();}
for(String s: lineList) {
if (s!=null) {
int i=0;
Long studyId;
String studyName;
StringBuilder b=new StringBuilder();
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
studyId=Long.parseLong(b.toString());
b=new StringBuilder();
i++;
while (i<s.length()) {
b.append(s.charAt(i));
i++;
}
studyName=b.toString();
studies.put(studyId, new Study(studyId,studyName));
}
}
return studies;
}
@Override
public Study getStudy(Long id) {
studies=getAllStudies();
return studies.get(id);
}
@Override
public Study addStudy(Study study) {
studies=getAllStudies();
study.setStudyId(getNextId());
studies.put(study.getStudyId(), study);
write();
return study;
}
@Override
public Study updateStudy(Study study) {
studies=getAllStudies();
if (studies.containsKey(study.getStudyId())) {
studies.put(study.getStudyId(), study);
}
write();
return study;
}
@Override
public void removeStudy(long studyId) {
studies=getAllStudies();
studies.remove(studyId);
write();
}
void write() {
try {
FileWriter fw = new FileWriter("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Studies.txt");
BufferedWriter bw = new BufferedWriter(fw);
String n = System.getProperty("line.separator");
ArrayList <String> lineList=new ArrayList<String>();
for (Study s:studies.values()) {
StringBuilder b=new StringBuilder();
b.append(s.getStudyId());
b.append("|");
b.append(s.getName());
lineList.add(b.toString());
}
for (String s:lineList) {bw.write(s); bw.write(n);}
bw.close();
} catch (Throwable t) {t.printStackTrace();}
}
public long getNextId() {
long nextId=0;
for (Study s:studies.values()) {
if (s.getStudyId()>nextId) {
nextId=s.getStudyId();
}
}
return nextId+1;
}
}
package university.at.jku.ce.dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import university.at.jku.ce.model.Study;
import university.at.jku.ce.model.Subject;
public class TextSubjectDao extends AbstractSubjectDao {
Map <Long,Subject> subjects;
Map <SubjectId,Subject> allSubjects;
class SubjectId{
Long studyId;
Long subjectId;
SubjectId(Long studyId, Long subjectId) {
this.studyId=studyId;
this.subjectId=subjectId;
}
@Override
public boolean equals (Object o) {
if (o instanceof SubjectId) {
SubjectId si=(SubjectId)o;
if (this.studyId.equals(si.studyId)&&this.subjectId.equals(si.subjectId)) return true;
}
return false;
}
@Override
public int hashCode() {
int code=(int) (((studyId+subjectId)*(studyId+subjectId+1))/2+subjectId);
return code;
}
}
@Override
public Map<Long,Subject> getAllSubjects(Long studyId) {
ArrayList <String> lineList=new ArrayList<String>();
allSubjects=new HashMap<SubjectId,Subject>();
subjects=new HashMap<Long,Subject>();
try {
FileReader fr = new FileReader("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Subjects.txt");
BufferedReader br = new BufferedReader(fr);
String line="";
do{
line = br.readLine();
lineList.add(line);
}
while (line != null);
br.close();
} catch (Throwable t) {t.printStackTrace();}
for(String s: lineList) {
if (s!=null) {
int i=0;
Long textStudyId;
Long id;
String subjectName;
String subjectDesc;
StringBuilder b=new StringBuilder();
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
textStudyId=Long.parseLong(b.toString());
b=new StringBuilder();
i++;
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
id=Long.parseLong(b.toString());
b=new StringBuilder();
i++;
while (s.charAt(i)!='|') {
b.append(s.charAt(i));
i++;
}
subjectName=b.toString();
b=new StringBuilder();
i++;
while (i<s.length()) {
b.append(s.charAt(i));
i++;
}
subjectDesc=b.toString();
Subject subject=new Subject(id,subjectName,subjectDesc);
if(studyId==textStudyId) {
subjects.put(id, subject);
}
allSubjects.put(new SubjectId(textStudyId,id), subject);
}
}
return subjects;
}
@Override
public Subject getSubject(long studyId, long id) {
subjects=getAllSubjects(studyId);
return subjects.get(id);
}
@Override
public Subject addSubject(long studyId, Subject subject) {
subjects=getAllSubjects(studyId);
subject.setId(getNextId());
subjects.put(subject.getId(), subject);
allSubjects.put(new SubjectId(studyId,subject.getId()), subject);
write();
return subject;
}
@Override
public Subject updateSubject(long studyId, Subject subject) {
subjects=getAllSubjects(studyId);
subjects.put(subject.getId(), subject);
allSubjects.put(new SubjectId(studyId,subject.getId()), subject);
write();
return subject;
}
@Override
public void removeSubject(long studyId, long id) {
subjects=getAllSubjects(studyId);
subjects.remove(id);
allSubjects.remove(new SubjectId(studyId, id));
write();
}
void write() {
ArrayList <String> lineList=new ArrayList<String>();
try {
FileWriter fw = new FileWriter("C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\src\\main\\resources\\Subjects.txt");
BufferedWriter bw = new BufferedWriter(fw);
String n = System.getProperty("line.separator");
for (Entry<SubjectId, Subject> entry:allSubjects.entrySet()) {
StringBuilder b=new StringBuilder();
b.append(entry.getKey().studyId);
b.append("|");
b.append(entry.getValue().getId());
b.append("|");
b.append(entry.getValue().getName());
b.append("|");
b.append(entry.getValue().getInfo());
lineList.add(b.toString());
}
for (String st:lineList) {bw.write(st); bw.write(n);}
bw.close();
} catch (Throwable t) {t.printStackTrace();}
}
public long getNextId() {
long nextId=0;
for (Subject s:subjects.values()) {
if (s.getId()>nextId) {
nextId=s.getId();
}
}
return nextId+1;
}
}
......@@ -22,6 +22,11 @@ public class Study {
this.subjects=subjects;
}
public Study (long studyId, String name) {
this.studyId=studyId;
this.name=name;
}
public Study(long studyId, String name, Map<Long, Subject> subjects) {
this.name=name;
this.studyId=studyId;
......
......@@ -44,4 +44,9 @@ public class Subject {
public void setId(long id) {
this.id = id;
}
}
......@@ -40,8 +40,8 @@ public class SubjectResource {
@DELETE
@Path("/{id}")
public Subject removeSubject(@PathParam("studyId")long studyId,@PathParam("id")long id) {
return subjectService.removeSubject(studyId, id);
public void removeSubject(@PathParam("studyId")long studyId,@PathParam("id")long id) {
subjectService.removeSubject(studyId, id);
}
@GET
......
package university.at.jku.ce.service;
import java.util.ArrayList;
import university.at.jku.ce.Database;
import java.util.List;
import java.util.Map;
......@@ -13,43 +12,27 @@ import university.at.jku.ce.model.Subject;
public class StudentService {
private Map <Long, Student> students=Database.getStudents();
public StudentService() {
Student s2=new Student(1, "Michael", "Schimpelsberger");
Student s1=new Student(2,"Franz", "Reschl");
Student s3=new Student(3,"Susanne", "Zaunmayr");
students.put(s1.getMatrNr(),s1);
students.put(s2.getMatrNr(),s2);
students.put(s3.getMatrNr(), s3);
}
public List<Student> getAllStudents(){
return new ArrayList<Student>(students.values());
return null;
}
public Student getStudent(Long id) {
return students.get(id);
return null;
}
public Student addStudent(Student student) {
if (student!=null) {
student.setMatrNr(students.size()+1);
students.put(student.getMatrNr(), student);
}
return student;
return null;
}
public Student updateStudent(Student student) {
if (student!=null&&students.containsKey(student.getMatrNr()));
students.put(student.getMatrNr(), student);
return student;
return null;
}
public Student removeStudent(long matrNr) {
return students.remove(matrNr);
return null;
}
......
......@@ -4,56 +4,40 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.Database;
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 Map <Long, Study> studies=Database.getStudies();
public StudyService() {
Map<Long,Subject> win=new HashMap<Long,Subject>();
win.put((long)1, new Subject(1,"Einführung Softwareentwicklung","Grundlagen der Softwareentwicklung"));
win.put((long)2, new Subject(2,"Datenmodellierung","Datenmodelle und Datenbanksysteme"));
private AbstractStudyDao studyDao=new TextStudyDao();
Map<Long,Subject> inf=new HashMap<Long,Subject>();
inf.put((long)1, new Subject(1,"Einführung Softwareentwicklung","Grundlagen der Softwareentwicklung"));
inf.put((long)2, new Subject(2,"Relationale Algebra","Grundlegendes zu Relationalen Algebra"));
Study s1=new Study(1,"Wirtschaftsinformatik",win);
Study s2=new Study(2,"Informatik", inf);
Study s3=new Study(3,"Wirtschaftswissenschaften",null);
studies.put(s1.getStudyId(),s1);
studies.put(s2.getStudyId(),s2);
studies.put(s3.getStudyId(), s3);
public StudyService() {
}
public List<Study> getAllStudies(){
return new ArrayList<Study>(studies.values());
return new ArrayList<Study>(studyDao.getAllStudies().values());
}
public Study getStudy(Long id) {
return studies.get(id);
return studyDao.getStudy(id);
}
public Study addStudy(Study study) {
if (study!=null) {
study.setStudyId(studies.size()+1);
studies.put(study.getStudyId(), study);
studyDao.addStudy(study);
}
return study;
}
public Study updateStudy(Study study) {
if (study!=null&&studies.containsKey(study.getStudyId()));
studies.put(study.getStudyId(), study);
if (study!=null);
studyDao.updateStudy(study);
return study;
}
public Study removeStudy(long studyId) {
return studies.remove(studyId);
public void removeStudy(long studyId) {
studyDao.removeStudy(studyId);
}
}
package university.at.jku.ce.service;
import java.util.ArrayList;
import university.at.jku.ce.Database;
import university.at.jku.ce.dao.AbstractSubjectDao;
import university.at.jku.ce.dao.TextSubjectDao;
import java.util.List;
import java.util.Map;
import university.at.jku.ce.model.Study;
......@@ -12,36 +14,33 @@ import university.at.jku.ce.model.Subject;
public class SubjectService {
private Map <Long, Study> studies=Database.getStudies();
//SubjectDao mit zugriff auf tabelle subjects und studyToSubjects
private AbstractSubjectDao subjectDao=new TextSubjectDao();
public List<Subject> getAllSubjects(long studyId){
Map<Long,Subject> subjects=studies.get(studyId).getSubjects();
Map<Long,Subject> subjects=subjectDao.getAllSubjects(studyId);
return new ArrayList<Subject>(subjects.values());
}
public Subject getSubject(long studyId,long id){
Map<Long,Subject> subjects=studies.get(studyId).getSubjects();
return subjects.get(id);
return subjectDao.getSubject(studyId,id);
}
public Subject addSubject (long studyId, Subject subject) {
Map<Long,Subject> subjects=studies.get(studyId).getSubjects();
subject.setId(subjects.size()+1);
subjects.put(subject.getId(),subject);
if (subject!=null) {
subjectDao.addSubject(studyId, subject);
}
return subject;
}
public Subject updateSubject (long studyId, Subject subject) {
Map<Long,Subject> subjects=studies.get(studyId).getSubjects();
if (subject.getId()<=0) return null;