diff --git a/src/core/operation/Delete.java b/src/core/operation/Delete.java index 9982766..2827091 100644 --- a/src/core/operation/Delete.java +++ b/src/core/operation/Delete.java @@ -9,11 +9,12 @@ import java.util.Map; public class Delete extends Operation { @Override - public void execute(User subject) throws GExcptSQL { + public Map execute(User subject) throws GExcptSQL { Map map = new HashMap<>(); - map.put("id",(String)options.get("id")); + map.put("id",(String)this.getOptions().get("id")); for(String table:DBManagement.graduationDesignTables){ DBManagement.delete(table,map); } + return options; } } diff --git a/src/core/operation/Delete.json b/src/core/operation/Delete.json index 183384f..585f898 100644 --- a/src/core/operation/Delete.json +++ b/src/core/operation/Delete.json @@ -1,3 +1,6 @@ { - "id": "java.lang.String" + "options":{ + "id": "String" + }, + "return":{} } \ No newline at end of file diff --git a/src/core/operation/DownloadFile.java b/src/core/operation/DownloadFile.java index 1154c74..f33f474 100644 --- a/src/core/operation/DownloadFile.java +++ b/src/core/operation/DownloadFile.java @@ -1,4 +1,12 @@ package core.operation; -public class DownloadFile { +import core.user.User; + +import java.util.Map; + +public class DownloadFile extends Operation{ + @Override + public Map execute(User subject) throws Exception { + return this.getOptions(); + } } diff --git a/src/core/operation/DownloadFile.json b/src/core/operation/DownloadFile.json new file mode 100644 index 0000000..ad27ff9 --- /dev/null +++ b/src/core/operation/DownloadFile.json @@ -0,0 +1,6 @@ +{ + "options":{ + + }, + "return":{} +} \ No newline at end of file diff --git a/src/core/operation/FillInformation.java b/src/core/operation/FillInformation.java index 4c954b2..cb41906 100644 --- a/src/core/operation/FillInformation.java +++ b/src/core/operation/FillInformation.java @@ -1,4 +1,18 @@ package core.operation; -public class FillInformation { +import core.user.User; +import dao.DBManagement; + +import java.util.Map; + +public class FillInformation extends Operation{ + @Override + public Map execute(User subject) throws Exception { + String table = (String) this.getOptions().get("table"); + Map vMap = (Map) this.getOptions().get("vMap"); + Map limits = (Map) this.getOptions().get("limits"); + DBManagement.update(table,vMap,limits); + return this.getOptions(); + } + } diff --git a/src/core/operation/FillInformation.json b/src/core/operation/FillInformation.json new file mode 100644 index 0000000..f183059 --- /dev/null +++ b/src/core/operation/FillInformation.json @@ -0,0 +1,8 @@ +{ + "options":{ + "table":"String", + "vMap":"Map", + "limits":"Map" + }, + "return":{} +} \ No newline at end of file diff --git a/src/core/operation/Operation.java b/src/core/operation/Operation.java index 7c7971f..729a987 100644 --- a/src/core/operation/Operation.java +++ b/src/core/operation/Operation.java @@ -2,7 +2,7 @@ package core.operation; import core.user.User; -import error.GExcptSQL; +import error.GExcptFormat; import gdms.Mode; import java.util.Map; @@ -14,19 +14,21 @@ public abstract class Operation { Operation(){ super(); } + + public abstract Map execute(User subject) throws Exception; + public void setSubject(User subject) { + this.subject = subject; + } public User getSubject() { return subject; } - public void setSubject(User subject) { - this.subject = subject; + public Map getOptions() { + return options; } - public void setOptions(String options) { + public void setOptions(String options) throws GExcptFormat { this.options = core.operation.utils.util.string2MapOptions(options); } - public void setProcessOperations(String processOptions){ - setOptions(processOptions); - } - public void addUserOperations(String key, Object value){ + public void addOptions(String key, Object value){ if ( Mode.strict == 1){ if(!this.options.containsKey(key)) { try { @@ -38,6 +40,7 @@ public abstract class Operation { } this.options.put(key,value); } - public abstract void execute(User subject) throws Exception; - + public void setOptions(Map options) { + this.options = options; + } } diff --git a/src/core/operation/Search.java b/src/core/operation/Search.java index 73d0af1..5638d4a 100644 --- a/src/core/operation/Search.java +++ b/src/core/operation/Search.java @@ -4,18 +4,31 @@ import core.user.User; import dao.DBManagement; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.util.ArrayList; import java.util.List; import java.util.Map; public class Search extends Operation { @Override - public void execute(User subject) throws Exception { - String table = (String)options.get("table"); - List fields = (List)options.get("fields"); - Map limits = (Map)options.get("limits"); - int start = (int)options.get("start"); - int end = (int)options.get("end"); + public Map execute(User subject) throws Exception { + String table = (String)this.getOptions().get("table"); + List fields = (List)this.getOptions().get("fields"); + Map limits = (Map)this.getOptions().get("limits"); + int start = (int)this.getOptions().get("start"); + int end = (int)this.getOptions().get("end"); ResultSet rs = DBManagement.select(fields,table,limits,start,end); - //todo + ResultSetMetaData rsm = rs.getMetaData() ; + int columnCount = rsm.getColumnCount(); + List ls = new ArrayList<>(); + while(rs.next()){ + String[] s = new String[columnCount]; + for(int i=0;i" + } } \ No newline at end of file diff --git a/src/core/operation/Select.java b/src/core/operation/Select.java index c3f5f18..9d09797 100644 --- a/src/core/operation/Select.java +++ b/src/core/operation/Select.java @@ -10,10 +10,10 @@ import java.util.Map; public class Select extends Operation { @Override - public void execute(User subject) throws GExcptSQL { + public Map execute(User subject) throws GExcptSQL { Map vMap = new HashMap<>(); - String id = (String)options.get("student_id"); - String teacher_id = (String)options.get("teacher_id"); + String id = (String)this.getOptions().get("student_id"); + String teacher_id = (String)this.getOptions().get("teacher_id"); vMap.put("id",id); vMap.put("student_id",id); vMap.put("teacher_id",teacher_id); @@ -23,5 +23,6 @@ public class Select extends Operation { for(int i=1;i options; - @Override - public void execute(User subject) throws GExcptSQL { - + public Map execute(User subject) throws GExcptSQL { + return this.getOptions(); } } diff --git a/src/core/operation/UploadFileOperation.json b/src/core/operation/UploadFileOperation.json index dae12c8..ed59a86 100644 --- a/src/core/operation/UploadFileOperation.json +++ b/src/core/operation/UploadFileOperation.json @@ -1,4 +1,6 @@ { - "java.lang.String": "activity", - "lava.io.File": "file" + "options":{ + "file": "File" + }, + "return":{} } \ No newline at end of file diff --git a/src/core/operation/utils/util.java b/src/core/operation/utils/util.java index 031ce17..edae565 100644 --- a/src/core/operation/utils/util.java +++ b/src/core/operation/utils/util.java @@ -1,5 +1,8 @@ package core.operation.utils; +import error.GExcptFormat; +import gdms.Mode; + import java.util.HashMap; import java.util.Map; @@ -7,12 +10,16 @@ public interface util { static String stringOptionsFormat(String options){ return options.replaceAll(" {2,}", " ") ; } - static Map string2MapOptions(String sOptions){ + static Map string2MapOptions(String sOptions) throws GExcptFormat { sOptions = stringOptionsFormat(sOptions); Map options = new HashMap<>(); String[] sOpts = sOptions.split(" "); for(int i=0;i vMap){ + super.setAttr(vMap); + } } diff --git a/src/core/user/Student.java b/src/core/user/Student.java index 8600327..7365272 100644 --- a/src/core/user/Student.java +++ b/src/core/user/Student.java @@ -1,7 +1,31 @@ package core.user; import java.io.File; +import java.util.Map; public class Student extends User { + private String grade; + private String profession_code; + public void setAttr(Map vMap){ + super.setAttr(vMap); + this.setGrade(vMap.get("grade")); + this.setProfession_code(vMap.get("profession_code")); + } + + public String getGrade() { + return grade; + } + + public void setGrade(String grade) { + this.grade = grade; + } + + public String getProfession_code() { + return profession_code; + } + + public void setProfession_code(String profession_code) { + this.profession_code = profession_code; + } } diff --git a/src/core/user/Teacher.java b/src/core/user/Teacher.java index 80b297d..2a24d05 100644 --- a/src/core/user/Teacher.java +++ b/src/core/user/Teacher.java @@ -1,7 +1,41 @@ package core.user; import java.io.File; +import java.util.Map; public class Teacher extends User { + String job_title; + String education; + String profession_code; + @Override + public void setAttr(Map vMap){ + super.setAttr(vMap); + this.setEducation(vMap.get("education")); + this.setJob_title(vMap.get("job_title")); + this.setProfession_code(vMap.get("profession_code")); + } + public String getJob_title() { + return job_title; + } + + public void setJob_title(String job_title) { + this.job_title = job_title; + } + + public String getEducation() { + return education; + } + + public void setEducation(String education) { + this.education = education; + } + + public String getProfession_code() { + return profession_code; + } + + public void setProfession_code(String profession_code) { + this.profession_code = profession_code; + } } diff --git a/src/core/user/User.java b/src/core/user/User.java index 5c34eb0..89b29b0 100644 --- a/src/core/user/User.java +++ b/src/core/user/User.java @@ -5,6 +5,7 @@ import core.user.userFun.AccountManageable; import core.user.userFun.ProcessConfigurable; import java.util.List; +import java.util.Map; public abstract class User implements AccountManageable, ProcessConfigurable{ @@ -109,4 +110,11 @@ public abstract class User implements AccountManageable, ProcessConfigurable{ public void syncFromDatabase() { } + + public void setAttr(Map vMap){ + this.setId(vMap.get("id")); + this.setName(vMap.get("name")); + this.setE_mail_location(vMap.get("e_mail_location")); + this.setPhone_number(vMap.get("phone_number")); + } } diff --git a/src/core/user/utils/AccountManagement.java b/src/core/user/utils/AccountManagement.java index 41db8ef..3467e77 100644 --- a/src/core/user/utils/AccountManagement.java +++ b/src/core/user/utils/AccountManagement.java @@ -8,23 +8,28 @@ import utils.Utils; import java.lang.reflect.InvocationTargetException; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static dao.DBManagement.userTables; public interface AccountManagement { static User login(String id, String password) throws GExcptSQL, GExcptAccount, SQLException { ResultSet rs = null; - String sql = null; String userType = null; for(String userTable:userTables){ - sql = "SELECT * FROM "+userTable+" WHERE id=\'"+id+"\'"; + List columns = new ArrayList<>(); + columns.add("*"); + Map limits = new HashMap<>(); + limits.put("id",id); try { - rs = DBManagement.query(sql); + rs = DBManagement.select(columns,userTable,limits,1,2); } catch (Exception e) { - throw new GExcptSQL("QUERY\n\t"+sql+"\nfailure"); + throw new GExcptSQL("QUERY\n\t"+id+"\nfailure"); } if(rs!=null){ userType = userTable; @@ -38,25 +43,21 @@ public interface AccountManagement { try { if(!rs.getString(2).equals(password)) throw new GExcptAccount("password wrong"); - List necessaryInfo = new ArrayList<>(); - for(int i=0;i<4;i++){ - necessaryInfo.add(rs.getString(i)); + Map vMap = new HashMap<>(); + ResultSetMetaData rsm = rs.getMetaData(); + rs.next(); + for(int i=0;i necessaryInfo) throws GExcptSQL { - necessaryInfo = Utils.formatUserInfo(userType,necessaryInfo); - String sql = "INSERT INTO "+userType+" VALUES(\'"; - for(String info:necessaryInfo){ - sql+=info+"\', \'"; - } - sql =Utils.cutTail(sql,3)+")"; - DBManagement.update(sql); - return createUser(userType, necessaryInfo); + static User register(String userType, Map vMap) throws GExcptSQL { + DBManagement.insert(userType,vMap); + return createUser(userType, vMap); } static void logout(User user){ //todo @@ -67,25 +68,14 @@ public interface AccountManagement { static User getUser(String userType){ try { return (User) Class.forName("core.user."+ Utils.toUpperFirstChar(userType)).getDeclaredConstructor().newInstance(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException e) { e.printStackTrace(); } return null; } - static User createUser(String userType, List necessaryInfo) { + static User createUser(String userType, Map vMap) { User user = getUser(userType); - user.setId(necessaryInfo.get(0)); - user.setName(necessaryInfo.get(1)); - user.setE_mail_location(necessaryInfo.get(2)); - user.setPhone_number(necessaryInfo.get(3)); + user.setAttr(vMap); return user; } } diff --git a/src/error/GExcptFormat.java b/src/error/GExcptFormat.java new file mode 100644 index 0000000..a0b3c6e --- /dev/null +++ b/src/error/GExcptFormat.java @@ -0,0 +1,7 @@ +package error; + +public class GExcptFormat extends GExcpt { + public GExcptFormat(String info) { + super(info); + } +} diff --git a/src/gdms/test.java b/src/gdms/test.java new file mode 100644 index 0000000..aac73b8 --- /dev/null +++ b/src/gdms/test.java @@ -0,0 +1,21 @@ +package gdms; + +import dao.Select; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class test { + public static void main(String[] args){ + List list = new ArrayList<>(); + list.add("q"); + list.add("w"); + Map map=new HashMap<>(); + map.put("a","b"); + map.put("c","d"); + Select sql = new Select(list,"zx",map,1,2); + System.out.println(sql.getSQL()); + } +}